Skip to content

Commit 62c995e

Browse files
committed
Use threads comms to blink and add baremetal version
The application has been re-written to use inter-thread communication in order to blink the LED. It now shows how threads can be created, started and how they can communicate with each other. Runtime statistics are made optional via an application configuration file. Instructions have been provided to use the configuration to enable runtime statistics to be printed on the serial output. Finally, a configuration file has been provided to build the application with Mbed OS bare metal profile
1 parent 80f1888 commit 62c995e

File tree

7 files changed

+255
-72
lines changed

7 files changed

+255
-72
lines changed

.vscode/settings.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"files.associations": {
3+
"*.original": "c",
4+
"mbed_printf_implementation.h": "c",
5+
"mstd_iterator": "cpp",
6+
"array": "cpp",
7+
"initializer_list": "cpp",
8+
"mstd_utility": "cpp",
9+
"string_view": "cpp",
10+
"utility": "cpp",
11+
"*.tpp": "cpp",
12+
"*.tcc": "cpp",
13+
"cerrno": "cpp",
14+
"mstd_type_traits": "cpp",
15+
"mstd_functional": "cpp",
16+
"mstd_memory": "cpp",
17+
"chrono": "cpp",
18+
"functional": "cpp",
19+
"istream": "cpp",
20+
"ostream": "cpp",
21+
"ratio": "cpp",
22+
"tuple": "cpp",
23+
"type_traits": "cpp"
24+
}
25+
}

README.md

Lines changed: 84 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,90 @@
1-
# Getting started example for Mbed OS
1+
# Blinky Mbed OS Example
22

