bootloader.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. // compatibility between ATMega8 and ATMega88
  63. #if !defined(MCUCSR)
  64. # if defined(MCUSR)
  65. # define MCUCSR MCUSR
  66. # endif
  67. #endif
  68. /** \brief Entering the Bootloader via Software
  69. *
  70. * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
  71. */
  72. #define BOOTLOADER_RESET_KEY 0xB007B007
  73. uint32_t reset_key __attribute__((section(".noinit,\"aw\",@nobits;")));
  74. /** \brief initialize MCU status by watchdog reset
  75. *
  76. * FIXME: needs doc
  77. */
  78. void bootloader_jump(void) {
  79. #if !defined(BOOTLOADER_SIZE)
  80. uint8_t high_fuse = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
  81. if (high_fuse & BOOT_SIZE_256) {
  82. bootloader_start = (FLASH_SIZE - 512) >> 1;
  83. } else if (high_fuse & BOOT_SIZE_512) {
  84. bootloader_start = (FLASH_SIZE - 1024) >> 1;
  85. } else if (high_fuse & BOOT_SIZE_1024) {
  86. bootloader_start = (FLASH_SIZE - 2048) >> 1;
  87. } else {
  88. bootloader_start = (FLASH_SIZE - 4096) >> 1;
  89. }
  90. #endif
  91. // Something like this might work, but it compiled larger than the block above
  92. // bootloader_start = FLASH_SIZE - (256 << (~high_fuse & 0b110 >> 1));
  93. #if defined(BOOTLOADER_HALFKAY)
  94. // http://www.pjrc.com/teensy/jump_to_bootloader.html
  95. cli();
  96. // disable watchdog, if enabled (it's not)
  97. // disable all peripherals
  98. // a shutdown call might make sense here
  99. UDCON = 1;
  100. USBCON = (1 << FRZCLK); // disable USB
  101. UCSR1B = 0;
  102. _delay_ms(5);
  103. # if defined(__AVR_AT90USB162__) // Teensy 1.0
  104. EIMSK = 0;
  105. PCICR = 0;
  106. SPCR = 0;
  107. ACSR = 0;
  108. EECR = 0;
  109. TIMSK0 = 0;
  110. TIMSK1 = 0;
  111. UCSR1B = 0;
  112. DDRB = 0;
  113. DDRC = 0;
  114. DDRD = 0;
  115. PORTB = 0;
  116. PORTC = 0;
  117. PORTD = 0;
  118. asm volatile("jmp 0x3E00");
  119. # elif defined(__AVR_ATmega32U4__) // Teensy 2.0
  120. EIMSK = 0;
  121. PCICR = 0;
  122. SPCR = 0;
  123. ACSR = 0;
  124. EECR = 0;
  125. ADCSRA = 0;
  126. TIMSK0 = 0;
  127. TIMSK1 = 0;
  128. TIMSK3 = 0;
  129. TIMSK4 = 0;
  130. UCSR1B = 0;
  131. TWCR = 0;
  132. DDRB = 0;
  133. DDRC = 0;
  134. DDRD = 0;
  135. DDRE = 0;
  136. DDRF = 0;
  137. TWCR = 0;
  138. PORTB = 0;
  139. PORTC = 0;
  140. PORTD = 0;
  141. PORTE = 0;
  142. PORTF = 0;
  143. asm volatile("jmp 0x7E00");
  144. # elif defined(__AVR_AT90USB646__) // Teensy++ 1.0
  145. EIMSK = 0;
  146. PCICR = 0;
  147. SPCR = 0;
  148. ACSR = 0;
  149. EECR = 0;
  150. ADCSRA = 0;
  151. TIMSK0 = 0;
  152. TIMSK1 = 0;
  153. TIMSK2 = 0;
  154. TIMSK3 = 0;
  155. UCSR1B = 0;
  156. TWCR = 0;
  157. DDRA = 0;
  158. DDRB = 0;
  159. DDRC = 0;
  160. DDRD = 0;
  161. DDRE = 0;
  162. DDRF = 0;
  163. PORTA = 0;
  164. PORTB = 0;
  165. PORTC = 0;
  166. PORTD = 0;
  167. PORTE = 0;
  168. PORTF = 0;
  169. asm volatile("jmp 0xFC00");
  170. # elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0
  171. EIMSK = 0;
  172. PCICR = 0;
  173. SPCR = 0;
  174. ACSR = 0;
  175. EECR = 0;
  176. ADCSRA = 0;
  177. TIMSK0 = 0;
  178. TIMSK1 = 0;
  179. TIMSK2 = 0;
  180. TIMSK3 = 0;
  181. UCSR1B = 0;
  182. TWCR = 0;
  183. DDRA = 0;
  184. DDRB = 0;
  185. DDRC = 0;
  186. DDRD = 0;
  187. DDRE = 0;
  188. DDRF = 0;
  189. PORTA = 0;
  190. PORTB = 0;
  191. PORTC = 0;
  192. PORTD = 0;
  193. PORTE = 0;
  194. PORTF = 0;
  195. asm volatile("jmp 0x1FC00");
  196. # endif
  197. #elif defined(BOOTLOADER_CATERINA)
  198. // this block may be optional
  199. // TODO: figure it out
  200. uint16_t *const bootKeyPtr = (uint16_t *)0x0800;
  201. // Value used by Caterina bootloader use to determine whether to run the
  202. // sketch or the bootloader programmer.
  203. uint16_t bootKey = 0x7777;
  204. *bootKeyPtr = bootKey;
  205. // setup watchdog timeout
  206. wdt_enable(WDTO_60MS);
  207. while (1) {
  208. } // wait for watchdog timer to trigger
  209. #elif defined(BOOTLOADER_USBASP)
  210. // Taken with permission of Stephan Baerwolf from https://github.com/tinyusbboard/API/blob/master/apipage.c
  211. wdt_enable(WDTO_15MS);
  212. wdt_reset();
  213. asm volatile("cli \n\t"
  214. "ldi r29 , %[ramendhi] \n\t"
  215. "ldi r28 , %[ramendlo] \n\t"
  216. # if (FLASHEND > 131071)
  217. "ldi r18 , %[bootaddrhi] \n\t"
  218. "st Y+, r18 \n\t"
  219. # endif
  220. "ldi r18 , %[bootaddrme] \n\t"
  221. "st Y+, r18 \n\t"
  222. "ldi r18 , %[bootaddrlo] \n\t"
  223. "st Y+, r18 \n\t"
  224. "out %[mcucsrio], __zero_reg__ \n\t"
  225. "bootloader_startup_loop%=: \n\t"
  226. "rjmp bootloader_startup_loop%= \n\t"
  227. :
  228. : [ mcucsrio ] "I"(_SFR_IO_ADDR(MCUCSR)),
  229. # if (FLASHEND > 131071)
  230. [ ramendhi ] "M"(((RAMEND - 2) >> 8) & 0xff), [ ramendlo ] "M"(((RAMEND - 2) >> 0) & 0xff), [ bootaddrhi ] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 16) & 0xff),
  231. # else
  232. [ ramendhi ] "M"(((RAMEND - 1) >> 8) & 0xff), [ ramendlo ] "M"(((RAMEND - 1) >> 0) & 0xff),
  233. # endif
  234. [ bootaddrme ] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 8) & 0xff), [ bootaddrlo ] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 0) & 0xff));
  235. #else // Assume remaining boards are DFU, even if the flag isn't set
  236. # if !(defined(__AVR_ATmega32A__) || defined(__AVR_ATmega328P__)) // no USB - maybe BOOTLOADER_BOOTLOADHID instead though?
  237. UDCON = 1;
  238. USBCON = (1 << FRZCLK); // disable USB
  239. UCSR1B = 0;
  240. _delay_ms(5); // 5 seems to work fine
  241. # endif
  242. # ifdef BOOTLOADER_BOOTLOADHID
  243. // force bootloadHID to stay in bootloader mode, so that it waits
  244. // for a new firmware to be flashed
  245. eeprom_write_byte((uint8_t *)1, 0x00);
  246. # endif
  247. // watchdog reset
  248. reset_key = BOOTLOADER_RESET_KEY;
  249. wdt_enable(WDTO_250MS);
  250. for (;;)
  251. ;
  252. #endif
  253. }
  254. /* this runs before main() */
  255. void bootloader_jump_after_watchdog_reset(void) __attribute__((used, naked, section(".init3")));
  256. void bootloader_jump_after_watchdog_reset(void) {
  257. #ifndef BOOTLOADER_HALFKAY
  258. if ((MCUCSR & (1 << WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
  259. reset_key = 0;
  260. // My custom USBasploader requires this to come up.
  261. MCUCSR = 0;
  262. // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog.
  263. MCUCSR &= ~(1 << WDRF);
  264. wdt_disable();
  265. // This is compled into 'icall', address should be in word unit, not byte.
  266. # ifdef BOOTLOADER_SIZE
  267. ((void (*)(void))((FLASH_SIZE - BOOTLOADER_SIZE) >> 1))();
  268. # else
  269. asm("ijmp" ::"z"(bootloader_start));
  270. # endif
  271. }
  272. #endif
  273. }