reflow-oven-control-sw/stm-firmware/updater/ram-code/main.c

98 lines
1.9 KiB
C

#include <stdint.h>
#include <stm32/stm32f4xx.h>
#include <cmsis/core_cm4.h>
#include "hex-parser.h"
#include <fatfs/ff.h>
/* 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>
#include <stdbool.h>
static volatile unsigned int wait_tick;
static void watchdog_ack(void)
{
IWDG->KR = 0xAAAA;
}
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);
}
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)
{
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);
NVIC_SystemReset();
while(1);
}
int ram_code_main(void)
{
FRESULT fres;
int res;
enum safety_memory_state safety_mem_state;
enum hex_parser_ret hex_ret;
struct hex_parser parser;
SysTick_Config(168000UL);
external_watchdog_disable();
__enable_irq();
res = safety_memory_init(&safety_mem_state);
if (res || safety_mem_state != SAFETY_MEMORY_INIT_VALID_MEMORY) {
ram_code_exit(false);
}
fres = f_mount(fs, "0:/", 1);
if (fres != FR_OK) {
ram_code_exit(false);
}
hex_ret = hex_parser_open(&parser, "update.hex");
if (hex_ret != HEX_PARSER_OK) {
ram_code_exit(false);
}
while(1) {
__WFI();
}
return 0;
}
void SysTick_Handler(void)
{
static uint32_t tick_cnt = 0;
wait_tick++;
tick_cnt++;
watchdog_ack();
if (tick_cnt >= 250) {
GPIOB->ODR ^= (1<<2);
tick_cnt = 0;
}
}