stm32: Reorganize usb bootloader code in stm32f0.c

Reorganize stm32f0.c into major code blocks.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2021-12-18 19:58:29 -05:00
parent e55011cde8
commit e14dca0f06
1 changed files with 65 additions and 38 deletions

View File

@ -1,6 +1,6 @@
// Code to setup clocks on stm32f0
//
// Copyright (C) 2019 Kevin O'Connor <kevin@koconnor.net>
// Copyright (C) 2019-2021 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
@ -11,6 +11,11 @@
#include "internal.h" // enable_pclock
#include "sched.h" // sched_main
/****************************************************************
* Clock setup
****************************************************************/
#define FREQ_PERIPH 48000000
// Enable a peripheral clock
@ -64,34 +69,6 @@ gpio_clock_enable(GPIO_TypeDef *regs)
RCC->AHBENR;
}
#define USB_BOOT_FLAG_ADDR (CONFIG_RAM_START + CONFIG_RAM_SIZE - 1024)
#define USB_BOOT_FLAG 0x55534220424f4f54 // "USB BOOT"
// Handle USB reboot requests
void
usb_request_bootloader(void)
{
irq_disable();
*(uint64_t*)USB_BOOT_FLAG_ADDR = USB_BOOT_FLAG;
NVIC_SystemReset();
}
// Copy vector table and remap ram so new vector table is used
static void
enable_ram_vectortable(void)
{
// Symbols created by armcm_link.lds.S linker script
extern uint32_t _ram_vectortable_start, _ram_vectortable_end;
extern uint32_t _text_vectortable_start;
uint32_t count = (&_ram_vectortable_end - &_ram_vectortable_start) * 4;
__builtin_memcpy(&_ram_vectortable_start, &_text_vectortable_start, count);
barrier();
enable_pclock(SYSCFG_BASE);
SYSCFG->CFGR1 |= 3 << SYSCFG_CFGR1_MEM_MODE_Pos;
}
#if !CONFIG_STM32_CLOCK_REF_INTERNAL
DECL_CONSTANT_STR("RESERVE_PINS_crystal", "PF0,PF1");
#endif
@ -169,20 +146,70 @@ hsi14_setup(void)
;
}
// Main entry point - called from armcm_boot.c:ResetHandler()
void
armcm_main(void)
/****************************************************************
* USB bootloader
****************************************************************/
#define USB_BOOT_FLAG_ADDR (CONFIG_RAM_START + CONFIG_RAM_SIZE - 1024)
#define USB_BOOT_FLAG 0x55534220424f4f54 // "USB BOOT"
// Flag that bootloader is desired and reboot
static void
usb_reboot_for_dfu_bootloader(void)
{
if (CONFIG_USBSERIAL && CONFIG_MACH_STM32F0x2
&& *(uint64_t*)USB_BOOT_FLAG_ADDR == USB_BOOT_FLAG) {
irq_disable();
*(uint64_t*)USB_BOOT_FLAG_ADDR = USB_BOOT_FLAG;
NVIC_SystemReset();
}
// Check if rebooting into system DFU Bootloader
static void
check_usb_dfu_bootloader(void)
{
if (!CONFIG_USBSERIAL || !CONFIG_MACH_STM32F0x2
|| *(uint64_t*)USB_BOOT_FLAG_ADDR != USB_BOOT_FLAG)
return;
*(uint64_t*)USB_BOOT_FLAG_ADDR = 0;
uint32_t *sysbase = (uint32_t*)0x1fffc400;
asm volatile("mov sp, %0\n bx %1"
: : "r"(sysbase[0]), "r"(sysbase[1]));
}
SystemInit();
// Handle USB reboot requests
void
usb_request_bootloader(void)
{
usb_reboot_for_dfu_bootloader();
}
/****************************************************************
* Startup
****************************************************************/
// Copy vector table and remap ram so new vector table is used
static void
enable_ram_vectortable(void)
{
// Symbols created by armcm_link.lds.S linker script
extern uint32_t _ram_vectortable_start, _ram_vectortable_end;
extern uint32_t _text_vectortable_start;
uint32_t count = (&_ram_vectortable_end - &_ram_vectortable_start) * 4;
__builtin_memcpy(&_ram_vectortable_start, &_text_vectortable_start, count);
barrier();
enable_pclock(SYSCFG_BASE);
SYSCFG->CFGR1 |= 3 << SYSCFG_CFGR1_MEM_MODE_Pos;
}
// Main entry point - called from armcm_boot.c:ResetHandler()
void
armcm_main(void)
{
check_usb_dfu_bootloader();
SystemInit();
if (CONFIG_ARMCM_RAM_VECTORTABLE)
enable_ram_vectortable();