Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add software method to enter bootloader #2

Merged
merged 4 commits into from
Nov 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();

}