FLASH Driver

[中文]

Overview

FLASH memory driver provides access to on-chip and external flash memory for program storage and data persistence.

Functional Description

  • Driver initialization and deinitialization

  • Concurrent access protection with mutex locks

  • Line mode configuration (1/2/4-line)

  • Flash ID identification and status register access

  • Clock configuration (DPLL/DCO)

  • Write protection and multiple protection types

  • Sector and block erase operations

  • Byte/word read/write operations

  • Deep sleep entry/exit support

  • Power management callbacks

  • Runtime clock switching

  • Busy status monitoring

Development Guide

  1. Initialization - Call bk_flash_driver_init() before any flash operations - Configure line mode: bk_flash_set_line_mode() - Set clock source: bk_flash_set_clock_dpll/dco()

  2. Basic Operations - Read: bk_flash_read_bytes(), bk_flash_read_word() - Write: bk_flash_write_bytes(), bk_flash_write_word() - Erase: bk_flash_erase_sector(), bk_flash_erase_block()

  3. Concurrent Protection - Use bk_flash_lock() and bk_flash_unlock() in multi-task environments - Ensure atomic flash operations

  4. Power Management - Deep sleep: bk_flash_deep_sleep_entry/exit() - Power save: bk_flash_power_save_suspend/resume()

Notes

  • Erase operations clear entire sectors/blocks

  • Write operations require prior erasure

  • Consider flash lifetime and erase cycles

  • Use proper concurrent protection

API Reference

  • bk_flash_driver_init/deinit() - Driver management

  • bk_flash_lock/unlock() - Concurrent protection

  • bk_flash_set_line_mode() - Configure line mode

  • bk_flash_get_id() - Get flash ID

  • bk_flash_set_clock_*() - Clock configuration

  • bk_flash_write_enable/disable() - Write protection

  • bk_flash_read/write_status_reg() - Status register access

  • bk_flash_erase_sector/block() - Erase operations

  • bk_flash_read/write_bytes/word() - Data operations

  • bk_flash_deep_sleep_entry/exit() - Deep sleep

  • bk_flash_power_save_*() - Power management

Examples

Basic read/write:

bk_flash_driver_init();
bk_flash_set_line_mode(FLASH_LINE_MODE_4);

uint32_t id = bk_flash_get_id();
printf("Flash ID: 0x%08X\n", id);

uint8_t data[256];
bk_flash_read_bytes(0x1000, data, sizeof(data));

uint8_t write_data[256];
for (int i = 0; i < 256; i++) {
    write_data[i] = i & 0xFF;
}
bk_flash_write_bytes(0x2000, write_data, sizeof(write_data));

Erase operations:

bk_flash_lock();
bk_flash_erase_sector(0x1000);
bk_flash_erase_block(0x2000);
bk_flash_unlock();

Status monitoring:

if (bk_flash_driver_inited()) {
    printf("Flash driver initialized\n");
}

uint32_t size = bk_flash_get_total_size();
printf("Flash size: %d bytes\n", size);

uint16_t status = bk_flash_read_status_reg();
printf("Status: 0x%04X\n", status);

FAQs

  • Write failure: Check write protection, erase status, address range

  • Read errors: Verify address range and line mode

  • Erase failure: Confirm sector/block address and write protection

Error Codes

  • BK_ERR_FLASH_ADDR_OUT_OF_RANGE: Address out of range

  • Other errors: Usually related to QSPI/SPI access failure, protection limits, or high frequency