essai

Committer:
gr91
Date:
Sun Nov 15 20:00:37 2020 +0100
Revision:
1:f2d4c846046a
xxx

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gr91 1:f2d4c846046a 1 ![](./resources/official_armmbed_example_badge.png)
gr91 1:f2d4c846046a 2 ![](./resources/official_armmbed_example_badge.png)
gr91 1:f2d4c846046a 3 # Bare metal blinky Mbed OS example
gr91 1:f2d4c846046a 4
gr91 1:f2d4c846046a 5 This example shows how to achieve memory optimizations in Mbed OS. Starting with a blinky application, the example illustrates how to enable the bare metal profile and further memory optimizations.
gr91 1:f2d4c846046a 6
gr91 1:f2d4c846046a 7 You can build this project with all supported [Mbed OS build tools](https://os.mbed.com/docs/mbed-os/latest/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).
gr91 1:f2d4c846046a 8
gr91 1:f2d4c846046a 9 1. Install Mbed CLI.
gr91 1:f2d4c846046a 10 1. From the command-line, import the example: `mbed import mbed-os-example-blinky-baremetal`
gr91 1:f2d4c846046a 11 1. Change the current directory to where the project was imported.
gr91 1:f2d4c846046a 12
gr91 1:f2d4c846046a 13 ## Application functionality
gr91 1:f2d4c846046a 14
gr91 1:f2d4c846046a 15 The `main()` function toggles the state of a digital output connected to an LED on the board.
gr91 1:f2d4c846046a 16
gr91 1:f2d4c846046a 17 ## Building and running
gr91 1:f2d4c846046a 18
gr91 1:f2d4c846046a 19 1. Connect a USB cable between the USB port on the target and the host computer.
gr91 1:f2d4c846046a 20 1. Run the following command to build the example project, program the microcontroller flash memory, and open a serial terminal:
gr91 1:f2d4c846046a 21
gr91 1:f2d4c846046a 22 ```
gr91 1:f2d4c846046a 23 $ mbed compile -m <TARGET> -t <TOOLCHAIN> --flash --sterm
gr91 1:f2d4c846046a 24 ```
gr91 1:f2d4c846046a 25
gr91 1:f2d4c846046a 26
gr91 1:f2d4c846046a 27 Your PC may take a few minutes to compile your code.
gr91 1:f2d4c846046a 28
gr91 1:f2d4c846046a 29 The binary is located at `./BUILD/<TARGET>/<TOOLCHAIN>/mbed-os-example-blinky-baremetal.bin`.
gr91 1:f2d4c846046a 30
gr91 1:f2d4c846046a 31 Alternatively, you can manually copy the binary to the target, which gets mounted on the host computer through USB.
gr91 1:f2d4c846046a 32
gr91 1:f2d4c846046a 33 Depending on the target, you can build the example project with the `GCC_ARM`, `ARM` or `IAR` toolchain. After installing Arm Mbed CLI, run the command below to determine which toolchain supports your target:
gr91 1:f2d4c846046a 34
gr91 1:f2d4c846046a 35 ```
gr91 1:f2d4c846046a 36 $ mbed compile -S
gr91 1:f2d4c846046a 37 ```
gr91 1:f2d4c846046a 38
gr91 1:f2d4c846046a 39 ## Expected output
gr91 1:f2d4c846046a 40
gr91 1:f2d4c846046a 41 The LED on your target turns on and off every 500 milliseconds, and the serial terminal shows an output similar to:
gr91 1:f2d4c846046a 42
gr91 1:f2d4c846046a 43 ```
gr91 1:f2d4c846046a 44 --- Terminal on /dev/tty.usbmodem21102 - 9600,8,N,1 ---
gr91 1:f2d4c846046a 45 This is the bare metal blinky example running on Mbed OS 99.99.99.
gr91 1:f2d4c846046a 46 ```
gr91 1:f2d4c846046a 47
gr91 1:f2d4c846046a 48 ## Configuring the application
gr91 1:f2d4c846046a 49
gr91 1:f2d4c846046a 50 ### The bare metal profile
gr91 1:f2d4c846046a 51
gr91 1:f2d4c846046a 52 The bare metal profile is a configuration of Mbed OS that excludes the RTOS, as well as other features. We designed it specifically for ultraconstrained devices because it gives you more control over the system. For more details, please see [the bare metal documentation](https://os.mbed.com/docs/mbed-os/latest/reference/mbed-os-bare-metal.html)
gr91 1:f2d4c846046a 53
gr91 1:f2d4c846046a 54 To build with the bare metal profile, the application configuration file must contain:
gr91 1:f2d4c846046a 55
gr91 1:f2d4c846046a 56 ```json
gr91 1:f2d4c846046a 57 {
gr91 1:f2d4c846046a 58 "requires": ["bare-metal"]
gr91 1:f2d4c846046a 59 }
gr91 1:f2d4c846046a 60 ```
gr91 1:f2d4c846046a 61
gr91 1:f2d4c846046a 62 ### Futher optimizations
gr91 1:f2d4c846046a 63 Some of the configurations shown below are already set by default in `targets/targets.json` and `platform/mbed_lib.json`.
gr91 1:f2d4c846046a 64 #### Linking with smaller C libraries
gr91 1:f2d4c846046a 65
gr91 1:f2d4c846046a 66 Both the `ARM` and `GCC_ARM` toolchains support optimized versions of their C standard libraries, microlib and newlib-nano. We recommend using them with the bare metal profile.
gr91 1:f2d4c846046a 67
gr91 1:f2d4c846046a 68 To build with the smaller C libraries, modify the application configuration file:
gr91 1:f2d4c846046a 69
gr91 1:f2d4c846046a 70 ```json
gr91 1:f2d4c846046a 71 {
gr91 1:f2d4c846046a 72 "target_overrides": {
gr91 1:f2d4c846046a 73 "*": {
gr91 1:f2d4c846046a 74 "target.c_lib": "small"
gr91 1:f2d4c846046a 75 }
gr91 1:f2d4c846046a 76 }
gr91 1:f2d4c846046a 77 }
gr91 1:f2d4c846046a 78 ```
gr91 1:f2d4c846046a 79
gr91 1:f2d4c846046a 80 The build system reverts to the standard C library if support for the small C library is not enabled for your target. You can find more information [here]( https://github.com/ARMmbed/mbed-os-5-docs/blob/development/docs/program-setup/bare_metal/c_small_libs.md).
gr91 1:f2d4c846046a 81
gr91 1:f2d4c846046a 82 #### Using Mbed minimal printf library
gr91 1:f2d4c846046a 83
gr91 1:f2d4c846046a 84 Mbed OS offers a smaller `printf()` alternative. The [minimal printf](https://github.com/ARMmbed/mbed-os/blob/master/platform/source/minimal-printf/README.md) library implements a subset of the `v/s/f/printf` function family, and you can disable floating points to further reduce code size.
gr91 1:f2d4c846046a 85
gr91 1:f2d4c846046a 86 To build with the minimal printf library and disable floating points printing, you need to modify the application configuration file:
gr91 1:f2d4c846046a 87
gr91 1:f2d4c846046a 88 ```json
gr91 1:f2d4c846046a 89 {
gr91 1:f2d4c846046a 90 "target_overrides": {
gr91 1:f2d4c846046a 91 "*": {
gr91 1:f2d4c846046a 92 "target.printf_lib": "minimal-printf",
gr91 1:f2d4c846046a 93 "platform.minimal-printf-enable-floating-point": false
gr91 1:f2d4c846046a 94 }
gr91 1:f2d4c846046a 95 }
gr91 1:f2d4c846046a 96 }
gr91 1:f2d4c846046a 97 ```
gr91 1:f2d4c846046a 98
gr91 1:f2d4c846046a 99 Further optimizations are possible. For more details, please see the minimal printf README.
gr91 1:f2d4c846046a 100
gr91 1:f2d4c846046a 101 #### Using a minimal console
gr91 1:f2d4c846046a 102
gr91 1:f2d4c846046a 103 If your application only needs unbuffered I/O operations, you can save additional memory by using a configuration of the platform library, which removes file handling functionality from the [system I/O retarget code](https://github.com/ARMmbed/mbed-os/blob/master/platform/source/mbed_retarget.cpp).
gr91 1:f2d4c846046a 104
gr91 1:f2d4c846046a 105 To build with the minimal console functionality, modify the application configuration file:
gr91 1:f2d4c846046a 106
gr91 1:f2d4c846046a 107 ```json
gr91 1:f2d4c846046a 108 {
gr91 1:f2d4c846046a 109 "target_overrides": {
gr91 1:f2d4c846046a 110 "*": {
gr91 1:f2d4c846046a 111 "platform.stdio-minimal-console-only": true
gr91 1:f2d4c846046a 112 }
gr91 1:f2d4c846046a 113 }
gr91 1:f2d4c846046a 114 }
gr91 1:f2d4c846046a 115 ```
gr91 1:f2d4c846046a 116
gr91 1:f2d4c846046a 117 #### Memory comparison
gr91 1:f2d4c846046a 118
gr91 1:f2d4c846046a 119 The below table shows the result for the blinky bare metal application compiled with the release profile on K64F for the GCC_ARM toolchain.
gr91 1:f2d4c846046a 120
gr91 1:f2d4c846046a 121 The baseline configuration used is the blinky bare metal application built with the standard C library.
gr91 1:f2d4c846046a 122
gr91 1:f2d4c846046a 123 Mbed OS release: mbed-os-6.0.0-alpha-2
gr91 1:f2d4c846046a 124
gr91 1:f2d4c846046a 125 |Standard C lib|Small C lib|Minimal printf|Minimal console|RAM|Flash|
gr91 1:f2d4c846046a 126 | :---: | :---: | :---: | :---: | :---: | :---: |
gr91 1:f2d4c846046a 127 | X | | | | 0 | 0 |
gr91 1:f2d4c846046a 128 | | X | | | -2,592 | -28,581 |
gr91 1:f2d4c846046a 129 | | X | X | | -2,592 | -29,918 |
gr91 1:f2d4c846046a 130 | | X | X | X | -2,592 | -30,810 |
gr91 1:f2d4c846046a 131
gr91 1:f2d4c846046a 132 ## Troubleshooting
gr91 1:f2d4c846046a 133
gr91 1:f2d4c846046a 134 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.
gr91 1:f2d4c846046a 135
gr91 1:f2d4c846046a 136 ## Related links
gr91 1:f2d4c846046a 137
gr91 1:f2d4c846046a 138 - [Mbed OS bare metal](https://os.mbed.com/docs/mbed-os/latest/reference/mbed-os-bare-metal.html).
gr91 1:f2d4c846046a 139 - [Mbed OS configuration](https://os.mbed.com/docs/latest/reference/configuration.html).
gr91 1:f2d4c846046a 140 - [Mbed OS serial communication](https://os.mbed.com/docs/latest/tutorials/serial-communication.html).
gr91 1:f2d4c846046a 141 - [Mbed boards](https://os.mbed.com/platforms/).
gr91 1:f2d4c846046a 142
gr91 1:f2d4c846046a 143 ### License and contributions
gr91 1:f2d4c846046a 144
gr91 1:f2d4c846046a 145 The software is provided under the Apache-2.0 license. Contributions to this project are accepted under the same license. Please see contributing.md for more information.
gr91 1:f2d4c846046a 146
gr91 1:f2d4c846046a 147 This project contains code from other projects. The original license text is included in those source files. They must comply with our license guide.