Skip to content

Commit b2c17d2

Browse files
author
Qinghao Shi
authored
Merge pull request #99 from maciejbocianski/move_block_device_examples
add BlockDevices examples
2 parents c9ee59f + 5e39f26 commit b2c17d2

File tree

14 files changed

+137
-10
lines changed

14 files changed

+137
-10
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# ChainingBlockDevice example
2+
3+
4+
This example shows how to use ChainingBlockDevice to format a FATFileSystem on two blocks of storage.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2017-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
#include "HeapBlockDevice.h"
8+
#include "ChainingBlockDevice.h"
9+
#include "FATFileSystem.h"
10+
11+
#define BLOCKSIZE 512
12+
13+
int main(void)
14+
{
15+
// Create two smaller block devices with
16+
// 64 and 32 blocks of size 512 bytes
17+
HeapBlockDevice mem1(64 * BLOCKSIZE, BLOCKSIZE);
18+
HeapBlockDevice mem2(32 * BLOCKSIZE, BLOCKSIZE);
19+
20+
// Create a block device backed by mem1 and mem2
21+
// contains 96 blocks of size 512 bytes
22+
BlockDevice *bds[] = {&mem1, &mem2};
23+
ChainingBlockDevice chainmem(bds);
24+
25+
// Format the new chained block device with a FAT filesystem
26+
FATFileSystem::format(&chainmem);
27+
28+
// Create the FAT filesystem instance, files can now be written to
29+
// the FAT filesystem as if to a single 96 x 512 byte storage device
30+
FATFileSystem fat("fat", &chainmem);
31+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# ChainingBlockDevice example
2+
3+
This example shows how to use ChainingBlockDevice to program and read from a chained group of HeapBlockDevices.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2017-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
#include "HeapBlockDevice.h"
8+
#include "ChainingBlockDevice.h"
9+
10+
#define BLOCKSIZE 512
11+
char buffer1[BLOCKSIZE];
12+
char buffer2[BLOCKSIZE];
13+
14+
int main(void)
15+
{
16+
// Create two smaller block devices with
17+
// 64 and 32 blocks of size 512 bytes
18+
HeapBlockDevice mem1(64 * BLOCKSIZE, BLOCKSIZE);
19+
HeapBlockDevice mem2(32 * BLOCKSIZE, BLOCKSIZE);
20+
21+
// Create a block device backed by mem1 and mem2
22+
// contains 96 blocks of size 512 bytes
23+
BlockDevice *bds[] = {&mem1, &mem2};
24+
ChainingBlockDevice chainmem(bds);
25+
26+
// Initialize the block devices
27+
chainmem.init();
28+
29+
// Erase the block device to prepare for programming. 64 and 32 refer to
30+
// the respective number of blocks in mem1 and mem2
31+
chainmem.erase(0, (BLOCKSIZE * (64 + 32)));
32+
33+
// Program strings to the block device at byte-addressable locations that
34+
// span both sub blocks. The second program will write past the end of the
35+
// first block
36+
chainmem.program("data for block", 0, BLOCKSIZE);
37+
chainmem.program("Some more data", (65 * BLOCKSIZE), BLOCKSIZE);
38+
39+
// Readback the written values
40+
chainmem.read(&buffer1, 0, BLOCKSIZE);
41+
chainmem.read(&buffer2, (65 * BLOCKSIZE), BLOCKSIZE);
42+
printf("Read back: %s, %s\r\n", buffer1, buffer2);
43+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# HeapBlockDevice example
2+
3+
This example shows how to initialize and use HeapBlockDevice.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2017-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
#include "HeapBlockDevice.h"
8+
9+
#define BLOCK_SIZE 512
10+
11+
HeapBlockDevice bd(2048, BLOCK_SIZE); // 2048 bytes with a block size of 512 bytes
12+
uint8_t block[BLOCK_SIZE] = "Hello World!\n";
13+
14+
int main()
15+
{
16+
bd.init();
17+
bd.erase(0, BLOCK_SIZE);
18+
bd.program(block, 0, BLOCK_SIZE);
19+
bd.read(block, 0, BLOCK_SIZE);
20+
printf("%s", block);
21+
bd.deinit();
22+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# MBRBlockDevice example 1
1+
# MBRBlockDevice example
22

3-
Partition a heap backed block device into two partitions. This example also uses the HeapBlockDevice. To see this example in context, please see its [published documentation](https://os.mbed.com/docs/mbed-os/latest/apis/mbrblockdevice.html).
3+
This example shows how to use MBRBlockDevice to partition a heap block device into two partitions. To see this example in context, please see its [published documentation](https://os.mbed.com/docs/mbed-os/latest/apis/mbrblockdevice.html).

APIs_Storage/MBRBlockDevice_ex_1/main.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1+
/*
2+
* Copyright (c) 2017-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
16
#include "mbed.h"
27
#include "HeapBlockDevice.h"
38
#include "MBRBlockDevice.h"
49

10+
#define BLOCK_SIZE 512
11+
512
int main(void)
613
{
714
// Create a block device with 64 blocks of size 512
815
printf("Create a block device with 64 blocks of size 512\n");
9-
HeapBlockDevice mem(64 * 512, 512);
16+
HeapBlockDevice mem(64 * BLOCK_SIZE, BLOCK_SIZE);
1017

1118
// Partition into two partitions with ~half the blocks
1219
printf("Partition into two partitions with ~half the blocks\n");
13-
MBRBlockDevice::partition(&mem, 1, 0x83, 0 * 512, 32 * 512);
14-
MBRBlockDevice::partition(&mem, 2, 0x83, 32 * 512);
20+
MBRBlockDevice::partition(&mem, 1, 0x83, 0 * BLOCK_SIZE, 32 * BLOCK_SIZE);
21+
MBRBlockDevice::partition(&mem, 2, 0x83, 32 * BLOCK_SIZE);
1522

1623
// Create a block device that maps to the first 32 blocks (excluding MBR block)
1724
printf("Create a block device that maps to the first 32 blocks (excluding MBR block)\n");
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# MBRBlockDevice example 2
2-
3-
Partition an SD card, and format the new partition with a FAT filesystem. The SD card is now recognizable to devices.
1+
# MBRBlockDevice example
42

3+
This example shows how to use MBRBlockDevice to partition an SD card and format the new partition with a FAT file system. The SD card is now recognizable to devices.

APIs_Storage/MBRBlockDevice_ex_2/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Copyright (c) 2017-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
16
#include "mbed.h"
27
#include "SDBlockDevice.h"
38
#include "MBRBlockDevice.h"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# ProfilingBlockDevice example
22

3-
Create a ProfilingBlockDevice, perform storage operations and report back the read, write and erase counts. To see this example in context, please see its [published documentation](https://os.mbed.com/docs/mbed-os/latest/apis/profilingblockdevice.html).
3+
This example shows how to use ProfilingBlockDevice to perform storage operations and report back the read, write and erase counts. To see this example in context, please see its [published documentation](https://os.mbed.com/docs/mbed-os/latest/apis/profilingblockdevice.html).

APIs_Storage/ProfilingBlockDevice/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Copyright (c) 2017-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
16
#include "mbed.h"
27
#include "HeapBlockDevice.h"
38
#include "ProfilingBlockDevice.h"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# SlicingBlockDevice example
22

3-
This SlicingBlockDevice example partitions a HeapBlockDevice into three subunits. It showcases programming and reading back data segments, through both the underlying master block device and sliced subunits. To see this example in context, please see its [published documentation](https://os.mbed.com/docs/mbed-os/latest/apis/slicingblockdevice.html).
3+
This example shows how to use SlicingBlockDevice to partition a heap block device into three subunits. It shows programming and reading back data segments, through both the underlying master block device and sliced subunits. To see this example in context, please see its [published documentation](https://os.mbed.com/docs/mbed-os/latest/apis/slicingblockdevice.html).

APIs_Storage/SlicingBlockDevice/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Copyright (c) 2017-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
16
#include "mbed.h"
27
#include "HeapBlockDevice.h"
38
#include "SlicingBlockDevice.h"

0 commit comments

Comments
 (0)