@@ -59,38 +59,26 @@ 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);
78
-
66
+ BlockDevice *bd = BlockDevice::get_default_instance();
79
67
80
68
// Entry point for the example
81
69
int main() {
82
70
printf("--- Mbed OS block device example ---\n");
83
71
84
72
// Initialize the block device
85
- printf("bd. init()\n");
86
- int err = bd. init();
87
- printf("bd. init -> %d\n", err);
73
+ printf("bd-> init()\n");
74
+ int err = bd-> init();
75
+ printf("bd-> init -> %d\n", err);
88
76
89
77
// 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();
78
+ bd_size_t read_size = bd-> get_read_size();
79
+ bd_size_t program_size = bd-> get_program_size();
80
+ bd_size_t erase_size = bd-> get_erase_size();
81
+ bd_size_t size = bd-> size();
94
82
95
83
printf("--- Block device geometry ---\n");
96
84
printf("read_size: %lld B\n", read_size);
@@ -106,47 +94,27 @@ int main() {
106
94
buffer_size = buffer_size - (buffer_size % program_size);
107
95
char *buffer = new char[buffer_size];
108
96
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
97
// Update buffer with our string we want to store
130
98
strncpy(buffer, "Hello Storage!", buffer_size);
131
99
132
100
// Write data to first block, write occurs in two parts,
133
101
// 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);
102
+ printf("bd-> erase(%d, %lld)\n", 0, erase_size);
103
+ err = bd-> erase(0, erase_size);
104
+ printf("bd-> erase -> %d\n", err);
137
105
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);
106
+ printf("bd-> program(%p, %d, %d)\n", buffer, 0, buffer_size);
107
+ err = bd-> program(buffer, 0, buffer_size);
108
+ printf("bd-> program -> %d\n", err);
141
109
142
110
// Clobber the buffer so we don't get old data
143
111
memset(buffer, 0xcc, buffer_size);
144
112
145
113
// Read the data from the first block, note that the program_size must be
146
114
// 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);
115
+ printf("bd-> read(%p, %d, %d)\n", buffer, 0, buffer_size);
116
+ err = bd-> read(buffer, 0, buffer_size);
117
+ printf("bd-> read -> %d\n", err);
150
118
151
119
printf("--- Stored data ---\n");
152
120
for (size_t i = 0; i < buffer_size; i += 16) {
@@ -163,9 +131,9 @@ int main() {
163
131
printf("---\n");
164
132
165
133
// Deinitialize the block device
166
- printf("bd. deinit()\n");
167
- err = bd. deinit();
168
- printf("bd. deinit -> %d\n", err);
134
+ printf("bd-> deinit()\n");
135
+ err = bd-> deinit();
136
+ printf("bd-> deinit -> %d\n", err);
169
137
170
138
printf("--- done! ---\n");
171
139
}
0 commit comments