Files
templates/c/set-protocol/ports/stm32g474-usb-boot/boot_request.c

32 lines
1021 B
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* Передача запроса входа в загрузчик через backup-состояние и отложенный сброс. Флаг должен
* пережить перезапуск, чтобы загрузчик отличил обновление от обычного старта приложения.
*/
#include "boot_port.h"
#include "stm32g4xx_hal.h"
#define REQUEST_MAGIC 0x42555342UL
static uint32_t reset_at;
static bool reset_pending;
static void backup_access(void)
{
__HAL_RCC_PWR_CLK_ENABLE();
HAL_PWR_EnableBkUpAccess();
__HAL_RCC_RTCAPB_CLK_ENABLE();
}
bool g474_boot_requested(void)
{
bool requested;
backup_access(); requested = TAMP->BKP0R == REQUEST_MAGIC;
TAMP->BKP0R = 0; return requested;
}
void g474_request_boot(uint32_t now)
{
backup_access(); TAMP->BKP0R = REQUEST_MAGIC;
reset_at = now + 500; reset_pending = true;
}
void g474_reset_poll(uint32_t now)
{
if (reset_pending && (int32_t)(now - reset_at) >= 0) NVIC_SystemReset();
}