3-
This guide reviews the steps required to get Blinky with the addition of dynamic OS statistics working on an Mbed OS platform. (Note: To see a rendered example you can import into the Arm Online Compiler, please see our [quick start](https://os.mbed.com/docs/mbed-os/latest/quick-start/online-with-the-online-compiler.html#importing-the-code).)
3+
## Introduction
4+
The example shows Mbed OS features suchs as threads, thread delay, and inter-thread communication to blink an LED on supported [Mbed boards](https://os.mbed.com/platforms/). It also comes with configuration files to enable RTOS statistic and to build with [Mbed OS bare metal](https://os.mbed.com/docs/mbed-os/v5.14/reference/mbed-os-bare-metal.html) profile.
45

5-
Please install [Mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli).
6+
The project can be built with all supported [Mbed OS build tools](https://os.mbed.com/docs/mbed-os/v5.14/tools/index.html). However, this example project specifically refers to the command line interface tool [Arm Mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli).
67

7-
## Import the example application
8+
Depending on the target, the example project can be built with GCC_ARM, ARM or IAR toolchain. Run the command below after installing ARM Mbed CLI to determine which toolchain support your target.
89

9-
From the command-line, import the example:
10-
11-
```
12-
mbed import mbed-os-example-blinky
13-
cd mbed-os-example-blinky
10+
```bash
11+
$ mbed compile -S
1412
```
1513

16-
### Now compile
14+
Clone this repository on your system and change the current directory to where the project was cloned.
1715

18-
Invoke `mbed compile`, and specify the name of your platform and your favorite toolchain (`GCC_ARM`, `ARM`, `IAR`). For example, for the Arm Compiler:
16+
Select the section for the version of the application you wish to build:
17+
* [Building and Running the RTOS application](#blinky_rtos)
18+
* [Building and Running the bare metal application](#blinky_bare_metal)
1919

20-
```
21-
mbed compile -m K64F -t ARM
22-
```
20+
## <a name="blinky_rtos"></a> Building and Running the RTOS application
2321

24-
Your PC may take a few minutes to compile your code. At the end, you see the following result:
22+
1. Connect a USB cable between the USB port on the target and the host computer.
23+
2. Run the following command to build the example project and program the microcontroller flash memory:
24+
```bash
25+
$ mbed compile -m <TARGET> -t <TOOLCHAIN> --flash
26+
```
27+
The binary is located at `./BUILD/<TARGET>/<TOOLCHAIN>/mbed-os-example-blinky.bin` and can alternatively be manually copied to the target which gets mounted on the host computer via USB.
2528

26-
```
27-
[snip]
29+
### Application functionality
30+
31+
The `main()` function calls `blinky_rtos()`, as part of the main thread, which starts two threads: `thread_producer()` and `thread_consumer`. `thread_producer()` periodically send messages to `thread_consumer()` via an inter-thread queue. `thread_consumer()` verifies the message correctness and toggle the state of a digital output connected to an LED on the target.
2832
29-
Image: ./BUILD/K64F/GCC_ARM/mbed-os-example-blinky.bin
33+
### Optional RTOS runtime statistics
34+
35+
`blinky_rtos()` can optionally take a snapshot of the device's runtime statistics and display it over serial to your PC. The example project has to be re-built with modified Mbed OS configuration parameters. Modifying default Mbed OS configuration parameters can be done at application level using an application configuration file. By default ARM Mbed CLI looks for `mbed_app.json`, however the configuration file can be named anything. It can be passed to ARM Mbed CLI using the optional argument `--app-config` of the `compile` sub-command. The application configuration file `config_rtos_stats.json` has been provided to enable runtime statistics to be printed.
36+
37+
Run the following command to build the example project with runtime statistics output:
38+
```bash
39+
$ mbed compile -m <TARGET> -t <TOOLCHAIN> --app-config=config_rtos_stats.json --flash
3040
```
3141
32-
### Program your board
42+
### View the serial output
43+
44+
To view the serial output you can use any terminal client of your choosing such as [PuTTY](http://www.putty.org/) or [CoolTerm](http://freeware.the-meiers.org/). Unless otherwise specified, printf defaults to a baud rate of 9600 on Mbed OS.
3345
34-
1. Connect your Mbed device to the computer over USB.
35-
1. Copy the binary file to the Mbed device.
36-
1. Press the reset button to start the program.
46+
You can find more information on the Mbed OS configuration tools and serial communication in Mbed OS in the related [related links section](#related-links).
3747
38-
The LED on your platform turns on and off. The main thread will additionally take a snapshot of the device's runtime statistics and display it over serial to your PC. The snapshot includes:
48+
The output should contain the following block transmitted at the blinking LED frequency (actual values may vary depending on your target, build profile, and toolchain):
3949
50+
```bash
51+
=============================== SYSTEM INFO ================================
52+
Mbed OS Version: 999999
53+
CPU ID: 0x410fc241
54+
Compiler ID: 2
55+
Compiler Version: 60300
56+
RAM0: Start 0x20000000 Size: 0x30000
57+
RAM1: Start 0x1fff0000 Size: 0x10000
58+
ROM0: Start 0x0 Size: 0x100000
59+
================= CPU STATS =================
60+
Idle: 98% Usage: 2%
61+
================ HEAP STATS =================
62+
Current heap: 1096
63+
Max heap size: 1096
64+
================ THREAD STATS ===============
65+
ID: 0x20001eac
66+
Name: main_thread
67+
State: 2
68+
Priority: 24
69+
Stack Size: 4096
70+
Stack Space: 3296
71+
72+
ID: 0x20000f5c
73+
Name: idle_thread
74+
State: 1
75+
Priority: 1
76+
Stack Size: 512
77+
Stack Space: 352
78+
79+
ID: 0x20000f18
80+
Name: timer_thread
81+
State: 3
82+
Priority: 40
83+
Stack Size: 768
84+
Stack Space: 664
85+
```
86+
87+
The snapshot includes:
4088
* System Information:
4189
* Mbed OS Version: Will currently default to 999999
4290
* Compiler ID
@@ -84,66 +132,37 @@ The LED on your platform turns on and off. The main thread will additionally tak
84132
| | | 0xD21 = Cortex-M33 |
85133
|[3:0] | Revision | Minor revision: 0x1 = Patch 1 |
86134
87-
88-
89135
You can view individual examples and additional API information of the statistics collection tools at the bottom of the page in the [related links section](#related-links).
90136
91137
92-
### Output
138+
## <a name="blinky_bare_metal"></a> Building and Running the bare metal application
93139
94-
To view the serial output you can use any terminal client of your choosing such as [PuTTY](http://www.putty.org/) or [CoolTerm](http://freeware.the-meiers.org/). Unless otherwise specified, printf defaults to a baud rate of 9600 on Mbed OS.
140+
An application configuration file, `config_bare_metal.json` is provided to build a version that uses [Mbed OS bare metal](https://os.mbed.com/docs/mbed-os/v5.14/reference/mbed-os-bare-metal.html) profile to create a single threaded application.
95141
96-
You can find more information on the Mbed OS configuration tools and serial communication in Mbed OS in the related [related links section](#related-links).
97-
98-
The output should contain the following block transmitted at the blinking LED frequency (actual values may vary depending on your target, build profile, and toolchain):
142+
1. Connect a USB cable between the USB port on the target and the host computer.
143+
2. Run the following command to build the example project with runtime statistics output:
144+
```bash
145+
$ mbed compile -m <TARGET> -t <TOOLCHAIN> --app-config=config_bare_metal.json --flash
146+
```
147+
The binary is located at `./BUILD/<TARGET>/<TOOLCHAIN>/mbed-os-example-blinky.bin` and can alternatively be manually copied to the target which gets mounted on the host computer via USB.
99148
100-
```
101-
=============================== SYSTEM INFO ================================
102-
Mbed OS Version: 999999
103-
CPU ID: 0x410fc241
104-
Compiler ID: 2
105-
Compiler Version: 60300
106-
RAM0: Start 0x20000000 Size: 0x30000
107-
RAM1: Start 0x1fff0000 Size: 0x10000
108-
ROM0: Start 0x0 Size: 0x100000
109-
================= CPU STATS =================
110-
Idle: 98% Usage: 2%
111-
================ HEAP STATS =================
112-
Current heap: 1096
113-
Max heap size: 1096
114-
================ THREAD STATS ===============
115-
ID: 0x20001eac
116-
Name: main_thread
117-
State: 2
118-
Priority: 24
119-
Stack Size: 4096
120-
Stack Space: 3296
149+
`"target.default_lib" : "small"` tells the build tool to use a small version of the C standard library for the toolchain selected if available. That would be Newlib-nano and MicroLib for GCC_ARM and ARM toolchains respectively.
121150
122-
ID: 0x20000f5c
123-
Name: idle_thread
124-
State: 1
125-
Priority: 1
126-
Stack Size: 512
127-
Stack Space: 352
151+
### Application functionality
128152
129-
ID: 0x20000f18
130-
Name: timer_thread
131-
State: 3
132-
Priority: 40
133-
Stack Size: 768
134-
Stack Space: 664
153+
The `main()` function calls `blinky_bare_metal()`, as part of the single thread, which toggles the state of a digital output connected to an LED on the target.
135154
136-
```
137155
138156
## Troubleshooting
139-
140157
If you have problems, you can review the [documentation](https://os.mbed.com/docs/latest/tutorials/debugging.html) for suggestions on what could be wrong and how to fix it.
141158
142159
## Related Links
143160
144161
* [Mbed OS Stats API](https://os.mbed.com/docs/latest/apis/mbed-statistics.html)
145162
* [Mbed OS Configuration](https://os.mbed.com/docs/latest/reference/configuration.html)
146163
* [Mbed OS Serial Communication](https://os.mbed.com/docs/latest/tutorials/serial-communication.html)
164+
* [Mbed OS bare metal](https://os.mbed.com/docs/mbed-os/v5.14/reference/mbed-os-bare-metal.html)
165+
* [Mbed boards](https://os.mbed.com/platforms/)
147166
148167
### License and contributions
149168

blinky_bare_metal.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2019 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#ifdef MBED_BARE_METAL
6+
#include "mbed.h"
7+
#include "platform/mbed_thread.h"
8+
9+
10+
// Blinking rate in milliseconds
11+
#define BLINKING_RATE_MS 500
12+
13+
// Initialise the digital pin LED1 as an output
14+
DigitalOut led(LED1);
15+
16+
void blinky_bare_metal()
17+
{
18+
while (true) {
19+
led = !led;
20+
thread_sleep_for(BLINKING_RATE_MS);
21+
}
22+
}
23+
#endif // MBED_BARE_METAL

blinky_rtos.cpp

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2019 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#ifndef MBED_BARE_METAL
6+
#include "mbed.h"
7+
#include "platform/mbed_thread.h"
8+
#include "rtos/Queue.h"
9+
#include "rtos/Thread.h"
10+
#include "rtos/MemoryPool.h"
11+
12+
13+
#if MBED_SYS_STAT_REPORT
14+
#include "stats_report.h"
15+
#endif // MBED_SYS_STAT_REPORT
16+
17+
18+
// Maximum number of item in the inter-thread communication queue
19+
#define MAX_QUEUE_SIZE 1
20+
21+
// Thread priorities
22+
#define THREAD_PRIORITY_PRODUCER osPriorityAboveNormal
23+
#define THREAD_PRIORITY_CONSUMER osPriorityHigh
24+
25+
// Delay between sending messages in milliseconds
26+
#define THREAD_SLEEP_TIME_MS_PRODUCER 500
27+
28+
#if MBED_SYS_STAT_REPORT
29+
// Main thread delay in milliseconds
30+
#define THREAD_SLEEP_TIME_MS_MAIN (THREAD_SLEEP_TIME_MS_PRODUCER * 20)
31+
#endif // MBED_SYS_STAT_REPORT
32+
33+
// Message to be sent from producer thread to consumer thread
34+
#define THREAD_PRODUCER_MSG 0xDEADBEEF
35+
36+
// Fixed size memory pool to hold messages
37+
MemoryPool<unsigned long, MAX_QUEUE_SIZE> mem_pool;
38+
39+
// Queue used for inter-thread communications
40+
Queue<unsigned long, MAX_QUEUE_SIZE> queue;
41+
42+
// Producer thread handle
43+
Thread thread_producer_handle(
44+
THREAD_PRIORITY_PRODUCER,
45+
OS_STACK_SIZE,
46+
nullptr,
47+
"PRODUCER"
48+
);
49+
50+
// Consumer thread handle
51+
Thread thread_consumer_handle(
52+
THREAD_PRIORITY_CONSUMER,
53+
OS_STACK_SIZE,
54+
nullptr,
55+
"CONSUMER"
56+
);
57+
58+
// Initialise the digital pin LED1 as an output
59+
DigitalOut led(LED1);
60+
61+
62+
static void thread_consumer();
63+
static void thread_producer();
64+
65+
// The function `blinky_rtos()` runs in the main thread in the OS
66+
void blinky_rtos()
67+
{
68+
// Start execution of the functions in the threads
69+
thread_consumer_handle.start(callback(thread_consumer));
70+
thread_producer_handle.start(callback(thread_producer));
71+
72+
#if MBED_SYS_STAT_REPORT
73+
SystemReport sys_state(THREAD_SLEEP_TIME_MS_MAIN);
74+
75+
while (true) {
76+
thread_sleep_for(THREAD_SLEEP_TIME_MS_MAIN);
77+
sys_state.report_state();
78+
}
79+
#endif // MBED_SYS_STAT_REPORT
80+
}
81+
82+
83+
static void thread_consumer()
84+
{
85+
while (true) {
86+
osEvent evt = queue.get();
87+
88+
if (evt.status == osEventMessage) {
89+
unsigned long *msg = (unsigned long *)evt.value.p;
90+
91+
if (*msg == THREAD_PRODUCER_MSG) {
92+
led = !led;
93+
}
94+
95+
mem_pool.free(msg);
96+
}
97+
}
98+
}
99+
100+
static void thread_producer()
101+
{
102+
while (true) {
103+
unsigned long *msg = mem_pool.alloc();
104+
*msg = THREAD_PRODUCER_MSG;
105+
106+
queue.put(msg);
107+
108+
thread_sleep_for(THREAD_SLEEP_TIME_MS_PRODUCER);
109+
}
110+
}
111+
#endif // MBED_BARE_METAL

config_bare_metal.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"requires": ["bare-metal"],
3+
"target_overrides": {
4+
"*": {
5+
"target.default_lib" : "small",
6+
"target.macros_add" : ["MBED_BARE_METAL"]
7+
}
8+
}
9+
}

mbed_app.json renamed to config_rtos_stats.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
2+
23
"target_overrides": {
34
"*": {
5+
"target.macros_add" : ["MBED_SYS_STAT_REPORT"],
46
"platform.stack-stats-enabled": true,
57
"platform.heap-stats-enabled": true,
68
"platform.cpu-stats-enabled": true,

main.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2018 ARM Limited
2+
* Copyright (c) 2018-2019 ARM Limited
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

66
#include "mbed.h"
77
#include "platform/mbed_thread.h"
88
#include "stats_report.h"
99

10-
DigitalOut led1(LED1);
11-
12-
#define SLEEP_TIME 500 // (msec)
13-
#define PRINT_AFTER_N_LOOPS 20
14-
15-
// main() runs in its own thread in the OS
1610
int main()
1711
{
1812
SystemReport sys_state( SLEEP_TIME * PRINT_AFTER_N_LOOPS /* Loop delay time in ms */);

0 commit comments

Comments
 (0)