bootloader.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include <avr/io.h>
  4. #include <avr/interrupt.h>
  5. #include <avr/wdt.h>
  6. #include <util/delay.h>
  7. #include "bootloader.h"
  8. #ifdef PROTOCOL_LUFA
  9. #include <LUFA/Drivers/USB/USB.h>
  10. #endif
  11. /* Bootloader Size in *bytes*
  12. *
  13. * AVR Boot section size are defined by setting BOOTSZ fuse in fact. Consult with your MCU datasheet.
  14. * Note that 'Word'(2 bytes) size and address are used in datasheet while TMK uses 'Byte'.
  15. *
  16. *
  17. * Size of Bootloaders in bytes:
  18. * Atmel DFU loader(ATmega32U4) 4096
  19. * Atmel DFU loader(AT90USB128) 8192
  20. * LUFA bootloader(ATmega32U4) 4096
  21. * Arduino Caterina(ATmega32U4) 4096
  22. * USBaspLoader(ATmega***) 2048
  23. * Teensy halfKay(ATmega32U4) 512
  24. * Teensy++ halfKay(AT90USB128) 1024
  25. *
  26. *
  27. * AVR Boot section is located at the end of Flash memory like the followings.
  28. *
  29. *
  30. * byte Atmel/LUFA(ATMega32u4) byte Atmel(AT90SUB128)
  31. * 0x0000 +---------------+ 0x00000 +---------------+
  32. * | | | |
  33. * | | | |
  34. * | Application | | Application |
  35. * | | | |
  36. * = = = =
  37. * | | 32KB-4KB | | 128KB-8KB
  38. * 0x6000 +---------------+ 0x1FC00 +---------------+
  39. * | Bootloader | 4KB | Bootloader | 8KB
  40. * 0x7FFF +---------------+ 0x1FFFF +---------------+
  41. *
  42. *
  43. * byte Teensy(ATMega32u4) byte Teensy++(AT90SUB128)
  44. * 0x0000 +---------------+ 0x00000 +---------------+
  45. * | | | |
  46. * | | | |
  47. * | Application | | Application |
  48. * | | | |
  49. * = = = =
  50. * | | 32KB-512B | | 128KB-1KB
  51. * 0x7E00 +---------------+ 0x1FC00 +---------------+
  52. * | Bootloader | 512B | Bootloader | 1KB
  53. * 0x7FFF +---------------+ 0x1FFFF +---------------+
  54. */
  55. #ifndef BOOTLOADER_SIZE
  56. #warning To use bootloader_jump() you need to define BOOTLOADER_SIZE in config.h.
  57. #define BOOTLOADER_SIZE 4096
  58. #endif
  59. #define FLASH_SIZE (FLASHEND + 1L)
  60. #define BOOTLOADER_START (FLASH_SIZE - BOOTLOADER_SIZE)
  61. /*
  62. * Entering the Bootloader via Software
  63. * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
  64. */
  65. #define BOOTLOADER_RESET_KEY 0xB007B007
  66. uint32_t reset_key __attribute__ ((section (".noinit")));
  67. /* initialize MCU status by watchdog reset */
  68. void bootloader_jump(void) {
  69. #ifndef CATERINA_BOOTLOADER
  70. #ifdef PROTOCOL_LUFA
  71. USB_Disable();
  72. cli();
  73. _delay_ms(2000);
  74. #endif
  75. #ifdef PROTOCOL_PJRC
  76. cli();
  77. UDCON = 1;
  78. USBCON = (1<<FRZCLK);
  79. UCSR1B = 0;
  80. _delay_ms(5);
  81. #endif
  82. // watchdog reset
  83. reset_key = BOOTLOADER_RESET_KEY;
  84. wdt_enable(WDTO_250MS);
  85. for (;;);
  86. #else
  87. // this block may be optional
  88. // TODO: figure it out
  89. uint16_t *const bootKeyPtr = (uint16_t *)0x0800;
  90. // Value used by Caterina bootloader use to determine whether to run the
  91. // sketch or the bootloader programmer.
  92. uint16_t bootKey = 0x7777;
  93. *bootKeyPtr = bootKey;
  94. // setup watchdog timeout
  95. wdt_enable(WDTO_60MS);
  96. while(1) {} // wait for watchdog timer to trigger
  97. #endif
  98. }
  99. /* this runs before main() */
  100. void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3")));
  101. void bootloader_jump_after_watchdog_reset(void)
  102. {
  103. if ((MCUSR & (1<<WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
  104. reset_key = 0;
  105. // My custom USBasploader requires this to come up.
  106. MCUSR = 0;
  107. // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog.
  108. MCUSR &= ~(1<<WDRF);
  109. wdt_disable();
  110. // This is compled into 'icall', address should be in word unit, not byte.
  111. ((void (*)(void))(BOOTLOADER_START/2))();
  112. }
  113. }
  114. #if 0
  115. /* Jumping To The Bootloader
  116. * http://www.pjrc.com/teensy/jump_to_bootloader.html
  117. *
  118. * This method doen't work when using LUFA. idk why.
  119. * - needs to initialize more regisers or interrupt setting?
  120. */
  121. void bootloader_jump(void) {
  122. #ifdef PROTOCOL_LUFA
  123. USB_Disable();
  124. cli();
  125. _delay_ms(2000);
  126. #endif
  127. #ifdef PROTOCOL_PJRC
  128. cli();
  129. UDCON = 1;
  130. USBCON = (1<<FRZCLK);
  131. UCSR1B = 0;
  132. _delay_ms(5);
  133. #endif
  134. /*
  135. * Initialize
  136. */
  137. #if defined(__AVR_AT90USB162__)
  138. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
  139. TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
  140. DDRB = 0; DDRC = 0; DDRD = 0;
  141. PORTB = 0; PORTC = 0; PORTD = 0;
  142. #elif defined(__AVR_ATmega32U4__)
  143. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  144. TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
  145. DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
  146. PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  147. #elif defined(__AVR_AT90USB646__)
  148. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  149. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  150. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  151. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  152. #elif defined(__AVR_AT90USB1286__)
  153. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  154. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  155. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  156. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  157. #endif
  158. /*
  159. * USBaspLoader
  160. */
  161. #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
  162. // This makes custom USBasploader come up.
  163. MCUSR = 0;
  164. // initialize ports
  165. PORTB = 0; PORTC= 0; PORTD = 0;
  166. DDRB = 0; DDRC= 0; DDRD = 0;
  167. // disable interrupts
  168. EIMSK = 0; EECR = 0; SPCR = 0;
  169. ACSR = 0; SPMCSR = 0; WDTCSR = 0; PCICR = 0;
  170. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0;
  171. ADCSRA = 0; TWCR = 0; UCSR0B = 0;
  172. #endif
  173. // This is compled into 'icall', address should be in word unit, not byte.
  174. ((void (*)(void))(BOOTLOADER_START/2))();
  175. }
  176. #endif