bootloader.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #include <avr/boot.h>
  10. #ifdef PROTOCOL_LUFA
  11. # include <LUFA/Drivers/USB/USB.h>
  12. #endif
  13. /** \brief Bootloader Size in *bytes*
  14. *
  15. * AVR Boot section size are defined by setting BOOTSZ fuse in fact. Consult with your MCU datasheet.
  16. * Note that 'Word'(2 bytes) size and address are used in datasheet while TMK uses 'Byte'.
  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. * AVR Boot section is located at the end of Flash memory like the followings.
  28. *
  29. * byte Atmel/LUFA(ATMega32u4) byte Atmel(AT90SUB128)
  30. * 0x0000 +---------------+ 0x00000 +---------------+
  31. * | | | |
  32. * | | | |
  33. * | Application | | Application |
  34. * | | | |
  35. * = = = =
  36. * | | 32KB-4KB | | 128KB-8KB
  37. * 0x7000 +---------------+ 0x1E000 +---------------+
  38. * | Bootloader | 4KB | Bootloader | 8KB
  39. * 0x7FFF +---------------+ 0x1FFFF +---------------+
  40. *
  41. *
  42. * byte Teensy(ATMega32u4) byte Teensy++(AT90SUB128)
  43. * 0x0000 +---------------+ 0x00000 +---------------+
  44. * | | | |
  45. * | | | |
  46. * | Application | | Application |
  47. * | | | |
  48. * = = = =
  49. * | | 32KB-512B | | 128KB-1KB
  50. * 0x7E00 +---------------+ 0x1FC00 +---------------+
  51. * | Bootloader | 512B | Bootloader | 1KB
  52. * 0x7FFF +---------------+ 0x1FFFF +---------------+
  53. */
  54. #define FLASH_SIZE (FLASHEND + 1L)
  55. #if !defined(BOOTLOADER_SIZE)
  56. uint16_t bootloader_start;
  57. #endif
  58. #define BOOT_SIZE_256 0b110
  59. #define BOOT_SIZE_512 0b100
  60. #define BOOT_SIZE_1024 0b010
  61. #define BOOT_SIZE_2048 0b000
  62. /** \brief Entering the Bootloader via Software
  63. *
  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,\"aw\",@nobits;")));
  68. /** \brief initialize MCU status by watchdog reset
  69. *
  70. * FIXME: needs doc
  71. */
  72. void bootloader_jump(void) {
  73. #if !defined(BOOTLOADER_SIZE)
  74. uint8_t high_fuse = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
  75. if (high_fuse & BOOT_SIZE_256) {
  76. bootloader_start = (FLASH_SIZE - 512) >> 1;
  77. } else if (high_fuse & BOOT_SIZE_512) {
  78. bootloader_start = (FLASH_SIZE - 1024) >> 1;
  79. } else if (high_fuse & BOOT_SIZE_1024) {
  80. bootloader_start = (FLASH_SIZE - 2048) >> 1;
  81. } else {
  82. bootloader_start = (FLASH_SIZE - 4096) >> 1;
  83. }
  84. #endif
  85. // Something like this might work, but it compiled larger than the block above
  86. // bootloader_start = FLASH_SIZE - (256 << (~high_fuse & 0b110 >> 1));
  87. #if defined(BOOTLOADER_HALFKAY)
  88. // http://www.pjrc.com/teensy/jump_to_bootloader.html
  89. cli();
  90. // disable watchdog, if enabled (it's not)
  91. // disable all peripherals
  92. // a shutdown call might make sense here
  93. UDCON = 1;
  94. USBCON = (1 << FRZCLK); // disable USB
  95. UCSR1B = 0;
  96. _delay_ms(5);
  97. # if defined(__AVR_AT90USB162__) // Teensy 1.0
  98. EIMSK = 0;
  99. PCICR = 0;
  100. SPCR = 0;
  101. ACSR = 0;
  102. EECR = 0;
  103. TIMSK0 = 0;
  104. TIMSK1 = 0;
  105. UCSR1B = 0;
  106. DDRB = 0;
  107. DDRC = 0;
  108. DDRD = 0;
  109. PORTB = 0;
  110. PORTC = 0;
  111. PORTD = 0;
  112. asm volatile("jmp 0x3E00");
  113. # elif defined(__AVR_ATmega32U4__) // Teensy 2.0
  114. EIMSK = 0;
  115. PCICR = 0;
  116. SPCR = 0;
  117. ACSR = 0;
  118. EECR = 0;
  119. ADCSRA = 0;
  120. TIMSK0 = 0;
  121. TIMSK1 = 0;
  122. TIMSK3 = 0;
  123. TIMSK4 = 0;
  124. UCSR1B = 0;
  125. TWCR = 0;
  126. DDRB = 0;
  127. DDRC = 0;
  128. DDRD = 0;
  129. DDRE = 0;
  130. DDRF = 0;
  131. TWCR = 0;
  132. PORTB = 0;
  133. PORTC = 0;
  134. PORTD = 0;
  135. PORTE = 0;
  136. PORTF = 0;
  137. asm volatile("jmp 0x7E00");
  138. # elif defined(__AVR_AT90USB646__) // Teensy++ 1.0
  139. EIMSK = 0;
  140. PCICR = 0;
  141. SPCR = 0;
  142. ACSR = 0;
  143. EECR = 0;
  144. ADCSRA = 0;
  145. TIMSK0 = 0;
  146. TIMSK1 = 0;
  147. TIMSK2 = 0;
  148. TIMSK3 = 0;
  149. UCSR1B = 0;
  150. TWCR = 0;
  151. DDRA = 0;
  152. DDRB = 0;
  153. DDRC = 0;
  154. DDRD = 0;
  155. DDRE = 0;
  156. DDRF = 0;
  157. PORTA = 0;
  158. PORTB = 0;
  159. PORTC = 0;
  160. PORTD = 0;
  161. PORTE = 0;
  162. PORTF = 0;
  163. asm volatile("jmp 0xFC00");
  164. # elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0
  165. EIMSK = 0;
  166. PCICR = 0;
  167. SPCR = 0;
  168. ACSR = 0;
  169. EECR = 0;
  170. ADCSRA = 0;
  171. TIMSK0 = 0;
  172. TIMSK1 = 0;
  173. TIMSK2 = 0;
  174. TIMSK3 = 0;
  175. UCSR1B = 0;
  176. TWCR = 0;
  177. DDRA = 0;
  178. DDRB = 0;
  179. DDRC = 0;
  180. DDRD = 0;
  181. DDRE = 0;
  182. DDRF = 0;
  183. PORTA = 0;
  184. PORTB = 0;
  185. PORTC = 0;
  186. PORTD = 0;
  187. PORTE = 0;
  188. PORTF = 0;
  189. asm volatile("jmp 0x1FC00");
  190. # endif
  191. #elif defined(BOOTLOADER_CATERINA)
  192. // this block may be optional
  193. // TODO: figure it out
  194. uint16_t *const bootKeyPtr = (uint16_t *)0x0800;
  195. // Value used by Caterina bootloader use to determine whether to run the
  196. // sketch or the bootloader programmer.
  197. uint16_t bootKey = 0x7777;
  198. *bootKeyPtr = bootKey;
  199. // setup watchdog timeout
  200. wdt_enable(WDTO_60MS);
  201. while (1) {
  202. } // wait for watchdog timer to trigger
  203. #else // Assume remaining boards are DFU, even if the flag isn't set
  204. # if !(defined(__AVR_ATmega32A__) || defined(__AVR_ATmega328P__)) // no USB - maybe BOOTLOADER_BOOTLOADHID instead though?
  205. UDCON = 1;
  206. USBCON = (1 << FRZCLK); // disable USB
  207. UCSR1B = 0;
  208. _delay_ms(5); // 5 seems to work fine
  209. # endif
  210. # ifdef BOOTLOADER_BOOTLOADHID
  211. // force bootloadHID to stay in bootloader mode, so that it waits
  212. // for a new firmware to be flashed
  213. eeprom_write_byte((uint8_t *)1, 0x00);
  214. # endif
  215. // watchdog reset
  216. reset_key = BOOTLOADER_RESET_KEY;
  217. wdt_enable(WDTO_250MS);
  218. for (;;)
  219. ;
  220. #endif
  221. }
  222. #ifdef __AVR_ATmega32A__
  223. // MCUSR is actually called MCUCSR in ATmega32A
  224. # define MCUSR MCUCSR
  225. #endif
  226. /* this runs before main() */
  227. void bootloader_jump_after_watchdog_reset(void) __attribute__((used, naked, section(".init3")));
  228. void bootloader_jump_after_watchdog_reset(void) {
  229. #ifndef BOOTLOADER_HALFKAY
  230. if ((MCUSR & (1 << WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
  231. reset_key = 0;
  232. // My custom USBasploader requires this to come up.
  233. MCUSR = 0;
  234. // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog.
  235. MCUSR &= ~(1 << WDRF);
  236. wdt_disable();
  237. // This is compled into 'icall', address should be in word unit, not byte.
  238. # ifdef BOOTLOADER_SIZE
  239. ((void (*)(void))((FLASH_SIZE - BOOTLOADER_SIZE) >> 1))();
  240. # else
  241. asm("ijmp" ::"z"(bootloader_start));
  242. # endif
  243. }
  244. #endif
  245. }
  246. #if 0
  247. /*
  248. * USBaspLoader - I'm not sure if this is used at all in any projects
  249. * would love to support it if it is -Jack
  250. */
  251. # if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
  252. // This makes custom USBasploader come up.
  253. MCUSR = 0;
  254. // initialize ports
  255. PORTB = 0; PORTC= 0; PORTD = 0;
  256. DDRB = 0; DDRC= 0; DDRD = 0;
  257. // disable interrupts
  258. EIMSK = 0; EECR = 0; SPCR = 0;
  259. ACSR = 0; SPMCSR = 0; WDTCSR = 0; PCICR = 0;
  260. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0;
  261. ADCSRA = 0; TWCR = 0; UCSR0B = 0;
  262. # endif
  263. // This is compled into 'icall', address should be in word unit, not byte.
  264. ((void (*)(void))(BOOTLOADER_START/2))();
  265. }
  266. #endif