From 34441b1af5143205415e507fafad33e516b00c03 Mon Sep 17 00:00:00 2001 From: Artem Lytkin Date: Sat, 5 Sep 2026 01:05:54 +0300 Subject: [PATCH 01/12] audiocore: fix WaveFile 8-bit sample padding at end of file The last buffer was padded by length_read % 4 bytes instead of 4 - length_read % 4, so a 5 or 7 byte tail still ended unaligned. Round caller-supplied buffer halves down to a multiple of 4 so the pad always fits, and add a unix coverage test. --- shared-module/audiocore/WaveFile.c | 5 +- tests/circuitpython/wavefile_8bit_padding.py | 76 +++++++++++++++++++ .../wavefile_8bit_padding.py.exp | 7 ++ 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 tests/circuitpython/wavefile_8bit_padding.py create mode 100644 tests/circuitpython/wavefile_8bit_padding.py.exp diff --git a/shared-module/audiocore/WaveFile.c b/shared-module/audiocore/WaveFile.c index 77ca2445edb..2bd3849e192 100644 --- a/shared-module/audiocore/WaveFile.c +++ b/shared-module/audiocore/WaveFile.c @@ -114,7 +114,8 @@ void common_hal_audioio_wavefile_construct(audioio_wavefile_obj_t *self, // Try to allocate two buffers, one will be loaded from file and the other // DMAed to DAC. if (buffer_size) { - self->len = buffer_size / 2; + // Each half must be a multiple of 4 bytes so the last buffer can be padded in place. + self->len = buffer_size / 2 / sizeof(uint32_t) * sizeof(uint32_t); self->buffer = buffer; self->second_buffer = buffer + self->len; } else { @@ -193,7 +194,7 @@ audioio_get_buffer_result_t audioio_wavefile_get_buffer(audioio_wavefile_obj_t * self->bytes_remaining -= length_read; // Pad the last buffer to word align it. if (self->bytes_remaining == 0 && length_read % sizeof(uint32_t) != 0) { - uint32_t pad = length_read % sizeof(uint32_t); + uint32_t pad = sizeof(uint32_t) - length_read % sizeof(uint32_t); length_read += pad; if (self->base.bits_per_sample == 8) { for (uint32_t i = 0; i < pad; i++) { diff --git a/tests/circuitpython/wavefile_8bit_padding.py b/tests/circuitpython/wavefile_8bit_padding.py new file mode 100644 index 00000000000..ae9b016cee0 --- /dev/null +++ b/tests/circuitpython/wavefile_8bit_padding.py @@ -0,0 +1,76 @@ +# The last buffer of a WaveFile is padded to a multiple of 4 bytes; for 8-bit +# samples the pad is 0x80 (silence) and never more than 3 bytes. +try: + import os, audiocore + + os.VfsFat +except (ImportError, AttributeError): + print("SKIP") + raise SystemExit + + +class RAMFS: + SEC_SIZE = 512 + + def __init__(self, blocks): + self.data = bytearray(blocks * self.SEC_SIZE) + + def readblocks(self, n, buf): + for i in range(len(buf)): + buf[i] = self.data[n * self.SEC_SIZE + i] + return 0 + + def writeblocks(self, n, buf): + for i in range(len(buf)): + self.data[n * self.SEC_SIZE + i] = buf[i] + return 0 + + def ioctl(self, op, arg): + if op == 4: # MP_BLOCKDEV_IOCTL_BLOCK_COUNT + return len(self.data) // self.SEC_SIZE + if op == 5: # MP_BLOCKDEV_IOCTL_BLOCK_SIZE + return self.SEC_SIZE + + +def le(value, size): + return value.to_bytes(size, "little") + + +def wav_u8_mono(data): + # PCM, mono, 16 kHz, 8 bits per sample + fmt = le(1, 2) + le(1, 2) + le(16000, 4) + le(16000, 4) + le(1, 2) + le(8, 2) + return ( + b"RIFF" + + le(4 + 8 + len(fmt) + 8 + len(data), 4) + + b"WAVEfmt " + + le(len(fmt), 4) + + fmt + + b"data" + + le(len(data), 4) + + data + ) + + +bdev = RAMFS(50) +os.VfsFat.mkfs(bdev) +fs = os.VfsFat(bdev) + +for length in range(4, 9): + name = "u8-%d.wav" % length + with fs.open(name, "wb") as f: + f.write(wav_u8_mono(bytes(range(32, 32 + length)))) + with fs.open(name, "rb") as f: + sample = audiocore.WaveFile(f) + audiocore.reset_buffer(sample) # what an AudioOut does on play() + result, buf = audiocore.get_buffer(sample) + print(length, result, len(buf), list(buf)) + +# A caller-supplied buffer is split into halves that are a multiple of 4 bytes, +# so padding the last buffer stays inside its half. +with fs.open("u8-5.wav", "rb") as f: + sample = audiocore.WaveFile(f, bytearray(10)) + audiocore.reset_buffer(sample) + result = 1 + while result == 1: + result, buf = audiocore.get_buffer(sample) + print(result, len(buf), list(buf)) diff --git a/tests/circuitpython/wavefile_8bit_padding.py.exp b/tests/circuitpython/wavefile_8bit_padding.py.exp new file mode 100644 index 00000000000..e49e2cb1c2c --- /dev/null +++ b/tests/circuitpython/wavefile_8bit_padding.py.exp @@ -0,0 +1,7 @@ +4 0 4 [32, 33, 34, 35] +5 0 8 [32, 33, 34, 35, 36, 128, 128, 128] +6 0 8 [32, 33, 34, 35, 36, 37, 128, 128] +7 0 8 [32, 33, 34, 35, 36, 37, 38, 128] +8 0 8 [32, 33, 34, 35, 36, 37, 38, 39] +1 4 [32, 33, 34, 35] +0 4 [36, 128, 128, 128] From 1727222d93718184287b7a10f949a19abd5faba2 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Tue, 1 Sep 2026 19:53:10 -0700 Subject: [PATCH 02/12] espressif: add esp32c5 target support C5 is the first dual-band ESP32. It differs from c6 in ways that build cleanly but fail at run time: - bootloader flash offset is 0x2000, not 0x0 - ADC1 channels start at GPIO1, not GPIO0 - efuse MAC registers are named like c61, not c6 - libphy.a references _rom_eco_version, so rom.version.ld must link - USB-Serial-JTAG pads (GPIO13/14) are cleared by gpio_ll_func_sel(), so reset_all_pins() must skip them along with the flash/PSRAM MSPI pins and VDD_SPI - boot button is GPIO28, not GPIO9 or GPIO0 - the 48MHz crystal leaves only 40/80/160/240MHz CPU valid - TWAI is the newer TWAI-FD, so canio is off pending a backend --- ports/espressif/Makefile | 28 ++++++++ .../common-hal/analogbufio/BufferedIn.c | 2 +- .../espressif/common-hal/analogio/AnalogIn.c | 2 + ports/espressif/common-hal/espidf/__init__.c | 3 +- .../common-hal/microcontroller/Pin.c | 26 +++++++ .../common-hal/microcontroller/Processor.c | 8 ++- .../common-hal/microcontroller/__init__.c | 5 +- .../esp-idf-config/sdkconfig-esp32c5.defaults | 54 ++++++++++++++ ports/espressif/mpconfigport.h | 2 + ports/espressif/mpconfigport.mk | 38 ++++++++++ ports/espressif/peripherals/esp32c5/pins.c | 37 ++++++++++ ports/espressif/peripherals/esp32c5/pins.h | 70 +++++++++++++++++++ ports/espressif/peripherals/pins.h | 2 + ports/espressif/tools/build_memory_info.py | 5 ++ 14 files changed, 277 insertions(+), 5 deletions(-) create mode 100644 ports/espressif/esp-idf-config/sdkconfig-esp32c5.defaults create mode 100644 ports/espressif/peripherals/esp32c5/pins.c create mode 100644 ports/espressif/peripherals/esp32c5/pins.h diff --git a/ports/espressif/Makefile b/ports/espressif/Makefile index ca70127565f..b772e384410 100644 --- a/ports/espressif/Makefile +++ b/ports/espressif/Makefile @@ -402,6 +402,29 @@ CHIP_COMPONENTS = \ esp_hal_twai \ esp_hal_usb +else ifeq ($(IDF_TARGET),esp32c5) +LDFLAGS += \ + -Tesp32c5.rom.phy.ld \ + -Tesp32c5.rom.pp.ld \ + -Tesp32c5.rom.net80211.ld \ + -Tesp32c5.rom.libc.ld \ + -Tesp32c5.rom.version.ld \ + -Tesp32c5.rom.coexist.ld \ + -Tesp32c5.rom.heap.ld \ + -Tesp32c5.rom.systimer.ld \ + -Trom.wdt.ld + +CHIP_COMPONENTS = \ + esp_driver_tsens \ + esp_driver_usb_serial_jtag \ + esp_hal_dma \ + esp_hal_i2s \ + esp_hal_pcnt \ + esp_hal_pmu \ + esp_hal_rmt \ + esp_hal_twai \ + esp_hal_usb + else ifeq ($(IDF_TARGET),esp32c6) LDFLAGS += \ -Tesp32c6.rom.phy.ld \ @@ -565,6 +588,8 @@ else ifeq ($(IDF_TARGET),esp32c2) CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_ESP32C2 else ifeq ($(IDF_TARGET),esp32c3) CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_ESP32C3 +else ifeq ($(IDF_TARGET),esp32c5) +CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_ESP32C5 else ifeq ($(IDF_TARGET),esp32c6) CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_ESP32C6 else ifeq ($(IDF_TARGET),esp32c61) @@ -912,6 +937,7 @@ ifneq ($(CIRCUITPY_BLEIO_NATIVE),0) BLE_IMPL_esp32s3 := esp32c3 BLE_IMPL_esp32c2 := libble BLE_IMPL_esp32c3 := esp32c3 + BLE_IMPL_esp32c5 := libble BLE_IMPL_esp32c6 := libble BLE_IMPL_esp32c61 := libble BLE_IMPL_esp32h2 := libble @@ -1006,6 +1032,8 @@ else ifeq ($(IDF_TARGET),esp32c2) BOOTLOADER_OFFSET = 0x0 else ifeq ($(IDF_TARGET),esp32c3) BOOTLOADER_OFFSET = 0x0 +else ifeq ($(IDF_TARGET),esp32c5) +BOOTLOADER_OFFSET = 0x2000 else ifeq ($(IDF_TARGET),esp32c6) BOOTLOADER_OFFSET = 0x0 else ifeq ($(IDF_TARGET),esp32c61) diff --git a/ports/espressif/common-hal/analogbufio/BufferedIn.c b/ports/espressif/common-hal/analogbufio/BufferedIn.c index 747d7d53878..e91c8aafca8 100644 --- a/ports/espressif/common-hal/analogbufio/BufferedIn.c +++ b/ports/espressif/common-hal/analogbufio/BufferedIn.c @@ -33,7 +33,7 @@ #elif defined(CONFIG_IDF_TARGET_ESP32S2) #define ADC_RESULT_BYTE 2 #define ADC_CONV_LIMIT_EN 0 -#elif defined(CONFIG_IDF_TARGET_ESP32C2) || defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32C6) || defined(CONFIG_IDF_TARGET_ESP32H2) || defined(CONFIG_IDF_TARGET_ESP32P4) || defined(CONFIG_IDF_TARGET_ESP32C61) +#elif defined(CONFIG_IDF_TARGET_ESP32C2) || defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32C5) || defined(CONFIG_IDF_TARGET_ESP32C6) || defined(CONFIG_IDF_TARGET_ESP32H2) || defined(CONFIG_IDF_TARGET_ESP32P4) || defined(CONFIG_IDF_TARGET_ESP32C61) #define ADC_RESULT_BYTE 4 #define ADC_CONV_LIMIT_EN 0 #elif defined(CONFIG_IDF_TARGET_ESP32S3) diff --git a/ports/espressif/common-hal/analogio/AnalogIn.c b/ports/espressif/common-hal/analogio/AnalogIn.c index 9809962b394..974cba8c5c9 100644 --- a/ports/espressif/common-hal/analogio/AnalogIn.c +++ b/ports/espressif/common-hal/analogio/AnalogIn.c @@ -30,6 +30,8 @@ #define DATA_WIDTH ADC_BITWIDTH_12 #elif defined(CONFIG_IDF_TARGET_ESP32C3) #define DATA_WIDTH ADC_BITWIDTH_12 +#elif defined(CONFIG_IDF_TARGET_ESP32C5) +#define DATA_WIDTH ADC_BITWIDTH_12 #elif defined(CONFIG_IDF_TARGET_ESP32C6) #define DATA_WIDTH ADC_BITWIDTH_12 #elif defined(CONFIG_IDF_TARGET_ESP32C61) diff --git a/ports/espressif/common-hal/espidf/__init__.c b/ports/espressif/common-hal/espidf/__init__.c index 83e30657750..bfb50a04ec7 100644 --- a/ports/espressif/common-hal/espidf/__init__.c +++ b/ports/espressif/common-hal/espidf/__init__.c @@ -27,7 +27,8 @@ static size_t psram_size_usable(void) { #ifdef CONFIG_SPIRAM /* PSRAM chip may be larger than the size we can map into address space */ - #ifdef CONFIG_IDF_TARGET_ESP32P4 + #if defined(CONFIG_IDF_TARGET_ESP32P4) || defined(CONFIG_IDF_TARGET_ESP32C5) + // These targets don't define SOC_EXTRAM_DATA_SIZE. size_t s = esp_psram_get_size(); #else size_t s = MIN(esp_psram_get_size(), SOC_EXTRAM_DATA_SIZE); diff --git a/ports/espressif/common-hal/microcontroller/Pin.c b/ports/espressif/common-hal/microcontroller/Pin.c index 34b9578d580..0c49318f4bc 100644 --- a/ports/espressif/common-hal/microcontroller/Pin.c +++ b/ports/espressif/common-hal/microcontroller/Pin.c @@ -136,6 +136,32 @@ static const uint64_t pin_mask_reset_forbidden = #endif #endif // ESP32C3 + #if defined(CONFIG_IDF_TARGET_ESP32C5) + // Never ever reset pins used to communicate with SPI flash. + // ports/espressif/esp-idf/components/esp_hal_gpspi/esp32c5/include/soc/spi_pins.h + GPIO_SEL_16 | // MSPI_IOMUX_PIN_NUM_CS0 (flash) + GPIO_SEL_17 | // MSPI_IOMUX_PIN_NUM_MISO + GPIO_SEL_18 | // MSPI_IOMUX_PIN_NUM_WP + GPIO_SEL_19 | // VDD_SPI (powers the in-module flash) + GPIO_SEL_20 | // MSPI_IOMUX_PIN_NUM_HD + GPIO_SEL_21 | // MSPI_IOMUX_PIN_NUM_CLK + GPIO_SEL_22 | // MSPI_IOMUX_PIN_NUM_MOSI + #if CIRCUITPY_ESP_USB_SERIAL_JTAG + // Never ever reset serial/JTAG communication pins. On C5, gpio_ll_func_sel() + // on either clears USB_SERIAL_JTAG.conf0.usb_pad_enable, killing the USB pads. + GPIO_SEL_13 | // USB D- (USB_INT_PHY0_DM_GPIO_NUM) + GPIO_SEL_14 | // USB D+ (USB_INT_PHY0_DP_GPIO_NUM) + #endif + #if defined(CONFIG_SPIRAM) + GPIO_SEL_15 | // MSPI_IOMUX_PIN_NUM_CS1 (PSRAM); keep if PSRAM in use + #endif + #if defined(CONFIG_ESP_CONSOLE_UART_DEFAULT) && CONFIG_ESP_CONSOLE_UART_DEFAULT && CONFIG_ESP_CONSOLE_UART_NUM == 0 + // Never reset debug UART/console pins. + GPIO_SEL_11 | + GPIO_SEL_12 | + #endif + #endif // ESP32C5 + #if defined(CONFIG_IDF_TARGET_ESP32C6) // Never ever reset pins used to communicate with SPI flash. GPIO_SEL_24 | // SPICS0 diff --git a/ports/espressif/common-hal/microcontroller/Processor.c b/ports/espressif/common-hal/microcontroller/Processor.c index 72c9845241c..26cec5eb467 100644 --- a/ports/espressif/common-hal/microcontroller/Processor.c +++ b/ports/espressif/common-hal/microcontroller/Processor.c @@ -65,6 +65,10 @@ static uint32_t get_valid_cpu_frequency(uint32_t requested_freq_mhz) { uint32_t valid_cpu_frequencies[] = {20, 40, 80, 120}; #elif defined(CONFIG_IDF_TARGET_ESP32H2) uint32_t valid_cpu_frequencies[] = {32, 48, 64, 96}; + #elif defined(CONFIG_IDF_TARGET_ESP32C5) + // C5's 48 MHz crystal does not divide evenly to 20 or 24 MHz, both of + // which esp_pm_configure() rejects. + uint32_t valid_cpu_frequencies[] = {40, 80, 160, 240}; #else uint32_t valid_cpu_frequencies[] = {20, 40, 80, 160, 240}; #endif @@ -126,7 +130,7 @@ void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) { uint32_t mac_address_part = REG_READ(EFUSE_RD_MAC_SYS_0_REG); #elif defined(CONFIG_IDF_TARGET_ESP32C2) uint32_t mac_address_part = REG_READ(EFUSE_RD_BLK2_DATA0_REG); - #elif defined(CONFIG_IDF_TARGET_ESP32C61) + #elif defined(CONFIG_IDF_TARGET_ESP32C61) || defined(CONFIG_IDF_TARGET_ESP32C5) uint32_t mac_address_part = REG_READ(EFUSE_RD_MAC_SYS0_REG); #else uint32_t mac_address_part = REG_READ(EFUSE_RD_MAC_SPI_SYS_0_REG); @@ -147,7 +151,7 @@ void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) { mac_address_part = REG_READ(EFUSE_RD_MAC_SYS_1_REG); #elif defined(CONFIG_IDF_TARGET_ESP32C2) mac_address_part = REG_READ(EFUSE_RD_BLK2_DATA1_REG); - #elif defined(CONFIG_IDF_TARGET_ESP32C61) + #elif defined(CONFIG_IDF_TARGET_ESP32C61) || defined(CONFIG_IDF_TARGET_ESP32C5) mac_address_part = REG_READ(EFUSE_RD_MAC_SYS1_REG); #else mac_address_part = REG_READ(EFUSE_RD_MAC_SPI_SYS_1_REG); diff --git a/ports/espressif/common-hal/microcontroller/__init__.c b/ports/espressif/common-hal/microcontroller/__init__.c index 9b50cd7b5d2..2da2d09f22d 100644 --- a/ports/espressif/common-hal/microcontroller/__init__.c +++ b/ports/espressif/common-hal/microcontroller/__init__.c @@ -33,6 +33,9 @@ #elif defined(CONFIG_IDF_TARGET_ESP32C3) #include "soc/rtc_cntl_reg.h" #include "esp32c3/rom/rtc.h" +#elif defined(CONFIG_IDF_TARGET_ESP32C5) +#include "soc/lp_aon_reg.h" +#include "esp32c5/rom/rtc.h" #elif defined(CONFIG_IDF_TARGET_ESP32C6) #include "soc/lp_aon_reg.h" #include "esp32c6/rom/rtc.h" @@ -86,7 +89,7 @@ void common_hal_mcu_enable_interrupts(void) { void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) { switch (runmode) { case RUNMODE_UF2: - #if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32C6) || defined(CONFIG_IDF_TARGET_ESP32C61) + #if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32C5) || defined(CONFIG_IDF_TARGET_ESP32C6) || defined(CONFIG_IDF_TARGET_ESP32C61) mp_arg_error_invalid(MP_QSTR_run_mode); #else // 0x11F2 is APP_REQUEST_UF2_RESET_HINT & is defined by TinyUF2 diff --git a/ports/espressif/esp-idf-config/sdkconfig-esp32c5.defaults b/ports/espressif/esp-idf-config/sdkconfig-esp32c5.defaults new file mode 100644 index 00000000000..eb6f64cbb78 --- /dev/null +++ b/ports/espressif/esp-idf-config/sdkconfig-esp32c5.defaults @@ -0,0 +1,54 @@ +# +# Espressif IoT Development Framework Configuration +# +# +# Component config +# +# +# Bluetooth +# +CONFIG_BT_ENABLED=y +CONFIG_BT_NIMBLE_ENABLED=y +# +# NimBLE Options +# +CONFIG_BT_NIMBLE_LOG_LEVEL_NONE=y +CONFIG_BT_NIMBLE_NVS_PERSIST=y +# +# Memory Settings +# +CONFIG_BT_NIMBLE_TRANSPORT_ACL_FROM_LL_COUNT=20 +CONFIG_BT_NIMBLE_TRANSPORT_EVT_SIZE=70 +# end of Memory Settings + +CONFIG_BT_NIMBLE_EXT_ADV=y +# end of NimBLE Options + +# end of Bluetooth + +# +# Driver Configurations +# +# +# PCNT Configuration +# +CONFIG_PCNT_CTRL_FUNC_IN_IRAM=y +# end of PCNT Configuration + +# end of Driver Configurations + +# +# Wi-Fi +# +CONFIG_ESP_WIFI_STATIC_RX_BUFFER_NUM=4 +# end of Wi-Fi + +# +# mbedTLS +# +# CONFIG_MBEDTLS_HARDWARE_SHA is not set +# end of mbedTLS + +# end of Component config + +# end of Espressif IoT Development Framework Configuration diff --git a/ports/espressif/mpconfigport.h b/ports/espressif/mpconfigport.h index 7f1a581571e..f0106f26b2c 100644 --- a/ports/espressif/mpconfigport.h +++ b/ports/espressif/mpconfigport.h @@ -29,6 +29,8 @@ #ifndef CIRCUITPY_BOOT_BUTTON #if defined(CONFIG_IDF_TARGET_ESP32C2) || defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32C6) || defined(CONFIG_IDF_TARGET_ESP32H2) || defined(CONFIG_IDF_TARGET_ESP32C61) #define CIRCUITPY_BOOT_BUTTON (&pin_GPIO9) + #elif defined(CONFIG_IDF_TARGET_ESP32C5) + #define CIRCUITPY_BOOT_BUTTON (&pin_GPIO28) #elif !defined(CONFIG_IDF_TARGET_ESP32) #define CIRCUITPY_BOOT_BUTTON (&pin_GPIO0) #endif diff --git a/ports/espressif/mpconfigport.mk b/ports/espressif/mpconfigport.mk index 98fe3224a49..58c6835fc09 100644 --- a/ports/espressif/mpconfigport.mk +++ b/ports/espressif/mpconfigport.mk @@ -11,6 +11,9 @@ CROSS_COMPILE = riscv32-esp-elf- else ifeq ($(IDF_TARGET),esp32c3) IDF_TARGET_ARCH = riscv CROSS_COMPILE = riscv32-esp-elf- +else ifeq ($(IDF_TARGET),esp32c5) +IDF_TARGET_ARCH = riscv +CROSS_COMPILE = riscv32-esp-elf- else ifeq ($(IDF_TARGET),esp32p4) IDF_TARGET_ARCH = riscv CROSS_COMPILE = riscv32-esp-elf- @@ -188,6 +191,41 @@ CIRCUITPY_SDIOIO = 0 CIRCUITPY_USB_DEVICE = 0 CIRCUITPY_ESP_USB_SERIAL_JTAG ?= 1 +#### esp32c5 ########################################################## +else ifeq ($(IDF_TARGET),esp32c5) +# Modules +CIRCUITPY_ESPCAMERA = 0 +CIRCUITPY_ESPULP = 0 +CIRCUITPY_MEMORYMAP = 0 +CIRCUITPY_RGBMATRIX = 0 + +# TWAI-FD generation, needs a new canio backend +CIRCUITPY_CANIO = 0 + +# No capacitive touch peripheral +CIRCUITPY_ALARM_TOUCH = 0 +CIRCUITPY_TOUCHIO_USE_NATIVE = 0 + +# No DAC +CIRCUITPY_AUDIOIO = 0 + +# No I2S peripheral PDM-to-PCM hardware support +CIRCUITPY_AUDIOBUSIO_PDMIN = 0 + +# No space for this +CIRCUITPY_AUDIOBUSIO = 0 +CIRCUITPY_AUDIOI2SIN = 0 + +# No LCD_CAM peripheral, so no esp_lcd_new_i80_bus() +CIRCUITPY_PARALLELDISPLAYBUS = 0 + +# No SDMMC +CIRCUITPY_SDIOIO = 0 + +# Features +CIRCUITPY_USB_DEVICE = 0 +CIRCUITPY_ESP_USB_SERIAL_JTAG ?= 1 + #### esp32c6 ########################################################## else ifeq ($(IDF_TARGET),esp32c6) # Modules diff --git a/ports/espressif/peripherals/esp32c5/pins.c b/ports/espressif/peripherals/esp32c5/pins.c new file mode 100644 index 00000000000..2abf905e18f --- /dev/null +++ b/ports/espressif/peripherals/esp32c5/pins.c @@ -0,0 +1,37 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2021 microDev +// +// SPDX-License-Identifier: MIT + +#include "peripherals/pins.h" + +const mcu_pin_obj_t pin_GPIO0 = PIN(0, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO1 = PIN(1, ADC_UNIT_1, ADC_CHANNEL_0, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO2 = PIN(2, ADC_UNIT_1, ADC_CHANNEL_1, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO3 = PIN(3, ADC_UNIT_1, ADC_CHANNEL_2, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO4 = PIN(4, ADC_UNIT_1, ADC_CHANNEL_3, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO5 = PIN(5, ADC_UNIT_1, ADC_CHANNEL_4, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO6 = PIN(6, ADC_UNIT_1, ADC_CHANNEL_5, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO7 = PIN(7, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO8 = PIN(8, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO9 = PIN(9, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO10 = PIN(10, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO11 = PIN(11, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO12 = PIN(12, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO13 = PIN(13, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO14 = PIN(14, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO15 = PIN(15, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO16 = PIN(16, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO17 = PIN(17, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO18 = PIN(18, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO19 = PIN(19, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO20 = PIN(20, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO21 = PIN(21, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO22 = PIN(22, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO23 = PIN(23, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO24 = PIN(24, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO25 = PIN(25, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO26 = PIN(26, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO27 = PIN(27, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO28 = PIN(28, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); diff --git a/ports/espressif/peripherals/esp32c5/pins.h b/ports/espressif/peripherals/esp32c5/pins.h new file mode 100644 index 00000000000..90c28621791 --- /dev/null +++ b/ports/espressif/peripherals/esp32c5/pins.h @@ -0,0 +1,70 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2023 Scott Shawcroft for Adafruit Industries LLC +// +// SPDX-License-Identifier: MIT + +// DO NOT include this file directly. +// Use shared-bindings/microcontroller/Pin.h instead. +// This ensures that all necessary includes are already included. + +#pragma once + +#define GPIO0_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO0; +#define GPIO1_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO1; +#define GPIO2_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO2; +#define GPIO3_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO3; +#define GPIO4_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO4; +#define GPIO5_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO5; +#define GPIO6_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO6; +#define GPIO7_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO7; +#define GPIO8_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO8; +#define GPIO9_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO9; +#define GPIO10_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO10; +#define GPIO11_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO11; +#define GPIO12_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO12; +#define GPIO13_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO13; +#define GPIO14_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO14; +#define GPIO15_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO15; +#define GPIO16_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO16; +#define GPIO17_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO17; +#define GPIO18_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO18; +#define GPIO19_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO19; +#define GPIO20_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO20; +#define GPIO21_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO21; +#define GPIO22_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO22; +#define GPIO23_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO23; +#define GPIO24_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO24; +#define GPIO25_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO25; +#define GPIO26_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO26; +#define GPIO27_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO27; +#define GPIO28_EXISTS 1 +extern const mcu_pin_obj_t pin_GPIO28; diff --git a/ports/espressif/peripherals/pins.h b/ports/espressif/peripherals/pins.h index 431a136545a..ada17075e50 100644 --- a/ports/espressif/peripherals/pins.h +++ b/ports/espressif/peripherals/pins.h @@ -49,6 +49,8 @@ extern const mp_obj_type_t mcu_pin_type; #include "esp32c2/pins.h" #elif defined(CONFIG_IDF_TARGET_ESP32C3) #include "esp32c3/pins.h" +#elif defined(CONFIG_IDF_TARGET_ESP32C5) +#include "esp32c5/pins.h" #elif defined(CONFIG_IDF_TARGET_ESP32C6) #include "esp32c6/pins.h" #elif defined(CONFIG_IDF_TARGET_ESP32C61) diff --git a/ports/espressif/tools/build_memory_info.py b/ports/espressif/tools/build_memory_info.py index 0cf609c4d63..27ad13b29e6 100644 --- a/ports/espressif/tools/build_memory_info.py +++ b/ports/espressif/tools/build_memory_info.py @@ -56,6 +56,11 @@ ("Internal SRAM 0", (0x4037_C000,), 16 * 1024), ("Internal SRAM 1", (0x3FC8_0000, 0x4038_0000), 384 * 1024), ], + "esp32c5": [ + # Name, Start, Length + ("LP SRAM", (0x5000_0000,), 16 * 1024), + ("HP SRAM", (0x4080_0000,), 384 * 1024), + ], "esp32c6": [ # Name, Start, Length ("LP SRAM", (0x5000_0000,), 16 * 1024), From 372420c2b75449252d03d2d826f66a15a8609c42 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Tue, 1 Sep 2026 19:53:20 -0700 Subject: [PATCH 03/12] espressif: add ESP32-C5-DevKitC-1-N8R8 board 8MB flash, 8MB quad PSRAM, WS2812B on GPIO27, UART0 on GPIO11/12. Flash mode is dio: qio locks the CPU during bootloader startup on this board. PSRAM stays qio, which is the only mode C5 offers. --- .../espressif_esp32c5_devkitc_1_n8r8/board.c | 9 +++++ .../mpconfigboard.h | 17 ++++++++ .../mpconfigboard.mk | 12 ++++++ .../espressif_esp32c5_devkitc_1_n8r8/pins.c | 40 +++++++++++++++++++ .../sdkconfig | 14 +++++++ 5 files changed, 92 insertions(+) create mode 100644 ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/board.c create mode 100644 ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/mpconfigboard.h create mode 100644 ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/mpconfigboard.mk create mode 100644 ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/pins.c create mode 100644 ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/sdkconfig diff --git a/ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/board.c b/ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/board.c new file mode 100644 index 00000000000..a3a9eec0471 --- /dev/null +++ b/ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/board.c @@ -0,0 +1,9 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2020 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include "supervisor/board.h" + +// Use the MP_WEAK supervisor/shared/board.c versions of routines not defined here. diff --git a/ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/mpconfigboard.h b/ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/mpconfigboard.h new file mode 100644 index 00000000000..ca05bf78b6a --- /dev/null +++ b/ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/mpconfigboard.h @@ -0,0 +1,17 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2019 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +// Micropython setup + +#define MICROPY_HW_BOARD_NAME "ESP32-C5-DevKitC-1-N8R8" +#define MICROPY_HW_MCU_NAME "ESP32C5" + +#define MICROPY_HW_NEOPIXEL (&pin_GPIO27) + +#define DEFAULT_UART_BUS_RX (&pin_GPIO12) +#define DEFAULT_UART_BUS_TX (&pin_GPIO11) diff --git a/ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/mpconfigboard.mk b/ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/mpconfigboard.mk new file mode 100644 index 00000000000..0da008596e3 --- /dev/null +++ b/ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/mpconfigboard.mk @@ -0,0 +1,12 @@ +CIRCUITPY_CREATOR_ID = 0x000C303A +CIRCUITPY_CREATION_ID = 0x00C50001 + +IDF_TARGET = esp32c5 + +CIRCUITPY_ESP_FLASH_MODE = dio +CIRCUITPY_ESP_FLASH_FREQ = 80m +CIRCUITPY_ESP_FLASH_SIZE = 8MB + +CIRCUITPY_ESP_PSRAM_SIZE = 8MB +CIRCUITPY_ESP_PSRAM_MODE = qio +CIRCUITPY_ESP_PSRAM_FREQ = 40m diff --git a/ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/pins.c b/ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/pins.c new file mode 100644 index 00000000000..dc4a478a592 --- /dev/null +++ b/ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/pins.c @@ -0,0 +1,40 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2020 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include "shared-bindings/board/__init__.h" + +static const mp_rom_map_elem_t board_module_globals_table[] = { + CIRCUITPYTHON_BOARD_DICT_STANDARD_ITEMS + + { MP_ROM_QSTR(MP_QSTR_IO0), MP_ROM_PTR(&pin_GPIO0) }, + { MP_ROM_QSTR(MP_QSTR_IO1), MP_ROM_PTR(&pin_GPIO1) }, + { MP_ROM_QSTR(MP_QSTR_IO2), MP_ROM_PTR(&pin_GPIO2) }, + { MP_ROM_QSTR(MP_QSTR_IO3), MP_ROM_PTR(&pin_GPIO3) }, + { MP_ROM_QSTR(MP_QSTR_IO4), MP_ROM_PTR(&pin_GPIO4) }, + { MP_ROM_QSTR(MP_QSTR_IO5), MP_ROM_PTR(&pin_GPIO5) }, + { MP_ROM_QSTR(MP_QSTR_IO6), MP_ROM_PTR(&pin_GPIO6) }, + { MP_ROM_QSTR(MP_QSTR_IO7), MP_ROM_PTR(&pin_GPIO7) }, + { MP_ROM_QSTR(MP_QSTR_IO8), MP_ROM_PTR(&pin_GPIO8) }, + { MP_ROM_QSTR(MP_QSTR_IO9), MP_ROM_PTR(&pin_GPIO9) }, + { MP_ROM_QSTR(MP_QSTR_IO10), MP_ROM_PTR(&pin_GPIO10) }, + { MP_ROM_QSTR(MP_QSTR_IO13), MP_ROM_PTR(&pin_GPIO13) }, + { MP_ROM_QSTR(MP_QSTR_IO14), MP_ROM_PTR(&pin_GPIO14) }, + + { MP_ROM_QSTR(MP_QSTR_IO23), MP_ROM_PTR(&pin_GPIO23) }, + { MP_ROM_QSTR(MP_QSTR_IO24), MP_ROM_PTR(&pin_GPIO24) }, + { MP_ROM_QSTR(MP_QSTR_IO25), MP_ROM_PTR(&pin_GPIO25) }, + { MP_ROM_QSTR(MP_QSTR_IO26), MP_ROM_PTR(&pin_GPIO26) }, + { MP_ROM_QSTR(MP_QSTR_IO27), MP_ROM_PTR(&pin_GPIO27) }, + { MP_ROM_QSTR(MP_QSTR_IO28), MP_ROM_PTR(&pin_GPIO28) }, + + { MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_GPIO27) }, + + { MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_GPIO11) }, + { MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_GPIO12) }, + + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, +}; +MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table); diff --git a/ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/sdkconfig b/ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/sdkconfig new file mode 100644 index 00000000000..e9628662160 --- /dev/null +++ b/ports/espressif/boards/espressif_esp32c5_devkitc_1_n8r8/sdkconfig @@ -0,0 +1,14 @@ +# +# Espressif IoT Development Framework Configuration +# +# +# Component config +# +# +# LWIP +# +# end of LWIP + +# end of Component config + +# end of Espressif IoT Development Framework Configuration From e66da0424e6ea0e9a99f6a5e2bc854f801768e56 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Fri, 4 Sep 2026 09:50:01 -0700 Subject: [PATCH 04/12] espressif: name the real limit on esp32c5 audio modules The comment was copied from the c6 block. The 8MB flash has a 2048K app partition, and enabling both leaves 42 KB free. --- ports/espressif/mpconfigport.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/espressif/mpconfigport.mk b/ports/espressif/mpconfigport.mk index 58c6835fc09..ca9d3c5fb09 100644 --- a/ports/espressif/mpconfigport.mk +++ b/ports/espressif/mpconfigport.mk @@ -212,7 +212,7 @@ CIRCUITPY_AUDIOIO = 0 # No I2S peripheral PDM-to-PCM hardware support CIRCUITPY_AUDIOBUSIO_PDMIN = 0 -# No space for this +# 2048K app partition, only 42 KB free with these on CIRCUITPY_AUDIOBUSIO = 0 CIRCUITPY_AUDIOI2SIN = 0 From ccf27daafc3d24c37877dce60b4a2e583cf021cc Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Fri, 4 Sep 2026 11:10:28 -0700 Subject: [PATCH 05/12] espressif: enable audiobusio and audioi2sin on esp32c5 The C5 has an I2S peripheral and the DevKitC breaks out 19 GPIOs, so external I2S hardware works the same as on the S3 DevKitC, which ships with these on and has no onboard audio either. Costs 78160 bytes, leaving 42928 free in the 2048K app partition. --- ports/espressif/mpconfigport.mk | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ports/espressif/mpconfigport.mk b/ports/espressif/mpconfigport.mk index ca9d3c5fb09..ee28bba9918 100644 --- a/ports/espressif/mpconfigport.mk +++ b/ports/espressif/mpconfigport.mk @@ -212,10 +212,6 @@ CIRCUITPY_AUDIOIO = 0 # No I2S peripheral PDM-to-PCM hardware support CIRCUITPY_AUDIOBUSIO_PDMIN = 0 -# 2048K app partition, only 42 KB free with these on -CIRCUITPY_AUDIOBUSIO = 0 -CIRCUITPY_AUDIOI2SIN = 0 - # No LCD_CAM peripheral, so no esp_lcd_new_i80_bus() CIRCUITPY_PARALLELDISPLAYBUS = 0 From 3ba6ad7a211d01d9576a391169b134f51aea5c9b Mon Sep 17 00:00:00 2001 From: Vladimir Smitka Date: Sun, 6 Sep 2026 20:45:31 +0000 Subject: [PATCH 06/12] build_memory_info: take the region size from the linker map Seven ports never write firmware.size.json, so build_release_files.py cannot tell whether the en_US build left room and builds all 17 languages for every board of those ports on every pull request. They cannot use this script as it stands: it reads the region size out of a linker script, which needs the size to be a literal there. raspberrypi writes LENGTH = firmware_size, a symbol the board can override; broadcom, cxd56 and silabs link a script from a submodule or an SDK; zephyr-cp has no script of its own at all. The linker map has the same numbers with the symbols already resolved, and every one of these ports either writes a map already or is one flag away from it. So read the Memory Configuration table when the file has one, and keep the linker script path for the ports that pass one. The region name is FLASH_FIRMWARE or FLASH, or --region for anything else. Wired into raspberrypi, renode, mimxrt10xx, broadcom, cxd56 and silabs, which only needed -Wl,-Map added. A missing map or an unknown region name prints what was found and writes nothing, so the language skip falls back to today's behaviour instead of failing the build. zephyr-cp still needs doing; its Makefile has none of the toolchain variables. --- ports/broadcom/Makefile | 1 + ports/cxd56/Makefile | 1 + ports/mimxrt10xx/Makefile | 1 + ports/raspberrypi/Makefile | 1 + ports/renode/Makefile | 1 + ports/silabs/Makefile | 3 +- tools/build_memory_info.py | 101 +++++++++++++++++++++++++++---------- 7 files changed, 82 insertions(+), 27 deletions(-) diff --git a/ports/broadcom/Makefile b/ports/broadcom/Makefile index dda93c6fd7c..b1b1bf07ed4 100644 --- a/ports/broadcom/Makefile +++ b/ports/broadcom/Makefile @@ -124,6 +124,7 @@ $(BUILD)/kernel$(SUFFIX).elf: $(OBJ) $(STEPECHO) "LINK $@" $(Q)echo $(OBJ) > $(BUILD)/firmware.objs $(Q)$(CC) -o $@ $(LDFLAGS) @$(BUILD)/firmware.objs -Wl,--print-memory-usage -Wl,--start-group $(LIBS) -Wl,--end-group + $(Q)$(SIZE) $@ | $(PYTHON) $(TOP)/tools/build_memory_info.py $@.map $(BUILD) endif $(BUILD)/kernel$(SUFFIX).img: $(BUILD)/kernel$(SUFFIX).elf diff --git a/ports/cxd56/Makefile b/ports/cxd56/Makefile index 88c39210220..5ad25eb7400 100644 --- a/ports/cxd56/Makefile +++ b/ports/cxd56/Makefile @@ -153,6 +153,7 @@ $(BUILD)/libmpy.a: $(SPRESENSE_SDK) $(OBJ) $(BUILD)/firmware.elf: $(BUILD)/libmpy.a $(ECHO) "LD $@" $(Q)$(LD) $(LDFLAGS) + $(Q)$(SIZE) $@ | $(PYTHON) $(TOP)/tools/build_memory_info.py $(BUILD)/output.map $(BUILD) $(MKSPK): $(MAKE) -C mkspk diff --git a/ports/mimxrt10xx/Makefile b/ports/mimxrt10xx/Makefile index 74667fd3d64..95fedc70d8e 100644 --- a/ports/mimxrt10xx/Makefile +++ b/ports/mimxrt10xx/Makefile @@ -189,6 +189,7 @@ else $(BUILD)/firmware.elf: $(OBJ) $(LD_FILES) $(STEPECHO) "LINK $@" $(Q)$(CC) -o $@ $(LDFLAGS) $(filter-out %.ld, $^) -Wl,--print-memory-usage -Wl,--start-group $(LIBS) -Wl,--end-group + $(Q)$(SIZE) $@ | $(PYTHON) $(TOP)/tools/build_memory_info.py $@.map $(BUILD) endif # -R excludes sections from the output files. diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile index 1c13d538e51..5512c7c9f47 100755 --- a/ports/raspberrypi/Makefile +++ b/ports/raspberrypi/Makefile @@ -834,6 +834,7 @@ $(BUILD)/firmware.elf: $(OBJ) $(BOARD_LD) link-$(CHIP_VARIANT_LOWER).ld $(Q)echo $(OBJ) > $(BUILD)/firmware.objs $(Q)echo $(PICO_LDFLAGS) > $(BUILD)/firmware.ldflags $(Q)$(CC) -o $@ $(CFLAGS) @$(BUILD)/firmware.ldflags $(LINKER_SCRIPTS) -Wl,--print-memory-usage -Wl,-Map=$@.map -Wl,-cref -Wl,--gc-sections @$(BUILD)/firmware.objs -Wl,-lc + $(Q)$(SIZE) $@ | $(PYTHON) $(TOP)/tools/build_memory_info.py $@.map $(BUILD) ifneq ($(CORE1_CHECK_ROOTS),) $(STEPECHO) "CHECK core1 flash calls" $(Q)$(PYTHON) $(TOP)/tools/check_core1_flash_calls.py $@ $(CORE1_CHECK_ROOTS) diff --git a/ports/renode/Makefile b/ports/renode/Makefile index c4ce66194e9..b69d8d251ef 100644 --- a/ports/renode/Makefile +++ b/ports/renode/Makefile @@ -88,6 +88,7 @@ $(BUILD)/firmware.elf: $(OBJ) $(BOARD_LD) link.ld $(Q)echo $(OBJ) > $(BUILD)/firmware.objs $(Q)echo $(PICO_LDFLAGS) > $(BUILD)/firmware.ldflags $(Q)$(CC) -o $@ $(CFLAGS) @$(BUILD)/firmware.ldflags $(LINKER_SCRIPTS) -Wl,--print-memory-usage -Wl,-Map=$@.map -Wl,-cref -Wl,--gc-sections @$(BUILD)/firmware.objs -Wl,-lc + $(Q)$(SIZE) $@ | $(PYTHON) $(TOP)/tools/build_memory_info.py $@.map $(BUILD) endif $(BUILD)/firmware.bin: $(BUILD)/firmware.elf diff --git a/ports/silabs/Makefile b/ports/silabs/Makefile index 810ab53f1f3..5c0f01e86b1 100644 --- a/ports/silabs/Makefile +++ b/ports/silabs/Makefile @@ -134,7 +134,8 @@ $(SILABS_BUILD)/$(PROJECTNAME).Makefile: | $(HEADER_BUILD)/mpversion.h $(OUTPUT_DIR)/firmware.out: $(SILABS_BUILD)/pin_functions.h $(SILABS_BUILD)/pins.c $(OBJ) $(OBJS) $(LIB_FILES) $(STEPECHO) 'Linking $(OUTPUT_DIR)/firmware.out' $(Q)echo "$(OBJS) $(OBJ)" > $(OUTPUT_DIR)/linker_objs - $(Q)$(CC) $(LD_FLAGS) @$(OUTPUT_DIR)/linker_objs -Wl,--print-memory-usage $(LIBS) -o $(OUTPUT_DIR)/firmware.out + $(Q)$(CC) $(LD_FLAGS) @$(OUTPUT_DIR)/linker_objs -Wl,--print-memory-usage -Wl,-Map=$(OUTPUT_DIR)/firmware.out.map $(LIBS) -o $(OUTPUT_DIR)/firmware.out + $(Q)$(SIZE) $(OUTPUT_DIR)/firmware.out | $(PYTHON) $(TOP)/tools/build_memory_info.py $(OUTPUT_DIR)/firmware.out.map $(BUILD) $(Q)$(OBJCOPY) $(OUTPUT_DIR)/firmware.out -O binary $(OUTPUT_DIR)/firmware.bin flash: $(OUTPUT_DIR)/firmware.bin diff --git a/tools/build_memory_info.py b/tools/build_memory_info.py index ff5bbfad710..3f0bf5d3532 100755 --- a/tools/build_memory_info.py +++ b/tools/build_memory_info.py @@ -17,6 +17,18 @@ M_PATTERN = re.compile(r"([0-9]+)[mM]") M_REPLACE = r"(\1*1024*1024)" +# A linker map lists every region with the lengths already resolved: +# Name Origin Length Attributes +# FLASH_FIRMWARE 0x10000000 0x0017f000 xr +MAP_REGION = re.compile(r"^(\w+)\s+0x[0-9a-f]+\s+0x([0-9a-f]+)\s+\S*$", re.MULTILINE) + +argv = sys.argv[1:] +flash_names = ["FLASH_FIRMWARE", "FLASH"] +if "--region" in argv: + i = argv.index("--region") + flash_names = argv[i + 1].split(",") + del argv[i : i + 2] + text = 0 data = 0 bss = 0 @@ -29,31 +41,73 @@ if not line.startswith("text"): text, data, bss = map(int, line.split()[:3]) -regions = {} -# This file is the linker script. -with open(sys.argv[1], "r") as f: - for line in f: +def regions_from_map(contents): + """Region sizes from the Memory Configuration table of a linker map.""" + start = contents.find("Memory Configuration") + if start < 0: + return None + end = contents.find("Linker script and memory map", start) + table = contents[start : end if end > 0 else len(contents)] + regions = {} + for name, length in MAP_REGION.findall(table): + if name not in ("Name", "Origin", "Length"): + regions[name] = int(length, 16) + return regions or None + + +def regions_from_linker_script(contents): + """Region sizes from a linker script, for the ports that pass one.""" + regions = {} + for line in contents.split("\n"): line = line.strip() if line.startswith(("FLASH_FIRMWARE", "RAM")): regions[line.split()[0]] = line.split("=")[-1] + for region, space in regions.items(): + if "/*" in space: + space = space.split("/*")[0] + space = K_PATTERN.sub(K_REPLACE, space) + space = M_PATTERN.sub(M_REPLACE, space) + regions[region] = int(eval(space)) + return regions -for region, space in regions.items(): - if "/*" in space: - space = space.split("/*")[0] - space = K_PATTERN.sub(K_REPLACE, space) - space = M_PATTERN.sub(M_REPLACE, space) - regions[region] = int(eval(space)) -firmware_region = regions["FLASH_FIRMWARE"] -ram_region = regions["RAM"] +# This file is either a linker map or the linker script. +try: + with open(argv[0], "r") as f: + contents = f.read() +except FileNotFoundError: + print() + print(f"No {argv[0]} to read the flash region from.") + print() + sys.exit(0) +regions = regions_from_map(contents) or regions_from_linker_script(contents) + +firmware_region = None +for name in flash_names: + if name in regions: + firmware_region = regions[name] + break used_flash = data + text -free_flash = firmware_region - used_flash used_ram = data + bss -free_ram = ram_region - used_ram -with open(f"{sys.argv[2]}/firmware.size.json", "w") as f: +if firmware_region is None: + # Not knowing the size is not worth failing a build over: the tools that read + # firmware.size.json fall back to assuming there is no headroom. + print() + print( + "No {} region in {}. Regions found: {}.".format( + " or ".join(flash_names), argv[0], ", ".join(sorted(regions)) or "none" + ) + ) + print("{} bytes used in flash firmware space.".format(used_flash)) + print() + sys.exit(0) + +free_flash = firmware_region - used_flash + +with open(f"{argv[1]}/firmware.size.json", "w") as f: json.dump({"used_flash": used_flash, "firmware_region": firmware_region}, f) print() @@ -62,16 +116,11 @@ used_flash, free_flash, firmware_region, firmware_region / 1024 ) ) -print( - "{} bytes used, {} bytes free in ram for stack and heap out of {} bytes ({}kB).".format( - used_ram, free_ram, ram_region, ram_region / 1024 +if "RAM" in regions: + ram_region = regions["RAM"] + print( + "{} bytes used, {} bytes free in ram for stack and heap out of {} bytes ({}kB).".format( + used_ram, ram_region - used_ram, ram_region, ram_region / 1024 + ) ) -) print() - -# Check that we have free flash space. GCC doesn't fail when the text + data -# sections don't fit in FLASH. It only counts data in RAM. -if free_flash < 0: - print("Too little flash!!!") - print() - sys.exit(-1) From 5577bec4c5f2772159b6b1c266687ad31861140b Mon Sep 17 00:00:00 2001 From: Vladimir Smitka Date: Sun, 6 Sep 2026 20:50:53 +0000 Subject: [PATCH 07/12] build_memory_info: let zephyr-cp report its size too Its makefile has none of the toolchain variables the other ports get from circuitpy_mkenv.mk, so there is no size(1) to pipe in. There does not need to be: what the image occupies in flash is the size of the binary, which is how ports/espressif has always measured it. Pass --image and the region still comes from the map. Measured on rpi_pico: 502256 bytes used of a 1040128 byte region, against the 502000 and 1040128 Zephyr prints itself -- the difference is the 256 byte second stage, which the .bin includes and the FLASH region does not. --- ports/zephyr-cp/Makefile | 1 + tools/build_memory_info.py | 35 +++++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/ports/zephyr-cp/Makefile b/ports/zephyr-cp/Makefile index fbde791afa9..e21a96e9eb8 100644 --- a/ports/zephyr-cp/Makefile +++ b/ports/zephyr-cp/Makefile @@ -52,6 +52,7 @@ export BSIM_OUT_PATH := $(CURDIR)/tools/bsim $(BUILD)/zephyr-cp/zephyr/zephyr.elf: python cptools/pre_zephyr_build_prep.py $(BOARD) west build -b $(BOARD) -d $(BUILD) $(WEST_SHIELD_ARGS) --sysbuild -- $(WEST_CMAKE_ARGS) + python ../../tools/build_memory_info.py $(BUILD)/zephyr-cp/zephyr/zephyr.map $(BUILD) --image $(BUILD)/zephyr-cp/zephyr/zephyr.bin $(BUILD)/firmware.elf: $(BUILD)/zephyr-cp/zephyr/zephyr.elf cp $^ $@ diff --git a/tools/build_memory_info.py b/tools/build_memory_info.py index 3f0bf5d3532..3f6f9139669 100755 --- a/tools/build_memory_info.py +++ b/tools/build_memory_info.py @@ -5,6 +5,7 @@ # # SPDX-License-Identifier: MIT +import os import re import sys import json @@ -29,17 +30,26 @@ flash_names = argv[i + 1].split(",") del argv[i : i + 2] +# Ports whose toolchain this makefile cannot reach pass the image instead of piping +# size(1) in; what it occupies in flash is its size on disk. +image = None +if "--image" in argv: + i = argv.index("--image") + image = argv[i + 1] + del argv[i : i + 2] + text = 0 data = 0 bss = 0 -# stdin is the linker output. -for line in sys.stdin: - # Uncomment to see linker output. - # print(line) - line = line.strip() - if not line.startswith("text"): - text, data, bss = map(int, line.split()[:3]) +if image is None: + # stdin is the linker output. + for line in sys.stdin: + # Uncomment to see linker output. + # print(line) + line = line.strip() + if not line.startswith("text"): + text, data, bss = map(int, line.split()[:3]) def regions_from_map(contents): @@ -89,6 +99,15 @@ def regions_from_linker_script(contents): firmware_region = regions[name] break +if image is not None: + try: + text = os.stat(image).st_size + except FileNotFoundError: + print() + print(f"No {image} to measure.") + print() + sys.exit(0) + used_flash = data + text used_ram = data + bss @@ -116,7 +135,7 @@ def regions_from_linker_script(contents): used_flash, free_flash, firmware_region, firmware_region / 1024 ) ) -if "RAM" in regions: +if image is None and "RAM" in regions: ram_region = regions["RAM"] print( "{} bytes used, {} bytes free in ram for stack and heap out of {} bytes ({}kB).".format( From bd9b603c9c5cc6ad09af0ba8e22944fba888f814 Mon Sep 17 00:00:00 2001 From: Artem Lytkin Date: Mon, 7 Sep 2026 01:45:31 +0300 Subject: [PATCH 08/12] audiocore: require WaveFile buffer length to be a multiple of 8 Rather than rounding each half of a caller-supplied buffer down to a multiple of 4, reject lengths that are not a multiple of 8 up front so the buffer the user passes is the buffer that gets used. --- shared-bindings/audiocore/WaveFile.c | 6 +++++- shared-module/audiocore/WaveFile.c | 4 ++-- tests/circuitpython/wavefile_8bit_padding.py | 10 +++++++--- tests/circuitpython/wavefile_8bit_padding.py.exp | 1 + 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/shared-bindings/audiocore/WaveFile.c b/shared-bindings/audiocore/WaveFile.c index c93bedd4213..d0aa011313c 100644 --- a/shared-bindings/audiocore/WaveFile.c +++ b/shared-bindings/audiocore/WaveFile.c @@ -27,7 +27,8 @@ //| :param Union[str, typing.BinaryIO] file: The name of a wave file (preferred) or an already opened wave file //| :param ~circuitpython_typing.WriteableBuffer buffer: Optional pre-allocated buffer, //| that will be split in half and used for double-buffering of the data. -//| The buffer must be 8 to 1024 bytes long. +//| The buffer must be 8 to 1024 bytes long and a multiple of 8 bytes, so that each half +//| holds a whole number of 32-bit words. //| If not provided, two 256 byte buffers are initially allocated internally. //| //| Playing a wave file from flash:: @@ -70,6 +71,9 @@ static mp_obj_t audioio_wavefile_make_new(const mp_obj_type_t *type, size_t n_ar mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE); buffer = bufinfo.buf; buffer_size = mp_arg_validate_length_range(bufinfo.len, 8, 1024, MP_QSTR_buffer); + if (buffer_size % 8 != 0) { + mp_raise_ValueError_varg(MP_ERROR_TEXT("Buffer must be a multiple of %d bytes"), 8); + } } audioio_wavefile_obj_t *self = mp_obj_malloc(audioio_wavefile_obj_t, &audioio_wavefile_type); diff --git a/shared-module/audiocore/WaveFile.c b/shared-module/audiocore/WaveFile.c index 2bd3849e192..16bbf965e8b 100644 --- a/shared-module/audiocore/WaveFile.c +++ b/shared-module/audiocore/WaveFile.c @@ -114,8 +114,8 @@ void common_hal_audioio_wavefile_construct(audioio_wavefile_obj_t *self, // Try to allocate two buffers, one will be loaded from file and the other // DMAed to DAC. if (buffer_size) { - // Each half must be a multiple of 4 bytes so the last buffer can be padded in place. - self->len = buffer_size / 2 / sizeof(uint32_t) * sizeof(uint32_t); + // buffer_size is a multiple of 8 (checked by the binding) so each half can be padded in place. + self->len = buffer_size / 2; self->buffer = buffer; self->second_buffer = buffer + self->len; } else { diff --git a/tests/circuitpython/wavefile_8bit_padding.py b/tests/circuitpython/wavefile_8bit_padding.py index ae9b016cee0..16bc1d868a3 100644 --- a/tests/circuitpython/wavefile_8bit_padding.py +++ b/tests/circuitpython/wavefile_8bit_padding.py @@ -65,10 +65,14 @@ def wav_u8_mono(data): result, buf = audiocore.get_buffer(sample) print(length, result, len(buf), list(buf)) -# A caller-supplied buffer is split into halves that are a multiple of 4 bytes, -# so padding the last buffer stays inside its half. +# A caller-supplied buffer is split in half and each half must be a multiple of +# 4 bytes for the pad to fit, so the whole buffer must be a multiple of 8 bytes. with fs.open("u8-5.wav", "rb") as f: - sample = audiocore.WaveFile(f, bytearray(10)) + try: + audiocore.WaveFile(f, bytearray(12)) + except ValueError as e: + print("ValueError:", e) + sample = audiocore.WaveFile(f, bytearray(8)) audiocore.reset_buffer(sample) result = 1 while result == 1: diff --git a/tests/circuitpython/wavefile_8bit_padding.py.exp b/tests/circuitpython/wavefile_8bit_padding.py.exp index e49e2cb1c2c..b7ec4b5c871 100644 --- a/tests/circuitpython/wavefile_8bit_padding.py.exp +++ b/tests/circuitpython/wavefile_8bit_padding.py.exp @@ -3,5 +3,6 @@ 6 0 8 [32, 33, 34, 35, 36, 37, 128, 128] 7 0 8 [32, 33, 34, 35, 36, 37, 38, 128] 8 0 8 [32, 33, 34, 35, 36, 37, 38, 39] +ValueError: Buffer must be a multiple of 8 bytes 1 4 [32, 33, 34, 35] 0 4 [36, 128, 128, 128] From 48e5ad1f36de002da05af952a1bae061aa01e378 Mon Sep 17 00:00:00 2001 From: Vladimir Smitka Date: Mon, 7 Sep 2026 09:04:46 +0000 Subject: [PATCH 09/12] build_memory_info: leave broadcom and cxd56 out Neither has a flash firmware region to measure against. cxd56 links the image into RAM through the Spresense SDK script, so its map has one region and it is called ram; broadcom boots kernel.img off an SD card and its regions are RAM and READONLY. Nothing there constrains the firmware, so there is no headroom to compute and the step only printed what it could not find. --- ports/broadcom/Makefile | 1 - ports/cxd56/Makefile | 1 - 2 files changed, 2 deletions(-) diff --git a/ports/broadcom/Makefile b/ports/broadcom/Makefile index b1b1bf07ed4..dda93c6fd7c 100644 --- a/ports/broadcom/Makefile +++ b/ports/broadcom/Makefile @@ -124,7 +124,6 @@ $(BUILD)/kernel$(SUFFIX).elf: $(OBJ) $(STEPECHO) "LINK $@" $(Q)echo $(OBJ) > $(BUILD)/firmware.objs $(Q)$(CC) -o $@ $(LDFLAGS) @$(BUILD)/firmware.objs -Wl,--print-memory-usage -Wl,--start-group $(LIBS) -Wl,--end-group - $(Q)$(SIZE) $@ | $(PYTHON) $(TOP)/tools/build_memory_info.py $@.map $(BUILD) endif $(BUILD)/kernel$(SUFFIX).img: $(BUILD)/kernel$(SUFFIX).elf diff --git a/ports/cxd56/Makefile b/ports/cxd56/Makefile index 5ad25eb7400..88c39210220 100644 --- a/ports/cxd56/Makefile +++ b/ports/cxd56/Makefile @@ -153,7 +153,6 @@ $(BUILD)/libmpy.a: $(SPRESENSE_SDK) $(OBJ) $(BUILD)/firmware.elf: $(BUILD)/libmpy.a $(ECHO) "LD $@" $(Q)$(LD) $(LDFLAGS) - $(Q)$(SIZE) $@ | $(PYTHON) $(TOP)/tools/build_memory_info.py $(BUILD)/output.map $(BUILD) $(MKSPK): $(MAKE) -C mkspk From b9cc0c404bb9d703d2f77429930ac1103b9249a5 Mon Sep 17 00:00:00 2001 From: Vladimir Smitka Date: Mon, 7 Sep 2026 09:26:26 +0000 Subject: [PATCH 10/12] build_memory_info: read every port's region from the map atmel-samd, analog, litex, nordic and stm were still passing their linker script, which left the script answering the same question two ways. Their maps carry the same numbers -- the script computed them, ld resolved them -- so point those five at the map as well and the linker script parser, the K and M suffix handling and the eval that needed them all come out. What a port supplies is now one rule: the map for the region, size(1) or --image for what is used. --- ports/analog/Makefile | 2 +- ports/atmel-samd/Makefile | 2 +- ports/litex/Makefile | 2 +- ports/nordic/Makefile | 2 +- ports/stm/Makefile | 2 +- tools/build_memory_info.py | 27 ++------------------------- 6 files changed, 7 insertions(+), 30 deletions(-) diff --git a/ports/analog/Makefile b/ports/analog/Makefile index 6b86bd70396..c81280e11ab 100644 --- a/ports/analog/Makefile +++ b/ports/analog/Makefile @@ -299,7 +299,7 @@ $(BUILD)/firmware.elf: $(OBJ) $(STEPECHO) "LINK $@" $(Q)echo $^ > $(BUILD)/firmware.objs $(Q)$(CC) -o $@ $(LDFLAGS) @$(BUILD)/firmware.objs -Wl,--print-memory-usage -Wl,--start-group $(LIBS) -Wl,--end-group - $(Q)$(SIZE) $@ | $(PYTHON) $(TOP)/tools/build_memory_info.py $(LINKERFILE) $(BUILD) + $(Q)$(SIZE) $@ | $(PYTHON) $(TOP)/tools/build_memory_info.py $@.map $(BUILD) $(BUILD)/firmware.hex: $(BUILD)/firmware.elf $(STEPECHO) "Create $@" diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index 0aaf6e3d849..ad4e65b6ddb 100644 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -348,7 +348,7 @@ $(BUILD)/firmware.elf: $(OBJ) $(GENERATED_LD_FILE) $(STEPECHO) "LINK $@" $(Q)echo $(OBJ) > $(BUILD)/firmware.objs $(Q)$(CC) -o $@ $(LDFLAGS) @$(BUILD)/firmware.objs -Wl,--print-memory-usage -Wl,--start-group $(LIBS) -Wl,--end-group - $(Q)$(SIZE) $@ | $(PYTHON) $(TOP)/tools/build_memory_info.py $(GENERATED_LD_FILE) $(BUILD) + $(Q)$(SIZE) $@ | $(PYTHON) $(TOP)/tools/build_memory_info.py $@.map $(BUILD) endif $(BUILD)/firmware.bin: $(BUILD)/firmware.elf diff --git a/ports/litex/Makefile b/ports/litex/Makefile index cc813a49f8e..9792ce301a3 100644 --- a/ports/litex/Makefile +++ b/ports/litex/Makefile @@ -118,7 +118,7 @@ else $(BUILD)/firmware.elf: $(OBJ) $(STEPECHO) "LINK $@" $(Q)$(CC) -o $@ $(LDFLAGS) $^ -Wl,--print-memory-usage -Wl,--start-group $(LIBS) -Wl,--end-group - $(Q)$(SIZE) $@ | $(PYTHON) $(TOP)/tools/build_memory_info.py $(LD_FILE) $(BUILD) + $(Q)$(SIZE) $@ | $(PYTHON) $(TOP)/tools/build_memory_info.py $@.map $(BUILD) endif $(BUILD)/firmware.bin: $(BUILD)/firmware.elf diff --git a/ports/nordic/Makefile b/ports/nordic/Makefile index ec755fac892..2abee97386c 100755 --- a/ports/nordic/Makefile +++ b/ports/nordic/Makefile @@ -200,7 +200,7 @@ $(BUILD)/firmware.elf: $(OBJ) $(GENERATED_LD_FILE) $(STEPECHO) "LINK $@" $(Q)echo $(OBJ) > $(BUILD)/firmware.objs $(Q)$(CC) -o $@ $(LDFLAGS) @$(BUILD)/firmware.objs -Wl,--print-memory-usage -Wl,--start-group $(LIBS) -Wl,--end-group - $(Q)$(SIZE) $@ | $(PYTHON) $(TOP)/tools/build_memory_info.py $(GENERATED_LD_FILE) $(BUILD) + $(Q)$(SIZE) $@ | $(PYTHON) $(TOP)/tools/build_memory_info.py $@.map $(BUILD) endif $(BUILD)/firmware.bin: $(BUILD)/firmware.elf diff --git a/ports/stm/Makefile b/ports/stm/Makefile index 5ad0c82654c..f7c17ea8d42 100755 --- a/ports/stm/Makefile +++ b/ports/stm/Makefile @@ -257,7 +257,7 @@ $(BUILD)/firmware.elf: $(OBJ) $(STEPECHO) "LINK $@" $(Q)echo $^ > $(BUILD)/firmware.objs $(Q)$(CC) -o $@ $(LDFLAGS) @$(BUILD)/firmware.objs -Wl,--print-memory-usage -Wl,--start-group $(LIBS) -Wl,--end-group - $(Q)$(SIZE) $@ | $(PYTHON) $(TOP)/tools/build_memory_info.py $(LD_FILE) $(BUILD) + $(Q)$(SIZE) $@ | $(PYTHON) $(TOP)/tools/build_memory_info.py $@.map $(BUILD) endif $(BUILD)/firmware.bin: $(BUILD)/firmware.elf diff --git a/tools/build_memory_info.py b/tools/build_memory_info.py index 3f6f9139669..8e24cbc709e 100755 --- a/tools/build_memory_info.py +++ b/tools/build_memory_info.py @@ -11,13 +11,6 @@ import json -# Handle size constants with K or M suffixes (allowed in .ld but not in Python). -K_PATTERN = re.compile(r"([0-9]+)[kK]") -K_REPLACE = r"(\1*1024)" - -M_PATTERN = re.compile(r"([0-9]+)[mM]") -M_REPLACE = r"(\1*1024*1024)" - # A linker map lists every region with the lengths already resolved: # Name Origin Length Attributes # FLASH_FIRMWARE 0x10000000 0x0017f000 xr @@ -66,23 +59,7 @@ def regions_from_map(contents): return regions or None -def regions_from_linker_script(contents): - """Region sizes from a linker script, for the ports that pass one.""" - regions = {} - for line in contents.split("\n"): - line = line.strip() - if line.startswith(("FLASH_FIRMWARE", "RAM")): - regions[line.split()[0]] = line.split("=")[-1] - for region, space in regions.items(): - if "/*" in space: - space = space.split("/*")[0] - space = K_PATTERN.sub(K_REPLACE, space) - space = M_PATTERN.sub(M_REPLACE, space) - regions[region] = int(eval(space)) - return regions - - -# This file is either a linker map or the linker script. +# The linker map lists the regions with every size the script computed resolved. try: with open(argv[0], "r") as f: contents = f.read() @@ -91,7 +68,7 @@ def regions_from_linker_script(contents): print(f"No {argv[0]} to read the flash region from.") print() sys.exit(0) -regions = regions_from_map(contents) or regions_from_linker_script(contents) +regions = regions_from_map(contents) or {} firmware_region = None for name in flash_names: From 41a5731fe575ad7207aa433f2f0ef54dfaeecd7d Mon Sep 17 00:00:00 2001 From: Vladimir Smitka Date: Fri, 4 Sep 2026 18:27:00 +0000 Subject: [PATCH 11/12] build_release_files: predict translation sizes instead of building them on PRs On pull requests the translations other than en_US are built only to prove that they still fit in flash. The compiled code is identical for every translation; only three generated data files differ: the compressed strings, the compression dictionary and the terminal font. When the first language leaves less than 10 KB of headroom, run only the generators (the make targets for translations-.c and autogen_display_resources-.c, about 4 s, no compiler), count the bytes of the generated tables and predict the flash usage. Only translations predicted within LANGUAGE_MARGIN (1 KB) of the limit, or whose build configuration differs (clean build), are really built. LANGUAGE_PREDICT=dryrun builds everything and prints the prediction error; LANGUAGE_PREDICT=off restores the old behaviour. Measured on feather_m4_can, pybadge and metro_m4_express with every language (41 predictions): error -5..+174 bytes, almost always an over-estimate. On metro_m4_express the prediction flagged fr as not fitting; the real build then overflowed by 56 bytes. feather_m4_can board job: 443 s -> 228 s; the remainder is the two clean builds. Push and release builds are unchanged. --- tools/build_release_files.py | 153 ++++++++++++++++++++++++++++++++--- 1 file changed, 143 insertions(+), 10 deletions(-) diff --git a/tools/build_release_files.py b/tools/build_release_files.py index 25ae0facd1b..3aa01234ea2 100755 --- a/tools/build_release_files.py +++ b/tools/build_release_files.py @@ -6,6 +6,7 @@ import os import multiprocessing +import re import sys import subprocess import shutil @@ -35,6 +36,91 @@ LANGUAGE_FIRST = "en_US" LANGUAGE_THRESHOLD = 10 * 1024 +# On pull requests the translations other than en_US are built only to prove that they +# still fit in flash. The compiled code is identical for every translation; only three +# generated data files differ: the compressed strings, the compression dictionary and +# the terminal font. So instead of relinking the firmware for each translation, generate +# those files, count their bytes and predict the flash usage. Only translations predicted +# within LANGUAGE_MARGIN of the region limit, or whose build configuration differs, are +# really built. LANGUAGE_PREDICT=dryrun builds everything and prints the prediction +# error; LANGUAGE_PREDICT=off restores the old behaviour. +LANGUAGE_MARGIN = int(os.environ.get("LANGUAGE_MARGIN", 1024)) +LANGUAGE_PREDICT = os.environ.get("LANGUAGE_PREDICT", "skip") + +C_TYPE_SIZES = { + "char": 1, + "int8_t": 1, + "uint8_t": 1, + "int16_t": 2, + "uint16_t": 2, + "int32_t": 4, + "uint32_t": 4, +} +C_ARRAY_RE = re.compile(r"const\s+(\w+)\s+\w+\[\d*\]\s*=\s*\{([^}]*)\}") +TRANSLATION_RE = re.compile(r"\.data = \d+, \.tail = \{([^}]*)\}") + + +def flash_usage(port, build_dir): + """Return (used, region) bytes of the firmware flash region, or None if unknown.""" + try: + with open(f"../ports/{port}/{build_dir}/firmware.size.json", "r") as f: + firmware = json.load(f) + return firmware["used_flash"], firmware["firmware_region"] + except FileNotFoundError: + return None + + +def c_array_bytes(path): + """Sum the bytes of the const arrays initialised in a generated C file.""" + if not path.exists(): + return 0 + text = path.read_text() + total = 0 + for match in C_ARRAY_RE.finditer(text): + c_type, body = match.groups() + if c_type not in C_TYPE_SIZES: + typedef = re.search(r"typedef\s+(\w+)\s+" + c_type + ";", text) + c_type = typedef.group(1) if typedef else "uint8_t" + total += C_TYPE_SIZES[c_type] * len([x for x in body.split(",") if x.strip()]) + return total + + +def translation_bytes(port, build_dir, language): + """Bytes of flash that depend on the translation: strings, dictionary and font.""" + build = pathlib.Path(f"../ports/{port}/{build_dir}") + strings = 0 + for match in TRANSLATION_RE.finditer( + (build / "py" / f"translations-{language}.c").read_text() + ): + strings += 1 + len([x for x in match.group(1).split(",") if x.strip()]) + dictionary = c_array_bytes(build / "genhdr" / "compressed_translations.generated.h") + font = c_array_bytes(build / f"autogen_display_resources-{language}.c") + return strings + dictionary + font + + +def generate_translation(port, board, build_dir, language): + """Generate the translation data files of a language without compiling anything.""" + targets = [f"{build_dir}/py/translations-{language}.c"] + if os.path.exists(f"../ports/{port}/{build_dir}/autogen_display_resources-{LANGUAGE_FIRST}.c"): + targets.append(f"{build_dir}/autogen_display_resources-{language}.c") + result = subprocess.run( + "make -C ../ports/{port} TRANSLATION={language} BOARD={board} BUILD={build} -j {cores} {targets}".format( + port=port, + language=language, + board=board, + build=build_dir, + cores=cores, + targets=" ".join(targets), + ), + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + ) + if result.returncode != 0: + print(result.stdout.decode("utf-8")) + return result.returncode == 0 + + languages = build_info.get_languages() all_languages = build_info.get_languages(list_all=True) @@ -71,6 +157,11 @@ languages.remove(LANGUAGE_FIRST) languages.insert(0, LANGUAGE_FIRST) + # Set after the first language when its flash usage is known and too tight to skip + # the other languages outright. + predict_flash = None + first_language_bytes = 0 + for language in languages: bin_directory = "../bin/{board}/{language}".format(board=board, language=language) os.makedirs(bin_directory, exist_ok=True) @@ -99,6 +190,34 @@ extensions = board_settings["CIRCUITPY_BUILD_EXTENSIONS"] artifacts = [os.path.join(build_dir, "firmware." + extension) for extension in extensions] + + prediction = None + if predict_flash is not None and language != LANGUAGE_FIRST and not clean_build: + if generate_translation(board_info["port"], board, build_dir, language): + delta = ( + translation_bytes(board_info["port"], build_dir, language) + - first_language_bytes + ) + predicted = predict_flash[0] + delta + fits = predicted + LANGUAGE_MARGIN <= predict_flash[1] + skip = fits and LANGUAGE_PREDICT == "skip" + prediction = predicted + print( + "Predict {board} for {language}: {predicted} of {region} bytes ({free} free, {delta:+d} vs {first}) -> {action}".format( + board=board, + language=language, + predicted=predicted, + region=predict_flash[1], + free=predict_flash[1] - predicted, + delta=delta, + first=LANGUAGE_FIRST, + action="skip" if skip else "build", + ), + flush=True, + ) + if skip: + continue + make_result = subprocess.run( "make -C ../ports/{port} TRANSLATION={language} BOARD={board} BUILD={build} -j {cores} {artifacts}".format( port=board_info["port"], @@ -154,19 +273,33 @@ print(make_result.stdout.decode("utf-8")) print(other_output) + if prediction is not None and make_result.returncode == 0: + usage = flash_usage(board_info["port"], build_dir) + if usage is not None: + print( + "Predict check {board} for {language}: predicted {predicted}, actual {actual}, error {error:+d}".format( + board=board, + language=language, + predicted=prediction, + actual=usage[0], + error=prediction - usage[0], + ) + ) + # Flush so we will see something before 10 minutes has passed. print(flush=True) if (not build_all) and (language == LANGUAGE_FIRST) and (exit_status == 0): - try: - with open( - f"../ports/{board_info['port']}/{build_dir}/firmware.size.json", "r" - ) as f: - firmware = json.load(f) - if firmware["used_flash"] + LANGUAGE_THRESHOLD < firmware["firmware_region"]: - print("Skipping languages") - break - except FileNotFoundError: - pass + usage = flash_usage(board_info["port"], build_dir) + if usage is None: + print("Flash usage unknown, building all languages") + elif usage[0] + LANGUAGE_THRESHOLD < usage[1]: + print("Skipping languages") + break + elif LANGUAGE_PREDICT != "off" and board_info["port"] != "zephyr-cp": + predict_flash = usage + first_language_bytes = translation_bytes( + board_info["port"], build_dir, LANGUAGE_FIRST + ) sys.exit(exit_status) From ee51781e39d2d35f1fcb5fd23a4df24ea01a87f1 Mon Sep 17 00:00:00 2001 From: Vladimir Smitka Date: Mon, 7 Sep 2026 14:35:28 +0000 Subject: [PATCH 12/12] ci: LANGUAGE_PREDICT=dryrun -- measurement run only, do not merge --- .github/workflows/build-boards.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build-boards.yml b/.github/workflows/build-boards.yml index 9d334a1f974..7aa94b69964 100644 --- a/.github/workflows/build-boards.yml +++ b/.github/workflows/build-boards.yml @@ -82,6 +82,7 @@ jobs: run: python3 -u build_release_files.py working-directory: tools env: + LANGUAGE_PREDICT: dryrun BOARDS: ${{ matrix.board }} PULL: ${{ github.event.number }} HEAD_COMMIT_MESSAGE: ${{ github.event.head_commit.message }}