md_boot.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Copyright 2017 Fred Sundvik
  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 2 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 "samd51j18a.h"
  18. // WARNING: These are only for CTRL bootloader release "v2.18Jun 22 2018 17:28:08" for bootloader_jump support
  19. extern uint32_t _eram;
  20. #define BOOTLOADER_MAGIC 0x3B9ACA00
  21. #define MAGIC_ADDR (uint32_t *)((intptr_t)(&_eram) - 4)
  22. // CTRL keyboards released with bootloader version below must use RAM method. Otherwise use WDT method.
  23. void bootloader_jump(void) {
  24. #ifdef KEYBOARD_massdrop_ctrl
  25. uint8_t ver_ram_method[] = "v2.18Jun 22 2018 17:28:08"; // The version to match (NULL terminated by compiler)
  26. uint8_t *ver_check = ver_ram_method; // Pointer to version match string for traversal
  27. uint8_t *ver_rom = (uint8_t *)0x21A0; // Pointer to address in ROM where this specific bootloader version would exist
  28. while (*ver_check && *ver_rom == *ver_check) { // While there are check version characters to match and bootloader's version matches check's version
  29. ver_check++; // Move check version pointer to next character
  30. ver_rom++; // Move ROM version pointer to next character
  31. }
  32. if (!*ver_check) { // If check version pointer is NULL, all characters have matched
  33. *MAGIC_ADDR = BOOTLOADER_MAGIC; // Set magic number into RAM
  34. NVIC_SystemReset(); // Perform system reset
  35. while (1)
  36. ; // Won't get here
  37. }
  38. #endif
  39. // Set watchdog timer to reset. Directs the bootloader to stay in programming mode.
  40. WDT->CTRLA.bit.ENABLE = 0;
  41. while (WDT->SYNCBUSY.bit.ENABLE)
  42. ;
  43. while (WDT->CTRLA.bit.ENABLE)
  44. ;
  45. WDT->CONFIG.bit.WINDOW = 0;
  46. WDT->CONFIG.bit.PER = 0;
  47. WDT->EWCTRL.bit.EWOFFSET = 0;
  48. WDT->CTRLA.bit.ENABLE = 1;
  49. while (WDT->SYNCBUSY.bit.ENABLE)
  50. ;
  51. while (!WDT->CTRLA.bit.ENABLE)
  52. ;
  53. while (1)
  54. ; // Wait on timeout
  55. }
  56. __attribute__((weak)) void mcu_reset(void) {
  57. NVIC_SystemReset();
  58. }