Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ESP32 support for IDF v4.4 #2612

Merged
merged 3 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
matrix:
os: [ubuntu-20.04, windows-latest]
variant: [esp8266, host, esp32, esp32s2, esp32c3, rp2040]
INSTALL_IDF_VER: ["4.3", "4.4"]
include:
- variant: esp8266
arch: Esp8266
Expand All @@ -26,13 +27,24 @@ jobs:
arch: Esp32
- variant: rp2040
arch: Rp2040
exclude:
- variant: esp8266
INSTALL_IDF_VER: "4.4"
- variant: host
INSTALL_IDF_VER: "4.4"
- variant: rp2040
INSTALL_IDF_VER: "4.4"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ toJson(matrix) }}
cancel-in-progress: true

runs-on: ${{ matrix.os }}

env:
SMING_ARCH: ${{ matrix.arch }}
SMING_SOC: ${{ matrix.variant }}

steps:
- name: Fix autocrlf setting
run: |
Expand All @@ -46,8 +58,7 @@ jobs:
run: |
"CI_BUILD_DIR=" + (Resolve-Path ".").path >> $env:GITHUB_ENV
"SMING_HOME=" + (Resolve-Path "Sming").path >> $env:GITHUB_ENV
"SMING_ARCH=${{ matrix.arch }}" >> $env:GITHUB_ENV
"SMING_SOC=${{ matrix.variant }}" >> $env:GITHUB_ENV
"INSTALL_IDF_VER=${{ matrix.INSTALL_IDF_VER }}" >> $env:GITHUB_ENV

- name: Install build tools for Ubuntu
if: ${{ matrix.os == 'ubuntu-20.04' }}
Expand Down
21 changes: 11 additions & 10 deletions Sming/Arch/Esp32/Components/driver/include/driver/hw_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <esp_attr.h>
#include <sming_attr.h>
#include <stdint.h>
#include <esp_idf_version.h>

#ifdef CONFIG_ESP_TIMER_IMPL_TG0_LAC
#include <soc/timer_group_reg.h>
Expand Down Expand Up @@ -129,18 +130,12 @@ uint32_t hw_timer1_read(void);

#if CONFIG_ESP_TIMER_IMPL_TG0_LAC
#define HW_TIMER2_CLK 2000000U
#define HW_TIMER2_INDEX 0
#elif defined(CONFIG_IDF_TARGET_ESP32S2)
#define HW_TIMER2_CLK 80000000U
#define HW_TIMER2_INDEX
#elif defined(CONFIG_IDF_TARGET_ESP32C3)
#define HW_TIMER2_CLK 16000000U
#define HW_TIMER2_INDEX 0
#elif defined(CONFIG_IDF_TARGET_ESP32S3)
#define HW_TIMER2_CLK 16000000U
#define HW_TIMER2_INDEX 0
#else
_Static_assert(false, "ESP32 Unsupported timer");
#define HW_TIMER2_CLK (SYSTIMER_LL_TICKS_PER_US * 1000000U)
#endif

/**
Expand All @@ -150,10 +145,16 @@ _Static_assert(false, "ESP32 Unsupported timer");
__forceinline static uint32_t hw_timer2_read(void)
{
#if CONFIG_ESP_TIMER_IMPL_TG0_LAC
return REG_READ(TIMG_LACTLO_REG(HW_TIMER2_INDEX));
return REG_READ(TIMG_LACTLO_REG(0));
#elif ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0)
systimer_ll_counter_snapshot(&SYSTIMER, 0);
return systimer_ll_get_counter_value_low(&SYSTIMER, 0);
#elif defined(CONFIG_IDF_TARGET_ESP32S2)
systimer_ll_counter_snapshot();
return systimer_ll_get_counter_value_low();
#else
systimer_ll_counter_snapshot(HW_TIMER2_INDEX);
return systimer_ll_get_counter_value_low(HW_TIMER2_INDEX);
systimer_ll_counter_snapshot(0);
return systimer_ll_get_counter_value_low(0);
#endif
}

Expand Down
12 changes: 9 additions & 3 deletions Sming/Arch/Esp32/Components/driver/uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
uart.cpp - esp32 UART HAL
*/

