bootloader.c 7.1 KB

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