Skip to content

Commit 3da86b0

Browse files
committed
add initial esp32c3 support
1 parent 10fdc80 commit 3da86b0

File tree

21 files changed

+477
-912
lines changed

21 files changed

+477
-912
lines changed

ports/espressif/Makefile

Lines changed: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ include $(TOP)/supervisor/supervisor.mk
5959
# Include make rules and variables common across CircuitPython builds.
6060
include $(TOP)/py/circuitpy_defns.mk
6161

62+
ifeq ($(IDF_TARGET),esp32s2)
63+
IDF_TARGET_ARCH = xtensa
6264
CROSS_COMPILE = xtensa-$(IDF_TARGET)-elf-
65+
else ifeq ($(IDF_TARGET),esp32c3)
66+
IDF_TARGET_ARCH = riscv
67+
CROSS_COMPILE = riscv32-esp-elf-
68+
endif
6369

6470
#######################################
6571
# CFLAGS
@@ -95,7 +101,7 @@ INC += -I.\
95101
-isystem esp-idf/components/esp_wifi/include \
96102
-isystem esp-idf/components/freertos/include \
97103
-isystem esp-idf/components/freertos/include/freertos \
98-
-isystem esp-idf/components/freertos/port/xtensa/include \
104+
-isystem esp-idf/components/freertos/port/$(IDF_TARGET_ARCH)/include \
99105
-isystem esp-idf/components/hal/include \
100106
-isystem esp-idf/components/hal/$(IDF_TARGET)/include \
101107
-isystem esp-idf/components/hal/platform_port/include \
@@ -112,8 +118,13 @@ INC += -I.\
112118
-isystem esp-idf/components/soc/include \
113119
-isystem esp-idf/components/soc/$(IDF_TARGET)/include \
114120
-isystem esp-idf/components/spi_flash/include \
115-
-isystem esp-idf/components/xtensa/include \
116-
-isystem esp-idf/components/xtensa/$(IDF_TARGET)/include
121+
-isystem esp-idf/components/$(IDF_TARGET_ARCH)/include \
122+
-isystem esp-idf/components/$(IDF_TARGET_ARCH)/$(IDF_TARGET)/include
123+
124+
# See https://github.com/espressif/esp-idf/issues/6906
125+
ifeq ($(IDF_TARGET),esp32c3)
126+
CFLAGS += -include "esp32c3_fix.h"
127+
endif
117128

118129
CFLAGS += -DHAVE_CONFIG_H \
119130
-DMBEDTLS_CONFIG_FILE=\"mbedtls/esp_config.h\" \
@@ -139,7 +150,11 @@ endif
139150
# option to override compiler optimization level, set in boards/$(BOARD)/mpconfigboard.mk
140151
CFLAGS += $(OPTIMIZATION_FLAGS)
141152

142-
CFLAGS += $(INC) -Werror -Wall -mlongcalls -std=gnu11 -Wl,--gc-sections $(BASE_CFLAGS) $(C_DEFS) $(CFLAGS_MOD) $(COPT)
153+
CFLAGS += $(INC) -Werror -Wall -std=gnu11 -Wl,--gc-sections $(BASE_CFLAGS) $(C_DEFS) $(CFLAGS_MOD) $(COPT)
154+
155+
ifeq ($(IDF_TARGET_ARCH),xtensa)
156+
CFLAGS += -mlongcalls
157+
endif
143158

144159
LDFLAGS = $(CFLAGS) -Wl,-nostdlib -Wl,-Map=$@.map -Wl,-cref -Wl,--undefined=uxTopUsedPriority
145160

@@ -153,9 +168,17 @@ LDFLAGS += -L$(BUILD)/esp-idf/esp-idf/$(IDF_TARGET) \
153168
-T$(IDF_TARGET).rom.ld \
154169
-T$(IDF_TARGET).rom.api.ld \
155170
-T$(IDF_TARGET).rom.libgcc.ld \
156-
-T$(IDF_TARGET).rom.newlib-data.ld \
171+
-T$(IDF_TARGET).rom.newlib-nano.ld
172+
173+
ifeq ($(IDF_TARGET),esp32c3)
174+
LDFLAGS += -Tesp32c3.rom.newlib.ld \
175+
-Tesp32c3.rom.version.ld \
176+
-Tesp32c3.rom.eco3.ld
177+
else
178+
LDFLAGS += -T$(IDF_TARGET).rom.newlib-data.ld \
157179
-T$(IDF_TARGET).rom.newlib-funcs.ld \
158180
-T$(IDF_TARGET).rom.spiflash.ld
181+
endif
159182

