6
6
* (RC5) is grounded, the device will stay in bootloader mode even if an
7
7
* application exists. While in bootloader mode, a new application can be
8
8
* flashed with the Unified Bootloader Host Application, or a similar tool.
9
- *
9
+ *
10
10
* From PSLab V6 revision onwards, there is a push button attached to BOOT pin
11
11
* 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
13
13
* main application to bootloader mode where one can flash a new firmware.
14
14
*/
15
+ #include <stdbool.h>
16
+ #include <stdint.h>
15
17
16
18
#include "mcc_generated_files/system.h"
17
19
#include "mcc_generated_files/boot/boot_process.h"
18
20
#include "mcc_generated_files/pin_manager.h"
21
+ #include "mcc_generated_files/uart1.h"
19
22
20
23
#include "delay.h"
21
24
#include "rgb_led.h"
22
25
26
+ bool received_magic_number (void );
27
+ bool is_boot_btn_pressed (void );
28
+
23
29
int main (void ) {
24
30
// Initialize the device.
25
31
SYSTEM_Initialize ();
@@ -33,25 +39,53 @@ int main(void) {
33
39
DELAY_ms (200 );
34
40
Light_RGB (32 , 8 , 16 );
35
41
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 ();
40
44
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 ()) {
43
46
BOOT_StartApplication ();
44
- } else {
45
- uint16_t i = 0 ;
47
+ }
46
48
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 ();
50
52
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 ();
54
55
}
55
56
56
57
return 1 ;
57
58
}
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