Skip to content

Commit fad81ab

Browse files
mprseQinghao Shi
andauthored
Bring platform layer examples (#70)
* Add FileHandle example * Add FileHandle_sigio example * Add Poll example * Add Shared_pointer example * Add ScopedRamExecutionLock example * Add ScopedRomWriteLock example * Fix FileHandle example: use BufferedSerial instead of UARTSerial * Poll example: add mbed_app.json * Shared_pointer example: Add missing readme.md * Add Span examples * Add uart config to test.json * Platform examples: Add headers * Platform examples: Fix after review Co-authored-by: Qinghao Shi <[email protected]>
1 parent bbc5cc4 commit fad81ab

File tree

18 files changed

+316
-0
lines changed

18 files changed

+316
-0
lines changed

APIs_Platform/FileHandle/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## FileHandle example
2+
3+
The example monitors a serial console device, and every time it reads a character, it sends it back to the console and toggles LED2.
4+
5+
1. Flash the board, and ensure the target's USB is plugged into the PC.
6+
2. Open serial console (select associated COM port, transmission speed: 9600 bps).
7+
3. Reset the target.
8+
4. Every time that device reads a character, the character should appear in the console and LED2 should be toggled.
9+
10+
**Note:** You can check the associated serial port using `mbedls` command.

APIs_Platform/FileHandle/main.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
7+
static DigitalOut led2(LED2);
8+
9+
// BufferedSerial derives from FileHandle
10+
static BufferedSerial device(STDIO_UART_TX, STDIO_UART_RX);
11+
12+
int main()
13+
{
14+
// Once set up, access through the C library
15+
FILE *devin = fdopen(&device, "r");
16+
17+
while (1) {
18+
putchar(fgetc(devin));
19+
led2 = !led2;
20+
}
21+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## FileHandle sigio example
2+
3+
The example monitors a serial console device, and every time it reads a character, it sends it back to the console and toggles LED2.
4+
5+
1. Flash the board, and ensure the target's USB is plugged into the PC.
6+
2. Open serial console (select associated COM port, transmission speed: 9600 bps).
7+
3. Reset the target.
8+
4. Every time that device reads a character, the character should appear in the console and LED2 should be toggled.
9+
10+
**Note:** You can check the associated serial port using `mbedls` command.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
7+
static DigitalOut led1(LED1);
8+
static DigitalOut led2(LED2);
9+
10+
static BufferedSerial device(STDIO_UART_TX, STDIO_UART_RX);
11+
12+
static void callback_ex()
13+
{
14+
// always read until data is exhausted - we may not get another
15+
// sigio otherwise
16+
while (1) {
17+
char c;
18+
if (device.read(&c, 1) != 1) {
19+
break;
20+
}
21+
putchar(c);
22+
putchar('\n');
23+
led2 = !led2;
24+
}
25+
}
26+
27+
int main()
28+
{
29+
// Ensure that device.read() returns -EAGAIN when out of data
30+
device.set_blocking(false);
31+
32+
// sigio callback is deferred to event queue, as we cannot in general
33+
// perform read() calls directly from the sigio() callback.
34+
device.sigio(mbed_event_queue()->event(callback_ex));
35+
36+
while (1) {
37+
led1 = !led1;
38+
ThisThread::sleep_for(500);
39+
}
40+
}

APIs_Platform/Poll/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## Poll example
2+
3+
The example transfers bidirectional data between two serial ports, acting as a virtual link.
4+
5+
1. Example `mbed_app.json` file (`K64F`):
6+
7+
```
8+
{
9+
"config": {
10+
"UART1_TX": "D1",
11+
"UART1_RX": "D0",
12+
"UART2_TX": "USBTX",
13+
"UART2_RX": "USBRX"
14+
}
15+
}
16+
```
17+
18+
2. Flash the board, and ensure the target's USB is plugged into the PC (UART2).
19+
3. Connect second serial port to PC via USB to serial adapter (UART1).
20+
4. Open two serial consoles (select associated COM ports, transmission speed: 9600 bps).
21+
5. Reset the target.
22+
6. Data sent from one console should appear in the other one and vice versa.
23+
24+
**Note:** You can check the associated serial port using `mbedls` command (UART2).
25+
**Note:** Note: On Windows you can check the associated COM port in the Device Manager (UART1).
26+
**Note:** Please adapt your pins for target different than `K64F`.

APIs_Platform/Poll/main.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
7+
// Pins for each port are specified using mbed_app.json. Assume no flow control.
8+
// (If there were flow control, being blocked by it could break the implementation,
9+
// as you would stop reading input in the opposite direction).
10+
BufferedSerial device1(MBED_CONF_APP_UART1_TX, MBED_CONF_APP_UART1_RX);
11+
BufferedSerial device2(MBED_CONF_APP_UART2_TX, MBED_CONF_APP_UART2_RX);
12+
13+
// Precondition: "in" is readable
14+
static void copy_some(FileHandle *out, FileHandle *in)
15+
{
16+
// To ensure performance, try to read multiple bytes at once,
17+
// but don't expect to read many in practice.
18+
char buffer[32];
19+
20+
21+
22+
// Despite the FileHandle being in its default blocking mode,
23+
// read() must return immediately with between 1-32 bytes, as
24+
// you've already checked that `in` is ready with poll()
25+
ssize_t read = in->read(buffer, sizeof buffer);
26+
if (read <= 0) {
27+
error("Input error");
28+
}
29+
30+
// Write everything out. Assuming output port is same speed as input,
31+
// and no flow control, this may block briefly but not significantly.
32+
ssize_t written = out->write(buffer, read);
33+
if (written < read) {
34+
error("Output error");
35+
}
36+
}
37+
38+
// Transfer bidirectional data between two ports, acting as a virtual link.
39+
// poll() is used to monitor both ports for input.
40+
int main()
41+
{
42+
pollfh fds[2];
43+
44+
fds[0].fh = &device1;
45+
fds[0].events = POLLIN;
46+
fds[1].fh = &device2;
47+
fds[1].events = POLLIN;
48+
49+
while (1) {
50+
// Block indefinitely until either of the 2 ports is readable (or has an error)
51+
poll(fds, 2, -1);
52+
53+
// Transfer some data in either or both directions
54+
if (fds[0].revents) {
55+
copy_some(fds[1].fh, fds[0].fh);
56+
}
57+
if (fds[1].revents) {
58+
copy_some(fds[0].fh, fds[1].fh);
59+
}
60+
}
61+
}

APIs_Platform/Poll/mbed_app.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"config": {
3+
"UART1_TX": "D1",
4+
"UART1_RX": "D0",
5+
"UART2_TX": "USBTX",
6+
"UART2_RX": "USBRX"
7+
}
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## ScopedRamExecutionLock example
2+
3+
The example shows how to use ScopedRamExecutionLock class for enabling execution from RAM.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
7+
int main()
8+
{
9+
// Enable execution from RAM while in main
10+
ScopedRamExecutionLock make_ram_executable;
11+
12+
//some_function_in_ram();
13+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## ScopedRomWriteLock example
2+
3+
The example shows how to use ScopedRomWriteLock class for enabling writing to ROM.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
7+
int main()
8+
{
9+
// Enable writing to ROM while in main
10+
ScopedRomWriteLock make_rom_writable;
11+
12+
//custom_flash_programming();
13+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Shared Pointer example
2+
3+
The example shows how to use "smart" pointer.

APIs_Platform/Shared_pointer/main.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "platform/SharedPtr.h"
6+
7+
int main(void)
8+
{
9+
struct MyStruct {
10+
int a;
11+
};
12+
13+
// Create shared pointer
14+
SharedPtr<MyStruct> ptr(new MyStruct);
15+
16+
// Increase reference count
17+
SharedPtr<MyStruct> ptr2(ptr);
18+
19+
ptr = nullptr; // Reference to the struct instance is still held by ptr2
20+
21+
ptr2 = nullptr; // The raw pointer is freed
22+
}

APIs_Platform/Span_c/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Span example
2+
3+
The example shows how to use Span class to operate on the string buffer.
4+
5+
**Note:** C example code

APIs_Platform/Span_c/main.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
#include "platform/Span.h"
7+
8+
template<typename T>
9+
void split(const T **in_ptr, ptrdiff_t *in_size, const T **token_ptr, ptrdiff_t *token_size, const T &separator)
10+
{
11+
const ptrdiff_t out_of_range = *in_size;
12+
13+
ptrdiff_t start;
14+
for (start = 0; start != out_of_range && (*in_ptr)[start] == separator; ++start) { }
15+
16+
ptrdiff_t last;
17+
for (last = start; last != out_of_range && (*in_ptr)[last] != separator; ++last) { }
18+
19+
*token_ptr = *in_ptr + start;
20+
*token_size = last - start;
21+
22+
*in_size = *in_size - last;
23+
*in_ptr = *in_ptr + last;
24+
}
25+
26+
int main()
27+
{
28+
const char str[] = "Hello World! Hello mbed-os!";
29+
const char *buffer_ptr = str;
30+
ptrdiff_t buffer_size = sizeof(str);
31+
while (buffer_size) {
32+
const char *token_ptr = NULL;
33+
ptrdiff_t token_size = 0;
34+
split(&buffer_ptr, &buffer_size, &token_ptr, &token_size, ' ');
35+
printf("token: %.*s\r\n", token_size, token_ptr);
36+
}
37+
}

APIs_Platform/Span_cpp/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Span example
2+
3+
The example shows how to use Span class to operate on the string buffer.
4+
5+
**Note:** C++ example code

APIs_Platform/Span_cpp/main.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
#include "platform/Span.h"
7+
8+
template<typename T>
9+
Span<const T> split(Span<const T> &range, const T &separator)
10+
{
11+
const ptrdiff_t out_of_range = range.size();
12+
13+
ptrdiff_t start;
14+
for (start = 0; start != out_of_range && range[start] == separator; ++start) { }
15+
16+
ptrdiff_t last;
17+
for (last = start; last != out_of_range && range[last] != separator; ++last) { }
18+
19+
Span<const T> result = range.subspan(start, last - start);
20+
range = range.subspan(last);
21+
return result;
22+
}
23+
24+
25+
int main()
26+
{
27+
Span<const char> buffer("Hello World! Hello mbed-os!");
28+
while (buffer.empty() == false) {
29+
Span<const char> token = split(buffer, ' ');
30+
printf("token: %.*s\r\n", token.size(), token.data());
31+
}
32+
}

test.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
22
"config": {
3+
"UART1_TX": "D1",
4+
"UART1_RX": "D0",
5+
"UART2_TX": "USBTX",
6+
"UART2_RX": "USBRX",
37
"wifi-ssid": {
48
"help": "WiFi SSID",
59
"value": "\"SSID\""

0 commit comments

Comments
 (0)