wb32_dfu.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* Copyright 2021 QMK
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "bootloader.h"
  17. #include <ch.h>
  18. #include <hal.h>
  19. #include "wait.h"
  20. extern uint32_t __ram0_end__;
  21. /* This code should be checked whether it runs correctly on platforms */
  22. #define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0))
  23. #define BOOTLOADER_MAGIC 0xDEADBEEF
  24. #define MAGIC_ADDR (unsigned long *)(SYMVAL(__ram0_end__) - 4)
  25. __attribute__((weak)) void bootloader_jump(void) {
  26. *MAGIC_ADDR = BOOTLOADER_MAGIC; // set magic flag => reset handler will jump into boot loader
  27. NVIC_SystemReset();
  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 *)WB32_BOOTLOADER_ADDRESS);
  35. __enable_irq();
  36. typedef void (*BootJump_t)(void);
  37. BootJump_t boot_jump = *(BootJump_t *)(WB32_BOOTLOADER_ADDRESS + 4);
  38. boot_jump();
  39. while (1)
  40. ;
  41. }
  42. }
  43. __attribute__((weak)) void mcu_reset(void) {
  44. NVIC_SystemReset();
  45. }