40 lines
1.6 KiB
C
40 lines
1.6 KiB
C
#include "stm32g4xx_hal.h"
|
|
#include "boot_port.h"
|
|
#include "usb_cdc.h"
|
|
static volatile uint32_t clock_fault;
|
|
void SysTick_Handler(void) { HAL_IncTick(); }
|
|
static void fatal(uint32_t code) { clock_fault = code; for (;;) {} }
|
|
static void usb_clock_init(void)
|
|
{
|
|
RCC_OscInitTypeDef osc = {0};
|
|
RCC_PeriphCLKInitTypeDef peripheral = {0};
|
|
RCC_CRSInitTypeDef crs = {0};
|
|
/* CPU stays on reset HSI16. USB uses HSI48 disciplined by USB SOF. */
|
|
osc.OscillatorType = RCC_OSCILLATORTYPE_HSI48;
|
|
osc.HSI48State = RCC_HSI48_ON;
|
|
if (HAL_RCC_OscConfig(&osc) != HAL_OK) fatal(2);
|
|
peripheral.PeriphClockSelection = RCC_PERIPHCLK_USB;
|
|
peripheral.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
|
|
if (HAL_RCCEx_PeriphCLKConfig(&peripheral) != HAL_OK) fatal(3);
|
|
__HAL_RCC_CRS_CLK_ENABLE();
|
|
crs.Prescaler = RCC_CRS_SYNC_DIV1;
|
|
crs.Source = RCC_CRS_SYNC_SOURCE_USB;
|
|
crs.Polarity = RCC_CRS_SYNC_POLARITY_RISING;
|
|
crs.ReloadValue = __HAL_RCC_CRS_RELOADVALUE_CALCULATE(48000000U, 1000U);
|
|
crs.ErrorLimitValue = RCC_CRS_ERRORLIMIT_DEFAULT;
|
|
crs.HSI48CalibrationValue = RCC_CRS_HSI48CALIBRATION_DEFAULT;
|
|
HAL_RCCEx_CRSConfig(&crs);
|
|
}
|
|
void boot_protocol_init(void);
|
|
void boot_protocol_poll(uint32_t now);
|
|
int main(void)
|
|
{
|
|
bool requested;
|
|
SCB->VTOR = FLASH_BASE;
|
|
requested = g474_boot_requested();
|
|
if (!requested && g474_app_valid()) g474_jump_app();
|
|
if (HAL_Init() != HAL_OK) fatal(1);
|
|
usb_clock_init(); boot_protocol_init(); usb_cdc_init();
|
|
for (;;) { uint32_t now = HAL_GetTick(); usb_cdc_poll(now); boot_protocol_poll(now); }
|
|
}
|