mbed-os-examples
/
mbed-os-example-mbed5-bootloader
.
Revision 0:8df79c088b12, committed 2017-03-09
- Comitter:
- mbed_official
- Date:
- Thu Mar 09 17:44:16 2017 +0000
- Child:
- 1:1702e76d6fbc
- Commit message:
- Initial commit.
Commit copied from https://github.com/ARMmbed/mbed-os-example-bootloader
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.gitignore Thu Mar 09 17:44:16 2017 +0000 @@ -0,0 +1,4 @@ +.build +.mbed +projectfiles +*.py*
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Thu Mar 09 17:44:16 2017 +0000 @@ -0,0 +1,93 @@ +# Getting started with bootloader on mbed OS + +This example shows how to create a bootloader. For steps on how to create an application which uses this bootloader, see [mbed-os-example-bootloader-blinky](https://github.com/ARMmbed/mbed-os-example-bootloader-blinky). + +## Required hardware +* A supported board - [u-blox EVK-ODIN-W2](https://developer.mbed.org/platforms/ublox-EVK-ODIN-W2/), [Nucleo F429ZI](https://developer.mbed.org/platforms/ST-Nucleo-F429ZI/) or [K64F](https://developer.mbed.org/platforms/FRDM-K64F/). +* CI Test shield. +* SD card. + +## Import the example application + +From the command-line, import the example: + +``` +mbed import mbed-os-example-bootloader-blinky +cd mbed-os-example-bootloader-blinky +``` + +## Set up application to be a bootloader + +All supported boards mentioned above are set up to build as a bootloader image. To add support for a new board, you must specify the size of the bootloader. +To do this, set the target value `restrict_size` to the maximum bootloader size in mbed_app.json: + +``` + "target_overrides": { + ... + "NUCLEO_F429ZI": { + "target.restrict_size": "0x20000" + }, + ... +``` + +Note - `restrict_size` pads the build image to the requested size. + + +## Now compile + +Invoke `mbed compile`, and specify the name of your platform and your favorite toolchain (`GCC_ARM`, `ARM`, `IAR`). For example, for the ARM Compiler 5: + +``` +mbed compile -m NUCLEO_F429ZI -t ARM +``` + +Your PC may take a few minutes to compile your code. At the end, you see the following result: + +``` +Merging Regions: + Filling region application with .\BUILD\NUCLEO_F429ZI\ARM\mbed-os-example-bootloader_application.bin + Padding region application with 0x11420 bytes +Space used after regions merged: 0x20000 ++-----------------------------+-------+-------+-------+ +| Module | .text | .data | .bss | ++-----------------------------+-------+-------+-------+ +| Misc | 18481 | 24 | 1268 | +| drivers | 2132 | 8 | 184 | +| features/FEATURE_LWIP | 46 | 0 | 12560 | +| features/filesystem | 12756 | 12 | 540 | +| hal | 414 | 8 | 0 | +| hal/TARGET_FLASH_CMSIS_ALGO | 520 | 28 | 0 | +| platform | 2759 | 32 | 380 | +| rtos | 144 | 8 | 0 | +| rtos/rtx | 6954 | 100 | 8396 | +| targets/TARGET_STM | 15560 | 568 | 736 | +| Subtotals | 59766 | 788 | 24064 | ++-----------------------------+-------+-------+-------+ +Allocated Heap: unknown +Allocated Stack: unknown +Total Static RAM memory (data + bss): 24852 bytes +Total RAM memory (data + bss + heap + stack): 24852 bytes +Total Flash memory (text + data + misc): 60554 bytes + +Image: .\BUILD\NUCLEO_F429ZI\ARM\mbed-os-example-bootloader.bin +``` + +## Next steps + +When the build succeeds, you have created a bootloader for your target. The next step is to build an application you can combine with your bootloader to create a loadable image. You can find a blinky application that uses this bootloader in the [mbed-os-example-bootloader-blinky](https://github.com/ARMmbed/mbed-os-example-bootloader-blinky) project. + +## Troubleshooting + +1. Make sure `mbed-cli` is working correctly and its version is `>1.0.0`. + + ``` + mbed --version + ``` + + If not, you can update it: + + ``` + pip install mbed-cli --upgrade + ``` + +2. If using Keil MDK, make sure you have a license installed. [MDK-Lite](http://www.keil.com/arm/mdk.asp) has a 32 KB restriction on code size. \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Mar 09 17:44:16 2017 +0000 @@ -0,0 +1,75 @@ +#include "mbed.h" +#include "SDBlockDevice.h" +#include "FATFileSystem.h" + +#define UPDATE_FILE "/sd/mbed-os-example-bootloader-blinky_application.bin" + +SDBlockDevice sd(D11, D12, D13, D10); +FATFileSystem fs("sd"); +FlashIAP flash; + +void apply_update(FILE *file, uint32_t address); + +int main() +{ + sd.init(); + fs.mount(&sd); + + FILE *file = fopen(UPDATE_FILE, "rb"); + if (file != NULL) { + printf("Firmware update found\r\n"); + + apply_update(file, POST_APPLICATION_ADDR); + + fclose(file); + remove(UPDATE_FILE); + } else { + printf("No update found to apply\r\n"); + } + + fs.unmount(); + sd.deinit(); + + printf("Starting application\r\n"); + + mbed_start_application(POST_APPLICATION_ADDR); +} + +void apply_update(FILE *file, uint32_t address) +{ + flash.init(); + + const uint32_t page_size = flash.get_page_size(); + char *page_buffer = new char[page_size]; + uint32_t addr = address; + uint32_t next_sector = addr + flash.get_sector_size(addr); + bool sector_erased = false; + while (true) { + + // Read data for this page + memset(page_buffer, 0, sizeof(page_buffer)); + int size_read = fread(page_buffer, 1, page_size, file); + if (size_read <= 0) { + break; + } + + // Erase this page if it hasn't been erased + if (!sector_erased) { + flash.erase(addr, flash.get_sector_size(addr)); + sector_erased = true; + } + + // Program page + flash.program(page_buffer, addr, page_size); + + addr += page_size; + if (addr >= next_sector) { + next_sector = addr + flash.get_sector_size(addr); + sector_erased = false; + + } + } + delete[] page_buffer; + + flash.deinit(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Thu Mar 09 17:44:16 2017 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#305f5c491e34d86098e9da91b322017549c189ff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_app.json Thu Mar 09 17:44:16 2017 +0000 @@ -0,0 +1,13 @@ +{ + "target_overrides": { + "K64F": { + "target.restrict_size": "0x20000" + }, + "NUCLEO_F429ZI": { + "target.restrict_size": "0x20000" + }, + "UBLOX_EVK_ODIN_W2": { + "target.restrict_size": "0x20000" + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver.lib Thu Mar 09 17:44:16 2017 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/sd-driver/#7e223bdaeb6a047b2d87e3eefe7a8864753bae4a