You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB

  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 <avr/wdt.h>
  18. #include <util/delay.h>
  19. #define FLASH_SIZE (FLASHEND + 1L)
  20. /** \brief Entering the Bootloader via Software
  21. *
  22. * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
  23. */
  24. #define BOOTLOADER_RESET_KEY 0xB007B007
  25. uint32_t reset_key __attribute__((section(".noinit,\"aw\",@nobits;")));
  26. __attribute__((weak)) void bootloader_jump(void) {
  27. UDCON = 1;
  28. USBCON = (1 << FRZCLK); // disable USB
  29. UCSR1B = 0;
  30. _delay_ms(5); // 5 seems to work fine
  31. // watchdog reset
  32. reset_key = BOOTLOADER_RESET_KEY;
  33. wdt_enable(WDTO_250MS);
  34. for (;;)
  35. ;
  36. }
  37. /* this runs before main() */
  38. void bootloader_jump_after_watchdog_reset(void) __attribute__((used, naked, section(".init3")));
  39. void bootloader_jump_after_watchdog_reset(void) {
  40. if ((MCUSR & (1 << WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
  41. reset_key = 0;
  42. ((void (*)(void))((FLASH_SIZE - BOOTLOADER_SIZE) >> 1))();
  43. }
  44. }