160183
LDFLAGS += -Wl,-Bstatic \
161184
-Wl,--no-warn-mismatch \
@@ -170,33 +193,44 @@ LIBS += -lm
170193
endif
171194

172195
# TinyUSB defines
173-
CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_ESP32S2 -DCFG_TUSB_OS=OPT_OS_FREERTOS -DCFG_TUD_CDC_RX_BUFSIZE=1024 -DCFG_TUD_CDC_TX_BUFSIZE=1024
174-
CFLAGS += -DCFG_TUD_MSC_BUFSIZE=4096 -DCFG_TUD_MIDI_RX_BUFSIZE=128 -DCFG_TUD_MIDI_TX_BUFSIZE=128
175-
CFLAGS += -DCFG_TUD_VENDOR_RX_BUFSIZE=128 -DCFG_TUD_VENDOR_TX_BUFSIZE=128
176-
196+
ifeq ($(CIRCUITPY_USB),1)
197+
CFLAGS += \
198+
-DCFG_TUSB_MCU=OPT_MCU_ESP32S2 \
199+
-DCFG_TUSB_OS=OPT_OS_FREERTOS \
200+
-DCFG_TUD_CDC_RX_BUFSIZE=1024 \
201+
-DCFG_TUD_CDC_TX_BUFSIZE=1024 \
202+
-DCFG_TUD_MSC_BUFSIZE=4096 \
203+
-DCFG_TUD_MIDI_RX_BUFSIZE=128 \
204+
-DCFG_TUD_MIDI_TX_BUFSIZE=128 \
205+
-DCFG_TUD_VENDOR_RX_BUFSIZE=128 \
206+
-DCFG_TUD_VENDOR_TX_BUFSIZE=128
207+
endif
177208

178209
######################################
179210
# source
180211
######################################
181212

182-
183213
SRC_C += \
184214
background.c \
185-
cam.c \
186-
i2s_lcd_esp32s2_driver.c \
187215
fatfs_port.c \
188216
mphalport.c \
189217
bindings/espidf/__init__.c \
190218
boards/$(BOARD)/board.c \
191219
boards/$(BOARD)/pins.c \
192220
modules/$(CIRCUITPY_MODULE).c \
193221
lib/netutils/netutils.c \
194-
peripherals/pcnt.c \
195222
peripherals/rmt.c \
196223
peripherals/timer.c \
197-
peripherals/touch.c \
198224
peripherals/$(IDF_TARGET)/pins.c
199225

226+
ifneq ($(IDF_TARGET),esp32c3)
227+
SRC_C += \
228+
cam.c \
229+
i2s_lcd_esp32s2_driver.c \
230+
peripherals/pcnt.c \
231+
peripherals/touch.c
232+
endif
233+
200234
$(BUILD)/i2s_lcd_esp32s2_driver.o: CFLAGS += -Wno-sign-compare
201235

202236
ifneq ($(CIRCUITPY_USB),0)
@@ -251,7 +285,7 @@ ifeq ($(DEBUG), 1)
251285
else
252286
DEBUG_SDKCONFIG = esp-idf-config/sdkconfig-opt.defaults
253287
endif
254-
SDKCONFIGS = esp-idf-config/$(IDF_TARGET)/sdkconfig.defaults;$(DEBUG_SDKCONFIG);$(FLASH_SDKCONFIG);boards/$(BOARD)/sdkconfig
288+
SDKCONFIGS = esp-idf-config/sdkconfig.defaults;$(DEBUG_SDKCONFIG);$(FLASH_SDKCONFIG);boards/$(BOARD)/sdkconfig
255289

256290
# create the config headers
257291
$(BUILD)/esp-idf/config/sdkconfig.h: boards/$(BOARD)/sdkconfig | $(BUILD)/esp-idf
@@ -280,26 +314,46 @@ menuconfig: $(BUILD)/esp-idf/config
280314
# qstr builds include headers so we need to make sure they are up to date
281315
$(HEADER_BUILD)/qstr.split: | $(BUILD)/esp-idf/config/sdkconfig.h
282316

