Skip to content

Commit 5cf3e7c

Browse files
author
Amanda Butler
authored
Merge pull request #909 from theamirocohen/write-a-thon
Fix BlockDevice example
2 parents 8dbb73a + 7374a0e commit 5cf3e7c

File tree

1 file changed

+26
-52
lines changed

1 file changed

+26
-52
lines changed

docs/api/storage/BlockDevice.md

Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -59,38 +59,32 @@ 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);
66+
// This will take the system's default block device
67+
BlockDevice *bd = BlockDevice::get_default_instance();
7868
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);
7973
8074
// Entry point for the example
8175
int main() {
8276
printf("--- Mbed OS block device example ---\n");
8377
8478
// 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);
8882
8983
// 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();
9488
9589
printf("--- Block device geometry ---\n");
9690
printf("read_size: %lld B\n", read_size);
@@ -106,47 +100,27 @@ int main() {
106100
buffer_size = buffer_size - (buffer_size % program_size);
107101
char *buffer = new char[buffer_size];
108102
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-
129103
// Update buffer with our string we want to store
130104
strncpy(buffer, "Hello Storage!", buffer_size);
131105
132106
// Write data to first block, write occurs in two parts,
133107
// 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);
137111
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);
141115
142116
// Clobber the buffer so we don't get old data
143117
memset(buffer, 0xcc, buffer_size);
144118
145119
// Read the data from the first block, note that the program_size must be
146120
// 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);
150124
151125
printf("--- Stored data ---\n");
152126
for (size_t i = 0; i < buffer_size; i += 16) {
@@ -163,9 +137,9 @@ int main() {
163137
printf("---\n");
164138
165139
// 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);
169143
170144
printf("--- done! ---\n");
171145
}

0 commit comments

Comments
 (0)