8000 esp32: Add initial support for ESP32S2 SoCs. · micropython/micropython@66a86a0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 66a86a0

Browse files
committed
esp32: Add initial support for ESP32S2 SoCs.
Builds against IDF v4.3-beta2. Signed-off-by: Damien George <damien@micropython.org>
1 parent 8459f53 commit 66a86a0

18 files changed

+124
-19
lines changed

ports/esp32/boards/sdkconfig.base

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# MicroPython on ESP32, ESP IDF configuration
22
# The following options override the defaults
33

4-
CONFIG_IDF_TARGET="esp32"
54
CONFIG_IDF_FIRMWARE_CHIP_ID=0x0000
65

76
# Compiler options: use -Os to reduce size, but keep full assertions

ports/esp32/esp32_ulp.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
#include "py/runtime.h"
2828

29+
#if CONFIG_IDF_TARGET_ESP32
30+
2931
#include "esp32/ulp.h"
3032
#include "esp_err.h"
3133

@@ -95,3 +97,5 @@ const mp_obj_type_t esp32_ulp_type = {
9597
.make_new = esp32_ulp_make_new,
9698
.locals_dict = (mp_obj_t)&esp32_ulp_locals_dict,
9799
};
100+
101+
#endif // CONFIG_IDF_TARGET_ESP32

ports/esp32/machine_adc.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ STATIC mp_obj_t madc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
6060

6161
static int initialized = 0;
6262
if (!initialized) {
63-
adc1_config_width(ADC_WIDTH_12Bit);
63+
#if CONFIG_IDF_TARGET_ESP32S2
64+
adc1_config_width(ADC_WIDTH_BIT_13);
65+
#else
66+
adc1_config_width(ADC_WIDTH_BIT_12);
67+
#endif
6468
adc_bit_width = 12;
6569
initialized = 1;
6670
}
@@ -128,6 +132,7 @@ STATIC mp_obj_t madc_width(mp_obj_t cls_in, mp_obj_t width_in) {
128132
mp_raise_ValueError(MP_ERROR_TEXT("parameter error"));
129133
}
130134
switch (width) {
135+
#if CONFIG_IDF_TARGET_ESP32
131136
case ADC_WIDTH_9Bit:
132137
adc_bit_width = 9;
133138
break;
@@ -140,6 +145,11 @@ STATIC mp_obj_t madc_width(mp_obj_t cls_in, mp_obj_t width_in) {
140145
case ADC_WIDTH_12Bit:
141146
adc_bit_width = 12;
142147
break;
148+
#elif CONFIG_IDF_TARGET_ESP32S2
149+
case ADC_WIDTH_BIT_13:
150+
adc_bit_width = 13;
151+
break;
152+
#endif
143153
default:
144154
break;
145155
}
@@ -160,10 +170,14 @@ STATIC const mp_rom_map_elem_t madc_locals_dict_table[] = {
160170
{ MP_ROM_QSTR(MP_QSTR_ATTN_6DB), MP_ROM_INT(ADC_ATTEN_6db) },
161171
{ MP_ROM_QSTR(MP_QSTR_ATTN_11DB), MP_ROM_INT(ADC_ATTEN_11db) },
162172

173+
#if CONFIG_IDF_TARGET_ESP32
163174
{ MP_ROM_QSTR(MP_QSTR_WIDTH_9BIT), MP_ROM_INT(ADC_WIDTH_9Bit) },
164175
{ MP_ROM_QSTR(MP_QSTR_WIDTH_10BIT), MP_ROM_INT(ADC_WIDTH_10Bit) },
165176
{ MP_ROM_QSTR(MP_QSTR_WIDTH_11BIT), MP_ROM_INT(ADC_WIDTH_11Bit) },
166177
{ MP_ROM_QSTR(MP_QSTR_WIDTH_12BIT), MP_ROM_INT(ADC_WIDTH_12Bit) },
178+
#elif CONFIG_IDF_TARGET_ESP32S2
179+
{ MP_ROM_QSTR(MP_QSTR_WIDTH_13BIT), MP_ROM_INT(ADC_WIDTH_BIT_13) },
180+
#endif
167181
};
168182

169183
STATIC MP_DEFINE_CONST_DICT(madc_locals_dict, madc_locals_dict_table);

ports/esp32/machine_dac.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@ typedef struct _mdac_obj_t {
4343
} mdac_obj_t;
4444

4545
STATIC const mdac_obj_t mdac_obj[] = {
46+
#if CONFIG_IDF_TARGET_ESP32
4647
{{&machine_dac_type}, GPIO_NUM_25, DAC_CHANNEL_1},
4748
{{&machine_dac_type}, GPIO_NUM_26, DAC_CHANNEL_2},
49+
#else
50+
{{&machine_dac_type}, GPIO_NUM_17, DAC_CHANNEL_1},
51+
{{&machine_dac_type}, GPIO_NUM_18, DAC_CHANNEL_2},
52+
#endif
4853
};
4954

5055
STATIC mp_obj_t mdac_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,

ports/esp32/machine_hw_spi.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,12 @@ STATIC void machine_hw_spi_init_internal(
184184
changed = true;
185185
}
186186

187-
if (self->host != HSPI_HOST && self->host != VSPI_HOST) {
188-
mp_raise_ValueError(MP_ERROR_TEXT("SPI ID must be either HSPI(1) or VSPI(2)"));
187+
if (self->host != HSPI_HOST
188+
#ifdef VSPI_HOST
189+
&& self->host != VSPI_HOST
190+
#endif
191+
) {
192+
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), self->host);
189193
}
190194

