Skip to content

Commit 5093d49

Browse files
committed
esp32: Extend support for S2 series, and S3 where applicable.
Improvements made: - PSRAM support for S2 - partition definition for 16MiB flash - correct ADC and DAC pins - correct GPIO and IRQ pins - S3 components in CMakeLists Based on original commit made by Seon Rozenblum aka @UnexpectedMaker. Signed-off-by: Damien George <[email protected]>
1 parent 4cdcbdb commit 5093d49

File tree

9 files changed

+201
-16
lines changed

9 files changed

+201
-16
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# MicroPython on ESP32-S2 and ESP32-PAD1_subscript_3, ESP IDF configuration with SPIRAM support
2+
CONFIG_ESP32S2_SPIRAM_SUPPORT=y
3+
CONFIG_SPIRAM_TYPE_AUTO=y
4+
CONFIG_DEFAULT_PSRAM_CLK_IO=30
5+
CONFIG_DEFAULT_PSRAM_CS_IO=26
6+
CONFIG_SPIRAM_SPEED_80M=y
7+
CONFIG_SPIRAM=y
8+
CONFIG_SPIRAM_BOOT_INIT=y
9+
CONFIG_SPIRAM_IGNORE_NOTFOUND=y
10+
CONFIG_SPIRAM_USE_MEMMAP=y
11+
CONFIG_SPIRAM_MEMTEST=y

ports/esp32/machine_adc.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ typedef struct _madc_obj_t {
4343
} madc_obj_t;
4444

