--- kind: configuration_system name: ESP-IDF + MicroPython Multi-Board Configuration System (sdkconfig, mpconfigboard, NVS, partition table) category: configuration_system scope: - '**' source_files: - esp32s3/firmware/boards/sdkconfig.base - esp32s3/firmware/boards/sdksdkconfig.ble - esp32s3/firmware/boards/sdksdksdkconfig.spiram - esp32s3/firmware/boards/mpython_v3/mpconfigboard.h - esp32s3/firmware/boards/labplus_Ledong_v2/mpconfigboard.h - esp32s3/firmware/boards/labplus_Ledong_v2/main.c - esp32s3/firmware/builtins/main.c - esp32s3/firmware/boards/mpython_v3/partitions-16MiB.csv - esp32s3/firmware/eim_config.toml --- # 1. System / Framework The runtime configuration system of the ESP32-S3 product line in this repository is built on top of **ESP-IDF v5.x + MicroPython v1.24.1** and is organized as three stacked layers: - **Build-time configuration**: composed from the ESP-IDF `sdkconfig.*` fragments through CMake, controlling compiler options and the low-level behaviour of LWIP/mbedTLS/FreeRTOS and friends. - **Board-level configuration**: the `mpconfigboard.h` of each board type determines the enabled Python built-in modules, VFS, USB REPL, I2C/SPI pin mapping, and so on. - **Runtime persistent configuration**: user settings are stored as key/value pairs in the ESP32-S3 NVS (Non-Volatile Storage) and exposed to Python as the `esp32.NVS` type. - **File system / application scripts**: at boot the frozen `_boot.py`, then `boot.py` and `main.py` in LittleFS, are executed in order and serve as the application-level configuration entry points. - **Development tool configuration**: `esp32s3/firmware/eim_config.toml` is the local environment configuration of the EIM (Espressif IoT Manager) tool and takes no part in firmware compilation. # 2. Key Files and Locations - **ESP-IDF sdkconfig fragments** (build time): `esp32s3/firmware/boards/sdkconfig.base`, `sdkconfig.ble`, `sdkconfig.spiram*`, `sdkconfig.p4_wifi_*`, `sdkconfig.riscv`, `sdkconfig.flash_2MiB`, `sdkconfig.free_ram`, and more; the `sdkconfig.board` and `sdkconfig.usb` files in each board directory override the defaults. - **MicroPython board-level configuration**: `esp32s3/firmware/boards/*/mpconfigboard.h` (one per board) declares `MICROPY_HW_BOARD_NAME`, `MICROPY_HW_MCU_NAME`, the pins, and the feature switches (such as `MICROPY_PY_AUDIO`, `MICROPY_PY_RFID`); `mpconfigboard.cmake` declares `IDF_TARGET=esp32s3`, `SDKCONFIG_DEFAULTS`, `MICROPY_SOURCE_BOARD`, and `MICROPY_FROZEN_MANIFEST`. - **Board-level main.c**: `esp32s3/firmware/builtins/main.c` (the common S3 entry point) and `esp32s3/firmware/boards/*/main.c` (board-specific entry points) are responsible for initializing NVS, creating mp_task, and loading `_boot.py` / `boot.py` / `main.py`. - **NVS Python interface**: `esp32s3/firmware/builtins/esp32_nvs.c` implements the `NVS(namespace)` object and the `get_i32/set_i32/get_blob/set_blob/erase_key/commit` methods, always using the `user_nvs` namespace. - **Partition table**: `esp32s3/firmware/boards/*/partitions-*.csv` (such as `partitions-16MiB.csv`) defines the `nvs`, `phy_init`, `factory`, `vfs` (spiffs/littlefs), `voice_data` (fat), `model` (spiffs), and `user_nvs` partitions. - **Application scripts**: each board type can place its own `boot.py`/`main.py` in the `file_system//` directory; `makeimg.py` packs them into a LittleFS image that is written to the `vfs` partition. - **EIM tool configuration**: `esp32s3/firmware/eim_config.toml` is only used for the local EIM toolchain installation path and mirror settings and does not end up in the firmware. # 3. Architecture and Design Conventions - **Layered configuration**: `sdkconfig.base` provides the common baseline, each `sdkconfig.*` fragment is stacked according to a feature (BLE, PSRAM, Wi-Fi chip family, RISC-V), and the board-level `sdkconfig.board`/`sdkconfig.usb` then override the concrete hardware differences. Adding a board type requires copying one set and modifying `mpconfigboard.h`, `partitions-*.csv`, and `sdkconfig.board`. - **Board type enumeration**: the S3 firmware supports 5 board types -- `mpython_v3`, `labplus_for_xuejing`, `labplus_Ledong_v2`, `labplus_xunfei_js_middle`, `labplus_xunfei_js_primary`. Each has its own `mpconfigboard.h`, `main.c`, `partitions-*.csv`, `manifest.py`, and so on under `esp32s3/firmware/boards//`. - **The boot flow is the configuration loading flow**: `app_main()` -> `MICROPY_BOARD_STARTUP()` (which by default calls `boardctrl_startup()` to initialize NVS) -> `xTaskCreatePinnedToCore(mp_task, ...)` -> inside `mp_task`, `pyexec_frozen_module("_boot.py")` -> `pyexec_file_if_exists("boot.py")` -> if the friendly REPL is active and no forced exit was requested, `pyexec_file_if_exists("main.py")`. Therefore `boot.py`/`main.py` are the main mount points for application-level configuration. - **NVS namespace convention**: all NVS reads and writes go through `esp32.NVS("user_nvs")`, opening the namespace named `user_nvs`, which is provided by the `user_nvs` partition in the partition table (offset `0xFFC000`, size `0x4000`). Keys are strings; values support i32 and blob. - **Log levels**: `sdkconfig.base` sets the default log level to `ERROR` and the maximum level to `INFO` so that `esp.osdebug()` can raise the log level at runtime. - **USB/serial REPL selection**: S3 board types switch between USB Serial JTAG, native USB OTG, and UART REPL through the `MICROPY_HW_ESP_USB_SERIAL_JTAG`, `MICROPY_HW_ENABLE_USBDEV`, and `MICROPY_HW_ENABLE_UART_REPL` macros. - **Memory and VFS**: S3 board types enable `MICROPY_VFS`, `MICROPY_FATFS_ENABLE_LFN`, and `MICROPY_MODULE_FROZEN_MPY=1`, which together with the `vfs` (LittleFS) partition in the partition table allows Python code to be loaded from the file system. # 4. Conventions and Constraints - **Configuration set required for a new board type**: at a minimum `mpconfigboard.h`, `mpconfigboard.cmake`, `main.c`, `partitions-*.csv`, `sdkconfig.board`, and `manifest.py` (optional), plus registration in the top-level CMake under `boards/`. - **NVS writes must be committed**: the `commit()` method exposed by `esp32_nvs.c` flushes the buffer to Flash; not calling it can cause data loss. - **NVS corruption recovery**: `boardctrl_startup()` automatically erases and retries when `nvs_flash_init()` returns `ESP_ERR_NVS_NO_FREE_PAGES` or `ESP_ERR_NVS_NEW_VERSION_FOUND`, avoiding a hang at boot. - **Application configuration takes precedence over build configuration**: `boot.py`/`main.py` can override at runtime the behaviour determined by build-time macros (for example enabling debug logs dynamically or switching network parameters), but they cannot change the `MICROPY_*` macros already compiled into the firmware. - **The partition layout must not be changed casually**: the `user_nvs` partition sits at the end of the partition table (`0xFFC000`) and its size `0x4000` is the upper limit of the NVS storage capacity; modifying the partition table requires a matching update of `CONFIG_PARTITION_TABLE_CUSTOM_FILENAME` in `sdkconfig.base`. - **The EIM configuration is for developer machines only**: fields such as `path`, `idf_path`, and `mirror` in `eim_config.toml` point at local paths and should not be committed to version control in a way that affects other people's builds (the current repository keeps sample values). - **Platform identification**: S3 board types distinguish the MCU model through `MICROPY_HW_MCU_NAME = "ESP32S3"`; applications can determine the target chip through `sys.platform` and the `machine` module. # 5. Relationship with Documentation and Examples - The documentation side (`esp32s3/docs/`) is mainly aimed at teachers, students, and developers, describing APIs and tutorials rather than build-time configuration details; however, `mpython.rst` under `library/mPython/` and the source file `mpython.py` wrap the on-board features, and the availability of those features is controlled by macros in `mpconfigboard.h` (such as `MICROPY_PY_AUDIO`, `MICROPY_PY_RFID`). - Example programs usually live in `esp32s3/firmware/examples/` or in the `file_system/` directory of each board type; they are loaded by the boot flow through `boot.py`/`main.py`, which is the real-world usage of "application-level configuration".