283-
ESP_IDF_COMPONENTS_LINK = $(IDF_TARGET) app_update bootloader_support driver efuse esp_adc_cal esp_common esp_event esp_hw_support esp_netif esp_pm esp_ringbuf esp_rom esp_system esp_timer esp-tls esp_wifi freertos hal heap log lwip mbedtls newlib nvs_flash pthread soc spi_flash vfs wpa_supplicant xtensa
317+
ESP_IDF_COMPONENTS_LINK = $(IDF_TARGET) $(IDF_TARGET_ARCH) app_update bootloader_support driver efuse esp_adc_cal esp_common esp_event esp_hw_support esp_netif esp_pm esp_ringbuf esp_rom esp_system esp_timer esp-tls esp_wifi freertos hal heap log lwip mbedtls newlib nvs_flash pthread soc spi_flash vfs wpa_supplicant
284318

285319
ESP_IDF_COMPONENTS_EXPANDED = $(foreach component, $(ESP_IDF_COMPONENTS_LINK), $(BUILD)/esp-idf/esp-idf/$(component)/lib$(component).a)
286320
ESP_IDF_WIFI_COMPONENTS_EXPANDED = $(foreach component, $(ESP_IDF_WIFI_COMPONENTS_LINK), $(BUILD)/esp-idf/esp-idf/$(component)/lib$(component).a)
287321

288322
MBEDTLS_COMPONENTS_LINK = crypto tls x509
289323
MBEDTLS_COMPONENTS_LINK_EXPANDED = $(foreach component, $(MBEDTLS_COMPONENTS_LINK), $(BUILD)/esp-idf/esp-idf/mbedtls/mbedtls/library/libmbed$(component).a)
290324

291-
BINARY_BLOBS = esp-idf/components/xtensa/$(IDF_TARGET)/libxt_hal.a
292-
BINARY_WIFI_BLOBS = libcoexist.a libcore.a libespnow.a libmesh.a libnet80211.a libpp.a librtc.a libsmartconfig.a libphy.a
293-
BINARY_BLOBS += $(addprefix esp-idf/components/esp_wifi/lib/$(IDF_TARGET)/, $(BINARY_WIFI_BLOBS))
325+
BINARY_WIFI_BLOBS = libcoexist.a libcore.a libespnow.a libmesh.a libnet80211.a libpp.a libsmartconfig.a libphy.a
326+
ifneq ($(IDF_TARGET),esp32c3)
327+
BINARY_WIFI_BLOBS += librtc.a
328+
endif
329+
BINARY_BLOBS = $(addprefix esp-idf/components/esp_wifi/lib/$(IDF_TARGET)/, $(BINARY_WIFI_BLOBS))
294330

331+
ifeq ($(IDF_TARGET_ARCH),xtensa)
332+
BINARY_BLOBS += esp-idf/components/xtensa/$(IDF_TARGET)/libxt_hal.a
295333
ESP_IDF_COMPONENTS_EXPANDED += esp-idf/components/xtensa/$(IDF_TARGET)/libxt_hal.a
334+
endif
335+
336+
ifeq ($(IDF_TARGET), esp32c3)
337+
ESPTOOL_BAUDRATE = 230400
338+
BOOTLOADER_OFFSET = 0x0000
339+
else
340+
ESPTOOL_BAUDRATE = 460800
341+
BOOTLOADER_OFFSET = 0x1000
342+
endif
343+
PARTITION_TABLE_OFFSET = 0x8000
344+
FIRMWARE_OFFSET = 0x10000
345+
296346
ESP_AUTOGEN_LD = $(BUILD)/esp-idf/esp-idf/$(IDF_TARGET)/$(IDF_TARGET)_out.ld $(BUILD)/esp-idf/esp-idf/$(IDF_TARGET)/ld/$(IDF_TARGET).project.ld
297347

298348
FLASH_FLAGS = --flash_mode $(CIRCUITPY_ESP_FLASH_MODE) --flash_freq $(CIRCUITPY_ESP_FLASH_FREQ) --flash_size $(CIRCUITPY_ESP_FLASH_SIZE)
299349

300-
ESPTOOL_FLAGS ?= -b 460800 --before=default_reset --after=no_reset
350+
ESPTOOL_FLAGS ?= -b $(ESPTOOL_BAUDRATE) --before=default_reset --after=no_reset
301351

352+
ifeq ($(IDF_TARGET),esp32c3)
353+
all: $(BUILD)/firmware.bin
354+
else
302355
all: $(BUILD)/firmware.bin $(BUILD)/firmware.uf2
356+
endif
303357

