mbed-os-examples
/
mbed-os-example-mbed5-bootloader
.
main.cpp@14:9476f399341e, 2017-06-19 (annotated)
- Committer:
- mbed_official
- Date:
- Mon Jun 19 22:00:16 2017 +0100
- Revision:
- 14:9476f399341e
- Parent:
- 11:d396becb5d76
- Child:
- 24:8235ee7fff3d
Distinguish app and bootloader binaries. Use app config.
This attempts to clarify a few vague points in the README. It helps to
distinguish between the different binaries created during the bootloader
building process.
It takes care of the somewhat confusing '/sd/' mount path for the
SD card. This is now handled by the program, allowing the user to just
specify the name of the binary that is placed in the root of the SD card.
It makes more use of app config to support onboard SD card slots
for the K64F and the UBLOX_EVK_ODIN_W2. It will use the Arduino SPI pins
for any other target.
The application update binary name is now specified in the app config as
well.
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-bootloader
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:8df79c088b12 | 1 | #include "mbed.h" |
mbed_official | 0:8df79c088b12 | 2 | #include "SDBlockDevice.h" |
mbed_official | 0:8df79c088b12 | 3 | #include "FATFileSystem.h" |
mbed_official | 0:8df79c088b12 | 4 | |
mbed_official | 14:9476f399341e | 5 | #define SD_MOUNT_PATH "sd" |
mbed_official | 14:9476f399341e | 6 | #define FULL_UPDATE_FILE_PATH "/" SD_MOUNT_PATH "/" MBED_CONF_APP_UPDATE_FILE |
mbed_official | 14:9476f399341e | 7 | |
mbed_official | 14:9476f399341e | 8 | //Pin order: MOSI, MISO, SCK, CS |
mbed_official | 14:9476f399341e | 9 | SDBlockDevice sd(MBED_CONF_APP_SD_CARD_MOSI, MBED_CONF_APP_SD_CARD_MISO, |
mbed_official | 14:9476f399341e | 10 | MBED_CONF_APP_SD_CARD_SCK, MBED_CONF_APP_SD_CARD_CS); |
mbed_official | 14:9476f399341e | 11 | FATFileSystem fs(SD_MOUNT_PATH); |
mbed_official | 0:8df79c088b12 | 12 | FlashIAP flash; |
mbed_official | 0:8df79c088b12 | 13 | |
mbed_official | 0:8df79c088b12 | 14 | void apply_update(FILE *file, uint32_t address); |
mbed_official | 0:8df79c088b12 | 15 | |
mbed_official | 0:8df79c088b12 | 16 | int main() |
mbed_official | 0:8df79c088b12 | 17 | { |
mbed_official | 0:8df79c088b12 | 18 | sd.init(); |
mbed_official | 0:8df79c088b12 | 19 | fs.mount(&sd); |
mbed_official | 0:8df79c088b12 | 20 | |
mbed_official | 14:9476f399341e | 21 | FILE *file = fopen(FULL_UPDATE_FILE_PATH, "rb"); |
mbed_official | 0:8df79c088b12 | 22 | if (file != NULL) { |
mbed_official | 0:8df79c088b12 | 23 | printf("Firmware update found\r\n"); |
mbed_official | 0:8df79c088b12 | 24 | |
mbed_official | 0:8df79c088b12 | 25 | apply_update(file, POST_APPLICATION_ADDR); |
mbed_official | 0:8df79c088b12 | 26 | |
mbed_official | 0:8df79c088b12 | 27 | fclose(file); |
mbed_official | 14:9476f399341e | 28 | remove(FULL_UPDATE_FILE_PATH); |
mbed_official | 0:8df79c088b12 | 29 | } else { |
mbed_official | 0:8df79c088b12 | 30 | printf("No update found to apply\r\n"); |
mbed_official | 0:8df79c088b12 | 31 | } |
mbed_official | 0:8df79c088b12 | 32 | |
mbed_official | 0:8df79c088b12 | 33 | fs.unmount(); |
mbed_official | 0:8df79c088b12 | 34 | sd.deinit(); |
mbed_official | 0:8df79c088b12 | 35 | |
mbed_official | 0:8df79c088b12 | 36 | printf("Starting application\r\n"); |
mbed_official | 0:8df79c088b12 | 37 | |
mbed_official | 0:8df79c088b12 | 38 | mbed_start_application(POST_APPLICATION_ADDR); |
mbed_official | 0:8df79c088b12 | 39 | } |
mbed_official | 0:8df79c088b12 | 40 | |
mbed_official | 0:8df79c088b12 | 41 | void apply_update(FILE *file, uint32_t address) |
mbed_official | 0:8df79c088b12 | 42 | { |
mbed_official | 0:8df79c088b12 | 43 | flash.init(); |
mbed_official | 0:8df79c088b12 | 44 | |
mbed_official | 0:8df79c088b12 | 45 | const uint32_t page_size = flash.get_page_size(); |
mbed_official | 0:8df79c088b12 | 46 | char *page_buffer = new char[page_size]; |
mbed_official | 0:8df79c088b12 | 47 | uint32_t addr = address; |
mbed_official | 0:8df79c088b12 | 48 | uint32_t next_sector = addr + flash.get_sector_size(addr); |
mbed_official | 0:8df79c088b12 | 49 | bool sector_erased = false; |
mbed_official | 0:8df79c088b12 | 50 | while (true) { |
mbed_official | 0:8df79c088b12 | 51 | |
mbed_official | 0:8df79c088b12 | 52 | // Read data for this page |
mbed_official | 0:8df79c088b12 | 53 | memset(page_buffer, 0, sizeof(page_buffer)); |
mbed_official | 0:8df79c088b12 | 54 | int size_read = fread(page_buffer, 1, page_size, file); |
mbed_official | 0:8df79c088b12 | 55 | if (size_read <= 0) { |
mbed_official | 0:8df79c088b12 | 56 | break; |
mbed_official | 0:8df79c088b12 | 57 | } |
mbed_official | 0:8df79c088b12 | 58 | |
mbed_official | 0:8df79c088b12 | 59 | // Erase this page if it hasn't been erased |
mbed_official | 0:8df79c088b12 | 60 | if (!sector_erased) { |
mbed_official | 0:8df79c088b12 | 61 | flash.erase(addr, flash.get_sector_size(addr)); |
mbed_official | 0:8df79c088b12 | 62 | sector_erased = true; |
mbed_official | 0:8df79c088b12 | 63 | } |
mbed_official | 0:8df79c088b12 | 64 | |
mbed_official | 0:8df79c088b12 | 65 | // Program page |
mbed_official | 0:8df79c088b12 | 66 | flash.program(page_buffer, addr, page_size); |
mbed_official | 0:8df79c088b12 | 67 | |
mbed_official | 0:8df79c088b12 | 68 | addr += page_size; |
mbed_official | 0:8df79c088b12 | 69 | if (addr >= next_sector) { |
mbed_official | 0:8df79c088b12 | 70 | next_sector = addr + flash.get_sector_size(addr); |
mbed_official | 0:8df79c088b12 | 71 | sector_erased = false; |
mbed_official | 0:8df79c088b12 | 72 | |
mbed_official | 0:8df79c088b12 | 73 | } |
mbed_official | 0:8df79c088b12 | 74 | } |
mbed_official | 0:8df79c088b12 | 75 | delete[] page_buffer; |
mbed_official | 0:8df79c088b12 | 76 | |
mbed_official | 0:8df79c088b12 | 77 | flash.deinit(); |
mbed_official | 0:8df79c088b12 | 78 | } |