Block devices are the basic building block of storage solutions in Mbed OS.
File systems are backed by blockdevice implementations. The BlockDevice API performs the low-level interactions with the hardware storage. To add your own block device implementation, we recommend you inherit from the BlockDevice class. For details on how to extend the BlockDevice interface, please refer to the and implementing BlockDevice section below.
- Erase leaves memory as undefined. It does not set memory to a predetermined value.
- Programming without erase is undefined behavior.
Erase, program and read block sizes may not be the same; however, they must be multiples of one another.
You can find the BlockDevice class on the master branch under the
features/filesystem/bd path in Mbed OS.
|Public Member Functions|
|virtual int||init ()=0|
|virtual int||deinit ()=0|
|virtual int||sync ()|
|virtual int||read (void *buffer, bd_addr_t addr, bd_size_t size)=0|
|virtual int||program (const void *buffer, bd_addr_t addr, bd_size_t size)=0|
|virtual int||erase (bd_addr_t addr, bd_size_t size)|
|virtual int||trim (bd_addr_t addr, bd_size_t size)|
|virtual bd_size_t||get_read_size () const =0|
|virtual bd_size_t||get_program_size () const =0|
|virtual bd_size_t||get_erase_size () const|
|virtual bd_size_t||get_erase_size (bd_addr_t addr) const|
|virtual int||get_erase_value () const|
|virtual bd_size_t||size () const =0|
|bool||is_valid_read (bd_addr_t addr, bd_size_t size) const|
|bool||is_valid_program (bd_addr_t addr, bd_size_t size) const|
|bool||is_valid_erase (bd_addr_t addr, bd_size_t size) const|
The primary functions to implement are:
int read(void *buffer, bd_addr_t addr, bd_size_t size);
int program(void *buffer, bd_addr_t addr, bd_size_t size);
int erase(bd_addr_t addr, bd_size_t size);
You can run BlockDevice tests for heap, MBR and util block devices with the following command:
mbed test -t <toolchain> -m <target> -n features-tests-filesystem-*_block_device
One way to add tests for new block devices is to copy an existing implementation, such as HeapBlocKDevice, and change the block device class to your own. You can find tests under the top level
TESTS folder in the Mbed OS repository.