Contiki-NG
Files | Macros | Functions
Generic external SPI flash driver

This is a generic driver for external SPI flash memories. More...

Files

file  ext-flash.c
 Implementation of a generic external SPI flash driver.
 
file  ext-flash.h
 Header file for the external SPI flash API.
 

Macros

#define BLS_CODE_PROGRAM   0x02
 Page Program.
 
#define BLS_CODE_READ   0x03
 Read Data.
 
#define BLS_CODE_READ_STATUS   0x05
 Read Status Register.
 
#define BLS_CODE_WRITE_ENABLE   0x06
 Write Enable.
 
#define BLS_CODE_SECTOR_ERASE   0x20
 Sector Erase.
 
#define BLS_CODE_MDID   0x90
 Manufacturer Device ID.
 
#define BLS_CODE_PD   0xB9
 Power down.
 
#define BLS_CODE_RPD   0xAB
 Release Power-Down.
 
#define BLS_CODE_ERASE_4K   0x20
 Sector Erase.
 
#define BLS_CODE_ERASE_ALL   0xC7
 Mass Erase.
 
#define BLS_STATUS_BIT_BUSY   0x01
 Busy bit of the status register.
 

Functions

static spi_device_tget_spi_conf (spi_device_t *conf)
 Get spi configuration, return default configuration if NULL.
 
static bool select_on_bus (spi_device_t *flash_spi_configuration)
 Clear external flash CSN line.
 
static void deselect (spi_device_t *flash_spi_configuration)
 Set external flash CSN line.
 
static bool wait_ready (spi_device_t *flash_spi_configuration)
 Wait till previous erase/program operation completes. More...
 
static uint8_t verify_part (spi_device_t *flash_spi_configuration)
 Verify the flash part. More...
 
static bool power_down (spi_device_t *flash_spi_configuration)
 Put the device in power save mode. More...
 
static bool power_standby (spi_device_t *flash_spi_configuration)
 Take device out of power save mode and prepare it for normal operation. More...
 
static bool write_enable (spi_device_t *flash_spi_configuration)
 Enable write. More...
 
bool ext_flash_open (spi_device_t *conf)
 Initialize storage driver. More...
 
bool ext_flash_close (spi_device_t *conf)
 Close the storage driver. More...
 
bool ext_flash_read (spi_device_t *conf, uint32_t offset, uint32_t length, uint8_t *buf)
 Read storage content. More...
 
bool ext_flash_write (spi_device_t *conf, uint32_t offset, uint32_t length, const uint8_t *buf)
 Write to storage sectors. More...
 
bool ext_flash_erase (spi_device_t *conf, uint32_t offset, uint32_t length)
 Erase storage sectors corresponding to the range. More...
 
bool ext_flash_init (spi_device_t *conf)
 Initialise the external flash. More...
 

Detailed Description

This is a generic driver for external SPI flash memories.

The driver has been tested and works with multiple external SPI flash parts. The list of parts the driver has been tested against is shown in the README in this directory.

If you successfully use this driver with a part that is not listed in the README, please let us know so we can update it.

Function Documentation

bool ext_flash_close ( spi_device_t conf)

Close the storage driver.

Parameters
confSPI bus configuration struct. NULL for default.
Returns
True when successful.

This call will put the device in its lower power mode (power down).

Definition at line 349 of file ext-flash.c.

References get_spi_conf(), power_down(), and spi_release().

Referenced by ext_flash_init().

bool ext_flash_erase ( spi_device_t conf,
uint32_t  offset,
uint32_t  length 
)

Erase storage sectors corresponding to the range.

Parameters
confSPI bus configuration struct. NULL for default.
offsetAddress to start erasing
lengthNumber of bytes to erase
Returns
True when successful.

The erase operation will be sector-wise, therefore a call to this function will generally start the erase procedure at an address lower than offset

Definition at line 469 of file ext-flash.c.

