FLASH

[中文]

FLASH Module Overview

The bk_flash component provides APIs for on-chip Flash data reading, writing, erasing, and memory-mapped operations.

Note

For external Flash (off-chip) or Flash connected via SPI/QSPI interfaces, please refer to other chapters.

FLASH Feature Support

Supported FLASH list:

Flash chips from different vendors have differences in features and operation methods. Some models require customized drivers for support. The current Flash driver is compatible with most vendors for standard fast read and slow read operations within a 24-bit address space, as well as dual-line transmission modes (DIO/DOUT) that do not require customized commands.

For quad-line transmission modes (QIO/QOUT), the driver has been adapted for the following vendors or Flash models:

  • GD

  • MXIC

  • XTX

  • Tsingteng

  • Winbond

Note

Not every Flash model from the above vendors is supported. The supported Flash chips are matched based on their Flash ID. Their common features and configurations are defined in the flash_config_t structure. To add support for new Flash chips, you can extend the flash_config[] array in flash_driver.c.

FLASH Notes and Limitations

  • Flash erase granularity is typically 4KB.

  • It is recommended to erase the target region before writing to avoid erase/write failures.

  • Flash operations should be performed with interrupts disabled or with appropriate locking to prevent conflicts caused by task preemption.

  • In multicore systems, Flash operations should be mutually exclusive. In the SDK, this is already handled: when the AP core needs to perform Flash operations, it notifies the CP core via mailbox.

  • Since each Flash chip has a fixed size, you should check whether the target address exceeds the actual Flash size before performing operations, although the functions include size and address checks.

  • By default, the SDK manages Flash partitions. Some partition addresses are subject to permission controls for read/write/execute operations. For details, refer to the BK_FLASH_PARTITIONS_MAP variable in partitions_gen.h.

FLASH Common Issues and Debugging Tips

Common error codes and descriptions:

  • BK_ERR_FLASH_NOT_INIT (-0x3701): Flash driver not initialized.

  • BK_ERR_FLASH_ADDR_OUT_OF_RANGE (-0x3702): Invalid Flash operation address.

  • BK_ERR_FLASH_PARTITION_NOT_FOUND (-0x3703): Flash partition not found.

  • BK_ERR_FLASH_WAIT_CB_FULL (-0x3704): Flash callback registration limit exceeded.

  • BK_ERR_FLASH_WAIT_CB_NOT_REGISTER (-0x3705): Attempted to deregister an unregistered callback.

Unrecognized Flash ID

When Flash is not recognized, the following log will appear. This does not affect subsequent booting, but subsequent Flash operations will fall back to 2-wire mode.

flash:E(0):This flash is not identified, choose default config

At this point, check whether the Flash chip ID and related configuration are present in the flash_config[] array, and also ensure that you are using the latest library version.

Possible Causes of Flash Write Failures

  • Verify that the target write address is within a valid range.

  • Verify that the target address is not in a write-protected partition. Compare the address with the permissions in the bk_flash_partitions variable.

Slow Flash Write Speeds

  • Check whether the initialization log indicates that the Flash was initialized using a recognized Flash ID; otherwise, read/write operations may default to 2-wire mode.

  • Single write operations may be fast, but overall throughput may appear slow. It is recommended to merge multiple write operations and minimize the number of bk_flash_write_bytes() calls.

  • If write speed is unstable, it may be due to interruptions from high-priority tasks during the write process, which can cause fragmented writes and reduce overall speed.

BKFIL Flash Programming Failures

  • Ensure that you are using the latest version of BKFIL.