Skip to content

Commit fb82678

Browse files
authored
Update blinky to use MCUboot with changes from Mbed OS team (#1)
* Update Mbed OS to the latest 6.4.0 release * Remove unused files * Temporary: use LDong-Arm's fork of MCUboot * Update the example
1 parent f7505f8 commit fb82678

File tree

5 files changed

+160
-23
lines changed

5 files changed

+160
-23
lines changed

main.cpp

Lines changed: 109 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
1-
/* mbed Microcontroller Library
2-
* Copyright (c) 2019 ARM Limited
1+
/*
2+
* Copyright (c) 2020 Embedded Planet
3+
* Copyright (c) 2020 ARM Limited
34
* SPDX-License-Identifier: Apache-2.0
45
*/
56

67
#include "mbed.h"
78

8-
#include "bootutil.h"
9+
#include "bootutil/bootutil.h"
10+
#include "FlashIAP/FlashIAPBlockDevice.h"
11+
#include "drivers/InterruptIn.h"
912

10-
// Change which LED and how fast we blink it for update binary
11-
#ifndef MCUBOOT_UPDATE
12-
#define LED LED1
13-
#define BLINKING_RATE 1000ms
14-
#else
15-
#define LED LED2
16-
#define BLINKING_RATE 250ms
17-
#endif
13+
#define TRACE_GROUP "main"
14+
#include "mbed-trace/mbed_trace.h"
15+
16+
mbed::BlockDevice* get_secondary_bd(void) {
17+
mbed::BlockDevice* default_bd = mbed::BlockDevice::get_default_instance();
18+
static mbed::SlicingBlockDevice sliced_bd(default_bd, 0x0, MCUBOOT_SLOT_SIZE);
19+
return &sliced_bd;
20+
}
21+
22+
const static char version[] = "1.0";
1823

1924
int main()
2025
{
26+
// Enable traces from relevant trace groups
27+
mbed_trace_init();
28+
mbed_trace_include_filters_set("main,MCUb,BL");
2129

2230
/**
2331
* Do whatever is needed to verify the firmware is okay
@@ -26,13 +34,97 @@ int main()
2634
* And then mark that the update succeeded
2735
*/
2836
//run_self_test();
29-
boot_set_confirmed();
37+
int ret = boot_set_confirmed();
38+
if (ret == 0) {
39+
tr_info("Boot confirmed");
40+
} else {
41+
tr_error("Failed to confirm boot: %d", ret);
42+
}
43+
44+
InterruptIn btn(DEMO_BUTTON);
45+
46+
tr_info("Hello version %s", version);
47+
48+
// Erase secondary slot
49+
// On the first boot, the secondary BlockDevice needs to be clean
50+
// If the first boot is not normal, please run the erase step, then reboot
3051

31-
// Initialise the digital pin LED1 as an output
32-
DigitalOut led(LED);
52+
tr_info("> Press button to erase secondary slot");
53+
54+
#if DEMO_BUTTON_ACTIVE_LOW
55+
while (btn) {
56+
#else
57+
while (!btn) {
58+
#endif
59+
sleep();
60+
}
61+
62+
BlockDevice *secondary_bd = get_secondary_bd();
63+
ret = secondary_bd->init();
64+
if (ret == 0) {
65+
tr_info("Secondary BlockDevice inited");
66+
} else {
67+
tr_error("Cannot init secondary BlockDevice: %d", ret);
68+
}
69+
70+
tr_info("Erasing secondary BlockDevice...");
71+
ret = secondary_bd->erase(0, secondary_bd->size());
72+
if (ret == 0) {
73+
tr_info("Secondary BlockDevice erased");
74+
} else {
75+
tr_error("Cannot erase secondary BlockDevice: %d", ret);
76+
}
77+
78+
tr_info("> Press button to copy update image to secondary BlockDevice");
79+
80+
#if DEMO_BUTTON_ACTIVE_LOW
81+
while (btn) {
82+
#else
83+
while (!btn) {
84+
#endif
85+
sleep();
86+
}
87+
88+
// Copy the update image from internal flash to secondary BlockDevice
89+
// This is a "hack" that requires you to preload the update image (i.e. with pyocd flash -a <address> <image>.bin)
90+
// TODO: Use Serial OTA to fetch image
91+
92+
FlashIAPBlockDevice fbd(MCUBOOT_PRIMARY_SLOT_START_ADDR + 0x40000, 0x20000);
93+
ret = fbd.init();
94+
if (ret == 0) {
95+
tr_info("FlashIAPBlockDevice inited");
96+
} else {
97+
tr_error("Cannot init FlashIAPBlockDevice: %d", ret);
98+
}
99+
100+
static uint8_t buffer[0x1000];
101+
for (size_t offset = 0; offset < 0x20000; offset+= sizeof(buffer)) {
102+
ret = fbd.read(buffer, offset, sizeof(buffer));
103+
if (ret != 0) {
104+
tr_error("Failed to read FlashIAPBlockDevice at offset %u", offset);
105+
}
106+
ret = secondary_bd->program(buffer, offset, sizeof(buffer));
107+
if (ret != 0) {
108+
tr_error("Failed to program secondary BlockDevice at offset %u", offset);
109+
}
110+
}
111+
112+
// Activate the image in the secondary BlockDevice
113+
114+
tr_info("> Image copied to secondary BlockDevice, press button to activate");
115+
116+
#if DEMO_BUTTON_ACTIVE_LOW
117+
while (btn) {
118+
#else
119+
while (!btn) {
120+
#endif
121+
sleep();
122+
}
33123

34-
while (true) {
35-
led = !led;
36-
ThisThread::sleep_for(BLINKING_RATE);
124+
ret = boot_set_pending(false);
125+
if (ret == 0) {
126+
tr_info("> Secondary image pending, reboot to update");
127+
} else {
128+
tr_error("Failed to set secondary image pending: %d", ret);
37129
}
38130
}

mbed-os.lib

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
https://github.com/ARMmbed/mbed-os/#e4b81f67f939a0c0b11c147ce74aa367271e1279
1+
https://github.com/ARMmbed/mbed-os/#8ef0a435b2356f8159dea8e427b2935d177309f8

mbed_app.json

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,60 @@
11
{
2+
"config": {
3+
"demo-button": {
4+
"macro_name": "DEMO_BUTTON",
5+
"required": true
6+
},
7+
"demo-button-active-low": {
8+
"help": "true if the button state is low when pressed, high when released",
9+
"macro_name": "DEMO_BUTTON_ACTIVE_LOW",
10+
"required": false
11+
}
12+
},
213
"target_overrides": {
314
"*": {
415
"mcuboot.bootloader-build": 0,
5-
"target.mbed_app_start": "0x20000",
6-
"target.mbed_app_size": "0xDC000"
16+
"target.c_lib": "small",
17+
"mcuboot.log-level": "MCUBOOT_LOG_LEVEL_DEBUG",
18+
"mbed-trace.enable": true
719
},
820
"NRF52840_DK": {
9-
"mcuboot.max-img-sectors": 256
21+
"demo-button": "BUTTON1",
22+
"demo-button-active-low": true,
23+
"target.mbed_app_start": "0x21000",
24+
"target.mbed_app_size": "0xBE000",
25+
"mcuboot.primary-slot-address": "0x20000",
26+
"mcuboot.slot-size": "0xC0000",
27+
"mcuboot.scratch-address": "0xE0000",
28+
"mcuboot.scratch-size": "0x20000",
29+
"mcuboot.max-img-sectors": "0x180",
30+
"mcuboot.read-granularity": 4,
31+
"qspif.QSPI_MIN_PROG_SIZE": 4
1032
},
1133
"EP_AGORA": {
12-
"mcuboot.max-img-sectors": 256
34+
"demo-button": "BUTTON1",
35+
"demo-button-active-low": true,
36+
"target.mbed_app_start": "0x21000",
37+
"target.mbed_app_size": "0xBE000",
38+
"mcuboot.primary-slot-address": "0x20000",
39+
"mcuboot.slot-size": "0xC0000",
40+
"mcuboot.scratch-address": "0xE0000",
41+
"mcuboot.scratch-size": "0x20000",
42+
"mcuboot.max-img-sectors": "0x180",
43+
"mcuboot.read-granularity": 4,
44+
"qspif.QSPI_MIN_PROG_SIZE": 4
45+
},
46+
"DISCO_L475VG_IOT01A": {
47+
"demo-button": "USER_BUTTON",
48+
"demo-button-active-low": true,
49+
"target.mbed_app_start": "0x8021000",
50+
"target.mbed_app_size": "0xBE000",
51+
"mcuboot.primary-slot-address": "0x8020000",
52+
"mcuboot.slot-size": "0xC0000",
53+
"mcuboot.scratch-address": "0x80E0000",
54+
"mcuboot.scratch-size": "0x20000",
55+
"mcuboot.max-img-sectors": "0x180",
56+
"mcuboot.read-granularity": 4,
57+
"qspif.QSPI_MIN_PROG_SIZE": 4
1358
}
1459
}
1560
}

mcuboot.lib

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
https://github.com/AGlass0fMilk/mcuboot/#aff9bd34ceb8e4fb1f0697045e2f09764e738844
1+
https://github.com/LDong-Arm/mcuboot.git/#03b472cb1800196a69e4878b5cdd7b529320d7d6
-8.06 KB
Binary file not shown.

0 commit comments

Comments
 (0)