References BLS_CODE_SECTOR_ERASE, deselect(), get_spi_conf(), select_on_bus(), spi_write(), wait_ready(), and write_enable().

bool ext_flash_init ( spi_device_t conf)

Initialise the external flash.

Parameters
confSPI bus configuration struct. NULL for default.

This function will explicitly put the part in its lowest power mode (power-down).

In order to perform any operation, the caller must first wake the device up by calling ext_flash_open()

Definition at line 521 of file ext-flash.c.

References ext_flash_close(), and ext_flash_open().

Referenced by board_init().

bool ext_flash_open ( spi_device_t conf)

Initialize storage driver.

Parameters
confSPI bus configuration struct. NULL for default.
Returns
True when successful.

Definition at line 319 of file ext-flash.c.

References deselect(), get_spi_conf(), GPIO_HAL_PIN_UNKNOWN, power_standby(), spi_acquire(), spi_release(), and verify_part().

Referenced by ext_flash_init().

bool ext_flash_read ( spi_device_t conf,
uint32_t  offset,
uint32_t  length,
uint8_t *  buf 
)

Read storage content.

Parameters
confSPI bus configuration struct. NULL for default.
offsetAddress to read from
lengthNumber of bytes to read
bufBuffer where to store the read bytes
Returns
True when successful.

buf must be allocated by the caller

Definition at line 368 of file ext-flash.c.

References BLS_CODE_READ, deselect(), get_spi_conf(), select_on_bus(), spi_read(), spi_write(), and wait_ready().

bool ext_flash_write ( spi_device_t conf,
uint32_t  offset,
uint32_t  length,
const uint8_t *  buf 
)

Write to storage sectors.

Parameters
confSPI bus configuration struct. NULL for default.
offsetAddress to write to
lengthNumber of bytes to write
bufBuffer holding the bytes to be written
Returns
True when successful.

Definition at line 409 of file ext-flash.c.

References BLS_CODE_PROGRAM, deselect(), get_spi_conf(), select_on_bus(), spi_write(), wait_ready(), and write_enable().

static bool power_down ( spi_device_t flash_spi_configuration)
static

Put the device in power save mode.

No access to data; only the status register is accessible.

Definition at line 233 of file ext-flash.c.

References BLS_CODE_PD, deselect(), select_on_bus(), spi_write_byte(), verify_part(), and wait_ready().

Referenced by ext_flash_close().

static bool power_standby ( spi_device_t flash_spi_configuration)
static

Take device out of power save mode and prepare it for normal operation.

Returns
True if the command was written successfully

Definition at line 274 of file ext-flash.c.

References BLS_CODE_RPD, deselect(), select_on_bus(), spi_write(), and wait_ready().

Referenced by ext_flash_open().

static uint8_t verify_part ( spi_device_t flash_spi_configuration)
static

Verify the flash part.

Return values
VERIFY_PART_OKThe part was identified successfully
VERIFY_PART_ERRORThere was an error communicating with the part
VERIFY_PART_POWERED_DOWNCommunication was successful, but the part was powered down

Definition at line 199 of file ext-flash.c.

References BLS_CODE_MDID, deselect(), select_on_bus(), spi_read(), and spi_write().

Referenced by ext_flash_open(), and power_down().

static bool wait_ready ( spi_device_t flash_spi_configuration)
static

Wait till previous erase/program operation completes.

Returns
True when successful.

Definition at line 151 of file ext-flash.c.

References BLS_CODE_READ_STATUS, BLS_STATUS_BIT_BUSY, deselect(), select_on_bus(), spi_read(), and spi_write().

Referenced by ext_flash_erase(), ext_flash_read(), ext_flash_write(), power_down(), and power_standby().

static bool write_enable ( spi_device_t flash_spi_configuration)
static

Enable write.

Returns
True when successful.

Definition at line 300 of file ext-flash.c.

References BLS_CODE_WRITE_ENABLE, deselect(), select_on_bus(), and spi_write().

Referenced by ext_flash_erase(), and ext_flash_write().