FLASH

[中文]

1 Functional Overview

Note

  • By default, FLASH is enabled on both CP and AP sides.

2 Code Paths

  • Demo path:
    components\bk_cli\cli_flash_test.c
  • Driver source path:
    middleware\driver\flash\flash_driver.c
    middleware\driver\flash\flash_driver_ext.c
    middleware\driver\flash\flash_server.c
    middleware\driver\flash\flash_client.c

Note

  • 1.flash_driver.c :FLASH driver implementation
    • 2.cli_flash_test.c:FLASH feature test command implementation

  • 3.flash_server.c:FLASH CP-side implementation in CP+AP architecture

  • 4.flash_client.c:FLASH AP-side implementation in CP+AP architecture

3 Key FLASH Data Structures

typedef struct {
    uint32_t flash_id;
    uint32_t flash_size;
    uint8_t status_reg_size;
    flash_line_mode_t line_mode;
    uint8_t cmp_post;
    uint8_t protect_post;
    uint8_t protect_mask;
    uint16_t protect_all;
    uint16_t protect_none;
    uint16_t unprotect_last_block;
    uint8_t quad_en_post;
    uint8_t quad_en_val;
    uint8_t coutinuous_read_mode_bits_val;
} flash_config_t;
The flash_config[] array of type flash_config_t stores parameters of currently supported flash vendors:
- flash_id: Flash ID
- flash_size: Supported address space size
- status_reg_size: Number of status register bytes
- line_mode: Flash line mode (2-line / 4-line)
- cmp_post / protect_post / protect_mask: Status register protection settings
- quad_en_post / quad_en_val: Quad enable bit configuration
- coutinuous_read_mode_bits_val: Continuous read mode value
typedef struct {
    flash_hal_t hal;
    uint32_t flash_id;
    uint32_t flash_status_reg_val;
    uint32_t flash_line_mode;
    const flash_config_t *flash_cfg;
} flash_driver_t;
The flash_config[] array of type flash_config_t stores parameters of currently supported flash vendors:
- flash_id: Flash ID information
- flash_size: Address space supported by Flash
- status_reg_size: Number of status register bytes
- line_mode: Flash line mode (2-line / 4-line)
- cmp_post / protect_post / protect_mask: Status register protection bits
- quad_en_post / quad_en_val: Quad enable bit configuration
- coutinuous_read_mode_bits_val: Continuous read mode value
typedef enum {
    FLASH_LINE_MODE_TWO = 2,
    FLASH_LINE_MODE_FOUR = 4
} flash_line_mode_t;
typedef enum {
    FLASH_PROTECT_NONE = 0,
    FLASH_PROTECT_ALL,
    FLASH_PROTECT_HALF,
    FLASH_UNPROTECT_LAST_BLOCK
} flash_protect_type_t;
The flash_protect_type_t defines FLASH protection modes:
- FLASH_PROTECT_NONE  --  No protection
- FLASH_PROTECT_ALL   --  Full protection
- FLASH_PROTECT_HALF  --  Half protection
- FLASH_UNPROTECT_LAST_BLOCK  -- Unprotect last block

4 Main FLASH API List

Initialization / Deinitialization

bk_err_t bk_flash_driver_init(void);

Description: Initialize FLASH driver and allocate control memory.

bk_err_t bk_flash_driver_deinit(void);

Description: Deinitialize FLASH driver and release resources.

Basic Control Interfaces

flash_line_mode_t bk_flash_get_line_mode(void);

Description: Get current FLASH line mode.

uint8_t bk_flash_get_coutinuous_read_mode(void);

Description: Get current continuous read mode.

uint32_t bk_flash_get_id(void);

Description: Get FLASH chip ID.

bool bk_flash_is_driver_inited(void);

Description: Check if FLASH driver is initialized.

FLASH Protection Functions

bk_err_t bk_flash_set_protect_type(flash_protect_type_t type);
flash_protect_type_t bk_flash_get_protect_type(void);

Description: Set/Get FLASH protection type.

bk_err_t bk_flash_write_enable(void);
bk_err_t bk_flash_write_disable(void);

Description: Enable/disable FLASH write operations.

uint16_t bk_flash_read_status_reg(void);
bk_err_t bk_flash_write_status_reg(uint16_t status_reg_data);

Description: Read/Write FLASH status register.

Erase Interfaces

bk_err_t bk_flash_erase_sector(uint32_t address);
bk_err_t bk_flash_erase_block(uint32_t address);
bk_err_t bk_flash_erase_fast(uint32_t erase_off, uint32_t len);

Description: Erase FLASH by sector (4K) or block (64K), supports fast erase.

Read/Write Interfaces

bk_err_t bk_flash_write_bytes(uint32_t address, const uint8_t *user_buf, uint32_t size);
bk_err_t bk_flash_read_bytes(uint32_t address, uint8_t *user_buf, uint32_t size);
bk_err_t bk_flash_read_word(uint32_t address, uint32_t *user_buf, uint32_t size);

