DataFlashBlockDevice
DataFlashBlockDevice class hierarchy
DataFlashBlockDevice is a block device driver for serial flash devices that support the Atmel DataFlash protocol, such as the Adesto AT45DB series of devices.
Note: Multiple protocols exist for SPI flash devices. This driver is for the DataFlash protocol. If your device uses the SFDP flash protocol, you need the SPIFlashBlockDevice.
DataFlash is a memory protocol that combines flash with SRAM buffers for a programming interface. DataFlash supports byte-sized read and writes, with an erase size of around 528 bytes or sometimes 1056 bytes. DataFlash provides erase sizes with an extra 16 bytes for error correction codes (ECC), so a flash translation layer (FTL) may still present 512 byte erase sizes.
You can configure the DataFlashBlockDevice to force the underlying device to use either the binary size (in other words, 512 bytes) or the raw DataFlash size (in other words, 528 bytes).
To configure this class, please see our BlockDevice configuration documentation.
DataFlashBlockDevice class reference
Public Member Functions | |
DataFlashBlockDevice (PinName mosi=NC, PinName miso=NC, PinName sclk=NC, PinName csel=NC, int freq=40000000, PinName nwp=NC) | |
Creates a DataFlashBlockDevice on a SPI bus specified by pins. More... | |
virtual int | init () |
Initialize a block device. More... | |
virtual int | deinit () |
Deinitialize a block device. 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 a eraseable block. More... | |
virtual mbed::bd_size_t | get_erase_size (mbed::bd_addr_t addr) const |
Get the size of an erasable block given address. 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 int | get_erase_value () const |
Get the value of storage when erased. 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 BlockDevice * | get_default_instance () |
Return the default block device. More... |
DataFlashBlockDevice example
/*
* Copyright (c) 2006-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
// Here's an example using the AT45DB on the K64F
#include "mbed.h"
#include "DataFlashBlockDevice.h"
// Create DataFlash on SPI bus with PTE5 as chip select
DataFlashBlockDevice dataflash(PTE1, PTE3, PTE2, PTE4);
// Create DataFlash on SPI bus with PTE6 as write-protect
// DataFlashBlockDevice dataflash(PTE1, PTE3, PTE2, PTE4, PTE6);
int main()
{
printf("dataflash test\n");
// Initialize the SPI flash device and print the memory layout
dataflash.init();
printf("dataflash size: %llu\n", dataflash.size());
printf("dataflash read size: %llu\n", dataflash.get_read_size());
printf("dataflash program size: %llu\n", dataflash.get_program_size());
printf("dataflash erase size: %llu\n", dataflash.get_erase_size());
// Write "Hello World!" to the first block
char *buffer = (char *)malloc(dataflash.get_erase_size());
sprintf(buffer, "Hello World!\n");
dataflash.erase(0, dataflash.get_erase_size());
dataflash.program(buffer, 0, dataflash.get_erase_size());
// Read back what was stored
dataflash.read(buffer, 0, dataflash.get_erase_size());
printf("%s", buffer);
// Deinitialize the device
dataflash.deinit();
}