Skip to content

Commit cfe874c

Browse files
author
Qinghao Shi
authored
Merge pull request #86 from mprse/storage_examples
Add storage examples
2 parents 1df371a + de99c10 commit cfe874c

File tree

6 files changed

+58
-32
lines changed

6 files changed

+58
-32
lines changed

APIs_Storage/BufferedBlockDevice/main.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
1-
/* mbed Microcontroller Library
2-
* Copyright (c) 2019 ARM Limited
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
154
*/
16-
175
#include "BufferedBlockDevice.h"
186
#include "HeapBlockDevice.h"
197
#include <cstdio>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## DataFlashBlockDevice Example
2+
3+
This example shows how to use an DataFlashBlockDevice class to controll AT45DB (SPI Serial Flash Memory) on the K64F.
4+
5+
**Note:** Please adapt `SPI` pins for target different than `K64F`.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
// Here's an example using the AT45DB on the K64F
6+
#include "mbed.h"
7+
#include "DataFlashBlockDevice.h"
8+
9+
// Create DataFlash on SPI bus with PTE5 as chip select
10+
DataFlashBlockDevice dataflash(PTE1, PTE3, PTE2, PTE4);
11+
12+
// Create DataFlash on SPI bus with PTE6 as write-protect
13+
// DataFlashBlockDevice dataflash(PTE1, PTE3, PTE2, PTE4, PTE6);
14+
15+
int main()
16+
{
17+
printf("dataflash test\n");
18+
19+
// Initialize the SPI flash device and print the memory layout
20+
dataflash.init();
21+
printf("dataflash size: %llu\n", dataflash.size());
22+
printf("dataflash read size: %llu\n", dataflash.get_read_size());
23+
printf("dataflash program size: %llu\n", dataflash.get_program_size());
24+
printf("dataflash erase size: %llu\n", dataflash.get_erase_size());
25+
26+
// Write "Hello World!" to the first block
27+
char *buffer = (char *)malloc(dataflash.get_erase_size());
28+
sprintf(buffer, "Hello World!\n");
29+
dataflash.erase(0, dataflash.get_erase_size());
30+
dataflash.program(buffer, 0, dataflash.get_erase_size());
31+
32+
// Read back what was stored
33+
dataflash.read(buffer, 0, dataflash.get_erase_size());
34+
printf("%s", buffer);
35+
36+
// Deinitialize the device
37+
dataflash.deinit();
38+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"target_overrides": {
3+
"K64F": {
4+
"target.components_add": ["DATAFLASH"]
5+
}
6+
}
7+
}

APIs_Storage/FlashSimBlockDevice/main.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
1-
/* mbed Microcontroller Library
2-
* Copyright (c) 2019 ARM Limited
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
154
*/
16-
175
#include "mbed.h"
186
#include "HeapBlockDevice.h"
197
#include "FlashSimBlockDevice.h"

test.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"spif-driver.SPI_MOSI" : "D11",
66
"spif-driver.SPI_MISO" : "D12",
77
"spif-driver.SPI_CLK" : "D13",
8-
"spif-driver.SPI_CS" : "D10"
9-
8+
"spif-driver.SPI_CS" : "D10",
9+
"target.components_add": ["DATAFLASH"]
1010
}
1111
}
1212
}

0 commit comments

Comments
 (0)