diff options
Diffstat (limited to 'platforms/chibios/bootloader.c')
-rw-r--r-- | platforms/chibios/bootloader.c | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/platforms/chibios/bootloader.c b/platforms/chibios/bootloader.c new file mode 100644 index 000000000..5cadadeee --- /dev/null +++ b/platforms/chibios/bootloader.c | |||
@@ -0,0 +1,145 @@ | |||
1 | #include "bootloader.h" | ||
2 | |||
3 | #include <ch.h> | ||
4 | #include <hal.h> | ||
5 | #include "wait.h" | ||
6 | |||
7 | /* This code should be checked whether it runs correctly on platforms */ | ||
8 | #define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0)) | ||
9 | #define BOOTLOADER_MAGIC 0xDEADBEEF | ||
10 | #define MAGIC_ADDR (unsigned long *)(SYMVAL(__ram0_end__) - 4) | ||
11 | |||
12 | #ifndef STM32_BOOTLOADER_DUAL_BANK | ||
13 | # define STM32_BOOTLOADER_DUAL_BANK FALSE | ||
14 | #endif | ||
15 | |||
16 | #ifdef BOOTLOADER_TINYUF2 | ||
17 | |||
18 | # define DBL_TAP_MAGIC 0xf01669ef // From tinyuf2's board_api.h | ||
19 | |||
20 | // defined by linker script | ||
21 | extern uint32_t _board_dfu_dbl_tap[]; | ||
22 | # define DBL_TAP_REG _board_dfu_dbl_tap[0] | ||
23 | |||
24 | void bootloader_jump(void) { | ||
25 | DBL_TAP_REG = DBL_TAP_MAGIC; | ||
26 | NVIC_SystemReset(); | ||
27 | } | ||
28 | |||
29 | void enter_bootloader_mode_if_requested(void) { /* not needed, no two-stage reset */ | ||
30 | } | ||
31 | |||
32 | #elif STM32_BOOTLOADER_DUAL_BANK | ||
33 | |||
34 | // Need pin definitions | ||
35 | # include "config_common.h" | ||
36 | |||
37 | # ifndef STM32_BOOTLOADER_DUAL_BANK_GPIO | ||
38 | # error "No STM32_BOOTLOADER_DUAL_BANK_GPIO defined, don't know which pin to toggle" | ||
39 | # endif | ||
40 | |||
41 | # ifndef STM32_BOOTLOADER_DUAL_BANK_POLARITY | ||
42 | # define STM32_BOOTLOADER_DUAL_BANK_POLARITY 0 | ||
43 | # endif | ||
44 | |||
45 | # ifndef STM32_BOOTLOADER_DUAL_BANK_DELAY | ||
46 | # define STM32_BOOTLOADER_DUAL_BANK_DELAY 100000 | ||
47 | # endif | ||
48 | |||
49 | extern uint32_t __ram0_end__; | ||
50 | |||
51 | __attribute__((weak)) void bootloader_jump(void) { | ||
52 | // For STM32 MCUs with dual-bank flash, and we're incapable of jumping to the bootloader. The first valid flash | ||
53 | // bank is executed unconditionally after a reset, so it doesn't enter DFU unless BOOT0 is high. Instead, we do | ||
54 | // it with hardware...in this case, we pull a GPIO high/low depending on the configuration, connects 3.3V to | ||
55 | // BOOT0's RC charging circuit, lets it charge the capacitor, and issue a system reset. See the QMK discord | ||
56 | // #hardware channel pins for an example circuit. | ||
57 | palSetPadMode(PAL_PORT(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_PAD(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_MODE_OUTPUT_PUSHPULL); | ||
58 | # if STM32_BOOTLOADER_DUAL_BANK_POLARITY | ||
59 | palSetPad(PAL_PORT(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_PAD(STM32_BOOTLOADER_DUAL_BANK_GPIO)); | ||
60 | # else | ||
61 | palClearPad(PAL_PORT(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_PAD(STM32_BOOTLOADER_DUAL_BANK_GPIO)); | ||
62 | # endif | ||
63 | |||
64 | // Wait for a while for the capacitor to charge | ||
65 | wait_ms(100); | ||
66 | |||
67 | // Issue a system reset to get the ROM bootloader to execute, with BOOT0 high | ||
68 | NVIC_SystemReset(); | ||
69 | } | ||
70 | |||
71 | void enter_bootloader_mode_if_requested(void) {} // not needed at all, but if anybody attempts to invoke it.... | ||
72 | |||
73 | #elif defined(STM32_BOOTLOADER_ADDRESS) // STM32_BOOTLOADER_DUAL_BANK | ||
74 | |||
75 | extern uint32_t __ram0_end__; | ||
76 | |||
77 | __attribute__((weak)) void bootloader_jump(void) { | ||
78 | *MAGIC_ADDR = BOOTLOADER_MAGIC; // set magic flag => reset handler will jump into boot loader | ||
79 | NVIC_SystemReset(); | ||
80 | } | ||
81 | |||
82 | void enter_bootloader_mode_if_requested(void) { | ||
83 | unsigned long *check = MAGIC_ADDR; | ||
84 | if (*check == BOOTLOADER_MAGIC) { | ||
85 | *check = 0; | ||
86 | __set_CONTROL(0); | ||
87 | __set_MSP(*(__IO uint32_t *)STM32_BOOTLOADER_ADDRESS); | ||
88 | __enable_irq(); | ||
89 | |||
90 | typedef void (*BootJump_t)(void); | ||
91 | BootJump_t boot_jump = *(BootJump_t *)(STM32_BOOTLOADER_ADDRESS + 4); | ||
92 | boot_jump(); | ||
93 | while (1) | ||
94 | ; | ||
95 | } | ||
96 | } | ||
97 | |||
98 | #elif defined(GD32VF103) | ||
99 | |||
100 | # define DBGMCU_KEY_UNLOCK 0x4B5A6978 | ||
101 | # define DBGMCU_CMD_RESET 0x1 | ||
102 | |||
103 | __IO uint32_t *DBGMCU_KEY = (uint32_t *)DBGMCU_BASE + 0x0CU; | ||
104 | __IO uint32_t *DBGMCU_CMD = (uint32_t *)DBGMCU_BASE + 0x08U; | ||
105 | |||
106 | __attribute__((weak)) void bootloader_jump(void) { | ||
107 | /* The MTIMER unit of the GD32VF103 doesn't have the MSFRST | ||
108 | * register to generate a software reset request. | ||
109 | * BUT instead two undocumented registers in the debug peripheral | ||
110 | * that allow issueing a software reset. WHO would need the MSFRST | ||
111 | * register anyway? Source: | ||
112 | * https://github.com/esmil/gd32vf103inator/blob/master/include/gd32vf103/dbg.h */ | ||
113 | *DBGMCU_KEY = DBGMCU_KEY_UNLOCK; | ||
114 | *DBGMCU_CMD = DBGMCU_CMD_RESET; | ||
115 | } | ||
116 | |||
117 | void enter_bootloader_mode_if_requested(void) { /* Jumping to bootloader is not possible from user code. */ | ||
118 | } | ||
119 | |||
120 | #elif defined(KL2x) || defined(K20x) || defined(MK66F18) || defined(MIMXRT1062) // STM32_BOOTLOADER_DUAL_BANK // STM32_BOOTLOADER_ADDRESS | ||
121 | /* Kinetis */ | ||
122 | |||
123 | # if defined(BOOTLOADER_KIIBOHD) | ||
124 | /* Kiibohd Bootloader (MCHCK and Infinity KB) */ | ||
125 | # define SCB_AIRCR_VECTKEY_WRITEMAGIC 0x05FA0000 | ||
126 | const uint8_t sys_reset_to_loader_magic[] = "\xff\x00\x7fRESET TO LOADER\x7f\x00\xff"; | ||
127 | __attribute__((weak)) void bootloader_jump(void) { | ||
128 | void *volatile vbat = (void *)VBAT; | ||
129 | __builtin_memcpy(vbat, (const void *)sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic)); | ||
130 | // request reset | ||
131 | SCB->AIRCR = SCB_AIRCR_VECTKEY_WRITEMAGIC | SCB_AIRCR_SYSRESETREQ_Msk; | ||
132 | } | ||
133 | |||
134 | # else /* defined(BOOTLOADER_KIIBOHD) */ | ||
135 | /* Default for Kinetis - expecting an ARM Teensy */ | ||
136 | # include "wait.h" | ||
137 | __attribute__((weak)) void bootloader_jump(void) { | ||
138 | wait_ms(100); | ||
139 | __BKPT(0); | ||
140 | } | ||
141 | # endif /* defined(BOOTLOADER_KIIBOHD) */ | ||
142 | |||
143 | #else /* neither STM32 nor KINETIS */ | ||
144 | __attribute__((weak)) void bootloader_jump(void) {} | ||
145 | #endif | ||