This is the documentation for the latest (main) development branch. If you are looking for the documentation of previous releases, use the drop-down menu on the left and select the desired version.

FLASH

Introduction

FLASH memory, also known as flash storage, is a type of non-volatile memory technology that retains data even when powered off. It supports electronic data erasure and writing while providing fast data reading capabilities.

Function List

  • Read Data

  • Write Data

  • Erase Data

  • Device Information Query

  • Support for both internal flash and external SPI-flash coexistence

Function Overview

Implements functions such as read, write, erase, and device query

  • Supports two writing methods: manual erasure before writing and automatic erasure before writing

  • Supports three erasure methods: erase from a specified offset, erase by sector, and erase all data on the entire flash chip

How to Add External FLASH

Steps to operate external flash:

  • Connect the flash via SPI; refer to SPI Master for SPI wiring

  • Add the flash to the device descriptor

  • In wm_drv_eflash_chips.h, mimic wm_external_flash_chip_p25q to add support for the corresponding flash

Main Functions

Write Data to FLASH

Prerequisites:

  • Initialize the flash before use

Related Sequence API:

  • Call wm_drv_flash_init to initialize the flash. If the device is already initialized, obtain the device pointer via the wm_dt_get_device_by_name function

  • Depending on your needs, call wm_drv_flash_write to write data to the specified address of the flash. The area must be erased before writing

  • Depending on your needs, call wm_drv_flash_write_with_erase to write data to the specified address of the flash, with the area being automatically erased before writing

Warning

If the area is not erased before writing data, the written data may be incorrect

Read FLASH Data

Prerequisites:

  • Initialize the flash before use

Related Sequence API:

  • Call wm_drv_flash_init to initialize the flash. If the device is already initialized, obtain the device pointer via the wm_dt_get_device_by_name function

  • Call wm_drv_flash_read to read data from the specified address of the flash into the provided buffer

Erase FLASH Data

Prerequisites:

  • Initialize the flash before use

Related Sequence API:

  • Call wm_drv_flash_init to initialize the flash. If the device is already initialized, obtain the device pointer via the wm_dt_get_device_by_name function

  • Depending on your needs, call wm_drv_flash_erase_region to erase a specified length of the region starting from the specified offset in the flash

  • Depending on your needs, call wm_drv_flash_erase_sector to erase the flash by sector, starting from the specified sector index and erasing the specified number of sectors

  • Depending on your needs, call wm_drv_flash_erase_chip to erase all data on the entire flash chip

Get FLASH Basic Information

Prerequisites:

  • Initialize the flash before use

Related Sequence API:

  • Call wm_drv_flash_init to initialize the flash. If the device is already initialized, obtain the device pointer via the wm_dt_get_device_by_name function

  • Call wm_drv_flash_get_device_info to get the relevant information of the flash device and store it in the provided structure

Results:

  • Returns device information, including total flash size, page size, sector size, manufacturer ID, and device ID

Notes

  • The provided flash address should not contain a base address

  • Obtain the corresponding device pointer before operating the flash

  • Understand the flash partition situation before operating it. For details on the partition table, see the partition table section

Application Example

wm_device_t *flash_dev = NULL;
uint32_t write_addr = 0x5000;
uint8_t *write_buf = NULL;
uint32_t write_len = 256;
uint32_t read_addr = 0x5000;
uint32_t read_len = 256;
uint8_t *read_buf = NULL;
uint32_t erase_addr = 0x5000;
uint32_t erase_len = 256;

write_buf = (uint8_t *)wm_os_malloc(write_len);
if (write_buf) {
    read_buf = (uint8_t *)wm_os_malloc(write_len);
    if (read_buf) {
        flash_dev = wm_drv_flash_init("internal_flash");
        wm_drv_flash_write(flash_dev, write_addr, write_buf, write_len);
        wm_drv_flash_read(flash_dev, read_addr, read_buf, read_len);
        wm_drv_flash_erase_region(flash_dev, erase_addr, erase_len);

        wm_os_free(read_buf);
    }
    wm_os_free(write_buf);
}

API Reference

Flash API