Extern Flash

概述

外接 Flash ,控制器对 Flash 进行读写通信操作。(现目前只支持 spi 接口)

调用 extern_flash_init() 时, 如果外接的 Flash 不支持 JEDEC JESD216 标准和基本参数表,程序会在 extern_flash_nsup_sfdp.h 文件的 EXTERN_FLASH_CHIP_TABLE 表中查找 flash 芯片信息。

如果 EXTERN_FLASH_CHIP_TABLE 表中没有此 FLASH 信息,用户可以自己根据 externs_flash_info 结构把 flash 相关信息添加到表格中。

默认通用指令如下:

序号

指令

说明

1

0x03

Flash normal 读数据

2

0x02

Flash page 写数据

3

0x06

Flash 写数据使能

4

0x04

Flash 写数据使无效

5

0x05

Flash 读状态寄存器

6

0xC7

Flash 片擦除

7

0x9F

Flash 读 JEDEC ID

注:现 SPI 控制器对 Flash 进行读写数据操作时,默认使用 DMA 传输通道。

资源依赖

  • SPI

  • DMA

  • GPIO

  • IRQ

用法流程

参考示例

examples/extern_flash_spi_demo

API 介绍

Defines

FLASH_ERASE_TYPE_MAX_NUM

Typedefs

typedef void *extern_flash_h

Enums

enum EXTERN_FLASH_PORT

Values:

enumerator EXTERN_FLASH_PORT_SPI_0
enumerator EXTERN_FLASH_PORT_SPI_1
enumerator EXTERN_FLASH_PORT_SPI_MAX
enumerator EXTERN_FLASH_PORT_MAX

Functions

extern_flash_h extern_flash_init(EXTERN_FLASH_PORT port)

This function is to initialize extern flash information.

备注

must call this function before you can call other operation functions.

参数:

port -- is the extern flash io port.

返回:

extern_flash_h NULL for error else extern flash information handle.

WQ_RET extern_flash_deinit(extern_flash_h extern_flash)

This function is to deinitialize extern flash information.

参数:

extern_flash -- is extern flash information handle.

返回:

  • WQ_RET_OK Success.

  • WQ_RET_INVAL Bad param.

WQ_RET extern_flash_read_data(const extern_flash_h extern_flash, uint32_t flash_addr, uint8_t *buffer, uint32_t buf_len)

This function is to read data from extern flash.

参数:
  • extern_flash -- is extern flash information handle.

  • flash_addr -- is extern flash address.(maximum 24 bits)

  • buffer -- is extern flash read data buffer.

  • buf_len -- is extern flash read data buffer length.

返回:

  • WQ_RET_OK Success.

  • WQ_RET_INVAL Bad param.

  • WQ_RET_TIMEOVER Transfer timeout or flash busy.

  • WQ_RET_FAIL Read data fail.

WQ_RET extern_flash_write_data(const extern_flash_h extern_flash, uint32_t flash_addr, const uint8_t *buffer, uint32_t buf_len)

This function is to write data to extern flash.

参数:
  • extern_flash -- is extern flash information handle.

  • flash_addr -- is extern flash address.(maximum 24 bits)

  • buffer -- is extern flash write data buffer.

  • buf_len -- is extern flash write data buffer length.

返回:

  • WQ_RET_OK Success.

  • WQ_RET_INVAL Bad param.

  • WQ_RET_NOMEM No memory to save command and write data.

  • WQ_RET_TIMEOVER Transfer timeout or flash busy.

  • WQ_RET_FAIL Write data fail or write enable fail.

WQ_RET extern_flash_erase(const extern_flash_h extern_flash, uint32_t flash_addr, uint32_t erase_len)

This function is to erase extern flash.

备注

The flash_addr is aligned with at least the minimum erasable granularity. The erase_len is at least a multiple of the smallest size that can be erased.

参数:
  • extern_flash -- is extern flash information handle.

  • flash_addr -- is extern flash address.(maximum 24 bits)

  • erase_len -- is extern flash erase length.

返回:

  • WQ_RET_OK Success.

  • WQ_RET_INVAL Bad param.

  • WQ_RET_TIMEOVER Erase timeout or flash busy.

  • WQ_RET_FAIL Erase flash fail or write enable fail.

WQ_RET extern_flash_chip_erase(const extern_flash_h extern_flash)

This function is to erase extern flash chip.

参数:

extern_flash -- is extern flash information handle.

返回:

  • WQ_RET_OK Success.

  • WQ_RET_INVAL Bad param.

  • WQ_RET_TIMEOVER Erase timeout or flash busy.

  • WQ_RET_FAIL Erase flash fail or write enable fail.

WQ_RET extern_flash_send_cmd(const extern_flash_h extern_flash, uint8_t cmd, const uint8_t *wbuf, uint32_t wbuf_len, uint8_t *rbuf, uint32_t rbuf_len)

This function is to send special command to extern flash chip.

参数:
  • extern_flash -- is extern flash information handle.

  • cmd -- is command.

  • wbuf -- is a pointer that needs to be written to flash in addition to command.

  • wbuf_len -- is the length of the array that needs to be written to flash in addition to command.

  • rbuf -- is is a pointer that needs to be read from flash.

  • rbuf_len -- is the length of the array that needs to be read from flash.

返回:

  • WQ_RET_OK Success.

  • WQ_RET_INVAL Bad param.

  • WQ_RET_NOMEM No memory to save command and wbuf data.

  • WQ_RET_TIMEOVER Transfer timeout or flash busy.

  • WQ_RET_FAIL send command fail or write enable fail.

struct erase_type_t

Public Members

uint32_t size

erase size (bytes). 0x00: not available

uint8_t cmd

erase command

struct extern_flash_info

Public Members

char *name

flash name

uint8_t mf_id

manufacturer ID

uint8_t type_id

memory type ID

uint8_t capacity_id

capacity ID

uint32_t capacity

flash capacity (bytes)

uint32_t page_size

flash page size (bytes)

erase_type_t eraser[FLASH_ERASE_TYPE_MAX_NUM]

erase type

struct extern_flash_t

Public Members

EXTERN_FLASH_PORT port
extern_flash_info info