304358
.PHONY: esp-idf-stamp
305359
esp-idf-stamp: $(BUILD)/esp-idf/config/sdkconfig.h
@@ -336,7 +390,7 @@ esp-idf-stamp: $(BUILD)/esp-idf/config/sdkconfig.h
336390
esp-idf/spi_flash/libspi_flash.a \
337391
esp-idf/vfs/libvfs.a \
338392
esp-idf/wpa_supplicant/libwpa_supplicant.a \
339-
esp-idf/xtensa/libxtensa.a
393+
esp-idf/$(IDF_TARGET_ARCH)/lib$(IDF_TARGET_ARCH).a
340394

341395
$(BUILD)/firmware.elf: $(OBJ) | esp-idf-stamp
342396
$(STEPECHO) "LINK $@"
@@ -348,7 +402,7 @@ $(BUILD)/circuitpython-firmware.bin: $(BUILD)/firmware.elf | tools/build_memory_
348402
$(Q)$(PYTHON3) tools/build_memory_info.py $< $(BUILD)/esp-idf/sdkconfig $@
349403

350404
$(BUILD)/firmware.bin: $(BUILD)/circuitpython-firmware.bin | esp-idf-stamp
351-
$(Q)$(PYTHON) ../../tools/join_bins.py $@ 0x1000 $(BUILD)/esp-idf/bootloader/bootloader.bin 0x8000 $(BUILD)/esp-idf/partition_table/partition-table.bin 0x10000 $(BUILD)/circuitpython-firmware.bin
405+
$(Q)$(PYTHON) ../../tools/join_bins.py $@ $(BOOTLOADER_OFFSET) $(BUILD)/esp-idf/bootloader/bootloader.bin $(PARTITION_TABLE_OFFSET) $(BUILD)/esp-idf/partition_table/partition-table.bin $(FIRMWARE_OFFSET) $(BUILD)/circuitpython-firmware.bin
352406

353407
$(BUILD)/firmware.uf2: $(BUILD)/circuitpython-firmware.bin
354408
$(STEPECHO) "Create $@"
@@ -358,7 +412,7 @@ flash: $(BUILD)/firmware.bin
358412
esptool.py --chip $(IDF_TARGET) -p $(PORT) $(ESPTOOL_FLAGS) write_flash $(FLASH_FLAGS) 0x0000 $^
359413

360414
flash-circuitpython-only: $(BUILD)/circuitpython-firmware.bin
361-
esptool.py --chip $(IDF_TARGET) -p $(PORT) $(ESPTOOL_FLAGS) write_flash $(FLASH_FLAGS) 0x10000 $^
415+
esptool.py --chip $(IDF_TARGET) -p $(PORT) $(ESPTOOL_FLAGS) write_flash $(FLASH_FLAGS) $(FIRMWARE_OFFSET) $^
362416

363417
monitor: $(BUILD)/firmware.elf
364418
cp $< build/circuitpython.elf

ports/espressif/common-hal/alarm/__init__.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
#include "esp_sleep.h"
4646

47-
#include "components/soc/esp32s2/include/soc/rtc_cntl_reg.h"
47+
#include "soc/rtc_cntl_reg.h"
4848
#include "components/driver/include/driver/uart.h"
4949

5050
// Singleton instance of SleepMemory.

ports/espressif/common-hal/alarm/pin/PinAlarm.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@
3232
#include "shared-bindings/microcontroller/Pin.h"
3333
#include "shared-bindings/microcontroller/__init__.h"
3434

35+
#include "hal/gpio_ll.h"
36+
#include "esp_debug_helpers.h"
37+
3538
#include "components/driver/include/driver/rtc_io.h"
3639
#include "components/esp_system/include/esp_sleep.h"
37-
#include "components/hal/esp32s2/include/hal/gpio_ll.h"
38-
#include "components/xtensa/include/esp_debug_helpers.h"
3940
#include "components/freertos/include/freertos/FreeRTOS.h"
4041

