Add External Flash Memory

Last Updated on : 2025-07-28 07:45:54download

This topic describes how to add your own QSPI drivers in Tuya’s SDK, as well as the sequence of API calls, using GD5F1GQ5 as an example.

Add driver file

Add a driver file in ./vendor/T5/t5_os/bk_idk/middleware/driver/qspi/. For example, ./vendor/T5/t5_os/bk_idk/middleware/driver/qspi/qspi_flash_GD5F1GQ5.c.

Driver data structure

The QSPI driver data structure is defined as:

typedef struct {
    const char *name;

    uint32_t page_size;
    uint32_t block_size;
    uint32_t total_size;

    QFLASH_INIT_T           init;
    QFLASH_DEINIT_T         deinit;
    QFLASH_READ_ID_T        read_id;
    QFLASH_UNLOCK_T         unblock;
    QFLASH_PAGE_READ_T      read_page;
    QFLASH_PAGE_WRITE_T     write_page;
    QFLASH_BLOCK_ERASE_T    erase_block;

} qspi_driver_desc_t;

Parameters

Parameter Description
name The unique identifier to index the driver.
  • page_size
  • block_size
  • total_size
Flash memory specifications that can be obtained from the datasheet. They are used for read/write/erase operations and filesystem management.
  • init
  • deinit
Initialization/deinitialization (such as setting QSPI clock frequency). Refer to GD51F1G implementation.
read_id Read the flash ID.
unblock Remove flash protection.
  • read_page
  • write_page
  • erase_block
Implement read/write/erase operations.
Send commands and parameters based on the flash datasheet. Use logic analyzer for debugging if behavior diverges from expectations.

Reference driver file

After defining the qspi_driver_desc_t structure, complete the following operations.

  1. Add a declaration in the vendor/T5/t5_os/bk_idk/include/driver/qspi_flash_common.h file. For example, extern qspi_driver_desc_t qspi_gd5f1g_desc;.

  2. Add a reference in qspi_flash_devices global variable of vendor/T5/t5_os/bk_idk/middleware/driver/qspi/qspi_flash_common.c.

    static qspi_driver_desc_t *qspi_flash_devices[] = {
        &qspi_gd5f1g_desc,
        &qspi_gd25q127c_desc,
    };
    
  3. In vendor/T5/t5_os/bk_idk/middleware/driver/CMakeLists.txt, add files to the compilation system. Add the following files to the if condition of CONFIG_QSPI:

    Add External Flash Memory

Configuration and initialization

Add the configuration item CONFIG_TUYA_QSPI_FLASH_TYPE to the vendor/T5/t5_os/projects/tuya_app/config/bk7258/config file, and assign it to the value of the parameter const char *name in the QSPI driver data structure.

Add External Flash Memory