SlicingBlockDevice example that showcases how to slice a larger block device into sub units.
Fork of ChainingBlockDevice_ex_2 by
main.cpp@3:62c01cd06ff7, 2017-10-25 (annotated)
- Committer:
- kgilbert
- Date:
- Wed Oct 25 20:18:23 2017 +0000
- Revision:
- 3:62c01cd06ff7
- Parent:
- 2:70419b9d778a
Add source for example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kgilbert | 0:daa62d7aa9f9 | 1 | #include "mbed.h" |
kgilbert | 0:daa62d7aa9f9 | 2 | #include "HeapBlockDevice.h" |
kgilbert | 3:62c01cd06ff7 | 3 | #include "SlicingBlockDevice.h" |
kgilbert | 2:70419b9d778a | 4 | |
kgilbert | 3:62c01cd06ff7 | 5 | #define BLOCK 512 |
kgilbert | 3:62c01cd06ff7 | 6 | #define NUMSLICES 3 |
kgilbert | 3:62c01cd06ff7 | 7 | |
kgilbert | 3:62c01cd06ff7 | 8 | char buff[BLOCK]; |
kgilbert | 2:70419b9d778a | 9 | |
kgilbert | 0:daa62d7aa9f9 | 10 | int main(void) { |
kgilbert | 3:62c01cd06ff7 | 11 | // Create a block device with 64 blocks of size 512 |
kgilbert | 3:62c01cd06ff7 | 12 | HeapBlockDevice mem(64*BLOCK, BLOCK); |
kgilbert | 3:62c01cd06ff7 | 13 | |
kgilbert | 3:62c01cd06ff7 | 14 | SlicingBlockDevice slices[NUMSLICES] = { |
kgilbert | 3:62c01cd06ff7 | 15 | // Create a block device that maps to the first 32 blocks |
kgilbert | 3:62c01cd06ff7 | 16 | SlicingBlockDevice(&mem, 0 *BLOCK, 32*BLOCK), |
kgilbert | 2:70419b9d778a | 17 | |
kgilbert | 3:62c01cd06ff7 | 18 | // Create a block device that maps to the middle 32 blocks |
kgilbert | 3:62c01cd06ff7 | 19 | SlicingBlockDevice(&mem, 16*BLOCK, -16*BLOCK), |
kgilbert | 3:62c01cd06ff7 | 20 | |
kgilbert | 3:62c01cd06ff7 | 21 | // Create a block device that maps to the last 32 blocks |
kgilbert | 3:62c01cd06ff7 | 22 | SlicingBlockDevice(&mem, 32*BLOCK) |
kgilbert | 3:62c01cd06ff7 | 23 | }; |
kgilbert | 3:62c01cd06ff7 | 24 | |
kgilbert | 3:62c01cd06ff7 | 25 | for (int i = 0; i < NUMSLICES; i++) { |
kgilbert | 3:62c01cd06ff7 | 26 | // Initialize and erase the slice to prepar for programming |
kgilbert | 3:62c01cd06ff7 | 27 | slices[i].init(); |
kgilbert | 3:62c01cd06ff7 | 28 | slices[i].erase(0, 32*BLOCK); |
kgilbert | 3:62c01cd06ff7 | 29 | |
kgilbert | 3:62c01cd06ff7 | 30 | // Construct the message for the block and write to the slice |
kgilbert | 3:62c01cd06ff7 | 31 | sprintf((char *)&buff, "test: %d", i); |
kgilbert | 3:62c01cd06ff7 | 32 | slices[i].program(&buff, 0, BLOCK); |
kgilbert | 3:62c01cd06ff7 | 33 | } |
kgilbert | 3:62c01cd06ff7 | 34 | |
kgilbert | 3:62c01cd06ff7 | 35 | for (int i = 0; i < NUMSLICES; i++) { |
kgilbert | 3:62c01cd06ff7 | 36 | // Read back the programmed blocks through the underlying block device |
kgilbert | 3:62c01cd06ff7 | 37 | mem.read(&buff, (i * 16 * BLOCK), BLOCK); |
kgilbert | 3:62c01cd06ff7 | 38 | printf("%s --> ", buff); |
kgilbert | 3:62c01cd06ff7 | 39 | |
kgilbert | 3:62c01cd06ff7 | 40 | // Read back the programmed blocks through the sliced block device. |
kgilbert | 3:62c01cd06ff7 | 41 | slices[i].read(&buff,0, BLOCK); |
kgilbert | 3:62c01cd06ff7 | 42 | printf("%s\r\n", buff); |
kgilbert | 3:62c01cd06ff7 | 43 | } |
kgilbert | 0:daa62d7aa9f9 | 44 | } |