Skip to content

Commit a63f5b6

Browse files
committed
Fix BlockDevice example
The example have been shorten and improved, we now receive the default block device according to the device target configuration.
1 parent 3e57249 commit a63f5b6

File tree

1 file changed

+21
-53
lines changed

1 file changed

+21
-53
lines changed

docs/api/storage/BlockDevice.md

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -59,38 +59,26 @@ For details regarding how to configure the default block device please refer to
5959
* limitations under the License.
6060
*/
6161
#include "mbed.h"
62+
#include "BlockDevice.h"
6263
#include <stdio.h>
6364
#include <algorithm>
6465
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();
7967
8068
// Entry point for the example
8169
int main() {
8270
printf("--- Mbed OS block device example ---\n");
8371
8472
// 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);
8876
8977
// 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();
9482
9583
printf("--- Block device geometry ---\n");
9684
printf("read_size: %lld B\n", read_size);
@@ -106,47 +94,27 @@ int main() {
10694
buffer_size = buffer_size - (buffer_size % program_size);
10795
char *buffer = new char[buffer_size];
10896
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-
12997
// Update buffer with our string we want to store
13098
strncpy(buffer, "Hello Storage!", buffer_size);
13199
132100
// Write data to first block, write occurs in two parts,
133101
// 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);
137105
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);
141109
142110
// Clobber the buffer so we don't get old data
143111
memset(buffer, 0xcc, buffer_size);
144112
145113
// Read the data from the first block, note that the program_size must be
146114
// 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);
150118
151119
printf("--- Stored data ---\n");
152120
for (size_t i = 0; i < buffer_size; i += 16) {
@@ -163,9 +131,9 @@ int main() {
163131
printf("---\n");
164132
165133
// 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);
169137
170138
printf("--- done! ---\n");
171139
}

0 commit comments

Comments
 (0)