]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/chibios/bootloader.c
2dd3ade34ea01aeccdf2089ace83e6595e8a5c07
[qmk_firmware.git] / tmk_core / common / chibios / bootloader.c
1 #include "bootloader.h"
2
3 #include "ch.h"
4 #include "hal.h"
5
6 #ifdef STM32_BOOTLOADER_ADDRESS
7 /* STM32 */
8
9 /* This code should be checked whether it runs correctly on platforms */
10 #define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0))
11 extern uint32_t __ram0_end__;
12 #define BOOTLOADER_MAGIC 0xDEADBEEF
13 #define MAGIC_ADDR (unsigned long*)(SYMVAL(__ram0_end__) - 4)
14
15
16 void bootloader_jump(void) {
17   *MAGIC_ADDR = BOOTLOADER_MAGIC; // set magic flag => reset handler will jump into boot loader
18    NVIC_SystemReset();
19 }
20
21 void enter_bootloader_mode_if_requested(void)  {
22   unsigned long* check = MAGIC_ADDR;
23   if(*check == BOOTLOADER_MAGIC)  {
24     *check = 0;
25     __set_CONTROL(0);
26     __set_MSP(*(__IO uint32_t*)STM32_BOOTLOADER_ADDRESS);
27     __enable_irq();
28
29     typedef void (*BootJump_t)(void);
30     BootJump_t boot_jump = *(BootJump_t*)(STM32_BOOTLOADER_ADDRESS + 4);
31     boot_jump();
32     while(1);
33   }
34  }
35
36 #elif defined(KL2x) || defined(K20x) /* STM32_BOOTLOADER_ADDRESS */
37 /* Kinetis */
38
39 #if defined(KIIBOHD_BOOTLOADER)
40 /* Kiibohd Bootloader (MCHCK and Infinity KB) */
41 #define SCB_AIRCR_VECTKEY_WRITEMAGIC 0x05FA0000
42 const uint8_t sys_reset_to_loader_magic[] = "\xff\x00\x7fRESET TO LOADER\x7f\x00\xff";
43 void bootloader_jump(void) {
44   __builtin_memcpy((void *)VBAT, (const void *)sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic));
45   // request reset
46   SCB->AIRCR = SCB_AIRCR_VECTKEY_WRITEMAGIC | SCB_AIRCR_SYSRESETREQ_Msk;
47 }
48
49 #else /* defined(KIIBOHD_BOOTLOADER) */
50 /* Default for Kinetis - expecting an ARM Teensy */
51 #include "wait.h"
52 void bootloader_jump(void) {
53         wait_ms(100);
54         __BKPT(0);
55 }
56 #endif /* defined(KIIBOHD_BOOTLOADER) */
57
58 #else /* neither STM32 nor KINETIS */
59 __attribute__((weak))
60 void bootloader_jump(void) {}
61 #endif