@@ -59,38 +59,32 @@ For details regarding how to configure the default block device please refer to
59
59
* limitations under the License.
60
60
*/
61
61
#include "mbed.h"
62
+ #include "BlockDevice.h"
62
63
#include <stdio.h>
63
64
#include <algorithm>
64
65
65
- // Block devices
66
- #include "SPIFBlockDevice.h"
67
- #include "DataFlashBlockDevice.h"
68
- #include "SDBlockDevice.h"
69
- #include "HeapBlockDevice.h"
70
-
71
-
72
- // Physical block device, can be any device that supports the BlockDevice API
73
- SPIFBlockDevice bd(
74
- MBED_CONF_SPIF_DRIVER_SPI_MOSI,
75
- MBED_CONF_SPIF_DRIVER_SPI_MISO,
76
- MBED_CONF_SPIF_DRIVER_SPI_CLK,
77
- MBED_CONF_SPIF_DRIVER_SPI_CS);
66
+ // This will take the system's default block device
67
+ BlockDevice *bd = BlockDevice::get_default_instance();
78
68
69
+ // Instead of the default block device, you can define your own block device.
70
+ // For example: HeapBlockDevice with size of 2048 bytes, read size 1, write size 1 and erase size 512.
71
+ // #include "HeapBlockDevice.h"
72
+ // BlockDevice *bd = new HeapBlockDevice(2048, 1, 1, 512);
79
73
80
74
// Entry point for the example
81
75
int main() {
82
76
printf("--- Mbed OS block device example ---\n");
83
77
84
78
// Initialize the block device
85
- printf("bd. init()\n");
86
- int err = bd. init();
87
- printf("bd. init -> %d\n", err);
79
+ printf("bd-> init()\n");
80
+ int err = bd-> init();
81
+ printf("bd-> init -> %d\n", err);
88
82
89
83
// Get device geometry
90
- bd_size_t read_size = bd. get_read_size();
91
- bd_size_t program_size = bd. get_program_size();
92
- bd_size_t erase_size = bd. get_erase_size();
93
- bd_size_t size = bd. size();
84
+ bd_size_t read_size = bd-> get_read_size();
85
+ bd_size_t program_size = bd-> get_program_size();
86
+ bd_size_t erase_size = bd-> get_erase_size();
87
+ bd_size_t size = bd-> size();
94
88
95
89
printf("--- Block device geometry ---\n");
96
90
printf("read_size: %lld B\n", read_size);
@@ -106,47 +100,27 @@ int main() {
106
100
buffer_size = buffer_size - (buffer_size % program_size);
107
101
char *buffer = new char[buffer_size];
108
102
109
- // Read what is currently stored on the block device. We haven't written
110
- // yet so this may be garbage
111
- printf("bd.read(%p, %d, %d)\n", buffer, 0, buffer_size);
112
- err = bd.read(buffer, 0, buffer_size);
113
- printf("bd.read -> %d\n", err);
114
-
115
- printf("--- Stored data ---\n");
116
- for (size_t i = 0; i < buffer_size; i += 16) {
117
- for (size_t j = 0; j < 16; j++) {
118
- if (i+j < buffer_size) {
119
- printf("%02x ", buffer[i+j]);
120
- } else {
121
- printf(" ");
122
- }
123
- }
124
-
125
- printf(" %.*s\n", buffer_size - i, &buffer[i]);
126
- }
127
- printf("---\n");
128
-
129
103
// Update buffer with our string we want to store
130
104
strncpy(buffer, "Hello Storage!", buffer_size);
131
105
132
106
// Write data to first block, write occurs in two parts,
133
107
// an erase followed by a program
134
- printf("bd. erase(%d, %lld)\n", 0, erase_size);
135
- err = bd. erase(0, erase_size);
136
- printf("bd. erase -> %d\n", err);
108
+ printf("bd-> erase(%d, %lld)\n", 0, erase_size);
109
+ err = bd-> erase(0, erase_size);
110
+ printf("bd-> erase -> %d\n", err);
137
111
138
- printf("bd. program(%p, %d, %d)\n", buffer, 0, buffer_size);
139
- err = bd. program(buffer, 0, buffer_size);
140
- printf("bd. program -> %d\n", err);
112
+ printf("bd-> program(%p, %d, %d)\n", buffer, 0, buffer_size);
113
+ err = bd-> program(buffer, 0, buffer_size);
114
+ printf("bd-> program -> %d\n", err);
141
115
142
116
// Clobber the buffer so we don't get old data
143
117
memset(buffer, 0xcc, buffer_size);
144
118
145
119
// Read the data from the first block, note that the program_size must be
146
120
// a multiple of the read_size, so we don't have to check for alignment
147
- printf("bd. read(%p, %d, %d)\n", buffer, 0, buffer_size);
148
- err = bd. read(buffer, 0, buffer_size);
149
- printf("bd. read -> %d\n", err);
121
+ printf("bd-> read(%p, %d, %d)\n", buffer, 0, buffer_size);
122
+ err = bd-> read(buffer, 0, buffer_size);
123
+ printf("bd-> read -> %d\n", err);
150
124
151
125
printf("--- Stored data ---\n");
152
126
for (size_t i = 0; i < buffer_size; i += 16) {
@@ -163,9 +137,9 @@ int main() {
163
137
printf("---\n");
164
138
165
139
// Deinitialize the block device
166
- printf("bd. deinit()\n");
167
- err = bd. deinit();
168
- printf("bd. deinit -> %d\n", err);
140
+ printf("bd-> deinit()\n");
141
+ err = bd-> deinit();
142
+ printf("bd-> deinit -> %d\n", err);
169
143
170
144
printf("--- done! ---\n");
171
145
}
0 commit comments