bootloader.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #ifndef BOOTLOADER_SIZE
  57. #warning To use bootloader_jump() you need to define BOOTLOADER_SIZE in config.h.
  58. #define BOOTLOADER_SIZE 4096
  59. #endif
  60. #define FLASH_SIZE (FLASHEND + 1L)
  61. #define BOOTLOADER_START (FLASH_SIZE - BOOTLOADER_SIZE)
  62. /*
  63. * Entering the Bootloader via Software
  64. * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
  65. */
  66. #define BOOTLOADER_RESET_KEY 0xB007B007
  67. uint32_t reset_key __attribute__ ((section (".noinit")));
  68. /* initialize MCU status by watchdog reset */
  69. void bootloader_jump(void) {
  70. #ifndef CATERINA_BOOTLOADER
  71. #ifdef PROTOCOL_LUFA
  72. USB_Disable();
  73. cli();
  74. _delay_ms(2000);
  75. #endif
  76. #ifdef PROTOCOL_PJRC
  77. cli();
  78. UDCON = 1;
  79. USBCON = (1<<FRZCLK);
  80. UCSR1B = 0;
  81. _delay_ms(5);
  82. #endif
  83. #ifdef EEPROM_BOOTLOADER_START
  84. eeprom_write_byte((uint8_t *)EEPROM_BOOTLOADER_START, 0x00);
  85. #endif
  86. // watchdog reset
  87. reset_key = BOOTLOADER_RESET_KEY;
  88. wdt_enable(WDTO_250MS);
  89. for (;;);
  90. #else
  91. // this block may be optional
  92. // TODO: figure it out
  93. uint16_t *const bootKeyPtr = (uint16_t *)0x0800;
  94. // Value used by Caterina bootloader use to determine whether to run the
  95. // sketch or the bootloader programmer.
  96. uint16_t bootKey = 0x7777;
  97. *bootKeyPtr = bootKey;
  98. // setup watchdog timeout
  99. wdt_enable(WDTO_60MS);
  100. while(1) {} // wait for watchdog timer to trigger
  101. #endif
  102. }
  103. #ifdef __AVR_ATmega32A__
  104. // MCUSR is actually called MCUCSR in ATmega32A
  105. #define MCUSR MCUCSR
  106. #endif
  107. /* this runs before main() */
  108. void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3")));
  109. void bootloader_jump_after_watchdog_reset(void)
  110. {
  111. if ((MCUSR & (1<<WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
  112. reset_key = 0;
  113. // My custom USBasploader requires this to come up.
  114. MCUSR = 0;
  115. // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog.
  116. MCUSR &= ~(1<<WDRF);
  117. wdt_disable();
  118. // This is compled into 'icall', address should be in word unit, not byte.
  119. ((void (*)(void))(BOOTLOADER_START/2))();
  120. }
  121. }
  122. #if 0
  123. /* Jumping To The Bootloader
  124. * http://www.pjrc.com/teensy/jump_to_bootloader.html
  125. *
  126. * This method doen't work when using LUFA. idk why.
  127. * - needs to initialize more regisers or interrupt setting?
  128. */
  129. void bootloader_jump(void) {
  130. #ifdef PROTOCOL_LUFA
  131. USB_Disable();
  132. cli();
  133. _delay_ms(2000);
  134. #endif
  135. #ifdef PROTOCOL_PJRC
  136. cli();
  137. UDCON = 1;
  138. USBCON = (1<<FRZCLK);
  139. UCSR1B = 0;
  140. _delay_ms(5);
  141. #endif
  142. /*
  143. * Initialize
  144. */
  145. #if defined(__AVR_AT90USB162__)
  146. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
  147. TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
  148. DDRB = 0; DDRC = 0; DDRD = 0;
  149. PORTB = 0; PORTC = 0; PORTD = 0;
  150. #elif defined(__AVR_ATmega32U4__)
  151. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  152. TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
  153. DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
  154. PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  155. #elif defined(__AVR_AT90USB646__)
  156. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  157. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  158. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  159. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  160. #elif defined(__AVR_AT90USB1286__)
  161. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  162. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  163. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  164. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  165. #endif
  166. /*
  167. * USBaspLoader
  168. */
  169. #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
  170. // This makes custom USBasploader come up.
  171. MCUSR = 0;
  172. // initialize ports
  173. PORTB = 0; PORTC= 0; PORTD = 0;
  174. DDRB = 0; DDRC= 0; DDRD = 0;
  175. // disable interrupts
  176. EIMSK = 0; EECR = 0; SPCR = 0;
  177. ACSR = 0; SPMCSR = 0; WDTCSR = 0; PCICR = 0;
  178. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0;
  179. ADCSRA = 0; TWCR = 0; UCSR0B = 0;
  180. #endif
  181. // This is compled into 'icall', address should be in word unit, not byte.
  182. ((void (*)(void))(BOOTLOADER_START/2))();
  183. }
  184. #endif