|
1 |
| -/* mbed Microcontroller Library |
2 |
| - * Copyright (c) 2019 ARM Limited |
3 |
| - * |
4 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
| - * you may not use this file except in compliance with the License. |
6 |
| - * You may obtain a copy of the License at |
7 |
| - * |
8 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
9 |
| - * |
10 |
| - * Unless required by applicable law or agreed to in writing, software |
11 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
12 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
| - * See the License for the specific language governing permissions and |
14 |
| - * limitations under the License. |
15 |
| - */ |
16 |
| - |
17 | 1 | #include "BufferedBlockDevice.h"
|
18 | 2 | #include "HeapBlockDevice.h"
|
19 | 3 | #include <cstdio>
|
20 | 4 |
|
21 | 5 | int main()
|
22 | 6 | {
|
23 |
| - // Define a HeapBlockDevice with program size 512 bytes and read size 256 |
24 |
| - HeapBlockDevice heap_bd(1024, 256, 512, 512); |
25 |
| - // BufferedBlockDevice is used to program or read a much smaller amount of data than the minimum program or read size supported by the underlying block device |
26 |
| - BufferedBlockDevice buf_bd(&heap_bd); |
27 |
| - |
28 |
| - // This initializes the buffered block device (as well as the underlying heap block device) |
29 |
| - int err = buf_bd.init(); |
30 |
| - |
31 |
| - uint8_t buf[8]; |
32 |
| - for (uint8_t i = 0; i < sizeof(buf); i++) { |
33 |
| - buf[i] = i; |
34 |
| - } |
35 |
| - |
36 |
| - // Now we can program an 8-byte buffer (we couldn't do that in the underlying block device, which had a 512-byte program size) |
37 |
| - err = buf_bd.program(buf, 0, sizeof(buf)); |
38 |
| - |
39 |
| - |
40 |
| - // Now read any amount of data from any address (for example, the last three bytes of data from address 5) |
41 |
| - memset(buf,0,8); |
42 |
| - err = buf_bd.read(buf, 5, 3); |
43 |
| - |
44 |
| - printf("Read Data (Expected 5,6,7): %d %d %d\n", (int)(buf[0]), (int)(buf[1]), (int)(buf[2])); |
45 |
| - |
46 |
| - // Ensure programmed data is flushed to the underlying block device |
47 |
| - err = buf_bd.sync(); |
48 |
| - |
49 |
| - // Finally, deinint the BufferedBlockDevice (which also deinits its underlying block device) |
50 |
| - err = buf_bd.deinit(); |
| 7 | + // Define a HeapBlockDevice with program size 512 bytes and read size 256 |
| 8 | + HeapBlockDevice heap_bd(1024, 256, 512, 512); |
| 9 | + // BufferedBlockDevice is used to program or read a much smaller amount of data than the minimum program or read size supported by the underlying block device |
| 10 | + BufferedBlockDevice buf_bd(&heap_bd); |
| 11 | + |
| 12 | + // This initializes the buffered block device (as well as the underlying heap block device) |
| 13 | + int err = buf_bd.init(); |
| 14 | + |
| 15 | + uint8_t buf[8]; |
| 16 | + for (uint8_t i = 0; i < sizeof(buf); i++) { |
| 17 | + buf[i] = i; |
| 18 | + } |
| 19 | + |
| 20 | + // Now we can program an 8-byte buffer (we couldn't do that in the underlying block device, which had a 512-byte program size) |
| 21 | + err = buf_bd.program(buf, 0, sizeof(buf)); |
| 22 | + |
| 23 | + |
| 24 | + // Now read any amount of data from any address (for example, the last three bytes of data from address 5) |
| 25 | + memset(buf, 0, 8); |
| 26 | + err = buf_bd.read(buf, 5, 3); |
| 27 | + |
| 28 | + printf("Read Data (Expected 5,6,7): %d %d %d\n", (int)(buf[0]), (int)(buf[1]), (int)(buf[2])); |
| 29 | + |
| 30 | + // Ensure programmed data is flushed to the underlying block device |
| 31 | + err = buf_bd.sync(); |
| 32 | + |
| 33 | + // Finally, deinint the BufferedBlockDevice (which also deinits its underlying block device) |
| 34 | + err = buf_bd.deinit(); |
51 | 35 | }
|
0 commit comments