bootloader.c 1.7 KB

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