bootloader.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "bootloader.h"
  2. #include "ch.h"
  3. #include "hal.h"
  4. #ifdef STM32_BOOTLOADER_ADDRESS
  5. /* STM32 */
  6. #if defined(STM32F0XX)
  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. extern uint32_t __ram0_end__;
  10. void bootloader_jump(void) {
  11. *((unsigned long *)(SYMVAL(__ram0_end__) - 4)) = 0xDEADBEEF; // set magic flag => reset handler will jump into boot loader
  12. NVIC_SystemReset();
  13. }
  14. #elif defined(STM32F3XX)
  15. /* This code should be checked whether it runs correctly on platforms.
  16. * It was added for clueboard60 BUT HAS NOT BEEN TESTED.
  17. * FIXME - Test this
  18. */
  19. #define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0))
  20. extern uint32_t __ram0_end__;
  21. void bootloader_jump(void) {
  22. *((unsigned long *)(SYMVAL(__ram0_end__) - 4)) = 0xDEADBEEF; // set magic flag => reset handler will jump into boot loader
  23. NVIC_SystemReset();
  24. }
  25. #else /* defined(STM32F0XX) */
  26. #error Check that the bootloader code works on your platform and add it to bootloader.c!
  27. #endif /* defined(STM32F0XX) */
  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