Skip to content

Commit a7b7a1c

Browse files
author
Amanda Butler
authored
Update example in BlockDevice.md
Replace hardcoded example with importable one.
1 parent d27f5c1 commit a7b7a1c

File tree

1 file changed

+1
-102
lines changed

1 file changed

+1
-102
lines changed

docs/api/storage/BlockDevice.md

Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -42,108 +42,7 @@ For details regarding how to configure the default block device please refer to
4242

4343
### BlockDevice example
4444

45-
```
46-
/* mbed Microcontroller Library
47-
* Copyright (c) 2006-2013 ARM Limited
48-
*
49-
* Licensed under the Apache License, Version 2.0 (the "License");
50-
* you may not use this file except in compliance with the License.
51-
* You may obtain a copy of the License at
52-
*
53-
* http://www.apache.org/licenses/LICENSE-2.0
54-
*
55-
* Unless required by applicable law or agreed to in writing, software
56-
* distributed under the License is distributed on an "AS IS" BASIS,
57-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
58-
* See the License for the specific language governing permissions and
59-
* limitations under the License.
60-
*/
61-
#include "mbed.h"
62-
#include "BlockDevice.h"
63-
#include <stdio.h>
64-
#include <algorithm>
65-
66-
// This will take the system's default block device
67-
BlockDevice *bd = BlockDevice::get_default_instance();
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);
73-
74-
// Entry point for the example
75-
int main() {
76-
printf("--- Mbed OS block device example ---\n");
77-
78-
// Initialize the block device
79-
printf("bd->init()\n");
80-
int err = bd->init();
81-
printf("bd->init -> %d\n", err);
82-
83-
// Get device geometry
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();
88-
89-
printf("--- Block device geometry ---\n");
90-
printf("read_size: %lld B\n", read_size);
91-
printf("program_size: %lld B\n", program_size);
92-
printf("erase_size: %lld B\n", erase_size);
93-
printf("size: %lld B\n", size);
94-
printf("---\n");
95-
96-
// Allocate a block with enough space for our data, aligned to the
97-
// nearest program_size. This is the minimum size necessary to write
98-
// data to a block.
99-
size_t buffer_size = sizeof("Hello Storage!") + program_size-1;
100-
buffer_size = buffer_size - (buffer_size % program_size);
101-
char *buffer = new char[buffer_size];
102-
103-
// Update buffer with our string we want to store
104-
strncpy(buffer, "Hello Storage!", buffer_size);
105-
106-
// Write data to first block, write occurs in two parts,
107-
// an erase followed by a program
108-
printf("bd->erase(%d, %lld)\n", 0, erase_size);
109-
err = bd->erase(0, erase_size);
110-
printf("bd->erase -> %d\n", err);
111-
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);
115-
116-
// Clobber the buffer so we don't get old data
117-
memset(buffer, 0xcc, buffer_size);
118-
119-
// Read the data from the first block, note that the program_size must be
120-
// a multiple of the read_size, so we don't have to check for alignment
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);
124-
125-
printf("--- Stored data ---\n");
126-
for (size_t i = 0; i < buffer_size; i += 16) {
127-
for (size_t j = 0; j < 16; j++) {
128-
if (i+j < buffer_size) {
129-
printf("%02x ", buffer[i+j]);
130-
} else {
131-
printf(" ");
132-
}
133-
}
134-
135-
printf(" %.*s\n", buffer_size - i, &buffer[i]);
136-
}
137-
printf("---\n");
138-
139-
// Deinitialize the block device
140-
printf("bd->deinit()\n");
141-
err = bd->deinit();
142-
printf("bd->deinit -> %d\n", err);
143-
144-
printf("--- done! ---\n");
145-
}
146-
```
45+
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/blockdevices/BlockDevice/)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/blockdevices/BlockDevice/main.cpp)
14746

14847
### Related content
14948

0 commit comments

Comments
 (0)