-
Notifications
You must be signed in to change notification settings - Fork 26
Bring platform layer examples #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b2eb281
Add FileHandle example
mprse ed97448
Add FileHandle_sigio example
mprse b8dac4e
Add Poll example
mprse 241de8a
Add Shared_pointer example
mprse 60ca464
Add ScopedRamExecutionLock example
mprse 8c67953
Add ScopedRomWriteLock example
mprse 6bc496c
Fix FileHandle example: use BufferedSerial instead of UARTSerial
mprse 958646a
Poll example: add mbed_app.json
mprse d1120f9
Shared_pointer example: Add missing readme.md
mprse fb2a5fb
Add Span examples
mprse 23a976b
Add uart config to test.json
mprse 5185986
Platform examples: Add headers
mprse 7587f69
Platform examples: Fix after review
mprse a0755d7
Merge branch 'master' into platform_examples
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
## FileHandle example | ||
|
||
The example monitors a serial console device, and every time it reads a character, it sends it back to the console and toggles LED2. | ||
|
||
1. Flash the board, and ensure the target's USB is plugged into the PC. | ||
2. Open serial console (select associated COM port, transmission speed: 9600 bps). | ||
3. Reset the target. | ||
4. Every time that device reads a character, the character should appear in the console and LED2 should be toggled. | ||
|
||
**Note:** You can check the associated serial port using `mbedls` command. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright (c) 2006-2020 Arm Limited and affiliates. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
#include "mbed.h" | ||
|
||
static DigitalOut led2(LED2); | ||
|
||
// BufferedSerial derives from FileHandle | ||
static BufferedSerial device(STDIO_UART_TX, STDIO_UART_RX); | ||
|
||
int main() | ||
{ | ||
// Once set up, access through the C library | ||
FILE *devin = fdopen(&device, "r"); | ||
|
||
while (1) { | ||
putchar(fgetc(devin)); | ||
led2 = !led2; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
## FileHandle sigio example | ||
|
||
The example monitors a serial console device, and every time it reads a character, it sends it back to the console and toggles LED2. | ||
|
||
1. Flash the board, and ensure the target's USB is plugged into the PC. | ||
2. Open serial console (select associated COM port, transmission speed: 9600 bps). | ||
3. Reset the target. | ||
4. Every time that device reads a character, the character should appear in the console and LED2 should be toggled. | ||
|
||
**Note:** You can check the associated serial port using `mbedls` command. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (c) 2006-2020 Arm Limited and affiliates. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
#include "mbed.h" | ||
|
||
static DigitalOut led1(LED1); | ||
static DigitalOut led2(LED2); | ||
|
||
static BufferedSerial device(STDIO_UART_TX, STDIO_UART_RX); | ||
|
||
static void callback_ex() | ||
{ | ||
// always read until data is exhausted - we may not get another | ||
// sigio otherwise | ||
while (1) { | ||
char c; | ||
if (device.read(&c, 1) != 1) { | ||
break; | ||
} | ||
putchar(c); | ||
putchar('\n'); | ||
led2 = !led2; | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
// Ensure that device.read() returns -EAGAIN when out of data | ||
device.set_blocking(false); | ||
|
||
// sigio callback is deferred to event queue, as we cannot in general | ||
// perform read() calls directly from the sigio() callback. | ||
device.sigio(mbed_event_queue()->event(callback_ex)); | ||
|
||
while (1) { | ||
led1 = !led1; | ||
ThisThread::sleep_for(500); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
## Poll example | ||
|
||
The example transfers bidirectional data between two serial ports, acting as a virtual link. | ||
|
||
1. Example `mbed_app.json` file (`K64F`): | ||
|
||
``` | ||
{ | ||
"config": { | ||
"UART1_TX": "D1", | ||
"UART1_RX": "D0", | ||
"UART2_TX": "USBTX", | ||
"UART2_RX": "USBRX" | ||
} | ||
} | ||
``` | ||
|
||
2. Flash the board, and ensure the target's USB is plugged into the PC (UART2). | ||
3. Connect second serial port to PC via USB to serial adapter (UART1). | ||
4. Open two serial consoles (select associated COM ports, transmission speed: 9600 bps). | ||
5. Reset the target. | ||
6. Data sent from one console should appear in the other one and vice versa. | ||
|
||
**Note:** You can check the associated serial port using `mbedls` command (UART2). | ||
**Note:** Note: On Windows you can check the associated COM port in the Device Manager (UART1). | ||
**Note:** Please adapt your pins for target different than `K64F`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (c) 2006-2020 Arm Limited and affiliates. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
#include "mbed.h" | ||
|
||
// Pins for each port are specified using mbed_app.json. Assume no flow control. | ||
// (If there were flow control, being blocked by it could break the implementation, | ||
// as you would stop reading input in the opposite direction). | ||
BufferedSerial device1(MBED_CONF_APP_UART1_TX, MBED_CONF_APP_UART1_RX); | ||
BufferedSerial device2(MBED_CONF_APP_UART2_TX, MBED_CONF_APP_UART2_RX); | ||
|
||
// Precondition: "in" is readable | ||
static void copy_some(FileHandle *out, FileHandle *in) | ||
{ | ||
// To ensure performance, try to read multiple bytes at once, | ||
// but don't expect to read many in practice. | ||
char buffer[32]; | ||
|
||
|
||
|
||
// Despite the FileHandle being in its default blocking mode, | ||
// read() must return immediately with between 1-32 bytes, as | ||
// you've already checked that `in` is ready with poll() | ||
ssize_t read = in->read(buffer, sizeof buffer); | ||
if (read <= 0) { | ||
error("Input error"); | ||
} | ||
|
||
// Write everything out. Assuming output port is same speed as input, | ||
// and no flow control, this may block briefly but not significantly. | ||
ssize_t written = out->write(buffer, read); | ||
if (written < read) { | ||
error("Output error"); | ||
} | ||
} | ||
|
||
// Transfer bidirectional data between two ports, acting as a virtual link. | ||
// poll() is used to monitor both ports for input. | ||
int main() | ||
{ | ||
pollfh fds[2]; | ||
|
||
fds[0].fh = &device1; | ||
fds[0].events = POLLIN; | ||
fds[1].fh = &device2; | ||
fds[1].events = POLLIN; | ||
|
||
while (1) { | ||
// Block indefinitely until either of the 2 ports is readable (or has an error) | ||
poll(fds, 2, -1); | ||
|
||
// Transfer some data in either or both directions | ||
if (fds[0].revents) { | ||
copy_some(fds[1].fh, fds[0].fh); | ||
} | ||
if (fds[1].revents) { | ||
copy_some(fds[0].fh, fds[1].fh); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"config": { | ||
"UART1_TX": "D1", | ||
"UART1_RX": "D0", | ||
"UART2_TX": "USBTX", | ||
"UART2_RX": "USBRX" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## ScopedRamExecutionLock example | ||
|
||
The example shows how to use ScopedRamExecutionLock class for enabling execution from RAM. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright (c) 2006-2020 Arm Limited and affiliates. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
#include "mbed.h" | ||
|
||
int main() | ||
{ | ||
// Enable execution from RAM while in main | ||
ScopedRamExecutionLock make_ram_executable; | ||
|
||
//some_function_in_ram(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## ScopedRomWriteLock example | ||
|
||
The example shows how to use ScopedRomWriteLock class for enabling writing to ROM. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright (c) 2006-2020 Arm Limited and affiliates. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
#include "mbed.h" | ||
|
||
int main() | ||
{ | ||
// Enable writing to ROM while in main | ||
ScopedRomWriteLock make_rom_writable; | ||
|
||
//custom_flash_programming(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## Shared Pointer example | ||
|
||
The example shows how to use "smart" pointer. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright (c) 2006-2020 Arm Limited and affiliates. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
#include "platform/SharedPtr.h" | ||
|
||
int main(void) | ||
{ | ||
struct MyStruct { | ||
int a; | ||
}; | ||
|
||
// Create shared pointer | ||
SharedPtr<MyStruct> ptr(new MyStruct); | ||
|
||
// Increase reference count | ||
SharedPtr<MyStruct> ptr2(ptr); | ||
|
||
ptr = nullptr; // Reference to the struct instance is still held by ptr2 | ||
|
||
ptr2 = nullptr; // The raw pointer is freed | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## Span example | ||
|
||
The example shows how to use Span class to operate on the string buffer. | ||
|
||
**Note:** C example code |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) 2006-2020 Arm Limited and affiliates. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
#include "mbed.h" | ||
#include "platform/Span.h" | ||
|
||
template<typename T> | ||
void split(const T **in_ptr, ptrdiff_t *in_size, const T **token_ptr, ptrdiff_t *token_size, const T &separator) | ||
{ | ||
const ptrdiff_t out_of_range = *in_size; | ||
|
||
ptrdiff_t start; | ||
for (start = 0; start != out_of_range && (*in_ptr)[start] == separator; ++start) { } | ||
|
||
ptrdiff_t last; | ||
for (last = start; last != out_of_range && (*in_ptr)[last] != separator; ++last) { } | ||
|
||
*token_ptr = *in_ptr + start; | ||
*token_size = last - start; | ||
|
||
*in_size = *in_size - last; | ||
*in_ptr = *in_ptr + last; | ||
} | ||
|
||
int main() | ||
{ | ||
const char str[] = "Hello World! Hello mbed-os!"; | ||
const char *buffer_ptr = str; | ||
ptrdiff_t buffer_size = sizeof(str); | ||
while (buffer_size) { | ||
const char *token_ptr = NULL; | ||
ptrdiff_t token_size = 0; | ||
split(&buffer_ptr, &buffer_size, &token_ptr, &token_size, ' '); | ||
printf("token: %.*s\r\n", token_size, token_ptr); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## Span example | ||
|
||
The example shows how to use Span class to operate on the string buffer. | ||
|
||
**Note:** C++ example code |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2006-2020 Arm Limited and affiliates. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
#include "mbed.h" | ||
#include "platform/Span.h" | ||
|
||
template<typename T> | ||
Span<const T> split(Span<const T> &range, const T &separator) | ||
{ | ||
const ptrdiff_t out_of_range = range.size(); | ||
|
||
ptrdiff_t start; | ||
for (start = 0; start != out_of_range && range[start] == separator; ++start) { } | ||
|
||
ptrdiff_t last; | ||
for (last = start; last != out_of_range && range[last] != separator; ++last) { } | ||
|
||
Span<const T> result = range.subspan(start, last - start); | ||
range = range.subspan(last); | ||
return result; | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
Span<const char> buffer("Hello World! Hello mbed-os!"); | ||
while (buffer.empty() == false) { | ||
Span<const char> token = split(buffer, ' '); | ||
printf("token: %.*s\r\n", token.size(), token.data()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.