esp32: Add initial support for ESP32S2 SoCs.

Builds against IDF v4.3-beta2.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2021-02-18 21:24:34 +11:00
parent 8459f538eb
commit 66a86a0615
18 changed files with 125 additions and 20 deletions

View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -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,

View File

@ -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);

View File

@ -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

View File

@ -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},

View File

@ -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

View File

@ -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;

View File

@ -30,12 +30,17 @@
#include <stdint.h>
#include <stdio.h>
#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();
}

View File

@ -24,18 +24,15 @@
* THE SOFTWARE.
*/
#include <stdio.h>
#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

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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 },

View File

@ -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) },

View File

@ -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)

View File

@ -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 {