Skip to content

Commit 04bc942

Browse files
LDong-Armiriark01
andauthored
Tutorial on creating a bare metal application (#1305)
* Add guide for using bare metal profile * Bare metal porting: stress that compatible Greentea tests are all enabled * mbed2_porting: explain Greentea errors due to small memory size * mbed2_porting: JSON syntax highlighting * Remove baremetal_example.md whose purpose duplicates using_bare_metal.md * Bare metal guide improved * Bare metal guide: events library path relative to application root * Apply suggestions from @iriark01 Co-authored-by: Irit Arkin <[email protected]> * Incorporate the latest review comments * Fomatting Co-authored-by: Irit Arkin <[email protected]>
1 parent 7c25ae9 commit 04bc942

File tree

4 files changed

+120
-29
lines changed

4 files changed

+120
-29
lines changed

docs.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@
7070
"sources": [{
7171
"path": "docs/bare_metal/bare_metal.md"
7272
},
73-
{
74-
"path": "docs/bare_metal/baremetal_example.md"
75-
},
7673
{
7774
"path": "docs/bare_metal/using_bare_metal.md"
7875
},

docs/bare_metal/baremetal_example.md

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

docs/bare_metal/mbed2_porting.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ Configure your target to support Mbed OS 6:
3434
3535
1. Remove the `release_versions` property; it is no longer required:
3636
37-
```
37+
```json
3838
"release_versions": ["2"]
3939
```
4040
4141
1. To indicate that the application profile supported by this target is bare metal, add the `supported_application_profiles` property:
4242
43-
```
43+
```json
4444
"supported_application_profiles" : ["bare-metal"]
4545
```
4646
4747
1. Override `supported_c_libs` property to link with the smaller C libraries. The default for all targets is defined as follows:
4848
49-
```
49+
```json
5050
"supported_c_libs": {
5151
"arm": ["std"],
5252
"gcc_arm": ["std", "small"],
@@ -56,7 +56,7 @@ Configure your target to support Mbed OS 6:
5656
5757
Both the ARM and GCC_ARM toolchains support optimized versions of the C standard libraries - microlib and newlib-nano, respectively. We recommend using them with the bare metal profile for lower memory footprints. Ultraconstrained targets should override `supported_c_libs`:
5858
59-
```
59+
```json
6060
"supported_c_libs": {
6161
"arm": ["small"],
6262
"gcc_arm": ["small"]
@@ -67,7 +67,7 @@ Configure your target to support Mbed OS 6:
6767
6868
For each toolchain, if there is enough memory to link with the standard library, add the corresponding `std` library to the list. For example:
6969
70-
```
70+
```json
7171
"supported_c_libs": {
7272
"arm": ["std", "small"],
7373
"gcc_arm": ["std", "small"],
@@ -92,7 +92,7 @@ Configure your target to support Mbed OS 6:
9292
9393
1. If your board does not have a low power ticker, ensure that tickless is enabled using the microsecond ticker:
9494
95-
```
95+
```json
9696
"overrides": {
9797
"tickless-from-us-ticker": true
9898
}
@@ -102,7 +102,7 @@ Configure your target to support Mbed OS 6:
102102
103103
The stack size is configured by setting a value for the `boot-stack-size` attribute; this value must be a multiple of 8 for alignment purposes. We recommend that you reduce the boot stack size to 0x400 (1,024 bytes) if your target has 8KB of RAM and to 0x300 (768 bytes) if your target has 4KB of RAM.
104104
105-
```
105+
```json
106106
"overrides": {
107107
"boot-stack-size": "0x400"
108108
}
@@ -122,7 +122,7 @@ Troubleshoot any issue.
122122
123123
## Validating the port
124124
125-
To validate the bare metal target configuration, execute the Mbed OS Greentea test suite with the bare metal profile. This profile causes Greentea to skip a subset of the tests, either because the underlying functionality has not been ported to bare metal or because some tests require RTOS features (for examples, more complex tests based on multi-threading).
125+
To validate the bare metal target configuration, execute the Mbed OS Greentea test suite with the bare metal profile. This profile causes Greentea to skip a subset of the tests, either because the underlying functionality has not been ported to bare metal or because some tests require RTOS features (for examples, more complex tests based on multi-threading). It performs all the tests compatible with bare metal.
126126
127127
If you haven't used Greentea before, [you can learn more in our documentation](../tools/greentea-testing-applications.html).
128128
@@ -140,15 +140,27 @@ If you haven't used Greentea before, [you can learn more in our documentation](.
140140
141141
<span class="tips">**Tip:** You can append `--compile` and fix build issues before running tests with `--run`.</span>
142142
143-
1. All tests should pass (or be automatically skipped).
143+
1. All tests should pass (or be automatically skipped), unless the target being ported is ultraconstrained (with 32KB or less of flash memory) - in which case linking may fail for _a few_ tests. For example:
144+
145+
ARM toolchain:
146+
```
147+
Error: L6407E: Sections of aggregate size 0x318 bytes could not fit into .ANY selector(s).
148+
```
149+
150+
GCC_ARM toolchain:
151+
```
152+
region `FLASH' overflowed by 792 bytes
153+
```
154+
155+
Please ignore tests with similar errors.
144156
145157
Further optimisations for targets with small flash memories:
146158
- Append `--profile release` to the command above. Use of the release profile helps keep some tests within the size limit.
147159
- Save additional memory by using a [minimal console](https://github.com/ARMmbed/mbed-os-example-blinky-baremetal#using-a-minimal-console) to remove file handling functionality from the system I/O retarget code.
148160
149161
Modify `TESTS/configs/baremetal.json` for your target:
150162
151-
```
163+
```json
152164
{
153165
"target_overrides": {
154166
"YOUR_TARGET": {

docs/bare_metal/using_bare_metal.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,100 @@
11
# Using the bare metal profile
22

3-
Content still in PR
3+
This guide shows how to create a bare metal profile application:
4+
5+
1. Set the profile: By default, the build tool uses the full profile for all application builds. To use the bare metal profile, set up your application to override this default behaviour.
6+
1. By default, the bare metal profile uses a minimal set of APIs. You can add additional ones [from the list of supported APIs](../bare-metal/index.html#features) if your application needs them.
7+
8+
To demonstrate how to create a bare metal application, here is an example that prints text at regular intervals using the `EventQueue` class:
9+
10+
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_RTOS/EventQueue_ex_2/)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_RTOS/EventQueue_ex_2/main.cpp)
11+
12+
## Creating a bare metal application
13+
14+
To create the application:
15+
16+
1. Create a new Mbed OS application and navigate to its directory:
17+
18+
```
19+
mbed new example_app && cd example_app
20+
```
21+
22+
The directory contains the full Mbed OS library (`mbed-os/`) and no application files.
23+
24+
1. Create a `main.cpp` file containing the `EventQueue` code snippet above.
25+
26+
1. Open `mbed_app.json` (in the root of the application) and replace it with the following content:
27+
28+
```json
29+
{
30+
"requires": ["bare-metal"],
31+
"target_overrides": {
32+
"*": {
33+
"target.c_lib": "small"
34+
}
35+
}
36+
}
37+
```
38+
39+
The file specifies which profile to use (`"requires": ["bare-metal"]`) and which C library to use (`"target.c_lib": "small"`).
40+
41+
In this example, we're using `"target.c_lib": "small"` (small C library). This means your application will use an optimised version of the C library with lower memory footprint. For more details, see [Using small C libraries in Mbed OS bare metal](../bare-metal/using-small-c-libraries.html).
42+
43+
You now have application code and a bare metal profile with the default APIs. However, this example uses APIs that are not part of the default bare metal profile - you need to manually add support for those APIs to the application.
44+
45+
## Adding APIs
46+
47+
Bare metal has a minimal set of default APIs - those that are always available to a bare metal application. You can add other supported APIs if you need the features they enable.
48+
49+
For a list of default and supported APIs, [please see our full API list](../apis/index.html).
50+
51+
This example depends on the `EventQueue` class, so you need to add the library that contains that class:
52+
53+
1. In `mbed-os/`, locate the API and the library in which it is declared.
54+
1. In the library folder, open `mbed_lib.json` and find the library's name. You will need it for the next step.
55+
56+
For example: `mbed-os/events/mbed_lib.json`:
57+
```json
58+
{
59+
"name": "events",
60+
"config": {
61+
"present": 1,
62+
...
63+
}
64+
}
65+
```
66+
To continue, go back to the application's root directory.
67+
68+
1. Open `mbed_app.json` again, and add the library to the `"requires"` field:
69+
70+
```json
71+
{
72+
"requires": ["bare-metal", "events"],
73+
"target_overrides": {
74+
"*": {
75+
"target.c_lib": "small"
76+
}
77+
}
78+
}
79+
```
80+
81+
## Compiling and Running the Application
82+
83+
Connect a supported board to your computer, and compile and run your application:
84+
85+
```
86+
mbed compile -t <TOOLCHAIN> -m <TARGET> --flash --sterm
87+
```
88+
89+
When the example is flashed, a serial terminal opens (because of `--sterm`). The outputis:
90+
91+
```
92+
called immediately
93+
called every 1 seconds
94+
called in 2 seconds
95+
called every 1 seconds
96+
called every 1 seconds
97+
called every 1 seconds
98+
```
99+
100+
To exit the serial terminal, press <kbd>Ctrl</kbd> + <kbd>C</kbd>.

0 commit comments

Comments
 (0)