ChainingBlockDevice example to showcase programming and reading from a chained group of HeapBlockDevices.
Fork of ChainingBlockDevice_ex_1 by
Revision 2:70419b9d778a, committed 2017-10-24
- Comitter:
- kgilbert
- Date:
- Tue Oct 24 23:32:54 2017 +0000
- Parent:
- 1:8ad9777787ba
- Commit message:
- Add source for example
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Oct 18 20:03:25 2017 +0000 +++ b/main.cpp Tue Oct 24 23:32:54 2017 +0000 @@ -1,23 +1,37 @@ #include "mbed.h" #include "HeapBlockDevice.h" #include "ChainingBlockDevice.h" -#include "FATFileSystem.h" - + +#define BLOCKSIZE 512 +char buffer1[512]; +char buffer2[512]; + int main(void) { // Create two smaller block devices with // 64 and 32 blocks of size 512 bytes - HeapBlockDevice mem1(64*512, 512); - HeapBlockDevice mem2(32*512, 512); + HeapBlockDevice mem1(64*BLOCKSIZE, BLOCKSIZE); + HeapBlockDevice mem2(32*BLOCKSIZE, BLOCKSIZE); // Create a block device backed by mem1 and mem2 // contains 96 blocks of size 512 bytes BlockDevice *bds[] = {&mem1, &mem2}; ChainingBlockDevice chainmem(bds); - // Format the new chained block device with a FAT filesystem - FATFileSystem::format(&chainmem); + // Initialize the block devices + chainmem.init(); + + // Erase the block device to prepare for programming. 64 and 32 refer to + // the respective number of blocks in mem1 and mem2 + chainmem.erase(0, (BLOCKSIZE * (64 + 32))); - // Create the FAT filesystem instance, files can now be written to - // the FAT filesystem as if to a single 96 x 512 byte storage device - FATFileSystem fat("fat", &chainmem); + // Program strings to the block device at byte-addressable locations that + // span both sub blocks. The second program will write past the end of the + // first block + chainmem.program("data for block", 0, BLOCKSIZE); + chainmem.program("Some more data", (65 * BLOCKSIZE), BLOCKSIZE); + + // Readback the written values + chainmem.read(&buffer1, 0, BLOCKSIZE); + chainmem.read(&buffer2, (65 * BLOCKSIZE), BLOCKSIZE); + printf("Read back: %s, %s\r\n", buffer1, buffer2); } \ No newline at end of file