Flash, partitions, and capacity
Overview
On MCU targets, firmware, OTA, authorization, and application persistence (KV, LittleFS, or vendor FS) share external flash (typically NOR). TKL exposes erase/write/read through tkl_flash_*. Flash size and partition boundaries are board- and product-specific: they come from Kconfig, partition tables, and the board BSP, not from a single global constant in application code.
Audience: Developers configuring storage, OTA, or KV, and BSP authors adapting a new flash size.
Prerequisites
- Memory and storage overview
- Kconfig and project configuration for
tos.py config menuwhen toggling flash-related options.
Requirements
- Raw
tkl_flash_*offsets must respect the partition plan for your board; wrong ranges brick OTA or corrupt KV.
TKL flash API (reference layer)
Read/write/erase and metadata are documented under TKL flash:
tkl_flash_read/tkl_flash_write/tkl_flash_erasetkl_flash_get_one_type_info— query layout for aTUYA_FLASH_TYPE_Eentry intoTUYA_FLASH_BASE_INFO_T
Use these from drivers and low-level migration; application key–value persistence is usually easier through TAL KV (LittleFS under the hood on typical ports).
Reserved flash and ENABLE_FLASH
When porting a new board, you must reserve a dedicated flash region for TuyaOpen (authorization, pairing data, filesystem). See Porting platform:
ENABLE_FLASH— must be enabled; pick an unused range that does not overlap the main firmware image and respects erase granularity.ENABLE_FILE_SYSTEM— if disabled, TuyaOpen may use internal LittleFS whose base/size are supplied bytkl_flash.cin the adaptation.
Flash size changes (different parts / BOM)
- Hardware: New SPI NOR density (for example 4 MB → 8 MB) — update schematics, pin strapping if any, and datasheet limits.
- Software: Partition table and Kconfig defaults in the board tree under
TuyaOpen/boards/<platform>/<board>/(and any CSV or IDF-style layout your port uses). - Adapter: Implement or extend
tkl_flash_get_one_type_infoso reported base + size match the new layout; re-runtos.py config choice/menuif preset configs embed sizes. - Validation: OTA slot sizes, KV partition, and factory data must still fit; run OTA and KV tests on a dev unit.
Do not hard-code absolute end-of-flash addresses in application code; derive from TKL info or high-level TAL APIs.
OTA and tkl_ota
Firmware upgrade flows use the TKL OTA surface (tkl_ota in the sidebar). OTA images must fit the OTA partition defined for that product. See TKL OTA and your platform’s OTA documentation.
Linux and block storage
On Linux targets, “flash” at the TKL level may be implemented as:
- A file on a mounted filesystem (eMMC, SD, NVMe), or
- A mtd block device mapped to the same read/write/erase contract.
Application code should still go through TAL/TKL when sharing code with MCU builds; POSIX file I/O is valid only where the board guide documents it. DDR holds the running image and heap; eMMC is not accessed through tkl_flash_write byte-by-byte without an adapter that defines the mapping.
Expected outcome
You know which API layer (TKL raw flash vs TAL KV), which Kconfig / partition artifacts to change when flash size changes, and when to read layout from tkl_flash_get_one_type_info instead of magic numbers.