4545
STATIC const madc_obj_t madc_obj[] = {
46+
#if CONFIG_IDF_TARGET_ESP32
4647
{{&machine_adc_type}, GPIO_NUM_36, ADC1_CHANNEL_0},
4748
{{&machine_adc_type}, GPIO_NUM_37, ADC1_CHANNEL_1},
4849
{{&machine_adc_type}, GPIO_NUM_38, ADC1_CHANNEL_2},
@@ -51,6 +52,18 @@ STATIC const madc_obj_t madc_obj[] = {
5152
{{&machine_adc_type}, GPIO_NUM_33, ADC1_CHANNEL_5},
5253
{{&machine_adc_type}, GPIO_NUM_34, ADC1_CHANNEL_6},
5354
{{&machine_adc_type}, GPIO_NUM_35, ADC1_CHANNEL_7},
55+
#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
56+
{{&machine_adc_type}, GPIO_NUM_1, ADC1_CHANNEL_0},
57+
{{&machine_adc_type}, GPIO_NUM_2, ADC1_CHANNEL_1},
58+
{{&machine_adc_type}, GPIO_NUM_3, ADC1_CHANNEL_2},
59+
{{&machine_adc_type}, GPIO_NUM_4, ADC1_CHANNEL_3},
60+
{{&machine_adc_type}, GPIO_NUM_5, ADC1_CHANNEL_4},
61+
{{&machine_adc_type}, GPIO_NUM_6, ADC1_CHANNEL_5},
62+
{{&machine_adc_type}, GPIO_NUM_7, ADC1_CHANNEL_6},
63+
{{&machine_adc_type}, GPIO_NUM_8, ADC1_CHANNEL_7},
64+
{{&machine_adc_type}, GPIO_NUM_9, ADC1_CHANNEL_8},
65+
{{&machine_adc_type}, GPIO_NUM_10, ADC1_CHANNEL_9},
66+
#endif
5467
};
5568

5669
STATIC uint8_t adc_bit_width;
@@ -145,7 +158,7 @@ STATIC mp_obj_t madc_width(mp_obj_t cls_in, mp_obj_t width_in) {
145158
case ADC_WIDTH_12Bit:
146159
adc_bit_width = 12;
147160
break;
148-
#elif CONFIG_IDF_TARGET_ESP32S2
161+
#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
149162
case ADC_WIDTH_BIT_13:
150163
adc_bit_width = 13;
151164
break;
@@ -175,7 +188,7 @@ STATIC const mp_rom_map_elem_t madc_locals_dict_table[] = {
175188
{ MP_ROM_QSTR(MP_QSTR_WIDTH_10BIT), MP_ROM_INT(ADC_WIDTH_10Bit) },
176189
{ MP_ROM_QSTR(MP_QSTR_WIDTH_11BIT), MP_ROM_INT(ADC_WIDTH_11Bit) },
177190
{ MP_ROM_QSTR(MP_QSTR_WIDTH_12BIT), MP_ROM_INT(ADC_WIDTH_12Bit) },
178-
#elif CONFIG_IDF_TARGET_ESP32S2
191+
#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
179192
{ MP_ROM_QSTR(MP_QSTR_WIDTH_13BIT), MP_ROM_INT(ADC_WIDTH_BIT_13) },
180193
#endif
181194
};

ports/esp32/machine_pin.c

Lines changed: 117 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ typedef struct _machine_pin_irq_obj_t {
5656
} machine_pin_irq_obj_t;
5757

5858
STATIC const machine_pin_obj_t machine_pin_obj[] = {
59+
#if CONFIG_IDF_TARGET_ESP32
60+
5961
{{&machine_pin_type}, GPIO_NUM_0},
6062
{{&machine_pin_type}, GPIO_NUM_1},
6163
{{&machine_pin_type}, GPIO_NUM_2},
@@ -78,17 +80,10 @@ STATIC const machine_pin_obj_t machine_pin_obj[] = {
7880
{{&machine_pin_type}, GPIO_NUM_19},
7981
{{NULL}, -1},
8082
{{&machine_pin_type}, GPIO_NUM_21},
81-
#if CONFIG_IDF_TARGET_ESP32
8283
{{&machine_pin_type}, GPIO_NUM_22},
8384
{{&machine_pin_type}, GPIO_NUM_23},
8485
{{NULL}, -1},
8586
{{&machine_pin_type}, GPIO_NUM_25},
86-
#else
87-
{{NULL}, -1},
88-
{{NULL}, -1},
89-
{{NULL}, -1},
90-
{{NULL}, -1},
91-
#endif
9287
{{&machine_pin_type}, GPIO_NUM_26},
9388
{{&machine_pin_type}, GPIO_NUM_27},
9489
{{NULL}, -1},
@@ -103,6 +98,63 @@ STATIC const machine_pin_obj_t machine_pin_obj[] = {
10398
{{&machine_pin_type}, GPIO_NUM_37},
10499
{{&machine_pin_type}, GPIO_NUM_38},
105100
{{&machine_pin_type}, GPIO_NUM_39},
101+
102+
#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
103+
104+
{{&machine_pin_type}, GPIO_NUM_0},
105+
{{&machine_pin_type}, GPIO_NUM_1},
106+
{{&machine_pin_type}, GPIO_NUM_2},
107+
{{&machine_pin_type}, GPIO_NUM_3},
108+
{{&machine_pin_type}, GPIO_NUM_4},
109+
{{&machine_pin_type}, GPIO_NUM_5},
110+
{{&machine_pin_type}, GPIO_NUM_6},
111+
{{&machine_pin_type}, GPIO_NUM_7},
112+
{{&machine_pin_type}, GPIO_NUM_8},
113+
{{&machine_pin_type}, GPIO_NUM_9},
114+
{{&machine_pin_type}, GPIO_NUM_10},
115+
{{&machine_pin_type}, GPIO_NUM_11},
116+
{{&machine_pin_type}, GPIO_NUM_12},
117+
{{&machine_pin_type}, GPIO_NUM_13},
118+
{{&machine_pin_type}, GPIO_NUM_14},
119+
{{&machine_pin_type}, GPIO_NUM_15},
120+
{{&machine_pin_type}, GPIO_NUM_16},
121+
{{&machine_pin_type}, GPIO_NUM_17},
122+
{{&machine_pin_type}, GPIO_NUM_18},
123+
#if CONFIG_USB_CDC_ENABLED
124+
{{NULL}, -1}, // 19 is for native USB D-
125+
{{NULL}, -1}, // 20 is for native USB D-
126+
#else
127+
{{&machine_pin_type}, GPIO_NUM_19},
128+
{{&machine_pin_type}, GPIO_NUM_20},
129+
#endif
130+
{{&machine_pin_type}, GPIO_NUM_21},
131+
{{NULL}, -1}, // 22 not a pin
132+
{{NULL}, -1}, // 23 not a pin
133+
{{NULL}, -1}, // 24 not a pin
134+
{{NULL}, -1}, // 25 not a pin
135+
{{NULL}, -1}, // 26 FLASH/PSRAM
136+
{{NULL}, -1}, // 27 FLASH/PSRAM
137+
{{NULL}, -1}, // 28 FLASH/PSRAM
138+
{{NULL}, -1}, // 29 FLASH/PSRAM
139+
{{NULL}, -1}, // 30 FLASH/PSRAM
140+
{{NULL}, -1}, // 31 FLASH/PSRAM
141+
{{NULL}, -1}, // 32 FLASH/PSRAM
142+
{{&machine_pin_type}, GPIO_NUM_33},
143+
{{&machine_pin_type}, GPIO_NUM_34},
144+
{{&machine_pin_type}, GPIO_NUM_35},
145+
{{&machine_pin_type}, GPIO_NUM_36},
146+
{{&machine_pin_type}, GPIO_NUM_37},
147+
{{&machine_pin_type}, GPIO_NUM_38},
148+
{{&machine_pin_type}, GPIO_NUM_39}, // MTCLK
149+
{{&machine_pin_type}, GPIO_NUM_40}, // MTDO
150+
{{&machine_pin_type}, GPIO_NUM_41}, // MTDI
151+
{{&machine_pin_type}, GPIO_NUM_42}, // MTMS
152+
{{&machine_pin_type}, GPIO_NUM_43}, // U0TXD
153+
{{&machine_pin_type}, GPIO_NUM_44}, // U0RXD
154+
{{&machine_pin_type}, GPIO_NUM_45},
155+
{{&machine_pin_type}, GPIO_NUM_46},
156+
157+
#endif
106158
};
107159

108160
// forward declaration
@@ -399,6 +451,8 @@ const mp_obj_type_t machine_pin_type = {
399451
STATIC const mp_obj_type_t machine_pin_irq_type;
400452

401453
STATIC const machine_pin_irq_obj_t machine_pin_irq_object[] = {
454+
#if CONFIG_IDF_TARGET_ESP32
455+
402456
{{&machine_pin_irq_type}, GPIO_NUM_0},
403457
{{&machine_pin_irq_type}, GPIO_NUM_1},
404458
{{&machine_pin_irq_type}, GPIO_NUM_2},
@@ -421,17 +475,10 @@ STATIC const machine_pin_irq_obj_t machine_pin_irq_object[] = {
421475
{{&machine_pin_irq_type}, GPIO_NUM_19},
422476
{{NULL}, -1},
423477
{{&machine_pin_irq_type}, GPIO_NUM_21},
424-
#if CONFIG_IDF_TARGET_ESP32
425478
{{&machine_pin_irq_type}, GPIO_NUM_22},
426479
{{&machine_pin_irq_type}, GPIO_NUM_23},
427480
{{NULL}, -1},
428481
{{&machine_pin_irq_type}, GPIO_NUM_25},
429-
#else
430-
{{NULL}, -1},
431-
{{NULL}, -1},
432-
{{NULL}, -1},
433-
{{NULL}, -1},
434-
#endif
435482
{{&machine_pin_irq_type}, GPIO_NUM_26},
436483
{{&machine_pin_irq_type}, GPIO_NUM_27},
437484
{{NULL}, -1},
@@ -446,6 +493,62 @@ STATIC const machine_pin_irq_obj_t machine_pin_irq_object[] = {
446493
{{&machine_pin_irq_type}, GPIO_NUM_37},
447494
{{&machine_pin_irq_type}, GPIO_NUM_38},
448495
{{&machine_pin_irq_type}, GPIO_NUM_39},
496+
497+
#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
498+
499+
{{&machine_pin_irq_type}, GPIO_NUM_0},
500+
{{&machine_pin_irq_type}, GPIO_NUM_1},
501+
{{&machine_pin_irq_type}, GPIO_NUM_2},
502+
{{&machine_pin_irq_type}, GPIO_NUM_3},
503+
{{&machine_pin_irq_type}, GPIO_NUM_4},
504+
{{&machine_pin_irq_type}, GPIO_NUM_5},
505+
{{&machine_pin_irq_type}, GPIO_NUM_6},
506+
{{&machine_pin_irq_type}, GPIO_NUM_7},
507+
{{&machine_pin_irq_type}, GPIO_NUM_8},
508+
{{&machine_pin_irq_type}, GPIO_NUM_9},
509+
{{&machine_pin_irq_type}, GPIO_NUM_10},
510+
{{&machine_pin_irq_type}, GPIO_NUM_11},
511+
{{&machine_pin_irq_type}, GPIO_NUM_12},
512+
{{&machine_pin_irq_type}, GPIO_NUM_13},
513+
{{&machine_pin_irq_type}, GPIO_NUM_14},
514+
{{&machine_pin_irq_type}, GPIO_NUM_15},
515+
{{&machine_pin_irq_type}, GPIO_NUM_16},
516+
{{&machine_pin_irq_type}, GPIO_NUM_17},
517+
{{&machine_pin_irq_type}, GPIO_NUM_18},
518+
#if CONFIG_USB_CDC_ENABLED
519+
{{NULL}, -1}, // 19 is for native USB D-
520+
{{NULL}, -1}, // 20 is for native USB D-
521+
#else
522+
{{&machine_pin_irq_type}, GPIO_NUM_19},
523+
{{&machine_pin_irq_type}, GPIO_NUM_20},
524+
#endif
525+
{{&machine_pin_irq_type}, GPIO_NUM_21},
526+
{{NULL}, -1}, // 22 not a pin
527+
{{NULL}, -1}, // 23 not a pin
528+
{{NULL}, -1}, // 24 not a pin
529+
{{NULL}, -1}, // 25 not a pin
530+
{{NULL}, -1}, // 26 FLASH/PSRAM
531+
{{NULL}, -1}, // 27 FLASH/PSRAM
532+
{{NULL}, -1}, // 28 FLASH/PSRAM
533+
{{NULL}, -1}, // 29 FLASH/PSRAM
534+
{{NULL}, -1}, // 30 FLASH/PSRAM
535+
{{NULL}, -1}, // 31 FLASH/PSRAM
536+
{{NULL}, -1}, // 32 FLASH/PSRAM
537+
{{&machine_pin_irq_type}, GPIO_NUM_33},
538+
{{&machine_pin_irq_type}, GPIO_NUM_34},
539+
{{&machine_pin_irq_type}, GPIO_NUM_35},
540+
{{&machine_pin_irq_type}, GPIO_NUM_36},
541+
{{&machine_pin_irq_type}, GPIO_NUM_37},
542+
{{&machine_pin_irq_type}, GPIO_NUM_38},
543+
{{&machine_pin_irq_type}, GPIO_NUM_39},
544+
{{&machine_pin_irq_type}, GPIO_NUM_40},
545+
{{&machine_pin_irq_type}, GPIO_NUM_41},
546+
{{&machine_pin_irq_type}, GPIO_NUM_42},
547+
{{&machine_pin_irq_type}, GPIO_NUM_43},
548+
{{&machine_pin_irq_type}, GPIO_NUM_44},
549+
{{&machine_pin_irq_type}, GPIO_NUM_45},
550+
551+
#endif
449552
};
450553

451554
STATIC mp_obj_t machine_pin_irq_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {

ports/esp32/machine_touchpad.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,19 @@
3131
#if CONFIG_IDF_TARGET_ESP32
3232

3333
#include "driver/gpio.h"
34+
#if CONFIG_IDF_TARGET_ESP32
3435
#include "driver/touch_pad.h"
36+
#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
37+
#include "driver/touch_sensor.h"
38+
#endif
3539

3640
typedef struct _mtp_obj_t {
3741
mp_obj_base_t base;
3842
gpio_num_t gpio_id;
3943
touch_pad_t touchpad_id;
4044
} mtp_obj_t;
4145

46+
#if CONFIG_IDF_TARGET_ESP32
4247
STATIC const mtp_obj_t touchpad_obj[] = {
4348
{{&machine_touchpad_type}, GPIO_NUM_4, TOUCH_PAD_NUM0},
4449
{{&machine_touchpad_type}, GPIO_NUM_0, TOUCH_PAD_NUM1},
@@ -51,6 +56,24 @@ STATIC const mtp_obj_t touchpad_obj[] = {
5156
{{&machine_touchpad_type}, GPIO_NUM_33, TOUCH_PAD_NUM8},
5257
{{&machine_touchpad_type}, GPIO_NUM_32, TOUCH_PAD_NUM9},
5358
};
59+
#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
60+
STATIC const mtp_obj_t touchpad_obj[] = {
61+
{{&machine_touchpad_type}, GPIO_NUM_1, TOUCH_PAD_NUM1},
62+
{{&machine_touchpad_type}, GPIO_NUM_2, TOUCH_PAD_NUM2},
63+
{{&machine_touchpad_type}, GPIO_NUM_3, TOUCH_PAD_NUM3},
64+
{{&machine_touchpad_type}, GPIO_NUM_4, TOUCH_PAD_NUM4},
65+
{{&machine_touchpad_type}, GPIO_NUM_5, TOUCH_PAD_NUM5},
66+
{{&machine_touchpad_type}, GPIO_NUM_6, TOUCH_PAD_NUM6},
67+
{{&machine_touchpad_type}, GPIO_NUM_7, TOUCH_PAD_NUM7},
68+
{{&machine_touchpad_type}, GPIO_NUM_8, TOUCH_PAD_NUM8},
69+
{{&machine_touchpad_type}, GPIO_NUM_9, TOUCH_PAD_NUM9},
70+
{{&machine_touchpad_type}, GPIO_NUM_10, TOUCH_PAD_NUM10},
71+
{{&machine_touchpad_type}, GPIO_NUM_11, TOUCH_PAD_NUM11},
72+
{{&machine_touchpad_type}, GPIO_NUM_12, TOUCH_PAD_NUM12},
73+
{{&machine_touchpad_type}, GPIO_NUM_13, TOUCH_PAD_NUM13},
74+
{{&machine_touchpad_type}, GPIO_NUM_14, TOUCH_PAD_NUM14},
75+
};
76+
#endif
5477

5578
STATIC mp_obj_t mtp_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,
5679
const mp_obj_t *args) {

ports/esp32/main.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#include "esp32/spiram.h"
4343
#elif CONFIG_IDF_TARGET_ESP32S2
4444
#include "esp32s2/spiram.h"
45+
#elif CONFIG_IDF_TARGET_ESP32S3
46+
#include "esp32s3/spiram.h"
4547
#endif
4648

4749
#include "py/stackctrl.h"
@@ -104,6 +106,18 @@ void mp_task(void *pvParameter) {
104106
mp_task_heap = malloc(mp_task_heap_size);
105107
break;
106108
}
109+
#elif CONFIG_ESP32S2_SPIRAM_SUPPORT || CONFIG_ESP32S3_SPIRAM_SUPPORT
110+
// Try to use the entire external SPIRAM directly for the heap
111+
size_t mp_task_heap_size;
112+
size_t esp_spiram_size = esp_spiram_get_size();
113+
void *mp_task_heap = (void *)0x3ff80000 - esp_spiram_size;
114+
if (esp_spiram_size > 0) {
115+
mp_task_heap_size = esp_spiram_size;
116+
} else {
117+
// No SPIRAM, fallback to normal allocation
118+
mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
119+
mp_task_heap = malloc(mp_task_heap_size);
120+
}
107121
#else
108122
// Allocate the uPy heap using malloc and get the largest available region
109123
size_t mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);

ports/esp32/main/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ if(IDF_TARGET STREQUAL "esp32")
128128
elseif(IDF_TARGET STREQUAL "esp32s2")
129129
list(APPEND IDF_COMPONENTS esp32s2)
130130
list(APPEND IDF_COMPONENTS tinyusb)
131+
elseif(IDF_TARGET STREQUAL "esp32s3")
132+
list(APPEND IDF_COMPONENTS esp32s3)
133+
list(APPEND IDF_COMPONENTS tinyusb)
131134
endif()
132135

133136
# Register the main IDF component.

ports/esp32/modmachine.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
#elif CONFIG_IDF_TARGET_ESP32S2
4343
#include "esp32s2/rom/rtc.h"
4444
#include "esp32s2/clk.h"
45+
#elif CONFIG_IDF_TARGET_ESP32S3
46+
#include "esp32s3/rom/rtc.h"
47+
#include "esp32s3/clk.h"
4548
#endif
4649

4750
#include "py/obj.h"

ports/esp32/mphalport.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@
3232

3333
#include "freertos/FreeRTOS.h"
3434
#include "freertos/task.h"
35+
36+
#if CONFIG_IDF_TARGET_ESP32
3537
#include "esp32/rom/uart.h"
38+
#elif CONFIG_IDF_TARGET_ESP32S2
39+
#include "esp32s2/rom/uart.h"
40+
#elif CONFIG_IDF_TARGET_ESP32S3
41+
#include "esp32s3/rom/uart.h"
42+
#endif
3643

3744
#include "py/obj.h"
3845
#include "py/objstr.h"

ports/esp32/partitions-16MiB.csv

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Notes: the offset of the partition table itself is set in
2+
# $ESPIDF/components/partition_table/Kconfig.projbuild and the
3+
# offset of the factory/ota_0 partition is set in makeimg.py
4+
# Name, Type, SubType, Offset, Size, Flags
5+
nvs, data, nvs, 0x9000, 0x6000,
6+
phy_init, data, phy, 0xf000, 0x1000,
7+
factory, app, factory, 0x10000, 0x180000,
8+
vfs, data, fat, 0x200000, 0xD59F80,

0 commit comments

Comments
 (0)