Mistake on this page?
Report an issue in GitHub or email us

SPI Flash block device

SPIFBlockDevice class hierarchy

This API is a block device for NOR-based SPI flash devices that support SFDP.

Note: Multiple protocols exist for SPI flash devices. This driver is for the SFDP SPI flash protocol. If your device uses the DataFlash protocol, you need the DataflashBlockDevice

NOR-based SPI flash supports byte-sized read and writes, with an erase size of around 4 kbytes. An erase sets a block to all 1s, with successive writes clearing set bits.

To configure this class, please see our BlockDevice configuration documentation.

SPIFBlockDevice class reference

Public Member Functions
 SPIFBlockDevice (PinName mosi, PinName miso, PinName sclk, PinName csel, int freq=40000000)
 Creates a SPIFBlockDevice on a SPI bus specified by pins. More...
virtual int init ()
 Initialize a block device. More...
virtual int deinit ()
 Deinitialize a block device. More...
 ~SPIFBlockDevice ()
 Desctruct SPIFBlockDevice. More...
virtual int read (void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size)
 Read blocks from a block device. More...
virtual int program (const void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size)
 Program blocks to a block device. More...
virtual int erase (mbed::bd_addr_t addr, mbed::bd_size_t size)
 Erase blocks on a block device. More...
virtual mbed::bd_size_t get_read_size () const
 Get the size of a readable block. More...
virtual mbed::bd_size_t get_program_size () const
 Get the size of a programable block. More...
virtual mbed::bd_size_t get_erase_size () const
 Get the size of an erasable block. More...
virtual mbed::bd_size_t get_erase_size (mbed::bd_addr_t addr) const
 Get the size of minimal erasable sector size of given address. More...
virtual int get_erase_value () const
 Get the value of storage byte after it was erased. More...
virtual mbed::bd_size_t size () const
 Get the total size of the underlying device. More...
virtual const char * get_type () const
 Get the BlockDevice class type. More...
virtual int sync ()
 Ensure data on storage is in sync with the driver. More...
virtual int trim (bd_addr_t addr, bd_size_t size)
 Mark blocks as no longer in use. More...
virtual bool is_valid_read (bd_addr_t addr, bd_size_t size) const
 Convenience function for checking block read validity. More...
virtual bool is_valid_program (bd_addr_t addr, bd_size_t size) const
 Convenience function for checking block program validity. More...
virtual bool is_valid_erase (bd_addr_t addr, bd_size_t size) const
 Convenience function for checking block erase validity. More...
Static Public Member Functions
static BlockDeviceget_default_instance ()
 Return the default block device. More...

SPIFBlockDevice example

#include "mbed.h"
#include "SPIFBlockDevice.h"

// Create flash device on SPI bus with PTE5 as chip select
SPIFBlockDevice spif(PTE2, PTE4, PTE1, PTE5);

int main()
{
    printf("spif test\n");

    // Initialize the SPI flash device, and print the memory layout
    spif.init();
    printf("spif size: %llu\n",         spif.size());
    printf("spif read size: %llu\n",    spif.get_read_size());
    printf("spif program size: %llu\n", spif.get_program_size());
    printf("spif erase size: %llu\n",   spif.get_erase_size());

    // Write "Hello World!" to the first block
    char *buffer = (char *)malloc(spif.get_erase_size());
    sprintf(buffer, "Hello World!\n");
    spif.erase(0, spif.get_erase_size());
    spif.program(buffer, 0, spif.get_erase_size());

    // Read back what was stored
    spif.read(buffer, 0, spif.get_erase_size());
    printf("%s", buffer);

    // Deinitialize the device
    spif.deinit();
}

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.