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

HeapBlockDevice

BlockDevice class hierarchy

The HeapBlockDevice class provides a way to simulate block devices for software development or testing. The created blocks are volatile; 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. To configure this class, please see our BlockDevice configuration documentation.

HeapBlockDevice class reference

Public Member Functions
 HeapBlockDevice (bd_size_t size, bd_size_t block=512)
 Lifetime of the memory block device. More...
 HeapBlockDevice (bd_size_t size, bd_size_t read, bd_size_t program, bd_size_t erase)
 Lifetime of the memory block device. More...
virtual int init ()
 Initialize a block device. More...
virtual int deinit ()
 Deinitialize a block device. 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 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 BlockDeviceget_default_instance ()
 Return the default block device. More...

HeapBlockDevice example

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

/*
 * Copyright (c) 2017-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */

#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.init();
    bd.erase(0, BLOCK_SIZE);
    bd.program(block, 0, BLOCK_SIZE);
    bd.read(block, 0, BLOCK_SIZE);
    printf("%s", block);
    bd.deinit();
}

The HeapBlockDevice used with MBRBlockDevice showcases partitioning.

/*
 * Copyright (c) 2017-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"
#include "HeapBlockDevice.h"
#include "MBRBlockDevice.h"

#define BLOCK_SIZE 512

int main(void)
{
    // Create a block device with 64 blocks of size 512
    printf("Create a block device with 64 blocks of size 512\n");
    HeapBlockDevice mem(64 * BLOCK_SIZE, BLOCK_SIZE);

    // Partition into two partitions with ~half the blocks
    printf("Partition into two partitions with ~half the blocks\n");
    MBRBlockDevice::partition(&mem, 1, 0x83, 0 * BLOCK_SIZE, 32 * BLOCK_SIZE);
    MBRBlockDevice::partition(&mem, 2, 0x83, 32 * BLOCK_SIZE);

    // Create a block device that maps to the first 32 blocks (excluding MBR block)
    printf("Create a block device that maps to the first 32 blocks (excluding MBR block)\n");
    MBRBlockDevice part1(&mem, 1);

    // Create a block device that maps to the last 32 blocks
    printf("Create a block device that maps to the last 32 blocks\n");
    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.