halfkay.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/interrupt.h>
  18. #include <avr/wdt.h>
  19. #include <util/delay.h>
  20. __attribute__((weak)) void bootloader_jump(void) {
  21. // http://www.pjrc.com/teensy/jump_to_bootloader.html
  22. cli();
  23. // disable watchdog, if enabled (it's not)
  24. // disable all peripherals
  25. // a shutdown call might make sense here
  26. UDCON = 1;
  27. USBCON = (1 << FRZCLK); // disable USB
  28. UCSR1B = 0;
  29. _delay_ms(5);
  30. #if defined(__AVR_AT90USB162__) // Teensy 1.0
  31. EIMSK = 0;
  32. PCICR = 0;
  33. SPCR = 0;
  34. ACSR = 0;
  35. EECR = 0;
  36. TIMSK0 = 0;
  37. TIMSK1 = 0;
  38. UCSR1B = 0;
  39. DDRB = 0;
  40. DDRC = 0;
  41. DDRD = 0;
  42. PORTB = 0;
  43. PORTC = 0;
  44. PORTD = 0;
  45. asm volatile("jmp 0x3E00");
  46. #elif defined(__AVR_ATmega32U4__) // Teensy 2.0
  47. EIMSK = 0;
  48. PCICR = 0;
  49. SPCR = 0;
  50. ACSR = 0;
  51. EECR = 0;
  52. ADCSRA = 0;
  53. TIMSK0 = 0;
  54. TIMSK1 = 0;
  55. TIMSK3 = 0;
  56. TIMSK4 = 0;
  57. UCSR1B = 0;
  58. TWCR = 0;
  59. DDRB = 0;
  60. DDRC = 0;
  61. DDRD = 0;
  62. DDRE = 0;
  63. DDRF = 0;
  64. TWCR = 0;
  65. PORTB = 0;
  66. PORTC = 0;
  67. PORTD = 0;
  68. PORTE = 0;
  69. PORTF = 0;
  70. asm volatile("jmp 0x7E00");
  71. #elif defined(__AVR_AT90USB646__) // Teensy++ 1.0
  72. EIMSK = 0;
  73. PCICR = 0;
  74. SPCR = 0;
  75. ACSR = 0;
  76. EECR = 0;
  77. ADCSRA = 0;
  78. TIMSK0 = 0;
  79. TIMSK1 = 0;
  80. TIMSK2 = 0;
  81. TIMSK3 = 0;
  82. UCSR1B = 0;
  83. TWCR = 0;
  84. DDRA = 0;
  85. DDRB = 0;
  86. DDRC = 0;
  87. DDRD = 0;
  88. DDRE = 0;
  89. DDRF = 0;
  90. PORTA = 0;
  91. PORTB = 0;
  92. PORTC = 0;
  93. PORTD = 0;
  94. PORTE = 0;
  95. PORTF = 0;
  96. asm volatile("jmp 0xFC00");
  97. #elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0
  98. EIMSK = 0;
  99. PCICR = 0;
  100. SPCR = 0;
  101. ACSR = 0;
  102. EECR = 0;
  103. ADCSRA = 0;
  104. TIMSK0 = 0;
  105. TIMSK1 = 0;
  106. TIMSK2 = 0;
  107. TIMSK3 = 0;
  108. UCSR1B = 0;
  109. TWCR = 0;
  110. DDRA = 0;
  111. DDRB = 0;
  112. DDRC = 0;
  113. DDRD = 0;
  114. DDRE = 0;
  115. DDRF = 0;
  116. PORTA = 0;
  117. PORTB = 0;
  118. PORTC = 0;
  119. PORTD = 0;
  120. PORTE = 0;
  121. PORTF = 0;
  122. asm volatile("jmp 0x1FC00");
  123. #endif
  124. }
  125. __attribute__((weak)) void mcu_reset(void) {
  126. // setup watchdog timeout
  127. wdt_enable(WDTO_60MS);
  128. // wait for watchdog timer to trigger
  129. while (1) {
  130. }
  131. }