aboutsummaryrefslogtreecommitdiffstats
path: root/src/stm32/stm32f4.c
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2019-10-26 13:24:59 -0400
committerKevin O'Connor <kevin@koconnor.net>2019-10-26 13:24:59 -0400
commit5da6a62759be14554702be681dc36fe8809769f2 (patch)
tree3aa9f13410323957573da9922f78fde161167b7e /src/stm32/stm32f4.c
parent0ba5819370d4d6c3fd105f3aaf8c29f7d775e395 (diff)
downloadkutter-5da6a62759be14554702be681dc36fe8809769f2.tar.gz
kutter-5da6a62759be14554702be681dc36fe8809769f2.tar.xz
kutter-5da6a62759be14554702be681dc36fe8809769f2.zip
stm32f4: Add support for flashing over USB
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/stm32/stm32f4.c')
-rw-r--r--src/stm32/stm32f4.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/stm32/stm32f4.c b/src/stm32/stm32f4.c
index e1f22b2f..92c40b22 100644
--- a/src/stm32/stm32f4.c
+++ b/src/stm32/stm32f4.c
@@ -6,6 +6,7 @@
#include "autoconf.h" // CONFIG_CLOCK_REF_8M
#include "board/armcm_boot.h" // VectorTable
+#include "board/irq.h" // irq_disable
#include "board/usb_cdc.h" // usb_request_bootloader
#include "command.h" // DECL_CONSTANT_STR
#include "internal.h" // enable_pclock
@@ -88,10 +89,16 @@ gpio_peripheral(uint32_t gpio, uint32_t mode, int pullup)
regs->OSPEEDR = (regs->OSPEEDR & ~m_msk) | (0x02 << m_shift);
}
+#define USB_BOOT_FLAG_ADDR (CONFIG_RAM_START + CONFIG_RAM_SIZE - 4096)
+#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();
}
#if CONFIG_CLOCK_REF_8M
@@ -202,6 +209,13 @@ clock_setup(void)
void
armcm_main(void)
{
+ if (*(uint64_t*)USB_BOOT_FLAG_ADDR == USB_BOOT_FLAG) {
+ *(uint64_t*)USB_BOOT_FLAG_ADDR = 0;
+ uint32_t *sysbase = (uint32_t*)0x1fff0000;
+ asm volatile("mov sp, %r0\n bx %r1"
+ : : "r"(sysbase[0]), "r"(sysbase[1]));
+ }
+
// Run SystemInit() and then restore VTOR
SystemInit();
SCB->VTOR = (uint32_t)VectorTable;