4142
void common_hal_alarm_pin_pinalarm_construct(alarm_pin_pinalarm_obj_t *self, const mcu_pin_obj_t *pin, bool value, bool edge, bool pull) {

ports/espressif/common-hal/microcontroller/Processor.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
#include "esp_system.h"
3939

4040
#include "soc/efuse_reg.h"
41-
42-
#include "components/driver/esp32s2/include/driver/temp_sensor.h"
41+
#include "driver/temp_sensor.h"
4342

4443
float common_hal_mcu_processor_get_temperature(void) {
4544
float tsens_out;

ports/espressif/common-hal/microcontroller/__init__.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@
4545
#include "soc/rtc_cntl_reg.h"
4646
#include "esp_private/system_internal.h"
4747

48+
#ifdef CONFIG_IDF_TARGET_ESP32C3
49+
#include "esp32c3/rom/rtc.h"
50+
#elif CONFIG_IDF_TARGET_ESP32S2
4851
#include "esp32s2/rom/rtc.h"
4952
#include "esp32s2/rom/usb/usb_persist.h"
5053
#include "esp32s2/rom/usb/chip_usb_dw_wrapper.h"
54+
#endif
55+
5156

5257
void common_hal_mcu_delay_us(uint32_t delay) {
5358
mp_hal_delay_us(delay);
@@ -77,8 +82,10 @@ void common_hal_mcu_enable_interrupts(void) {
7782
void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) {
7883
switch (runmode) {
7984
case RUNMODE_UF2:
85+
#ifndef CONFIG_IDF_TARGET_ESP32C3
8086
// 0x11F2 is APP_REQUEST_UF2_RESET_HINT & is defined by TinyUF2
8187
esp_reset_reason_set_hint(0x11F2);
88+
#endif
8289
break;
8390
case RUNMODE_NORMAL:
8491
// revert back to normal boot
@@ -92,7 +99,9 @@ void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) {
9299
break;
93100
case RUNMODE_BOOTLOADER:
94101
// DFU download
102+
#ifndef CONFIG_IDF_TARGET_ESP32C3
95103
chip_usb_set_persist_flags(USBDC_BOOT_DFU);
104+
#endif
96105
REG_WRITE(RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT);
97106
break;
98107
default:
@@ -159,7 +168,7 @@ STATIC const mp_rom_map_elem_t mcu_pin_global_dict_table[] = {
159168
{ MP_ROM_QSTR(MP_QSTR_GPIO19), MP_ROM_PTR(&pin_GPIO19) },
160169
{ MP_ROM_QSTR(MP_QSTR_GPIO20), MP_ROM_PTR(&pin_GPIO20) },
161170
{ MP_ROM_QSTR(MP_QSTR_GPIO21), MP_ROM_PTR(&pin_GPIO21) },
162-
171+
#ifndef CONFIG_IDF_TARGET_ESP32C3
163172
{ MP_ROM_QSTR(MP_QSTR_GPIO26), MP_ROM_PTR(&pin_GPIO26) },
164173
{ MP_ROM_QSTR(MP_QSTR_GPIO27), MP_ROM_PTR(&pin_GPIO27) },
165174
{ MP_ROM_QSTR(MP_QSTR_GPIO28), MP_ROM_PTR(&pin_GPIO28) },
@@ -181,5 +190,6 @@ STATIC const mp_rom_map_elem_t mcu_pin_global_dict_table[] = {
181190
{ MP_ROM_QSTR(MP_QSTR_GPIO44), MP_ROM_PTR(&pin_GPIO44) },
182191
{ MP_ROM_QSTR(MP_QSTR_GPIO45), MP_ROM_PTR(&pin_GPIO45) },
183192
{ MP_ROM_QSTR(MP_QSTR_GPIO46), MP_ROM_PTR(&pin_GPIO46) },
193+
#endif
184194
};
185195
MP_DEFINE_CONST_DICT(mcu_pin_globals, mcu_pin_global_dict_table);

ports/espressif/common-hal/pulseio/PulseIn.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ STATIC pulseio_pulsein_obj_t *handles[RMT_CHANNEL_MAX];
3434
// Requires rmt.c void peripherals_reset_all(void) to reset
3535

3636
STATIC void update_internal_buffer(pulseio_pulsein_obj_t *self) {
37-
uint32_t length = 0;
37+
size_t length = 0;
3838
rmt_item32_t *items = (rmt_item32_t *)xRingbufferReceive(self->buf_handle, &length, 0);
3939
if (items) {
4040
length /= 4;

ports/espressif/common-hal/socketpool/Socket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ bool common_hal_socketpool_socket_listen(socketpool_socket_obj_t *self, int back
203203
}
204204

205205
mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t *self,
206-
uint8_t *buf, uint32_t len, uint8_t *ip, uint *port) {
206+
uint8_t *buf, uint32_t len, uint8_t *ip, uint32_t *port) {
207207

208208
struct sockaddr_in source_addr;
209209
socklen_t socklen = sizeof(source_addr);

0 commit comments

Comments
 (0)