Skip to content

Commit acdb35f

Browse files
committed
Add application variants and Mbed OS bare metal tutorial
The example project has been re-written to include variants of an application to blink an LED. The variants include a simple variant that uses a delay within a loop and a more complex that creates threads and shows inter-thread communication. As the simpler variant is single-threaded, it showcases that some of Mbed OS APIs can be used both with and without rtos (using Mbed OS bare metal profile). Instructions are also provided to build the simple blinky application with Mbed OS bare metal profile. Runtime statistics has been removed and is only mentioned in the README. The reader is invited to find out more. Runtime statistics is a topic on its own and should not be implemented here. The example project also exposes the user to Mbed OS parameter configuration option.
1 parent 1cff977 commit acdb35f

File tree

8 files changed

+76
-180
lines changed

8 files changed

+76
-180
lines changed

README.md

Lines changed: 33 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Blinky Mbed OS Example
22

33
## 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.
4+
The example project contains variants of an application to blink an LED on supported [Mbed boards](https://os.mbed.com/platforms/).
55

66
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).
77

@@ -13,11 +13,20 @@ $ mbed compile -S
1313

1414
Clone this repository on your system and change the current directory to where the project was cloned.
1515

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)
16+
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`](https://os.mbed.com/docs/mbed-os/v5.14/reference/configuration.html), 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. This project comes with configuration files for the different variants and configurations of the blinky application.
1917

20-
## <a name="blinky_rtos"></a> Building and Running the RTOS application
18+
Select the section for the variant of the application you wish to build:
19+
* [Blinky Wait application](#blinky_wait)
20+
* [Bare metal](#blinky_wait_bare_metal)
21+
* [Blinky multi-threaded application](#blinky_multi_threaded)
22+
23+
## <a name="blinky_wait"></a> Blinky Wait application
24+
25+
### Application functionality
26+
27+
The `main()` function calls `blinky_wait()`, as part of the single thread, which toggles the state of a digital output connected to an LED on the target.
28+
29+
### <a name="build_blinky_wait_rtos"></a> Building and Running
2130

2231
1. Connect a USB cable between the USB port on the target and the host computer.
2332
2. Run the following command to build the example project and program the microcontroller flash memory:
@@ -26,131 +35,40 @@ Select the section for the version of the application you wish to build:
2635
```
2736
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.
2837

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.
32-
33-
### Optional RTOS runtime statistics
38+
### <a name="blinky_wait_bare_metal"></a> Bare metal
39+
The variant of the application [above](#build_blinky_wait_rtos) is built with the full Mbed OS library including its RTOS components. However, for single-threaded applications running on targets with ultraconstrains, it is possible to obtain an application with an even smaller memory footprint using [Mbed OS bare metal](https://os.mbed.com/docs/mbed-os/v5.14/reference/mbed-os-bare-metal.html) profile.
40+
An application configuration file, [`config_blinky_wait_bare_metal.json`](./blinky_wait/config_blinky_wait_bare_metal.json) is provided to build a binary with Mbed OS bare metal profile.
3441

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.
42+
1. Connect a USB cable between the USB port on the target and the host computer.
43+
2. Run the following command to build the example project with runtime statistics output:
44+
```bash
45+
$ mbed compile -m <TARGET> -t <TOOLCHAIN> --app-config=./blinky_wait/config_blinky_bare_metal.json --flash
46+
```
47+
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.
3648

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
40-
```
49+
`"target.default_lib" : "small"` tells the build tool to use a small implementation 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.
4150

42-
### View the serial output
4351

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.
52+
## <a name="blinky_multi_threaded"></a> Blinky multi-threaded application
4553

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).
54+
This variant shows Mbed OS features suchs as threads, thread delay, and inter-thread communication to blink an LED.
4755

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):
56+
### Application functionality
4957

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-
```
58+
The `main()` function calls `blinky_multithreaded()`, 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 toggles the state of a digital output connected to an LED on the target.
8659
87-
The snapshot includes:
88-
* System Information:
89-
* Mbed OS Version: Will currently default to 999999
90-
* Compiler ID
91-
* ARM = 1
92-
* GCC_ARM = 2
93-
* IAR = 3
94-
* [CPUID Register Information](#cpuid-register-information)
95-
* [Compiler Version](#compiler-version)
96-
* CPU Statistics
97-
* Percentage of runtime that the device has spent awake versus in sleep
98-
* Heap Statistics
99-
* Current heap size
100-
* Max heap size which refers to the largest the heap has grown to
101-
* Thread Statistics
102-
* Provides information on all running threads in the OS including
103-
* Thread ID
104-
* Thread Name
105-
* Thread State
106-
* Thread Priority
107-
* Thread Stack Size
108-
* Thread Stack Space
109-
110-
#### Compiler Version
111-
112-
| Compiler | Version Layout |
113-
| -------- | -------------- |
114-
| ARM | PVVbbbb (P = Major; VV = Minor; bbbb = build number) |
115-
| GCC | VVRRPP (VV = Version; RR = Revision; PP = Patch) |
116-
| IAR | VRRRPPP (V = Version; RRR = Revision; PPP = Patch) |
117-
118-
#### CPUID Register Information
119-
120-
| Bit Field | Field Description | Values |
121-
| --------- | ----------------- | ------ |
122-
|[31:24] | Implementer | 0x41 = ARM |
123-
|[23:20] | Variant | Major revision 0x0 = Revision 0 |
124-
|[19:16] | Architecture | 0xC = Baseline Architecture |
125-
| | | 0xF = Constant (Mainline Architecture) |
126-
|[15:4] | Part Number | 0xC20 = Cortex-M0 |
127-
| | | 0xC60 = Cortex-M0+ |
128-
| | | 0xC23 = Cortex-M3 |
129-
| | | 0xC24 = Cortex-M4 |
130-
| | | 0xC27 = Cortex-M7 |
131-
| | | 0xD20 = Cortex-M23 |
132-
| | | 0xD21 = Cortex-M33 |
133-
|[3:0] | Revision | Minor revision: 0x1 = Patch 1 |
134-
135-
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).
136-
137-
138-
## <a name="blinky_bare_metal"></a> Building and Running the bare metal application
139-
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.
60+
### Building and Running
14161
14262
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:
63+
2. Run the following command to build the example project and program the microcontroller flash memory:
14464
```bash
145-
$ mbed compile -m <TARGET> -t <TOOLCHAIN> --app-config=config_bare_metal.json --flash
65+
$ mbed compile -m <TARGET> -t <TOOLCHAIN> --flash --app-config=./blinky_multithreaded/config_blinky_multithreaded.json
14666
```
14767
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.
14868

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.
150-
151-
### Application functionality
69+
### Optional RTOS runtime statistics
15270

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.
71+
It is possible to take a snapshot of the device's runtime statistics and display it over serial to your PC. See how [here](https://os.mbed.com/docs/latest/apis/mbed-statistics.html).
15472
15573
15674
## Troubleshooting

blinky_rtos.cpp renamed to blinky_multithreaded/blinky_multithreaded.cpp

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,28 @@
22
* Copyright (c) 2019 ARM Limited
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5-
#ifndef MBED_BARE_METAL
5+
#ifdef BLINKY_MULTITHREADED
6+
67
#include "mbed.h"
78
#include "platform/mbed_thread.h"
89
#include "rtos/Queue.h"
910
#include "rtos/Thread.h"
1011
#include "rtos/MemoryPool.h"
1112

1213

13-
#if MBED_SYS_STAT_REPORT
14-
#include "stats_report.h"
15-
#endif // MBED_SYS_STAT_REPORT
16-
17-
1814
// Maximum number of item in the inter-thread communication queue
1915
#define MAX_QUEUE_SIZE 1
2016

2117
// Thread priorities
22-
#define THREAD_PRIORITY_PRODUCER osPriorityAboveNormal
23-
#define THREAD_PRIORITY_CONSUMER osPriorityHigh
18+
#define THREAD_PRIORITY_PRODUCER osPriorityAboveNormal
19+
#define THREAD_PRIORITY_CONSUMER osPriorityHigh
2420

2521
// 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
22+
#define THREAD_SLEEP_TIME_MS_PRODUCER 500
3223

3324
// Message to be sent from producer thread to consumer thread
34-
#define THREAD_PRODUCER_MSG 0xDEADBEEF
25+
#define THREAD_PRODUCER_MSG 0xDEADBEEF
26+
3527

3628
// Fixed size memory pool to hold messages
3729
MemoryPool<unsigned long, MAX_QUEUE_SIZE> mem_pool;
@@ -55,33 +47,25 @@ Thread thread_consumer_handle(
5547
"CONSUMER"
5648
);
5749

58-
// Initialise the digital pin LED1 as an output
59-
DigitalOut led(LED1);
60-
6150

6251
static void thread_consumer();
6352
static void thread_producer();
6453

65-
// The function `blinky_rtos()` runs in the main thread in the OS
66-
void blinky_rtos()
54+
55+
// The function `blinky_multithreaded()` runs in the main thread in the OS
56+
void blinky_multithreaded()
6757
{
6858
// Start execution of the functions in the threads
6959
thread_consumer_handle.start(callback(thread_consumer));
7060
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
8061
}
8162

8263

8364
static void thread_consumer()
8465
{
66+
// Initialise the digital pin LED1 as an output
67+
DigitalOut led(LED1);
68+
8569
while (true) {
8670
osEvent evt = queue.get();
8771

@@ -108,4 +92,5 @@ static void thread_producer()
10892
thread_sleep_for(THREAD_SLEEP_TIME_MS_PRODUCER);
10993
}
11094
}
111-
#endif // MBED_BARE_METAL
95+
96+
#endif // BLINKY_MULTITHREADED
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"target_overrides": {
3+
"*": {
4+
"target.macros_add" : ["BLINKY_MULTITHREADED"]
5+
}
6+
}
7+
}

blinky_bare_metal.cpp renamed to blinky_wait/blinky_wait.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22
* Copyright (c) 2019 ARM Limited
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5-
#ifdef MBED_BARE_METAL
5+
66
#include "mbed.h"
77
#include "platform/mbed_thread.h"
88

99

1010
// Blinking rate in milliseconds
1111
#define BLINKING_RATE_MS 500
1212

13-
// Initialise the digital pin LED1 as an output
14-
DigitalOut led(LED1);
1513

16-
void blinky_bare_metal()
14+
void blinky_wait()
1715
{
16+
// Initialise the digital pin LED1 as an output
17+
DigitalOut led(LED1);
18+
1819
while (true) {
1920
led = !led;
2021
thread_sleep_for(BLINKING_RATE_MS);
2122
}
2223
}
23-
#endif // MBED_BARE_METAL
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"requires": ["bare-metal"],
3+
"target_overrides": {
4+
"*": {
5+
"target.default_lib" : "small"
6+
}
7+
}
8+
}

config_bare_metal.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

config_rtos_stats.json

Lines changed: 0 additions & 13 deletions
This file was deleted.

main.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
#if !MBED_BARE_METAL
7-
extern void blinky_rtos();
6+
#ifdef BLINKY_MULTITHREADED
7+
extern void blinky_multithreaded();
88
#else
9-
extern void blinky_bare_metal();
10-
#endif // !MBED_BARE_METAL
9+
extern void blinky_wait();
10+
#endif
1111

1212
int main()
1313
{
14-
#ifndef MBED_BARE_METAL
15-
blinky_rtos();
14+
#ifdef BLINKY_MULTITHREADED
15+
blinky_multithreaded();
1616
#else
17-
blinky_bare_metal();
18-
#endif // !MBED_BARE_METAL
17+
blinky_wait();
18+
#endif
1919
}

0 commit comments

Comments
 (0)