]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/chibios/bootloader.c
Generate API docs from source code comments (#2491)
[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 /** \brief Jump to the bootloader
17  *
18  * FIXME: needs doc
19  */
20 void bootloader_jump(void) {
21   *MAGIC_ADDR = BOOTLOADER_MAGIC; // set magic flag => reset handler will jump into boot loader
22    NVIC_SystemReset();
23 }
24
25 /** \brief Enter bootloader mode if requested
26  *
27  * FIXME: needs doc
28  */
29 void enter_bootloader_mode_if_requested(void)  {
30   unsigned long* check = MAGIC_ADDR;
31   if(*check == BOOTLOADER_MAGIC)  {
32     *check = 0;
33     __set_CONTROL(0);
34     __set_MSP(*(__IO uint32_t*)STM32_BOOTLOADER_ADDRESS);
35     __enable_irq();
36
37     typedef void (*BootJump_t)(void);
38     BootJump_t boot_jump = *(BootJump_t*)(STM32_BOOTLOADER_ADDRESS + 4);
39     boot_jump();
40     while(1);
41   }
42  }
43
44 #elif defined(KL2x) || defined(K20x) /* STM32_BOOTLOADER_ADDRESS */
45 /* Kinetis */
46
47 #if defined(KIIBOHD_BOOTLOADER)
48 /* Kiibohd Bootloader (MCHCK and Infinity KB) */
49 #define SCB_AIRCR_VECTKEY_WRITEMAGIC 0x05FA0000
50 const uint8_t sys_reset_to_loader_magic[] = "\xff\x00\x7fRESET TO LOADER\x7f\x00\xff";
51 void bootloader_jump(void) {
52   __builtin_memcpy((void *)VBAT, (const void *)sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic));
53   // request reset
54   SCB->AIRCR = SCB_AIRCR_VECTKEY_WRITEMAGIC | SCB_AIRCR_SYSRESETREQ_Msk;
55 }
56
57 #else /* defined(KIIBOHD_BOOTLOADER) */
58 /* Default for Kinetis - expecting an ARM Teensy */
59 #include "wait.h"
60 void bootloader_jump(void) {
61         wait_ms(100);
62         __BKPT(0);
63 }
64 #endif /* defined(KIIBOHD_BOOTLOADER) */
65
66 #else /* neither STM32 nor KINETIS */
67 __attribute__((weak))
68 void bootloader_jump(void) {}
69 #endif