ChainingBlockDevice example to showcase programming and reading from a chained group of HeapBlockDevices.

Fork of ChainingBlockDevice_ex_1 by mbed_example

Committer:
kgilbert
Date:
Tue Oct 24 23:32:54 2017 +0000
Revision:
2:70419b9d778a
Parent:
1:8ad9777787ba
Add source for example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kgilbert 0:daa62d7aa9f9 1 #include "mbed.h"
kgilbert 0:daa62d7aa9f9 2 #include "HeapBlockDevice.h"
kgilbert 1:8ad9777787ba 3 #include "ChainingBlockDevice.h"
kgilbert 2:70419b9d778a 4
kgilbert 2:70419b9d778a 5 #define BLOCKSIZE 512
kgilbert 2:70419b9d778a 6 char buffer1[512];
kgilbert 2:70419b9d778a 7 char buffer2[512];
kgilbert 2:70419b9d778a 8
kgilbert 0:daa62d7aa9f9 9 int main(void) {
kgilbert 1:8ad9777787ba 10 // Create two smaller block devices with
kgilbert 1:8ad9777787ba 11 // 64 and 32 blocks of size 512 bytes
kgilbert 2:70419b9d778a 12 HeapBlockDevice mem1(64*BLOCKSIZE, BLOCKSIZE);
kgilbert 2:70419b9d778a 13 HeapBlockDevice mem2(32*BLOCKSIZE, BLOCKSIZE);
kgilbert 0:daa62d7aa9f9 14
kgilbert 1:8ad9777787ba 15 // Create a block device backed by mem1 and mem2
kgilbert 1:8ad9777787ba 16 // contains 96 blocks of size 512 bytes
kgilbert 1:8ad9777787ba 17 BlockDevice *bds[] = {&mem1, &mem2};
kgilbert 1:8ad9777787ba 18 ChainingBlockDevice chainmem(bds);
kgilbert 0:daa62d7aa9f9 19
kgilbert 2:70419b9d778a 20 // Initialize the block devices
kgilbert 2:70419b9d778a 21 chainmem.init();
kgilbert 2:70419b9d778a 22
kgilbert 2:70419b9d778a 23 // Erase the block device to prepare for programming. 64 and 32 refer to
kgilbert 2:70419b9d778a 24 // the respective number of blocks in mem1 and mem2
kgilbert 2:70419b9d778a 25 chainmem.erase(0, (BLOCKSIZE * (64 + 32)));
kgilbert 1:8ad9777787ba 26
kgilbert 2:70419b9d778a 27 // Program strings to the block device at byte-addressable locations that
kgilbert 2:70419b9d778a 28 // span both sub blocks. The second program will write past the end of the
kgilbert 2:70419b9d778a 29 // first block
kgilbert 2:70419b9d778a 30 chainmem.program("data for block", 0, BLOCKSIZE);
kgilbert 2:70419b9d778a 31 chainmem.program("Some more data", (65 * BLOCKSIZE), BLOCKSIZE);
kgilbert 2:70419b9d778a 32
kgilbert 2:70419b9d778a 33 // Readback the written values
kgilbert 2:70419b9d778a 34 chainmem.read(&buffer1, 0, BLOCKSIZE);
kgilbert 2:70419b9d778a 35 chainmem.read(&buffer2, (65 * BLOCKSIZE), BLOCKSIZE);
kgilbert 2:70419b9d778a 36 printf("Read back: %s, %s\r\n", buffer1, buffer2);
kgilbert 0:daa62d7aa9f9 37 }