Skip to content

Commit a7ca7a1

Browse files
author
Qinghao Shi
authored
Add more example snippets (#110)
* add Port In and Out example * add QSPIBlockdevice example * fix a bug
1 parent ce61a77 commit a7ca7a1

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-0
lines changed

APIs_Drivers/PortInOut_ex_1/main.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
// Switch on an LED if any of mbed pins 21-26 is high
9+
PortIn p(PortA, 0x0000003F); // p21-p26
10+
DigitalOut ind(LED4);
11+
12+
int main()
13+
{
14+
while (1) {
15+
int pins = p.read();
16+
if (pins) {
17+
ind = 1;
18+
} else {
19+
ind = 0;
20+
}
21+
}
22+
}

APIs_Drivers/PortIn_ex_1/main.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
// Toggle all four LEDs
9+
// LED1 = P1.18 LED2 = P1.20 LED3 = P1.21 LED4 = P1.23
10+
#define LED_MASK 0x00B40000
11+
12+
PortInOut ledport(PortA, LED_MASK);
13+
14+
int main()
15+
{
16+
int v = ledport;
17+
ledport.output();
18+
while (1) {
19+
ledport = LED_MASK;
20+
ThisThread::sleep_for(500);
21+
ledport = 0;
22+
ThisThread::sleep_for(1000);
23+
}
24+
}

APIs_Drivers/PortOut_ex_1/main.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
// Toggle all four LEDs
9+
// LED1 = P1.18 LED2 = P1.20 LED3 = P1.21 LED4 = P1.23
10+
#define LED_MASK 0x00B40000
11+
12+
PortOut ledport(PortA, LED_MASK);
13+
14+
int main()
15+
{
16+
while (1) {
17+
ledport = LED_MASK;
18+
ThisThread::sleep_for(1000);
19+
ledport = 0;
20+
ThisThread::sleep_for(1000);
21+
}
22+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#if !DEVICE_QSPI
7+
#error [NOT_SUPPORTED] QSPI not supported for this target
8+
#endif
9+
10+
#include "mbed.h"
11+
#include <stdio.h>
12+
#include <algorithm>
13+
#include "QSPIFBlockDevice.h"
14+
15+
QSPIFBlockDevice bd(QSPI_FLASH1_IO0, QSPI_FLASH1_IO1, QSPI_FLASH1_IO2, QSPI_FLASH1_IO3,
16+
QSPI_FLASH1_SCK, QSPI_FLASH1_CSN, QSPIF_POLARITY_MODE_0, MBED_CONF_QSPIF_QSPI_FREQ);
17+
18+
// Entry point for the example
19+
int main()
20+
{
21+
printf("--- Mbed OS QSPIF block device example ---\n");
22+
23+
// Initialize the block device
24+
int err = bd.init();
25+
printf("bd.init -> %d\n", err);
26+
27+
int erase_val = bd.get_erase_value();
28+
29+
// Get device geometry
30+
bd_size_t read_size = bd.get_read_size();
31+
bd_size_t program_size = bd.get_program_size();
32+
bd_size_t erase_size = bd.get_erase_size();
33+
bd_size_t size = bd.size();
34+
35+
printf("--- Block device geometry ---\n");
36+
printf("read_size: %lld B\n", read_size);
37+
printf("program_size: %lld B\n", program_size);
38+
printf("erase_size: %lld B\n", erase_size);
39+
printf("size: %lld B\n", size);
40+
printf("---\n");
41+
42+
// Allocate a block with enough space for our data, aligned to the
43+
// nearest program_size. This is the minimum size necessary to write
44+
// data to a block.
45+
size_t buffer_size = sizeof("Hello Storage!") + program_size - 1;
46+
buffer_size = buffer_size - (buffer_size % program_size);
47+
char *buffer = new char[buffer_size];
48+
49+
// Read what is currently stored on the block device. We haven't written
50+
// yet so this may be garbage
51+
printf("bd.read(%p, %d, %d)\n", buffer, 0, buffer_size);
52+
err = bd.read(buffer, 0, buffer_size);
53+
printf("bd.read -> %d\n", err);
54+
55+
printf("--- Stored data ---\n");
56+
for (size_t i = 0; i < buffer_size; i += 16) {
57+
for (size_t j = 0; j < 16; j++) {
58+
if (i + j < buffer_size) {
59+
printf("%02x ", buffer[i + j]);
60+
} else {
61+
printf(" ");
62+
}
63+
}
64+
65+
printf(" %.*s\n", buffer_size - i, &buffer[i]);
66+
}
67+
printf("---\n");
68+
69+
// Update buffer with our string we want to store
70+
strncpy(buffer, "Hello Storage!", buffer_size);
71+
72+
// Write data to first block, write occurs in two parts,
73+
// an erase followed by a program
74+
printf("bd.erase(%d, %lld)\n", 0, erase_size);
75+
err = bd.erase(0, erase_size);
76+
printf("bd.erase -> %d\n", err);
77+
78+
printf("bd.program(%p, %d, %d)\n", buffer, 0, buffer_size);
79+
err = bd.program(buffer, 0, buffer_size);
80+
printf("bd.program -> %d\n", err);
81+
82+
// Clobber the buffer so we don't get old data
83+
memset(buffer, 0xcc, buffer_size);
84+
85+
// Read the data from the first block, note that the program_size must be
86+
// a multiple of the read_size, so we don't have to check for alignment
87+
printf("bd.read(%p, %d, %d)\n", buffer, 0, buffer_size);
88+
err = bd.read(buffer, 0, buffer_size);
89+
printf("bd.read -> %d\n", err);
90+
91+
printf("--- Stored data ---\n");
92+
for (size_t i = 0; i < buffer_size; i += 16) {
93+
for (size_t j = 0; j < 16; j++) {
94+
if (i + j < buffer_size) {
95+
printf("%02x ", buffer[i + j]);
96+
} else {
97+
printf(" ");
98+
}
99+
}
100+
101+
printf(" %.*s\n", buffer_size - i, &buffer[i]);
102+
}
103+
printf("---\n");
104+
105+
// Deinitialize the block device
106+
printf("bd.deinit()\n");
107+
err = bd.deinit();
108+
printf("bd.deinit -> %d\n", err);
109+
110+
printf("--- done! ---\n");
111+
}

0 commit comments

Comments
 (0)