191195
if (changed) {
@@ -220,8 +224,10 @@ STATIC void machine_hw_spi_init_internal(
220224
int dma_chan = 0;
221225
if (self->host == HSPI_HOST) {
222226
dma_chan = 1;
227+
#ifdef VSPI_HOST
223228
} else if (self->host == VSPI_HOST) {
224229
dma_chan = 2;
230+
#endif
225231
}
226232

227233
ret = spi_bus_initialize(self->host, &buscfg, dma_chan);

ports/esp32/machine_i2c.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@
3434

3535
#define I2C_0_DEFAULT_SCL (GPIO_NUM_18)
3636
#define I2C_0_DEFAULT_SDA (GPIO_NUM_19)
37+
#if CONFIG_IDF_TARGET_ESP32
3738
#define I2C_1_DEFAULT_SCL (GPIO_NUM_25)
3839
#define I2C_1_DEFAULT_SDA (GPIO_NUM_26)
40+
#else
41+
#define I2C_1_DEFAULT_SCL (GPIO_NUM_9)
42+
#define I2C_1_DEFAULT_SDA (GPIO_NUM_8)
43+
#endif
3944

4045
#define I2C_DEFAULT_TIMEOUT_US (10000) // 10ms
4146

ports/esp32/machine_pin.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,17 @@ STATIC const machine_pin_obj_t machine_pin_obj[] = {
7777
{{&machine_pin_type}, GPIO_NUM_19},
7878
{{NULL}, -1},
7979
{{&machine_pin_type}, GPIO_NUM_21},
80+
#if CONFIG_IDF_TARGET_ESP32
8081
{{&machine_pin_type}, GPIO_NUM_22},
8182
{{&machine_pin_type}, GPIO_NUM_23},
8283
{{NULL}, -1},
8384
{{&machine_pin_type}, GPIO_NUM_25},
85+
#else
86+
{{NULL}, -1},
87+
{{NULL}, -1},
88+
{{NULL}, -1},
89+
{{NULL}, -1},
90+
#endif
8491
{{&machine_pin_type}, GPIO_NUM_26},
8592
{{&machine_pin_type}, GPIO_NUM_27},
8693
{{NULL}, -1},
@@ -411,10 +418,17 @@ STATIC const machine_pin_irq_obj_t machine_pin_irq_object[] = {
411418
{{&machine_pin_irq_type}, GPIO_NUM_19},
412419
{{NULL}, -1},
413420
{{&machine_pin_irq_type}, GPIO_NUM_21},
421+
#if CONFIG_IDF_TARGET_ESP32
414422
{{&machine_pin_irq_type}, GPIO_NUM_22},
415423
{{&machine_pin_irq_type}, GPIO_NUM_23},
416424
{{NULL}, -1},
417425
{{&machine_pin_irq_type}, GPIO_NUM_25},
426+
#else
427+
{{NULL}, -1},
428+
{{NULL}, -1},
429+
{{NULL}, -1},
430+
{{NULL}, -1},
431+
#endif
418432
{{&machine_pin_irq_type}, GPIO_NUM_26},
419433
{{&machine_pin_irq_type}, GPIO_NUM_27},
420434
{{NULL}, -1},

ports/esp32/machine_pwm.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ STATIC int chan_gpio[LEDC_CHANNEL_MAX];
5050
// 5khz
5151
#define PWFREQ (5000)
5252
// High speed mode
53+
#if CONFIG_IDF_TARGET_ESP32
5354
#define PWMODE (LEDC_HIGH_SPEED_MODE)
55+
#else
56+
#define PWMODE (LEDC_LOW_SPEED_MODE)
57+
#endif
5458
// 10-bit resolution (compatible with esp8266 PWM)
5559
#define PWRES (LEDC_TIMER_10_BIT)
5660
// Timer 1

ports/esp32/machine_sdcard.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include "py/mperrno.h"
3232
#include "extmod/vfs_fat.h"
3333

34+
#if MICROPY_HW_ENABLE_SDCARD
35+
3436
#include "driver/sdmmc_host.h"
3537
#include "driver/sdspi_host.h"
3638
#include "sdmmc_cmd.h"
@@ -50,10 +52,6 @@
5052
// Hosts are de-inited in __del__. Slots do not need de-initing.
5153
//
5254

53-
// Currently the ESP32 Library doesn't support MMC cards, so
54-
// we don't enable on MICROPY_HW_ENABLE_MMCARD.
55-
#if MICROPY_HW_ENABLE_SDCARD
56-
5755
// Forward declaration
5856
const mp_obj_type_t machine_sdcard_type;
5957

ports/esp32/machine_timer.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@
3030
#include <stdint.h>
3131
#include <stdio.h>
3232

33-
#include "driver/timer.h"
3433
#include "py/obj.h"
3534
#include "py/runtime.h"
3635
#include "modmachine.h"
3736
#include "mphalport.h"
3837

38+
#include "driver/timer.h"
39+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 1)
40+
#include "hal/timer_ll.h"
41+
#define HAVE_TIMER_LL (1)
42+
#endif
43+
3944
#define TIMER_INTR_SEL TIMER_INTR_LEVEL
4045
#define TIMER_DIVIDER 8
4146

@@ -127,6 +132,18 @@ STATIC void machine_timer_isr(void *self_in) {
127132
machine_timer_obj_t *self = self_in;
128133
timg_dev_t *device = self->group ? &(TIMERG1) : &(TIMERG0);
129134

135+
#if HAVE_TIMER_LL
136+
137+
#if CONFIG_IDF_TARGET_ESP32
138+
device->hw_timer[self->index].update = 1;
139+
#else
140+
device->hw_timer[self->index].update.update = 1;
141+
#endif
142+
timer_ll_clear_intr_status(device, self->index);
143+
timer_ll_set_alarm_enable(device, self->index, self->repeat);
144+
145+
#else
146+
130147
device->hw_timer[self->index].update = 1;
131148
if (self->index) {
132149
device->int_clr_timers.t1 = 1;
@@ -135,6 +152,8 @@ STATIC void machine_timer_isr(void *self_in) {
135152
}
136153
device->hw_timer[self->index].config.alarm_en = self->repeat;
137154

155+
#endif
156+
138157
mp_sched_schedule(self->callback, self);
139158
mp_hal_wake_main_task_from_isr();
140159
}

ports/esp32/machine_touchpad.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,15 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "py/runtime.h"
28+
#include "py/mphal.h"
29+
#include "modmachine.h"
2730

28-
#include <stdio.h>
29-
30-
#include "esp_log.h"
31+
#if CONFIG_IDF_TARGET_ESP32
3132

3233
#include "driver/gpio.h"
3334
#include "driver/touch_pad.h"
3435

35-
#include "py/runtime.h"
36-
#include "py/mphal.h"
37-
#include "modmachine.h"
38-
3936
typedef struct _mtp_obj_t {
4037
mp_obj_base_t base;
4138
gpio_num_t gpio_id;
@@ -120,3 +117,5 @@ const mp_obj_type_t machine_touchpad_type = {
120117
.make_new = mtp_make_new,
121118
.locals_dict = (mp_obj_t)&mtp_locals_dict,
122119
};
120+
121+
#endif // CONFIG_IDF_TARGET_ESP32

ports/esp32/machine_uart.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,12 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args,
307307
self->rx = 9;
308308
self->tx = 10;
309309
break;
310+
#if SOC_UART_NUM > 2
310311
case UART_NUM_2:
311312
self->rx = 16;
312313
self->tx = 17;
313314
break;
315+
#endif
314316
}
315317

316318
// Remove any existing configuration

ports/esp32/main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@
3737
#include "esp_task.h"
3838
#include "soc/cpu.h"
3939
#include "esp_log.h"
40+
41+
#if CONFIG_IDF_TARGET_ESP32
4042
#include "esp32/spiram.h"
43+
#elif CONFIG_IDF_TARGET_ESP32S2
44+
#include "esp32s2/spiram.h"
45+
#endif
4146

4247
#include "py/stackctrl.h"
4348
#include "py/nlr.h"

ports/esp32/main/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ set(IDF_COMPONENTS
8484
bootloader_support
8585
bt
8686
driver
87-
esp32
8887
esp_common
8988
esp_eth
9089
esp_event
@@ -123,6 +122,12 @@ if(IDF_VERSION_MINOR GREATER_EQUAL 3)
123122
list(APPEND IDF_COMPONENTS hal)
124123
endif()
125124

125+
if(IDF_TARGET STREQUAL "esp32")
126+
list(APPEND IDF_COMPONENTS esp32)
127+
elseif(IDF_TARGET STREQUAL "esp32s2")
128+
list(APPEND IDF_COMPONENTS esp32s2)
129+
endif()
130+
126131
# Register the main IDF component.
127132
idf_component_register(
128133
SRCS

ports/esp32/modesp32.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ STATIC mp_obj_t esp32_wake_on_ext1(size_t n_args, const mp_obj_t *pos_args, mp_m
132132
}
133133
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp32_wake_on_ext1_obj, 0, esp32_wake_on_ext1);
134134

135+
#if CONFIG_IDF_TARGET_ESP32
136+
135137
STATIC mp_obj_t esp32_raw_temperature(void) {
136138
SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR, 3, SENS_FORCE_XPD_SAR_S);
137139
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) {
154156
}
155157
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32_hall_sensor_obj, esp32_hall_sensor);
156158

159+
#endif
160+
157161
STATIC mp_obj_t esp32_idf_heap_info(const mp_obj_t cap_in) {
158162
mp_int_t cap = mp_obj_get_int(cap_in);
159163
multi_heap_info_t info;
@@ -182,14 +186,18 @@ STATIC const mp_rom_map_elem_t esp32_module_globals_table[] = {
182186
{ MP_ROM_QSTR(MP_QSTR_wake_on_touch), MP_ROM_PTR(&esp32_wake_on_touch_obj) },
183187
{ MP_ROM_QSTR(MP_QSTR_wake_on_ext0), MP_ROM_PTR(&esp32_wake_on_ext0_obj) },
184188
{ MP_ROM_QSTR(MP_QSTR_wake_on_ext1), MP_ROM_PTR(&esp32_wake_on_ext1_obj) },
189+
#if CONFIG_IDF_TARGET_ESP32
185190
{ MP_ROM_QSTR(MP_QSTR_raw_temperature), MP_ROM_PTR(&esp32_raw_temperature_obj) },
186191
{ MP_ROM_QSTR(MP_QSTR_hall_sensor), MP_ROM_PTR(&esp32_hall_sensor_obj) },
192+
#endif
187193
{ MP_ROM_QSTR(MP_QSTR_idf_heap_info), MP_ROM_PTR(&esp32_idf_heap_info_obj) },
188194

189195
{ MP_ROM_QSTR(MP_QSTR_NVS), MP_ROM_PTR(&esp32_nvs_type) },
190196
{ MP_ROM_QSTR(MP_QSTR_Partition), MP_ROM_PTR(&esp32_partition_type) },
191197
{ MP_ROM_QSTR(MP_QSTR_RMT), MP_ROM_PTR(&esp32_rmt_type) },
198+
#if CONFIG_IDF_TARGET_ESP32
192199
{ MP_ROM_QSTR(MP_QSTR_ULP), MP_ROM_PTR(&esp32_ulp_type) },
200+
#endif
193201

194202
{ MP_ROM_QSTR(MP_QSTR_WAKEUP_ALL_LOW), MP_ROM_FALSE },
195203
{ MP_ROM_QSTR(MP_QSTR_WAKEUP_ANY_HIGH), MP_ROM_TRUE },

ports/esp32/modmachine.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,18 @@
3232

3333
#include "freertos/FreeRTOS.h"
3434
#include "freertos/task.h"
35-
#include "esp32/rom/rtc.h"
36-
#include "esp32/clk.h"
3735
#include "esp_sleep.h"
3836
#include "esp_pm.h"
3937
#include "driver/touch_pad.h"
4038

39+
#if CONFIG_IDF_TARGET_ESP32
40+
#include "esp32/rom/rtc.h"
41+
#include "esp32/clk.h"
42+
#elif CONFIG_IDF_TARGET_ESP32S2
43+
#include "esp32s2/rom/rtc.h"
44+
#include "esp32s2/clk.h"
45+
#endif
46+
4147
#include "py/obj.h"
4248
#include "py/runtime.h"
4349
#include "lib/utils/pyexec.h"
@@ -71,7 +77,11 @@ STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) {
7177
if (freq != 20 && freq != 40 && freq != 80 && freq != 160 && freq != 240) {
7278
mp_raise_ValueError(MP_ERROR_TEXT("frequency must be 20MHz, 40MHz, 80Mhz, 160MHz or 240MHz"));
7379
}
80+
#if CONFIG_IDF_TARGET_ESP32
7481
esp_pm_config_esp32_t pm;
82+
#elif CONFIG_IDF_TARGET_ESP32S2
83+
esp_pm_config_esp32s2_t pm;
84+
#endif
7585
pm.max_freq_mhz = freq;
7686
pm.min_freq_mhz = freq;
7787
pm.light_sleep_enable = false;
@@ -260,7 +270,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
260270
{ MP_ROM_QSTR(MP_QSTR_DEEPSLEEP), MP_ROM_INT(MACHINE_WAKE_DEEPSLEEP) },
261271
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) },
262272
{ MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) },
273+
#if CONFIG_IDF_TARGET_ESP32
263274
{ MP_ROM_QSTR(MP_QSTR_TouchPad), MP_ROM_PTR(&machine_touchpad_type) },
275+
#endif
264276
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
265277
{ MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) },
266278
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hw_i2c_type) },

0 commit comments

Comments
 (0)
0