bootloader.c 2.5 KB

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