bootloader.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #elif defined(STM32L4XX)
  26. /* This code should be checked whether it runs correctly on platforms.
  27. * It was added for chibios_test/stm32_l476_onekey BUT HAS NOT BEEN TESTED.
  28. * FIXME - Test this
  29. */
  30. #define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0))
  31. extern uint32_t __ram0_end__;
  32. void bootloader_jump(void) {
  33. *((unsigned long *)(SYMVAL(__ram0_end__) - 4)) = 0xDEADBEEF; // set magic flag => reset handler will jump into boot loader
  34. NVIC_SystemReset();
  35. }
  36. #else /* defined(STM32L4XX) */
  37. #error Check that the bootloader code works on your platform and add it to bootloader.c!
  38. #endif /* defined(STM32F0XX) */
  39. #elif defined(KL2x) || defined(K20x) /* STM32_BOOTLOADER_ADDRESS */
  40. /* Kinetis */
  41. #if defined(KIIBOHD_BOOTLOADER)
  42. /* Kiibohd Bootloader (MCHCK and Infinity KB) */
  43. #define SCB_AIRCR_VECTKEY_WRITEMAGIC 0x05FA0000
  44. const uint8_t sys_reset_to_loader_magic[] = "\xff\x00\x7fRESET TO LOADER\x7f\x00\xff";
  45. void bootloader_jump(void) {
  46. __builtin_memcpy((void *)VBAT, (const void *)sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic));
  47. // request reset
  48. SCB->AIRCR = SCB_AIRCR_VECTKEY_WRITEMAGIC | SCB_AIRCR_SYSRESETREQ_Msk;
  49. }
  50. #else /* defined(KIIBOHD_BOOTLOADER) */
  51. /* Default for Kinetis - expecting an ARM Teensy */
  52. #include "wait.h"
  53. void bootloader_jump(void) {
  54. wait_ms(100);
  55. __BKPT(0);
  56. }
  57. #endif /* defined(KIIBOHD_BOOTLOADER) */
  58. #else /* neither STM32 nor KINETIS */
  59. __attribute__((weak))
  60. void bootloader_jump(void) {}
  61. #endif