SlicingBlockDevice
SlicingBlockDevice class hierarchy
The SlicingBlockDevice class provides a way to break up a block device into subunits without the need to manually track offsets. The SlicingBlockDevice acts as an opposite of the ChainingBlockDevice class.
The constructor takes in the master block device pointer and the start and end addresses of where you would like to partition the sub-block. By not specifying the end address, you create a block device that spans from the provided start address to the end of the underlying block device.
- bd - Block device to back the SlicingBlockDevice.
- start - Start block address to map to block 0. Negative addresses are calculated from the end of the underlying block device.
- end - End block address to mark the end of the block device. This block is not mapped; negative addresses are calculated from the end of the underlying block device.
To configure this class, please see our BlockDevice configuration documentation.
SlicingBlockDevice class reference
Public Member Functions | |
SlicingBlockDevice (BlockDevice *bd, bd_addr_t start, bd_addr_t end=0) | |
Lifetime of the memory block device. More... | |
virtual | ~SlicingBlockDevice () |
Lifetime of a block device. More... | |
virtual int | init () |
Initialize a block device. More... | |
virtual int | deinit () |
Deinitialize a 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 a block device. More... | |
virtual int | program (const void *buffer, bd_addr_t addr, bd_size_t size) |
Program blocks to a block device. More... | |
virtual int | erase (bd_addr_t addr, bd_size_t size) |
Erase blocks on a 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 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_read (bd_addr_t addr, bd_size_t size) const |
Convenience function for checking block read validity. More... | |
virtual bool | is_valid_erase (bd_addr_t addr, bd_size_t size) const |
Convenience function for checking block erase validity. More... | |
virtual int | trim (bd_addr_t addr, bd_size_t size) |
Mark blocks as no longer in use. More... |
Static Public Member Functions | |
static BlockDevice * | get_default_instance () |
Return the default block device. More... |
SlicingBlockDevice example
This SlicingBlockDevice example partitions a HeapBlockDevice into three subunits and showcases programming and reading back data segments through both the underlying master block device and the sliced subunits.
/*
* Copyright (c) 2017-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
#include "HeapBlockDevice.h"
#include "SlicingBlockDevice.h"
#define BLOCK 512
#define NUMSLICES 3
char buff[BLOCK];
int main(void)
{
// Create a block device with 64 blocks of size 512
HeapBlockDevice mem(64 * BLOCK, BLOCK);
SlicingBlockDevice slices[NUMSLICES] = {
// Create a block device that maps to the first 32 blocks
SlicingBlockDevice(&mem, 0 * BLOCK, 32 * BLOCK),
// Create a block device that maps to the middle 32 blocks
SlicingBlockDevice(&mem, 16 * BLOCK, -16 * BLOCK),
// Create a block device that maps to the last 32 blocks
SlicingBlockDevice(&mem, 32 * BLOCK)
};
for (int i = 0; i < NUMSLICES; i++) {
// Initialize and erase the slice to prepar for programming
slices[i].init();
slices[i].erase(0, 32 * BLOCK);
// Construct the message for the block and write to the slice
sprintf((char *)&buff, "test: %d", i);
slices[i].program(&buff, 0, BLOCK);
}
for (int i = 0; i < NUMSLICES; i++) {
// Read back the programmed blocks through the underlying block device
mem.read(&buff, (i * 16 * BLOCK), BLOCK);
printf("%s --> ", buff);
// Read back the programmed blocks through the sliced block device.
slices[i].read(&buff, 0, BLOCK);
printf("%s\r\n", buff);
}
}
Related content
- ChainingBlockDevice API reference.
- HeapBlockDevice API reference.
- BlockDevice configuration documentation.