Skip to content

Commit

Permalink
Add software method to enter bootloader (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
bessman authored Nov 15, 2023
1 parent f00ff1f commit 8374704
Showing 1 changed file with 50 additions and 16 deletions.
66 changes: 50 additions & 16 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@
* (RC5) is grounded, the device will stay in bootloader mode even if an
* application exists. While in bootloader mode, a new application can be
* flashed with the Unified Bootloader Host Application, or a similar tool.
*
*
* From PSLab V6 revision onwards, there is a push button attached to BOOT pin
* with a label `BOOT`. Holding this button down at power up or when clicking on
* `Read Device Settings` in Unified Bootloader application will switch from
* `Read Device Settings` in Unified Bootloader application will switch from
* main application to bootloader mode where one can flash a new firmware.
*/
#include <stdbool.h>
#include <stdint.h>

#include "mcc_generated_files/system.h"
#include "mcc_generated_files/boot/boot_process.h"
#include "mcc_generated_files/pin_manager.h"
#include "mcc_generated_files/uart1.h"

#include "delay.h"
#include "rgb_led.h"

bool received_magic_number(void);
bool is_boot_btn_pressed(void);

int main(void) {
// Initialize the device.
SYSTEM_Initialize();
Expand All @@ -33,25 +39,53 @@ int main(void) {
DELAY_ms(200);
Light_RGB(32, 8, 16);

BOOT_PIN_SetDigitalOutput();
BOOT_PIN_SetHigh();
DELAY_us(1000); // Wait for BOOT to go high.

// If no application is detected in program area, stay in boot.
bool const app_detected = BOOT_Verify();

// If BOOT is grounded or no application is detected, stay in bootloader.
if (BOOT_PIN_GetValue() && BOOT_Verify()) {
if (app_detected && !is_boot_btn_pressed() && !received_magic_number()) {
BOOT_StartApplication();
} else {
uint16_t i = 0;
}

while (1) {
// Monitor serial bus for commands, e.g. flashing new application.
BOOT_ProcessCommand();
for (uint16_t i = 0; 1; ++i) {
// Monitor serial bus for commands, e.g. flashing new application.
BOOT_ProcessCommand();

// Blink system LED while in bootloader mode.
if (!i++) STATUS_LED_Toggle();
}
// Blink system LED while in bootloader mode.
if (!i) STATUS_LED_Toggle();
}

return 1;
}

/**
* @brief After reset, host may send magic number stay in bootloader mode.
*
* @return bool
*/
bool received_magic_number(void) {
uint8_t magic[4] = {0xAD, 0xFB, 0xCA, 0xDE};
bool match = true;

for (uint8_t i = 0; i < 4; ++i) {
if (UART1_IsRxReady()) {
match = match && (magic[i] == UART1_Read());
} else {
match = false;
}
}

return match;
}

/**
* @brief If the BOOT button is pressed, stay in bootloader mode.
*
* @return bool
*/
bool is_boot_btn_pressed(void) {
BOOT_PIN_SetDigitalOutput();
BOOT_PIN_SetHigh();
DELAY_us(1000); // Wait for BOOT to go high.
return !BOOT_PIN_GetValue();

}

0 comments on commit 8374704

Please sign in to comment.