2020-12-07 21:39:07 +01:00
|
|
|
#include <stdint.h>
|
2020-12-14 20:29:51 +01:00
|
|
|
#include <stm32/stm32f4xx.h>
|
|
|
|
#include <cmsis/core_cm4.h>
|
2020-12-21 17:20:49 +01:00
|
|
|
#include "hex-parser.h"
|
|
|
|
#include <fatfs/ff.h>
|
2021-04-06 20:55:41 +02:00
|
|
|
|
|
|
|
/* This is used to get the defines for the external watchdog */
|
|
|
|
#include <reflow-controller/safety/safety-config.h>
|
|
|
|
#include <helper-macros/helper-macros.h>
|
|
|
|
#include <stm-periph/stm32-gpio-macros.h>
|
|
|
|
|
|
|
|
#include <reflow-controller/safety/safety-memory.h>
|
|
|
|
|
2020-12-21 17:20:49 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
static volatile unsigned int wait_tick;
|
2020-12-14 20:29:51 +01:00
|
|
|
|
|
|
|
static void watchdog_ack(void)
|
|
|
|
{
|
|
|
|
IWDG->KR = 0xAAAA;
|
|
|
|
}
|
|
|
|
|
2021-04-06 20:55:41 +02:00
|
|
|
static void external_watchdog_disable(void)
|
|
|
|
{
|
|
|
|
RCC->AHB1ENR |= SAFETY_EXT_WATCHDOG_RCC_MASK;
|
|
|
|
__DSB();
|
|
|
|
/* Set Pin to input. This disables the external watchdog. */
|
|
|
|
SAFETY_EXT_WATCHDOG_PORT->MODER &= MODER_DELETE(SAFETY_EXT_WATCHDOG_PIN);
|
|
|
|
}
|
|
|
|
|
2020-12-21 17:20:49 +01:00
|
|
|
void sdio_wait_ms(unsigned int ms)
|
|
|
|
{
|
|
|
|
wait_tick = 0;
|
|
|
|
while (wait_tick < ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
static FATFS _fs;
|
|
|
|
#define fs (&_fs)
|
|
|
|
|
|
|
|
static void __attribute__((noreturn)) ram_code_exit(bool updated)
|
|
|
|
{
|
2021-04-06 20:55:41 +02:00
|
|
|
struct safety_memory_boot_status boot_status;
|
|
|
|
safety_memory_get_boot_status(&boot_status);
|
|
|
|
boot_status.code_updated = updated ? 0xFFFFFFFFUL : 0x0UL;
|
|
|
|
boot_status.reboot_to_bootloader = 0x0UL;
|
|
|
|
safety_memory_set_boot_status(&boot_status);
|
2020-12-21 17:20:49 +01:00
|
|
|
|
|
|
|
NVIC_SystemReset();
|
|
|
|
while(1);
|
|
|
|
}
|
2020-12-07 21:39:07 +01:00
|
|
|
|
|
|
|
int ram_code_main(void)
|
|
|
|
{
|
2020-12-21 17:20:49 +01:00
|
|
|
FRESULT fres;
|
2021-04-06 20:55:41 +02:00
|
|
|
int res;
|
|
|
|
enum safety_memory_state safety_mem_state;
|
|
|
|
enum hex_parser_ret hex_ret;
|
|
|
|
struct hex_parser parser;
|
2020-12-21 17:20:49 +01:00
|
|
|
|
2020-12-14 20:29:51 +01:00
|
|
|
SysTick_Config(168000UL);
|
2021-04-06 20:55:41 +02:00
|
|
|
external_watchdog_disable();
|
2020-12-14 20:29:51 +01:00
|
|
|
__enable_irq();
|
|
|
|
|
2021-04-06 20:55:41 +02:00
|
|
|
res = safety_memory_init(&safety_mem_state);
|
|
|
|
if (res || safety_mem_state != SAFETY_MEMORY_INIT_VALID_MEMORY) {
|
|
|
|
ram_code_exit(false);
|
|
|
|
}
|
|
|
|
|
2020-12-21 17:20:49 +01:00
|
|
|
fres = f_mount(fs, "0:/", 1);
|
|
|
|
if (fres != FR_OK) {
|
|
|
|
ram_code_exit(false);
|
|
|
|
}
|
|
|
|
|
2021-04-06 20:55:41 +02:00
|
|
|
hex_ret = hex_parser_open(&parser, "update.hex");
|
|
|
|
if (hex_ret != HEX_PARSER_OK) {
|
|
|
|
ram_code_exit(false);
|
|
|
|
}
|
|
|
|
|
2020-12-14 20:29:51 +01:00
|
|
|
while(1) {
|
|
|
|
__WFI();
|
|
|
|
}
|
|
|
|
|
2020-12-07 21:39:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2020-12-14 20:29:51 +01:00
|
|
|
|
|
|
|
void SysTick_Handler(void)
|
|
|
|
{
|
|
|
|
static uint32_t tick_cnt = 0;
|
|
|
|
|
2020-12-21 17:20:49 +01:00
|
|
|
wait_tick++;
|
2020-12-14 20:29:51 +01:00
|
|
|
tick_cnt++;
|
|
|
|
watchdog_ack();
|
|
|
|
if (tick_cnt >= 250) {
|
|
|
|
GPIOB->ODR ^= (1<<2);
|
|
|
|
tick_cnt = 0;
|
|
|
|
}
|
|
|
|
}
|