// #define typeof(x) std::remove_volatile<decltype(x)>::type
#define typeof(x) decltype(x)
// These conflict with enumerated types defined in IDF
#define UART_PARITY_NONE IDF_UART_PARITY_NONE
#define UART_PARITY_EVEN IDF_UART_PARITY_EVEN
Expand Down Expand Up @@ -65,7 +63,7 @@ struct smg_uart_pins_t {
#elif defined(SOC_ESP32S3)
#define UART0_PIN_DEFAULT 43, 44
#define UART1_PIN_DEFAULT UART_NUM_1_TXD_DIRECT_GPIO_NUM, UART_NUM_1_RXD_DIRECT_GPIO_NUM
#define UART2_PIN_DEFAULT UART_NUM_2_TXD_DIRECT_GPIO_NUM, UART_NUM_2_RXD_DIRECT_GPIO_NUM
#define UART2_PIN_DEFAULT 17, 16
#else
static_assert(false, "Must define default UART pins for selected ESP_VARIANT");
#endif
Expand Down Expand Up @@ -763,15 +761,23 @@ bool smg_uart_set_pins(smg_uart_t* uart, int tx_pin, int rx_pin)
if(tx_pin != UART_PIN_NO_CHANGE) {
gpio_ll_iomux_func_sel(GPIO_PIN_MUX_REG[tx_pin], PIN_FUNC_GPIO);
gpio_set_level(gpio_num_t(tx_pin), true);
#ifdef SOC_UART_TX_PIN_IDX
gpio_matrix_out(tx_pin, conn.pins[SOC_UART_TX_PIN_IDX].signal, false, false);
#else
gpio_matrix_out(tx_pin, conn.tx_sig, false, false);
#endif
uart->tx_pin = tx_pin;
}

if(rx_pin != UART_PIN_NO_CHANGE) {
gpio_ll_iomux_func_sel(GPIO_PIN_MUX_REG[rx_pin], PIN_FUNC_GPIO);
gpio_set_pull_mode(gpio_num_t(rx_pin), GPIO_PULLUP_ONLY);
gpio_set_direction(gpio_num_t(rx_pin), GPIO_MODE_INPUT);
#ifdef SOC_UART_RX_PIN_IDX
gpio_matrix_in(rx_pin, conn.pins[SOC_UART_RX_PIN_IDX].signal, false);
#else
gpio_matrix_in(rx_pin, conn.rx_sig, false);
#endif
}

return true;
Expand Down
31 changes: 27 additions & 4 deletions Sming/Arch/Esp32/Components/esp32/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ ifeq ($(CREATE_EVENT_TASK),1)
COMPONENT_CPPFLAGS += -DCREATE_EVENT_TASK
endif

include $(IDF_PATH)/make/version.mk
IDF_VERSION := $(IDF_VERSION_MAJOR).$(IDF_VERSION_MINOR)

SDK_BUILD_BASE := $(COMPONENT_BUILD_BASE)/sdk
SDK_COMPONENT_LIBDIR := $(COMPONENT_BUILD_BASE)/lib

SDKCONFIG_H := $(SDK_BUILD_BASE)/config/sdkconfig.h

SDK_LIBDIRS := \
esp_wifi/lib/$(ESP_VARIANT) \
esp_phy/lib/$(ESP_VARIANT) \
xtensa/$(ESP_VARIANT) \
hal/$(ESP_VARIANT) \
$(ESP_VARIANT)/ld \
soc/$(ESP_VARIANT)/ld \
esp_rom/$(ESP_VARIANT)/ld

# BLUETOOTH
Expand All @@ -45,7 +49,9 @@ LIBDIRS += \
$(SDK_BUILD_BASE)/esp-idf/mbedtls/mbedtls/library \
$(SDK_BUILD_BASE)/esp-idf/$(ESP_VARIANT) \
$(SDK_BUILD_BASE)/esp-idf/$(ESP_VARIANT)/ld \
$(SDK_BUILD_BASE)/esp-idf/esp_system/ld \
$(ESP32_COMPONENT_PATH)/ld \
$(SDK_COMPONENTS_PATH)/$(ESP_VARIANT)/ld \
$(addprefix $(SDK_COMPONENTS_PATH)/,$(SDK_LIBDIRS))

SDK_INCDIRS := \
Expand All @@ -63,10 +69,13 @@ SDK_INCDIRS := \
esp_timer/include \
soc/include \
soc/$(ESP_VARIANT)/include \
soc/include/soc \
heap/include \
log/include \
nvs_flash/include \
freertos/include \
freertos/include/esp_additions \
freertos/include/esp_additions/freertos \
esp_event/include \
lwip/lwip/src/include \
lwip/port/esp32/include \
Expand All @@ -75,7 +84,9 @@ SDK_INCDIRS := \
wpa_supplicant/include \
wpa_supplicant/port/include \
esp_hw_support/include \
esp_hw_support/include/soc \
hal/include \
hal/platform_port/include \
hal/$(ESP_VARIANT)/include \
esp_system/include \
esp_common/include \
Expand Down Expand Up @@ -137,7 +148,6 @@ SDK_COMPONENTS := \
cxx \
driver \
efuse \
$(ESP_VARIANT) \
esp_common \
esp_event \
esp_gdbstub \
Expand All @@ -159,6 +169,12 @@ SDK_COMPONENTS := \
soc \
spi_flash

ifeq ($(IDF_VERSION),4.3)
SDK_COMPONENTS += $(ESP_VARIANT)
else
SDK_COMPONENTS += esp_phy
endif

ifneq ($(DISABLE_NETWORK),1)
SDK_COMPONENTS += \
esp_wifi \
Expand Down Expand Up @@ -254,8 +270,6 @@ SDK_UNDEF_SYMBOLS :=
$(foreach c,$(wildcard $(SDK_DEFAULT_PATH)/*.mk),$(eval include $c))

EXTRA_LDFLAGS := \
-T $(ESP_VARIANT)_out.ld \
$(call LinkerScript,project) \
$(call LinkerScript,peripherals) \
$(call LinkerScript,rom) \
$(call LinkerScript,rom.api) \
Expand All @@ -272,6 +286,15 @@ EXTRA_LDFLAGS := \
$(call Undef,$(SDK_UNDEF_SYMBOLS)) \
$(call Wrap,$(SDK_WRAP_SYMBOLS))

ifeq ($(IDF_VERSION),4.3)
EXTRA_LDFLAGS += \
-T $(ESP_VARIANT)_out.ld \
$(call LinkerScript,project)
else
EXTRA_LDFLAGS += \
-T memory.ld \
-T sections.ld
endif

SDK_PROJECT_PATH := $(ESP32_COMPONENT_PATH)/project/$(ESP_VARIANT)/$(BUILD_TYPE)
SDK_CONFIG_DEFAULTS := $(SDK_PROJECT_PATH)/sdkconfig.defaults
Expand Down
4 changes: 2 additions & 2 deletions Sming/Arch/Esp32/Components/esp32/src/include/esp_systemapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
/** @brief Disable interrupts
* @retval Current interrupt level
*/
#define noInterrupts() portENTER_CRITICAL_NESTED()
#define noInterrupts() portSET_INTERRUPT_MASK_FROM_ISR()

/** @brief Enable interrupts
*/
#define interrupts() portENABLE_INTERRUPTS()

/** @brief Restore interrupts to level saved from previous noInterrupts() call
*/
#define restoreInterrupts(state) portEXIT_CRITICAL_NESTED(state)
#define restoreInterrupts(state) portCLEAR_INTERRUPT_MASK_FROM_ISR(state)
12 changes: 11 additions & 1 deletion Sming/Arch/Esp32/Components/esp32/src/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,18 @@ void system_restart(void)
* Doing so causes a deadlock and a task watchdog timeout.
*
* Delegating this call to any other task fixes the issue.
*
* We can use the timer task to handle the call.
* This method does not free the allocated timer resources but as the system
* is restarting this doesn't matter.
*/
esp_ipc_call(0, esp_ipc_func_t(esp_restart), nullptr);
const esp_timer_create_args_t create_args = {
.callback = esp_timer_cb_t(esp_restart),
.dispatch_method = ESP_TIMER_TASK,
};
esp_timer_handle_t handle{nullptr};
esp_timer_create(&create_args, &handle);
esp_timer_start_once(handle, 100);
}

/* Watchdog */
Expand Down
3 changes: 2 additions & 1 deletion Sming/Arch/Esp32/Tools/install.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ if "%IDF_PATH%"=="" goto :EOF
if "%IDF_TOOLS_PATH%"=="" goto :EOF

if "%IDF_REPO%"=="" set IDF_REPO="https://github.com/mikee47/esp-idf.git"
if "%IDF_BRANCH%"=="" set IDF_BRANCH="sming/release/v4.3"
if "%INSTALL_IDF_VER%"=="" set INSTALL_IDF_VER=4.4
set IDF_BRANCH="sming/release/v%INSTALL_IDF_VER%"

git clone -b %IDF_BRANCH% %IDF_REPO% %IDF_PATH%

Expand Down
5 changes: 3 additions & 2 deletions Sming/Arch/Esp32/Tools/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ if [ ! -L "$IDF_PATH" ] && [ -d "$IDF_PATH" ]; then
mv "$IDF_PATH" "$IDF_PATH-old"
fi

IDF_CLONE_PATH="$(readlink -m "$IDF_PATH/..")/esp-idf-4.3"
INSTALL_IDF_VER="${INSTALL_IDF_VER:=4.4}"
IDF_CLONE_PATH="$(readlink -m "$IDF_PATH/..")/esp-idf-${INSTALL_IDF_VER}"
IDF_REPO="${IDF_REPO:=https://github.com/mikee47/esp-idf.git}"
IDF_BRANCH="${IDF_BRANCH:=sming/release/v4.3}"
IDF_BRANCH="sming/release/v${INSTALL_IDF_VER}"

if [ -d "$IDF_CLONE_PATH" ]; then
printf "\n\n** Skipping ESP-IDF clone: '$IDF_CLONE_PATH' exists\n\n"
Expand Down
3 changes: 2 additions & 1 deletion Sming/Arch/Esp32/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ CPPFLAGS += \
-D__ets__ \
-D_GNU_SOURCE \
-DCONFIG_NONE_OS \
-Dasm=__asm__
-Dasm=__asm__ \
-Dtypeof=__typeof__

PROJECT_VER ?=
export IDF_VER
Expand Down
4 changes: 3 additions & 1 deletion Sming/Components/Network/Arch/Esp32/Network/DM9051.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ bool DM9051Service::begin(const Config& config)
esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
netif = esp_netif_new(&netif_cfg);

#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 4, 0)
// Set default handlers to process TCP/IP stuffs
CHECK_RET(esp_eth_set_default_handlers(netif));
#endif

// And register our own event handlers
enableEventCallback(true);
Expand Down Expand Up @@ -69,7 +71,7 @@ bool DM9051Service::begin(const Config& config)

setMacAddress(MacAddress({0x02, 0x00, 0x00, 0x12, 0x34, 0x56}));

netif_glue = esp_eth_new_netif_glue(handle);
netif_glue = static_cast<void*>(esp_eth_new_netif_glue(handle));
CHECK_RET(esp_netif_attach(netif, netif_glue));
CHECK_RET(esp_eth_start(handle));

Expand Down
4 changes: 3 additions & 1 deletion Sming/Components/Network/Arch/Esp32/Network/W5500.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ bool W5500Service::begin(const Config& config)
esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
netif = esp_netif_new(&netif_cfg);

#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 4, 0)
// Set default handlers to process TCP/IP stuffs
CHECK_RET(esp_eth_set_default_handlers(netif));
#endif

// And register our own event handlers
enableEventCallback(true);
Expand Down Expand Up @@ -69,7 +71,7 @@ bool W5500Service::begin(const Config& config)

setMacAddress(MacAddress({0x02, 0x00, 0x00, 0x12, 0x34, 0x56}));

netif_glue = esp_eth_new_netif_glue(handle);
netif_glue = static_cast<void*>(esp_eth_new_netif_glue(handle));
CHECK_RET(esp_netif_attach(netif, netif_glue));
CHECK_RET(esp_eth_start(handle));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ bool EmbeddedEthernet::begin(const Config& config)
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
netif = esp_netif_new(&cfg);

#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 4, 0)
// Set default handlers to process TCP/IP stuffs
ESP_ERROR_CHECK(esp_eth_set_default_handlers(netif));
#endif

// And register our own event handlers
enableEventCallback(true);
Expand All @@ -57,7 +59,7 @@ bool EmbeddedEthernet::begin(const Config& config)

esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
ESP_ERROR_CHECK(esp_eth_driver_install(&eth_config, &handle));
netif_glue = esp_eth_new_netif_glue(handle);
netif_glue = static_cast<void*>(esp_eth_new_netif_glue(handle));
ESP_ERROR_CHECK(esp_netif_attach(netif, netif_glue));
ESP_ERROR_CHECK(esp_eth_start(handle));

Expand Down
8 changes: 7 additions & 1 deletion Sming/Components/Network/Arch/Esp32/Platform/IdfService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include <esp_event.h>
#include <debug_progmem.h>

#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 4, 0)
using esp_eth_netif_glue_handle_t = void*;
#endif

namespace Ethernet
{
void IdfService::end()
Expand All @@ -22,9 +26,11 @@ void IdfService::end()
}

ESP_ERROR_CHECK(esp_eth_stop(handle));
ESP_ERROR_CHECK(esp_eth_del_netif_glue(netif_glue));
ESP_ERROR_CHECK(esp_eth_del_netif_glue(static_cast<esp_eth_netif_glue_handle_t>(netif_glue)));
netif_glue = nullptr;
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 4, 0)
ESP_ERROR_CHECK(esp_eth_clear_default_handlers(netif));
#endif

ESP_ERROR_CHECK(esp_eth_driver_uninstall(handle));
handle = nullptr;
Expand Down
1 change: 0 additions & 1 deletion Sming/Libraries/SPI/src/Arch/Esp32/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <esp_systemapi.h>
#undef FLAG_ATTR
#define FLAG_ATTR(TYPE)
#define typeof decltype
#include <soc/spi_periph.h>
#include <hal/spi_ll.h>
#include <hal/clk_gate_ll.h>
Expand Down
Loading