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 <[email protected]>
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
}

0 commit comments

Comments
 (0)