2020-12-02 15:57:22 -08:00
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
|
2020-12-09 00:24:06 -08:00
|
|
|
#include "flash.h"
|
|
|
|
|
2020-12-02 15:57:22 -08:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
// These are defined in the linker script
|
|
|
|
extern uint32_t __appflash_start__;
|
2020-12-09 00:24:06 -08:00
|
|
|
extern uint32_t __appflash_size__;
|
2020-12-02 15:57:22 -08:00
|
|
|
extern uint32_t __ram_vectors_start__;
|
2020-12-09 00:24:06 -08:00
|
|
|
extern uint32_t __ram_vectors_size__;
|
2020-12-02 15:57:22 -08:00
|
|
|
|
|
|
|
__attribute__((noreturn))
|
|
|
|
void boot_app() {
|
|
|
|
// Goodbye, ChibiOS
|
|
|
|
chSysDisable();
|
|
|
|
|
|
|
|
// Reset peripherals we might have used
|
|
|
|
rccDisableCAN1();
|
|
|
|
|
2020-12-02 15:59:13 -08:00
|
|
|
const uint32_t* appFlash = &__appflash_start__;
|
2020-12-02 15:57:22 -08:00
|
|
|
|
|
|
|
// copy vector table to sram
|
|
|
|
// TODO: use __ram_vectors_size__
|
2020-12-02 15:59:13 -08:00
|
|
|
memcpy(reinterpret_cast<char*>(&__ram_vectors_start__), appFlash, 256);
|
2020-12-02 15:57:22 -08:00
|
|
|
|
|
|
|
// The reset vector is at offset 4 (second uint32)
|
|
|
|
uint32_t reset_vector = appFlash[1];
|
|
|
|
|
|
|
|
// switch to use vectors in ram
|
|
|
|
SYSCFG->CFGR1 |= 3;
|
|
|
|
|
|
|
|
// TODO: is this necessary?
|
|
|
|
//uint32_t app_msp = appLocation[0];
|
|
|
|
//__set_MSP(app_msp);
|
|
|
|
|
2020-12-02 21:10:53 -08:00
|
|
|
typedef void (*ResetVectorFunction)(void);
|
|
|
|
((ResetVectorFunction)reset_vector)();
|
2020-12-02 15:57:22 -08:00
|
|
|
}
|
|
|
|
|
2020-12-09 00:24:06 -08:00
|
|
|
void EraseAppPages()
|
|
|
|
{
|
|
|
|
uint32_t appFlashAddr = (uint32_t)&__appflash_start__;
|
|
|
|
uintptr_t blSize = (uintptr_t)(appFlashAddr - 0x08000000);
|
|
|
|
size_t pageIdx = blSize / 1024;
|
|
|
|
|
|
|
|
size_t appSizeKb = __appflash_size__ / 1024;
|
|
|
|
|
|
|
|
for (int i = 0; i <= appSizeKb; i++)
|
|
|
|
{
|
|
|
|
Flash::ErasePage(pageIdx);
|
|
|
|
pageIdx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 15:57:22 -08:00
|
|
|
/*
|
|
|
|
* Application entry point.
|
|
|
|
*/
|
|
|
|
int main(void) {
|
|
|
|
halInit();
|
|
|
|
chSysInit();
|
|
|
|
|
|
|
|
palSetPadMode(GPIOB, 3, PAL_MODE_OUTPUT_PUSHPULL);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 20; i++)
|
|
|
|
{
|
|
|
|
palTogglePad(GPIOB, 3);
|
2020-12-02 21:15:22 -08:00
|
|
|
chThdSleepMilliseconds(40);
|
2020-12-02 15:57:22 -08:00
|
|
|
}
|
|
|
|
|
2020-12-09 00:24:06 -08:00
|
|
|
//EraseAppPages();
|
|
|
|
|
2020-12-02 15:57:22 -08:00
|
|
|
boot_app();
|
|
|
|
}
|