Description: Read/Write FLASH in byte or word mode; word mode requires 4-byte alignment.

Low Power Interfaces

bk_err_t bk_flash_enter_deep_sleep(void);
bk_err_t bk_flash_exit_deep_sleep(void);

Description: Enter/Exit FLASH deep sleep mode.

Power Callback & Speed Adjustment

bk_err_t bk_flash_register_ps_suspend_callback(flash_ps_callback_t cb);
bk_err_t bk_flash_register_ps_resume_callback(flash_ps_callback_t cb);

Description: Register FLASH power management suspend/resume callbacks.

bk_err_t bk_flash_clk_switch(uint32_t speed_type, uint32_t modules);

Description: Dynamically adjust FLASH clock speed based on modules.

Operation Status Management

bk_err_t bk_flash_set_operate_status(flash_op_status_t status);
flash_op_status_t bk_flash_get_operate_status(void);

Description: Set/Get FLASH current operation status.

Wait & Async Notification Interfaces

bk_err_t bk_flash_register_wait_cb(flash_wait_callback_t wait_cb);
bk_err_t bk_flash_unregister_wait_cb(flash_wait_callback_t wait_cb);

Description: Register/Unregister FLASH wait callbacks, used when FLASH is busy (callbacks should be placed in ITCM).

Other Interfaces

uint32_t bk_flash_get_current_total_size(void);

Description: Get current FLASH total capacity (bytes).

5 CLI Command Overview

Demo macro configuration dependencies:

NAME

Description

File

value

CONFIG_FLASH

support FLASH

middleware\soc\bk7236\bk7236.defconfig

y

CONFIG_FLASH_TEST

support FLASH test command

middleware\soc\bk7236\bk7236.defconfig

y

Supported CLI Commands:

Command

Param

Description

flash_test R

test start address (hex)

Read the data from the address starting with the start address to the length of the size and print, e.g.: flash_test R 0x7fe000 0x100

test size (hex)

flash_test W

test start address (hex)

Write data from the address of the start address to the length of the size, the data is looped from 0 to 0xFF every 256 bytes, e.g.: flash_test W 0x7fe000 0x100

test size (hex)

flash_test E

test start address (hex)

Erase the data from the address starting from the start address to the size of the data, and the data after erasure is all 0xFF, eg: flash_test E 0x7fe000 0x100

test size (hex)

flash_test C

test start address (hex)

Test the read/write/erase function from the address starting from the start address to the length of size and count the time, and print the statistical time print_cnt every interval in us, eg: flash_test C 0x7fe000 0x1000 100

test size (hex)

print interval

flash_test P

none

Flash unlocks all areas

flash_test U

none

Flash unlocks the last block area

flash_test RSR

none

Read the status register value

flash_test WSR

status register data

Write status register values

fmap_test

none

Print partition information

6 FLASH Example Code

The following example functions demonstrate FLASH erase, write, and read operations, suitable for quick functional verification during development.

Function: test_flash_erase

static bk_err_t test_flash_erase(volatile uint32_t start_addr, uint32_t len)
{
    uint32_t addr = start_addr;
    uint32_t tmp = addr + len;

    for (; addr < tmp; addr += 0x1000) {
        bk_flash_erase_sector(addr);
    }
    return kNoErr;
}

Description: - Calls bk_flash_erase_sector to erase FLASH by 4KB sector.

  • start_addr should be 4KB aligned.

  • Erase range is [start_addr, start_addr + len).

  • It is recommended to call this function before writing FLASH to avoid write failures.

Function: test_flash_write

static bk_err_t test_flash_write(volatile uint32_t start_addr, uint32_t len)
{
    uint32_t i;
    u8 buf[256];
    uint32_t addr = start_addr;
    uint32_t tmp = addr + len;

    for (i = 0; i < 256; i++)
        buf[i] = i;

    for (; addr < tmp; addr += 256) {
        bk_flash_write_bytes(addr, (uint8_t *)buf, 256);
    }

    return kNoErr;
}

Description: - Writes fixed 256-byte data. The data content is incrementing from 0x00 to 0xFF.

  • Start address start_addr can be any valid address. Length len is recommended to be a multiple of 256.

  • Ensure that the target region is erased (call test_flash_erase) before writing.

Function: test_flash_read_print

static bk_err_t test_flash_read_print(volatile uint32_t start_addr, uint32_t len)
{
    uint32_t tmp;
    u8 buf[256];
    uint32_t addr = start_addr;
    tmp = addr + len;

    for (; addr < tmp; addr += 256) {
        os_memset(buf, 0, 256);
        bk_flash_read_bytes(addr, (uint8_t *)buf, 256);
    }

    return kNoErr;
}

Description:

  • Reads data from FLASH, 256 bytes per read, into temporary buffer buf.

  • This function does not print the read content; it is used to verify read operations or for performance testing.

  • Can be used together with test_flash_write to confirm that data written can be read correctly.