SPI Flash

[English]

SPI_Flash API Brief

Application

Brief

spi_flash_init()

Initialize the SPI flash function

spi_flash_deinit()

De-initialize the SPI flash function

spi_flash_drv_tx()

Only spi tx

spi_flash_drv_trx()

tx and rx flash data

spi_flash_write_enable()

Write enable/disable

spi_flash_erase_sector()

Erase flash a sector

spi_flash_erase()

Erase flash with user command

spi_flash_erase_chip()

Erase the whole flash

spi_flash_write()

Write data to flash

spi_flash_read()

Read data from flash

spi_flash_4K_read()

Read SPI flash data less than 4K bytess

spi_flash_calc_crc()

Calculate crc value from flash data

spi_flash_get_boot_timeout()

Get boot timeout from flash

spi_flash_wait_op_done()

Wait spi flash process done

SPI_Flash API Reference

Header File

Functions

int32_t spi_flash_init(uint32_t spi_dev_inst)

Initialize the SPI flash function.

Register DMA callback functions for SPI flash tx and rx.

参数:

spi_dev_inst[in] spi device instance, return value of drv_spi_init()

返回值:
  • 0 – success [BK_RET_OK]

  • other – fail

返回:

result

int32_t spi_flash_deinit(uint32_t spi_dev_inst)

De-initialize the SPI flash function.

Release DMA callback functions for SPI flash tx and rx.

参数:

spi_dev_inst[in] spi device instance, return value of drv_spi_init()

返回值:
  • 0 – success [BK_RET_OK]

  • other – fail

返回:

result

int32_t spi_flash_drv_tx(uint32_t spi_dev_inst, uint8_t *pu1_buf, uint32_t size)

Only spi tx.

Only tx flash data goes out.

参数:
  • spi_dev_inst[in] spi device instance, return value of drv_spi_init()

  • pu1_buf[in] pointer to the tx buffer

  • size[in] tx buffer size

返回值:
  • 0 – success [BK_RET_OK]

  • other – fail

返回:

result

int32_t spi_flash_drv_trx(uint32_t spi_dev_inst, uint8_t *pu1_tx_buf, uint32_t tx_size, uint8_t *pu1_rx_buf, uint32_t rx_size)

tx and rx flash data.

tx while rx flash data, pu1_rx_buf points to receive buffer, content does not need to be offset

参数:
  • spi_dev_inst[in] spi device instance, return value of drv_spi_init()

  • pu1_tx_buf[in] pointer to the tx buffer

  • tx_size[in] tx buffer size in bytes

  • pu1_rx_buf[in] pointer to the rx buffer

  • rx_size[in] rx buffer size in bytes

返回值:
  • 0 – success [BK_RET_OK]

  • other – fail

返回:

result

int32_t spi_flash_write_enable(uint32_t spi_dev_inst, bool enable)

Write enable/disable.

Enable or disable writing operations to the SPI Flash.

参数:
  • spi_dev_inst[in] spi device instance, return value of drv_spi_init()

  • enable[in] true: enable write false: disable write

返回值:
  • 0 – success [BK_RET_OK]

  • other – fail

返回:

result

int32_t spi_flash_erase_sector(uint32_t spi_dev_inst, uint32_t addr)

Erase flash a sector.

Erase a sector and addr is automatically aligned to the address of the next sector.

参数:
  • spi_dev_inst[in] spi device instance, return value of drv_spi_init()

  • addr[in] offset address in flash, aligns to sector when erased.

返回值:
  • 0 – success [BK_RET_OK]

  • other – fail

返回:

result

int32_t spi_flash_erase(uint32_t spi_dev_inst, uint32_t addr, uint8_t erase_cmd)

Erase flash with user command.

Erase the flash memory using user command.

参数:
  • spi_dev_inst[in] spi device instance, return value of drv_spi_init()

  • addr[in] offset address in flash

  • erase_cmd[in] erase command

返回值:
  • 0 – success [BK_RET_OK]

  • other – fail

返回:

result

int32_t spi_flash_erase_chip(uint32_t spi_dev_inst, uint32_t timeout_ms)

Erase the whole flash.

Erase the whole flash.

参数:
  • spi_dev_inst[in] spi device instance, return value of drv_spi_init()

  • timeout_ms[in] tiimeout in ms

返回值:
  • 0 – success [BK_RET_OK]

  • other – fail

返回:

result

int32_t spi_flash_write(uint32_t spi_dev_inst, uint32_t addr, uint8_t *pu1_buf, uint32_t write_size)

Write data to flash.

Write data to flash. Reserve 4 bytes of memory before the address pointed to by pu1_buf. The 4Byte will be temporarily occupied in this function, but will not be restored.

参数:
  • spi_dev_inst[in] spi device instance, return value of drv_spi_init()

  • addr[in] offset address in flash

  • pu1_buf[in] pointer to the write buffer

  • write_size[in] write size

返回值:
  • 0 – success [BK_RET_OK]

  • other – fail

返回:

result

int32_t spi_flash_read(uint32_t spi_dev_inst, uint32_t addr, uint8_t *pu1_buf, uint32_t read_size)

Read data from flash.

Read data from flash. Reserve 4 bytes of memory before the address pointed to by pu1_buf. The 4Byte will be temporarily occupied in this function, but will not be restored.

参数:
  • spi_dev_inst[in] spi device instance, return value of drv_spi_init()

  • addr[in] offset address in flash

  • pu1_buf[in] pointer to the read buffer

  • read_size[in] read size

返回值:
  • 0 – success [BK_RET_OK]

  • other – fail

返回:

result

int32_t spi_flash_4K_read(uint32_t spi_dev_inst, uint32_t addr, uint8_t *pu1_buf, uint32_t size)

Read SPI flash data less than 4K bytess.

Hardware limitation, the maximum amount of data read at one time is 4K-1 bytes. Reserve 4 bytes of memory before the address pointed to by pu1_buf. The 4Byte will be temporarily occupied in this function, but will not be restored.

参数:
  • spi_dev_inst[in] spi device instance, return value of drv_spi_init()

  • addr[in] offset address in flash

  • pu1_buf[in] pointer to the read buffer

  • size[in] read size

返回值:
  • 0 – success [BK_RET_OK]

  • other – fail

返回:

result

uint32_t spi_flash_calc_crc(uint32_t spi_dev_inst, uint32_t addr, uint8_t *pu1_buf, uint32_t size)

Calculate crc value from flash data.

Calculate crc of data in flash. pu1_buf requires a 4K buffer to temporarily store data.

参数:
  • spi_dev_inst[in] spi device instance, return value of drv_spi_init()

  • addr[in] offset address in flash

  • pu1_buf[in] pointer to a 4K buffer for reading data

  • size[in] calculate size, Can be much larger than 4K.

返回:

crc value

uint8_t spi_flash_get_boot_timeout(uint32_t spi_dev_inst)

Get boot timeout from flash.

Read the ms time value of boot timeout from flash. Returning 0 means that it was not obtained.

参数:

spi_dev_inst[in] spi device instance, return value of drv_spi_init()

返回值:

0 – flash empty or data is invalid

返回:

boot timeout in ms

void spi_flash_wait_op_done(uint32_t spi_dev_inst, uint32_t timeout_ms_req)

Wait spi flash process done.

Wait for the SPI flash operation to complete or for the time to exceed timeout_ms_req.

参数:
  • spi_dev_inst[in] spi device instance, return value of drv_spi_init()

  • timeout_ms_req[in] timeout wait in ms. 0 if don’t known

返回:

N/A SPI_FLASH_H EOF