FlashSimBlockDevice
FlashSimBlockDevice class hierarchy
The FlashSimBlockDevice class is a block device adapator, whose purpose is to simulate the behavior of a flash component if the underlying block device doesn't support such a behavior. This includes the following:
- Support the
erase
API (fills the erase unit with the predefined erase value). - Only allow programming of erased area or ones whose content is the same as the one given by the user.
- Support the
get\_erase\_value
API, returning the predefined erase value.
The constructor requires the following:
- bd - Block device to back the FlashSimBlockDevice.
- erase_value - Value given to each byte of the erase unit following an erase operation (defaults to 0xFF).
To configure this class, please see our BlockDevice configuration documentation.
FlashSimBlockDevice class reference
Public Member Functions | |
FlashSimBlockDevice (BlockDevice *bd, uint8_t erase_value=0xFF) | |
Constructor. More... | |
virtual int | init () |
Initialize a block device. More... | |
virtual int | deinit () |
Deinitialize the block device. More... | |
virtual int | sync () |
Ensure data on storage is in sync with the driver. More... | |
virtual int | read (void *buffer, bd_addr_t addr, bd_size_t size) |
Read blocks from the block device. More... | |
virtual int | program (const void *buffer, bd_addr_t addr, bd_size_t size) |
Program blocks to the block device. More... | |
virtual int | erase (bd_addr_t addr, bd_size_t size) |
Erase blocks on the block device. More... | |
virtual bd_size_t | get_read_size () const |
Get the size of a readable block. More... | |
virtual bd_size_t | get_program_size () const |
Get the size of a programmable block. More... | |
virtual bd_size_t | get_erase_size () const |
Get the size of an erasable block. More... | |
virtual bd_size_t | get_erase_size (bd_addr_t addr) const |
Get the size of an erasable block given address. More... | |
virtual int | get_erase_value () const |
Get the value of storage when erased. More... | |
virtual 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 | 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 BlockDevice * | get_default_instance () |
Return the default block device. More... |
FlashSimBlockDevice example
This FlashSimBlockDevice example takes a HeapBlockDevice and turns it into a simulated flash BD.
/*
* Copyright (c) 2006-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
#include "HeapBlockDevice.h"
#include "FlashSimBlockDevice.h"
#define BLOCK_SIZE 512
HeapBlockDevice bd(2048, BLOCK_SIZE); // 2048 bytes with a block size of 512 bytes.
uint8_t block[BLOCK_SIZE] = "Hello World!\n";
int main()
{
int erase_unit_size = 512;
HeapBlockDevice heap_bd(4 * erase_unit_size, 1, 4, erase_unit_size);
FlashSimBlockDevice flash_bd(&heap_bd);
// This initializes the flash simulator block device (as well as the underlying heap block device).
int err = flash_bd.init();
uint8_t buf[16];
for (int i = 0; i < sizeof(buf); i++) {
buf[i] = i;
}
// This will fail because the erase unit in address 0 has not been erased.
err = flash_bd.program(buf, 0, sizeof(buf));
// Erase the erase unit in address 0.
err = flash_bd.erase(0, erase_unit_size);
// This will succeed now that the erase unit is erased.
err = flash_bd.program(buf, 0, sizeof(buf));
}
Related content
- HeapBlockDevice API reference.
- BlockDevice configuration documentation.