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


BlockDevice class hierarchy

The HeapBlockDevice class provides a way to simulate block devices for software development or testing. The created blocks are nonvolatile; they do not persist across power cycles.

HeapBlockDevices have the following configurable parameters in either one of two constructors:

Verbose constructor

  • size - Size of the block device in bytes.
  • read_size - Minimum read size required in bytes.
  • program_size - Minimum program size required in bytes.
  • erase_size - Minimum erase size required in bytes.

Optionally, you can create a HeapBlockDevice that will set the read, program and erase sizes to the same size rather than requiring each parameter be repeated if all of the values are constrained by a common block size.

Shortened constructor

  • size - Size of the block device in bytes.
  • block - Block size in bytes. You can use this to configure the minimum read, program and erase sizes to this value. Default value is 512 bytes.

You can view more information about the configurable settings and functions in the class reference.

HeapBlockDevice class reference

Public Member Functions
 HeapBlockDevice (bd_size_t size, bd_size_t block=512)
 HeapBlockDevice (bd_size_t size, bd_size_t read, bd_size_t program, bd_size_t erase)
virtual int init ()
virtual int deinit ()
virtual int read (void *buffer, bd_addr_t addr, bd_size_t size)
virtual int program (const void *buffer, bd_addr_t addr, bd_size_t size)
virtual int erase (bd_addr_t addr, bd_size_t size)
virtual bd_size_t get_read_size () const
virtual bd_size_t get_program_size () const
virtual bd_size_t get_erase_size () const
virtual bd_size_t get_erase_size (bd_addr_t addr) const
virtual bd_size_t size () const
 Public Member Functions inherited from BlockDevice
virtual ~BlockDevice ()
virtual int sync ()
virtual int trim (bd_addr_t addr, bd_size_t size)
virtual int get_erase_value () const
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

HeapBlockDevice example

Create a HeapBlockDevice, program it, read the block back and clean up.

#include "mbed.h"
#include "HeapBlockDevice.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() {
    bd.erase(0, BLOCK_SIZE);
    bd.program(block, 0, BLOCK_SIZE);
    bd.read(block, 0, BLOCK_SIZE);
    printf("%s", block);

The HeapBlockDevice used with MBRBlockDevice showcases partitioning.

#include "mbed.h"
#include "HeapBlockDevice.h"
#include "MBRBlockDevice.h"
int main(void) {
    // Create a block device with 64 blocks of size 512
    HeapBlockDevice mem(64*512, 512);
    // Partition into two partitions with ~half the blocks
    MBRBlockDevice::partition(&mem, 1, 0x83, 0*512, 32*512);
    MBRBlockDevice::partition(&mem, 2, 0x83, 32*512);
    // Create a block device that maps to the first 32 blocks (excluding MBR block)
    MBRBlockDevice part1(&mem, 1);
    // Create a block device that maps to the last 32 blocks
    MBRBlockDevice part2(&mem, 2);
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.