tkl_flash | Flash Driver
The tkl_flash API provides the kernel-layer (TKL) operations for on-chip Flash storage: read, write, erase, lock, unlock, and partition-layout queries. Every function returns OPRT_OK on success or an error code defined in tuya_error_code.h.
The tkl_flash.c implementation is generated by the porting tool. You add platform-specific code between the BEGIN and END comment markers so it survives regeneration.
tkl_flash_read
OPERATE_RET tkl_flash_read(uint32_t addr, uint8_t *dst, uint32_t size);
Reads data from Flash.
| Parameter | Direction | Description |
|---|---|---|
addr | in | Flash address to read from. |
dst | out | Pointer to the buffer that receives the data. |
size | in | Number of bytes to read. |
Returns OPRT_OK on success. Other values indicate an error; see tuya_error_code.h.
tkl_flash_write
OPERATE_RET tkl_flash_write(uint32_t addr, const uint8_t *src, uint32_t size);
Writes data to Flash.
| Parameter | Direction | Description |
|---|---|---|
addr | in | Flash address to write to. |
src | in | Pointer to the buffer that holds the data to write. |
size | in | Number of bytes to write. |
Returns OPRT_OK on success. Other values indicate an error; see tuya_error_code.h.
Erase the target region before writing. Flash bits can only be cleared by an erase, so writing over data that has not been erased produces incorrect results.
tkl_flash_erase
OPERATE_RET tkl_flash_erase(uint32_t addr, uint32_t size);
Erases a region of Flash.
| Parameter | Direction | Description |
|---|---|---|
addr | in | Flash address to erase. |
size | in | Size of the Flash block to erase. |
Returns OPRT_OK on success. Other values indicate an error; see tuya_error_code.h.
tkl_flash_lock
OPERATE_RET tkl_flash_lock(uint32_t addr, uint32_t size);
Locks a region of Flash against writes and erases.
| Parameter | Direction | Description |
|---|---|---|
addr | in | Start address of the region to lock. |
size | in | Size of the region to lock. |
Returns OPRT_OK on success. Other values indicate an error; see tuya_error_code.h.
tkl_flash_unlock
OPERATE_RET tkl_flash_unlock(uint32_t addr, uint32_t size);
Unlocks a previously locked region of Flash.
| Parameter | Direction | Description |
|---|---|---|
addr | in | Start address of the region to unlock. |
size | in | Size of the region to unlock. |
Returns OPRT_OK on success. Other values indicate an error; see tuya_error_code.h.
tkl_flash_get_one_type_info
OPERATE_RET tkl_flash_get_one_type_info(TUYA_FLASH_TYPE_E type, TUYA_FLASH_BASE_INFO_T *info);
Queries the partition layout for a given Flash type.
| Parameter | Direction | Description |
|---|---|---|
type | in | Flash type to query. See TUYA_FLASH_TYPE_E. |
info | out | Pointer to a TUYA_FLASH_BASE_INFO_T that receives the partition layout. |
Returns OPRT_OK on success. Other values indicate an error; see tuya_error_code.h.
TUYA_FLASH_TYPE_E
Identifies the logical storage area to query. Selected values:
| Enumerator | Meaning |
|---|---|
TUYA_FLASH_TYPE_BTL0 | Bootloader area 0. |
TUYA_FLASH_TYPE_BTL1 | Bootloader area 1. |
TUYA_FLASH_TYPE_APP | Application firmware area. |
TUYA_FLASH_TYPE_OTA | OTA download area. |
TUYA_FLASH_TYPE_KV_DATA | Key-value data area. |
TUYA_FLASH_TYPE_UF | User file area. |
TUYA_FLASH_TYPE_ALL | All partitions. |
See tuya_cloud_types.h for the full enumeration.
TUYA_FLASH_BASE_INFO_T
typedef struct {
uint32_t block_size;
uint32_t start_addr;
uint32_t size;
} TUYA_FLASH_PARTITION_T;
typedef struct {
uint32_t partition_num;
TUYA_FLASH_PARTITION_T partition[TUYA_FLASH_TYPE_MAX_PARTITION_NUM];
} TUYA_FLASH_BASE_INFO_T;
| Field | Description |
|---|---|
partition_num | Number of valid entries in partition. |
partition | Array of partition descriptors, each holding block_size, start_addr, and size. |