mbed_example / Mbed OS SlicingBlockDevice_ex_1

Fork of ChainingBlockDevice_ex_2 by mbed_example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "HeapBlockDevice.h"
00003 #include "SlicingBlockDevice.h"
00004 
00005 #define BLOCK 512
00006 #define NUMSLICES 3
00007 
00008 char buff[BLOCK];
00009 
00010 int main(void) {
00011     // Create a block device with 64 blocks of size 512
00012     HeapBlockDevice mem(64*BLOCK, BLOCK);
00013 
00014     SlicingBlockDevice slices[NUMSLICES] = {
00015         // Create a block device that maps to the first 32 blocks
00016         SlicingBlockDevice(&mem, 0 *BLOCK, 32*BLOCK),
00017     
00018         // Create a block device that maps to the middle 32 blocks
00019         SlicingBlockDevice(&mem, 16*BLOCK, -16*BLOCK),
00020 
00021         // Create a block device that maps to the last 32 blocks
00022         SlicingBlockDevice(&mem, 32*BLOCK)
00023     };
00024 
00025     for (int i = 0; i < NUMSLICES; i++) {
00026         // Initialize and erase the slice to prepar for programming
00027         slices[i].init();
00028         slices[i].erase(0, 32*BLOCK);
00029         
00030         // Construct the message for the block and write to the slice
00031         sprintf((char *)&buff, "test: %d", i);
00032         slices[i].program(&buff, 0, BLOCK);
00033     }
00034 
00035     for (int i = 0; i < NUMSLICES; i++) {
00036         // Read back the programmed blocks through the underlying block device
00037         mem.read(&buff, (i * 16 * BLOCK), BLOCK);
00038         printf("%s  --> ", buff);
00039         
00040         // Read back the programmed blocks through the sliced block device.
00041         slices[i].read(&buff,0, BLOCK);
00042         printf("%s\r\n", buff);
00043     }
00044 }