diff --git a/ports/esp32/boards/sdkconfig.base b/ports/esp32/boards/sdkconfig.base index 91e68c7bf6..4d9e38077d 100644 --- a/ports/esp32/boards/sdkconfig.base +++ b/ports/esp32/boards/sdkconfig.base @@ -1,7 +1,6 @@ # MicroPython on ESP32, ESP IDF configuration # The following options override the defaults -CONFIG_IDF_TARGET="esp32" CONFIG_IDF_FIRMWARE_CHIP_ID=0x0000 # Compiler options: use -Os to reduce size, but keep full assertions diff --git a/ports/esp32/esp32_ulp.c b/ports/esp32/esp32_ulp.c index 50244cdf25..8e4ce9c5a4 100644 --- a/ports/esp32/esp32_ulp.c +++ b/ports/esp32/esp32_ulp.c @@ -26,6 +26,8 @@ #include "py/runtime.h" +#if CONFIG_IDF_TARGET_ESP32 + #include "esp32/ulp.h" #include "esp_err.h" @@ -95,3 +97,5 @@ const mp_obj_type_t esp32_ulp_type = { .make_new = esp32_ulp_make_new, .locals_dict = (mp_obj_t)&esp32_ulp_locals_dict, }; + +#endif // CONFIG_IDF_TARGET_ESP32 diff --git a/ports/esp32/machine_adc.c b/ports/esp32/machine_adc.c index 811a208e6d..4c19d5992b 100644 --- a/ports/esp32/machine_adc.c +++ b/ports/esp32/machine_adc.c @@ -60,7 +60,11 @@ STATIC mp_obj_t madc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n static int initialized = 0; if (!initialized) { - adc1_config_width(ADC_WIDTH_12Bit); + #if CONFIG_IDF_TARGET_ESP32S2 + adc1_config_width(ADC_WIDTH_BIT_13); + #else + adc1_config_width(ADC_WIDTH_BIT_12); + #endif adc_bit_width = 12; initialized = 1; } @@ -128,6 +132,7 @@ STATIC mp_obj_t madc_width(mp_obj_t cls_in, mp_obj_t width_in) { mp_raise_ValueError(MP_ERROR_TEXT("parameter error")); } switch (width) { + #if CONFIG_IDF_TARGET_ESP32 case ADC_WIDTH_9Bit: adc_bit_width = 9; break; @@ -140,6 +145,11 @@ STATIC mp_obj_t madc_width(mp_obj_t cls_in, mp_obj_t width_in) { case ADC_WIDTH_12Bit: adc_bit_width = 12; break; + #elif CONFIG_IDF_TARGET_ESP32S2 + case ADC_WIDTH_BIT_13: + adc_bit_width = 13; + break; + #endif default: break; } @@ -160,10 +170,14 @@ STATIC const mp_rom_map_elem_t madc_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_ATTN_6DB), MP_ROM_INT(ADC_ATTEN_6db) }, { MP_ROM_QSTR(MP_QSTR_ATTN_11DB), MP_ROM_INT(ADC_ATTEN_11db) }, + #if CONFIG_IDF_TARGET_ESP32 { MP_ROM_QSTR(MP_QSTR_WIDTH_9BIT), MP_ROM_INT(ADC_WIDTH_9Bit) }, { MP_ROM_QSTR(MP_QSTR_WIDTH_10BIT), MP_ROM_INT(ADC_WIDTH_10Bit) }, { MP_ROM_QSTR(MP_QSTR_WIDTH_11BIT), MP_ROM_INT(ADC_WIDTH_11Bit) }, { MP_ROM_QSTR(MP_QSTR_WIDTH_12BIT), MP_ROM_INT(ADC_WIDTH_12Bit) }, + #elif CONFIG_IDF_TARGET_ESP32S2 + { MP_ROM_QSTR(MP_QSTR_WIDTH_13BIT), MP_ROM_INT(ADC_WIDTH_BIT_13) }, + #endif }; STATIC MP_DEFINE_CONST_DICT(madc_locals_dict, madc_locals_dict_table); diff --git a/ports/esp32/machine_dac.c b/ports/esp32/machine_dac.c index 02855fcf80..146ef60aa8 100644 --- a/ports/esp32/machine_dac.c +++ b/ports/esp32/machine_dac.c @@ -43,8 +43,13 @@ typedef struct _mdac_obj_t { } mdac_obj_t; STATIC const mdac_obj_t mdac_obj[] = { + #if CONFIG_IDF_TARGET_ESP32 {{&machine_dac_type}, GPIO_NUM_25, DAC_CHANNEL_1}, {{&machine_dac_type}, GPIO_NUM_26, DAC_CHANNEL_2}, + #else + {{&machine_dac_type}, GPIO_NUM_17, DAC_CHANNEL_1}, + {{&machine_dac_type}, GPIO_NUM_18, DAC_CHANNEL_2}, + #endif }; STATIC mp_obj_t mdac_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, diff --git a/ports/esp32/machine_hw_spi.c b/ports/esp32/machine_hw_spi.c index d59f2c750e..a49a58035a 100644 --- a/ports/esp32/machine_hw_spi.c +++ b/ports/esp32/machine_hw_spi.c @@ -184,8 +184,12 @@ STATIC void machine_hw_spi_init_internal( changed = true; } - if (self->host != HSPI_HOST && self->host != VSPI_HOST) { - mp_raise_ValueError(MP_ERROR_TEXT("SPI ID must be either HSPI(1) or VSPI(2)")); + if (self->host != HSPI_HOST + #ifdef VSPI_HOST + && self->host != VSPI_HOST + #endif + ) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), self->host); } if (changed) { @@ -220,8 +224,10 @@ STATIC void machine_hw_spi_init_internal( int dma_chan = 0; if (self->host == HSPI_HOST) { dma_chan = 1; + #ifdef VSPI_HOST } else if (self->host == VSPI_HOST) { dma_chan = 2; + #endif } ret = spi_bus_initialize(self->host, &buscfg, dma_chan); diff --git a/ports/esp32/machine_i2c.c b/ports/esp32/machine_i2c.c index 3993c7b52b..fd5180b8d0 100644 --- a/ports/esp32/machine_i2c.c +++ b/ports/esp32/machine_i2c.c @@ -34,8 +34,13 @@ #define I2C_0_DEFAULT_SCL (GPIO_NUM_18) #define I2C_0_DEFAULT_SDA (GPIO_NUM_19) +#if CONFIG_IDF_TARGET_ESP32 #define I2C_1_DEFAULT_SCL (GPIO_NUM_25) #define I2C_1_DEFAULT_SDA (GPIO_NUM_26) +#else +#define I2C_1_DEFAULT_SCL (GPIO_NUM_9) +#define I2C_1_DEFAULT_SDA (GPIO_NUM_8) +#endif #define I2C_DEFAULT_TIMEOUT_US (10000) // 10ms diff --git a/ports/esp32/machine_pin.c b/ports/esp32/machine_pin.c index dcdd53be92..8290c77a48 100644 --- a/ports/esp32/machine_pin.c +++ b/ports/esp32/machine_pin.c @@ -77,10 +77,17 @@ STATIC const machine_pin_obj_t machine_pin_obj[] = { {{&machine_pin_type}, GPIO_NUM_19}, {{NULL}, -1}, {{&machine_pin_type}, GPIO_NUM_21}, + #if CONFIG_IDF_TARGET_ESP32 {{&machine_pin_type}, GPIO_NUM_22}, {{&machine_pin_type}, GPIO_NUM_23}, {{NULL}, -1}, {{&machine_pin_type}, GPIO_NUM_25}, + #else + {{NULL}, -1}, + {{NULL}, -1}, + {{NULL}, -1}, + {{NULL}, -1}, + #endif {{&machine_pin_type}, GPIO_NUM_26}, {{&machine_pin_type}, GPIO_NUM_27}, {{NULL}, -1}, @@ -411,10 +418,17 @@ STATIC const machine_pin_irq_obj_t machine_pin_irq_object[] = { {{&machine_pin_irq_type}, GPIO_NUM_19}, {{NULL}, -1}, {{&machine_pin_irq_type}, GPIO_NUM_21}, + #if CONFIG_IDF_TARGET_ESP32 {{&machine_pin_irq_type}, GPIO_NUM_22}, {{&machine_pin_irq_type}, GPIO_NUM_23}, {{NULL}, -1}, {{&machine_pin_irq_type}, GPIO_NUM_25}, + #else + {{NULL}, -1}, + {{NULL}, -1}, + {{NULL}, -1}, + {{NULL}, -1}, + #endif {{&machine_pin_irq_type}, GPIO_NUM_26}, {{&machine_pin_irq_type}, GPIO_NUM_27}, {{NULL}, -1}, diff --git a/ports/esp32/machine_pwm.c b/ports/esp32/machine_pwm.c index 7592f243b7..a7d7d29df8 100644 --- a/ports/esp32/machine_pwm.c +++ b/ports/esp32/machine_pwm.c @@ -50,7 +50,11 @@ STATIC int chan_gpio[LEDC_CHANNEL_MAX]; // 5khz #define PWFREQ (5000) // High speed mode +#if CONFIG_IDF_TARGET_ESP32 #define PWMODE (LEDC_HIGH_SPEED_MODE) +#else +#define PWMODE (LEDC_LOW_SPEED_MODE) +#endif // 10-bit resolution (compatible with esp8266 PWM) #define PWRES (LEDC_TIMER_10_BIT) // Timer 1 diff --git a/ports/esp32/machine_sdcard.c b/ports/esp32/machine_sdcard.c index 40686508a1..c9a9face7c 100644 --- a/ports/esp32/machine_sdcard.c +++ b/ports/esp32/machine_sdcard.c @@ -31,6 +31,8 @@ #include "py/mperrno.h" #include "extmod/vfs_fat.h" +#if MICROPY_HW_ENABLE_SDCARD + #include "driver/sdmmc_host.h" #include "driver/sdspi_host.h" #include "sdmmc_cmd.h" @@ -50,10 +52,6 @@ // Hosts are de-inited in __del__. Slots do not need de-initing. // -// Currently the ESP32 Library doesn't support MMC cards, so -// we don't enable on MICROPY_HW_ENABLE_MMCARD. -#if MICROPY_HW_ENABLE_SDCARD - // Forward declaration const mp_obj_type_t machine_sdcard_type; diff --git a/ports/esp32/machine_timer.c b/ports/esp32/machine_timer.c index 6e5824094e..696127af77 100644 --- a/ports/esp32/machine_timer.c +++ b/ports/esp32/machine_timer.c @@ -30,12 +30,17 @@ #include #include -#include "driver/timer.h" #include "py/obj.h" #include "py/runtime.h" #include "modmachine.h" #include "mphalport.h" +#include "driver/timer.h" +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 1) +#include "hal/timer_ll.h" +#define HAVE_TIMER_LL (1) +#endif + #define TIMER_INTR_SEL TIMER_INTR_LEVEL #define TIMER_DIVIDER 8 @@ -127,6 +132,18 @@ STATIC void machine_timer_isr(void *self_in) { machine_timer_obj_t *self = self_in; timg_dev_t *device = self->group ? &(TIMERG1) : &(TIMERG0); + #if HAVE_TIMER_LL + + #if CONFIG_IDF_TARGET_ESP32 + device->hw_timer[self->index].update = 1; + #else + device->hw_timer[self->index].update.update = 1; + #endif + timer_ll_clear_intr_status(device, self->index); + timer_ll_set_alarm_enable(device, self->index, self->repeat); + + #else + device->hw_timer[self->index].update = 1; if (self->index) { device->int_clr_timers.t1 = 1; @@ -135,6 +152,8 @@ STATIC void machine_timer_isr(void *self_in) { } device->hw_timer[self->index].config.alarm_en = self->repeat; + #endif + mp_sched_schedule(self->callback, self); mp_hal_wake_main_task_from_isr(); } diff --git a/ports/esp32/machine_touchpad.c b/ports/esp32/machine_touchpad.c index 44efac3756..335157b154 100644 --- a/ports/esp32/machine_touchpad.c +++ b/ports/esp32/machine_touchpad.c @@ -24,18 +24,15 @@ * THE SOFTWARE. */ - -#include - -#include "esp_log.h" - -#include "driver/gpio.h" -#include "driver/touch_pad.h" - #include "py/runtime.h" #include "py/mphal.h" #include "modmachine.h" +#if CONFIG_IDF_TARGET_ESP32 + +#include "driver/gpio.h" +#include "driver/touch_pad.h" + typedef struct _mtp_obj_t { mp_obj_base_t base; gpio_num_t gpio_id; @@ -120,3 +117,5 @@ const mp_obj_type_t machine_touchpad_type = { .make_new = mtp_make_new, .locals_dict = (mp_obj_t)&mtp_locals_dict, }; + +#endif // CONFIG_IDF_TARGET_ESP32 diff --git a/ports/esp32/machine_uart.c b/ports/esp32/machine_uart.c index 7fce83f2c4..e256b9be43 100644 --- a/ports/esp32/machine_uart.c +++ b/ports/esp32/machine_uart.c @@ -307,10 +307,12 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, self->rx = 9; self->tx = 10; break; + #if SOC_UART_NUM > 2 case UART_NUM_2: self->rx = 16; self->tx = 17; break; + #endif } // Remove any existing configuration diff --git a/ports/esp32/main.c b/ports/esp32/main.c index 7413798d0c..0c2f56699f 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -37,7 +37,12 @@ #include "esp_task.h" #include "soc/cpu.h" #include "esp_log.h" + +#if CONFIG_IDF_TARGET_ESP32 #include "esp32/spiram.h" +#elif CONFIG_IDF_TARGET_ESP32S2 +#include "esp32s2/spiram.h" +#endif #include "py/stackctrl.h" #include "py/nlr.h" diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index aacdd40d31..63a221e4d6 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -84,7 +84,6 @@ set(IDF_COMPONENTS bootloader_support bt driver - esp32 esp_common esp_eth esp_event @@ -123,6 +122,12 @@ if(IDF_VERSION_MINOR GREATER_EQUAL 3) list(APPEND IDF_COMPONENTS hal) endif() +if(IDF_TARGET STREQUAL "esp32") + list(APPEND IDF_COMPONENTS esp32) +elseif(IDF_TARGET STREQUAL "esp32s2") + list(APPEND IDF_COMPONENTS esp32s2) +endif() + # Register the main IDF component. idf_component_register( SRCS diff --git a/ports/esp32/modesp32.c b/ports/esp32/modesp32.c index 53ca7fdc60..3ed5343380 100644 --- a/ports/esp32/modesp32.c +++ b/ports/esp32/modesp32.c @@ -132,6 +132,8 @@ STATIC mp_obj_t esp32_wake_on_ext1(size_t n_args, const mp_obj_t *pos_args, mp_m } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp32_wake_on_ext1_obj, 0, esp32_wake_on_ext1); +#if CONFIG_IDF_TARGET_ESP32 + STATIC mp_obj_t esp32_raw_temperature(void) { SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR, 3, SENS_FORCE_XPD_SAR_S); SET_PERI_REG_BITS(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_CLK_DIV, 10, SENS_TSENS_CLK_DIV_S); @@ -154,6 +156,8 @@ STATIC mp_obj_t esp32_hall_sensor(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32_hall_sensor_obj, esp32_hall_sensor); +#endif + STATIC mp_obj_t esp32_idf_heap_info(const mp_obj_t cap_in) { mp_int_t cap = mp_obj_get_int(cap_in); multi_heap_info_t info; @@ -182,14 +186,18 @@ STATIC const mp_rom_map_elem_t esp32_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_wake_on_touch), MP_ROM_PTR(&esp32_wake_on_touch_obj) }, { MP_ROM_QSTR(MP_QSTR_wake_on_ext0), MP_ROM_PTR(&esp32_wake_on_ext0_obj) }, { MP_ROM_QSTR(MP_QSTR_wake_on_ext1), MP_ROM_PTR(&esp32_wake_on_ext1_obj) }, + #if CONFIG_IDF_TARGET_ESP32 { MP_ROM_QSTR(MP_QSTR_raw_temperature), MP_ROM_PTR(&esp32_raw_temperature_obj) }, { MP_ROM_QSTR(MP_QSTR_hall_sensor), MP_ROM_PTR(&esp32_hall_sensor_obj) }, + #endif { MP_ROM_QSTR(MP_QSTR_idf_heap_info), MP_ROM_PTR(&esp32_idf_heap_info_obj) }, { MP_ROM_QSTR(MP_QSTR_NVS), MP_ROM_PTR(&esp32_nvs_type) }, { MP_ROM_QSTR(MP_QSTR_Partition), MP_ROM_PTR(&esp32_partition_type) }, { MP_ROM_QSTR(MP_QSTR_RMT), MP_ROM_PTR(&esp32_rmt_type) }, + #if CONFIG_IDF_TARGET_ESP32 { MP_ROM_QSTR(MP_QSTR_ULP), MP_ROM_PTR(&esp32_ulp_type) }, + #endif { MP_ROM_QSTR(MP_QSTR_WAKEUP_ALL_LOW), MP_ROM_FALSE }, { MP_ROM_QSTR(MP_QSTR_WAKEUP_ANY_HIGH), MP_ROM_TRUE }, diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c index 3925bcb64e..2eb5cd2fee 100644 --- a/ports/esp32/modmachine.c +++ b/ports/esp32/modmachine.c @@ -32,12 +32,18 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "esp32/rom/rtc.h" -#include "esp32/clk.h" #include "esp_sleep.h" #include "esp_pm.h" #include "driver/touch_pad.h" +#if CONFIG_IDF_TARGET_ESP32 +#include "esp32/rom/rtc.h" +#include "esp32/clk.h" +#elif CONFIG_IDF_TARGET_ESP32S2 +#include "esp32s2/rom/rtc.h" +#include "esp32s2/clk.h" +#endif + #include "py/obj.h" #include "py/runtime.h" #include "lib/utils/pyexec.h" @@ -71,7 +77,11 @@ STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { if (freq != 20 && freq != 40 && freq != 80 && freq != 160 && freq != 240) { mp_raise_ValueError(MP_ERROR_TEXT("frequency must be 20MHz, 40MHz, 80Mhz, 160MHz or 240MHz")); } + #if CONFIG_IDF_TARGET_ESP32 esp_pm_config_esp32_t pm; + #elif CONFIG_IDF_TARGET_ESP32S2 + esp_pm_config_esp32s2_t pm; + #endif pm.max_freq_mhz = freq; pm.min_freq_mhz = freq; pm.light_sleep_enable = false; @@ -260,7 +270,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_DEEPSLEEP), MP_ROM_INT(MACHINE_WAKE_DEEPSLEEP) }, { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, { MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) }, + #if CONFIG_IDF_TARGET_ESP32 { MP_ROM_QSTR(MP_QSTR_TouchPad), MP_ROM_PTR(&machine_touchpad_type) }, + #endif { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, { MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hw_i2c_type) }, diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index c9c4b6268b..8788963cb3 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -153,7 +153,9 @@ #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SPI_MSB (0) #define MICROPY_PY_MACHINE_SPI_LSB (1) +#ifndef MICROPY_HW_ENABLE_SDCARD #define MICROPY_HW_ENABLE_SDCARD (1) +#endif #define MICROPY_HW_SOFTSPI_MIN_DELAY (0) #define MICROPY_HW_SOFTSPI_MAX_BAUDRATE (ets_get_cpu_frequency() * 1000000 / 200) // roughly #define MICROPY_PY_USSL (1) diff --git a/ports/esp32/uart.c b/ports/esp32/uart.c index c837c8dcfe..bd3eea9f6b 100644 --- a/ports/esp32/uart.c +++ b/ports/esp32/uart.c @@ -49,7 +49,11 @@ STATIC void IRAM_ATTR uart_irq_handler(void *arg) { uart->int_clr.frm_err = 1; uart->int_clr.rxfifo_tout = 1; while (uart->status.rxfifo_cnt) { + #if CONFIG_IDF_TARGET_ESP32 uint8_t c = uart->fifo.rw_byte; + #elif CONFIG_IDF_TARGET_ESP32S2 + uint8_t c = READ_PERI_REG(UART_FIFO_AHB_REG(0)); // UART0 + #endif if (c == mp_interrupt_char) { mp_keyboard_interrupt(); } else {