Skip to main content

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.

ParameterDirectionDescription
addrinFlash address to read from.
dstoutPointer to the buffer that receives the data.
sizeinNumber 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.

ParameterDirectionDescription
addrinFlash address to write to.
srcinPointer to the buffer that holds the data to write.
sizeinNumber of bytes to write.

Returns OPRT_OK on success. Other values indicate an error; see tuya_error_code.h.

note

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.

ParameterDirectionDescription
addrinFlash address to erase.
sizeinSize 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.

ParameterDirectionDescription
addrinStart address of the region to lock.
sizeinSize 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.

ParameterDirectionDescription
addrinStart address of the region to unlock.
sizeinSize 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.

ParameterDirectionDescription
typeinFlash type to query. See TUYA_FLASH_TYPE_E.
infooutPointer 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:

EnumeratorMeaning
TUYA_FLASH_TYPE_BTL0Bootloader area 0.
TUYA_FLASH_TYPE_BTL1Bootloader area 1.
TUYA_FLASH_TYPE_APPApplication firmware area.
TUYA_FLASH_TYPE_OTAOTA download area.
TUYA_FLASH_TYPE_KV_DATAKey-value data area.
TUYA_FLASH_TYPE_UFUser file area.
TUYA_FLASH_TYPE_ALLAll 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;
FieldDescription
partition_numNumber of valid entries in partition.
partitionArray of partition descriptors, each holding block_size, start_addr, and size.