Skip to content

Commit 8374704

Browse files
authored
Add software method to enter bootloader (#2)
1 parent f00ff1f commit 8374704

File tree

1 file changed

+50
-16
lines changed

1 file changed

+50
-16
lines changed

src/main.c

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,26 @@
66
* (RC5) is grounded, the device will stay in bootloader mode even if an
77
* application exists. While in bootloader mode, a new application can be
88
* flashed with the Unified Bootloader Host Application, or a similar tool.
9-
*
9+
*
1010
* From PSLab V6 revision onwards, there is a push button attached to BOOT pin
1111
* with a label `BOOT`. Holding this button down at power up or when clicking on
12-
* `Read Device Settings` in Unified Bootloader application will switch from
12+
* `Read Device Settings` in Unified Bootloader application will switch from
1313
* main application to bootloader mode where one can flash a new firmware.
1414
*/
15+
#include <stdbool.h>
16+
#include <stdint.h>
1517

1618
#include "mcc_generated_files/system.h"
1719
#include "mcc_generated_files/boot/boot_process.h"
1820
#include "mcc_generated_files/pin_manager.h"
21+
#include "mcc_generated_files/uart1.h"
1922

2023
#include "delay.h"
2124
#include "rgb_led.h"
2225

26+
bool received_magic_number(void);
27+
bool is_boot_btn_pressed(void);
28+
2329
int main(void) {
2430
// Initialize the device.
2531
SYSTEM_Initialize();
@@ -33,25 +39,53 @@ int main(void) {
3339
DELAY_ms(200);
3440
Light_RGB(32, 8, 16);
3541

36-
BOOT_PIN_SetDigitalOutput();
37-
BOOT_PIN_SetHigh();
38-
DELAY_us(1000); // Wait for BOOT to go high.
39-
42+
// If no application is detected in program area, stay in boot.
43+
bool const app_detected = BOOT_Verify();
4044

41-
// If BOOT is grounded or no application is detected, stay in bootloader.
42-
if (BOOT_PIN_GetValue() && BOOT_Verify()) {
45+
if (app_detected && !is_boot_btn_pressed() && !received_magic_number()) {
4346
BOOT_StartApplication();
44-
} else {
45-
uint16_t i = 0;
47+
}
4648

47-
while (1) {
48-
// Monitor serial bus for commands, e.g. flashing new application.
49-
BOOT_ProcessCommand();
49+
for (uint16_t i = 0; 1; ++i) {
50+
// Monitor serial bus for commands, e.g. flashing new application.
51+
BOOT_ProcessCommand();
5052

51-
// Blink system LED while in bootloader mode.
52-
if (!i++) STATUS_LED_Toggle();
53-
}
53+
// Blink system LED while in bootloader mode.
54+
if (!i) STATUS_LED_Toggle();
5455
}
5556

5657
return 1;
5758
}
59+
60+
/**
61+
* @brief After reset, host may send magic number stay in bootloader mode.
62+
*
63+
* @return bool
64+
*/
65+
bool received_magic_number(void) {
66+
uint8_t magic[4] = {0xAD, 0xFB, 0xCA, 0xDE};
67+
bool match = true;
68+
69+
for (uint8_t i = 0; i < 4; ++i) {
70+
if (UART1_IsRxReady()) {
71+
match = match && (magic[i] == UART1_Read());
72+
} else {
73+
match = false;
74+
}
75+
}
76+
77+
return match;
78+
}
79+
80+
/**
81+
* @brief If the BOOT button is pressed, stay in bootloader mode.
82+
*
83+
* @return bool
84+
*/
85+
bool is_boot_btn_pressed(void) {
86+
BOOT_PIN_SetDigitalOutput();
87+
BOOT_PIN_SetHigh();
88+
DELAY_us(1000); // Wait for BOOT to go high.
89+
return !BOOT_PIN_GetValue();
90+
91+
}

0 commit comments

Comments
 (0)