bootloader.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
  105. TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
  106. DDRB = 0; DDRC = 0; DDRD = 0;
  107. PORTB = 0; PORTC = 0; PORTD = 0;
  108. asm volatile("jmp 0x3E00");
  109. #elif defined(__AVR_ATmega32U4__) // Teensy 2.0
  110. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  111. TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
  112. DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
  113. PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  114. asm volatile("jmp 0x7E00");
  115. #elif defined(__AVR_AT90USB646__) // Teensy++ 1.0
  116. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  117. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  118. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  119. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  120. asm volatile("jmp 0xFC00");
  121. #elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0
  122. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  123. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  124. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  125. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  126. asm volatile("jmp 0x1FC00");
  127. #endif
  128. #elif defined(BOOTLOADER_CATERINA)
  129. // this block may be optional
  130. // TODO: figure it out
  131. uint16_t *const bootKeyPtr = (uint16_t *)0x0800;
  132. // Value used by Caterina bootloader use to determine whether to run the
  133. // sketch or the bootloader programmer.
  134. uint16_t bootKey = 0x7777;
  135. *bootKeyPtr = bootKey;
  136. // setup watchdog timeout
  137. wdt_enable(WDTO_60MS);
  138. while(1) {} // wait for watchdog timer to trigger
  139. #elif defined(BOOTLOADER_USBASP)
  140. // Taken with permission of Stephan Baerwolf from https://github.com/tinyusbboard/API/blob/master/apipage.c
  141. wdt_enable(WDTO_15MS);
  142. wdt_reset();
  143. asm volatile (
  144. "cli \n\t"
  145. "ldi r29 , %[ramendhi] \n\t"
  146. "ldi r28 , %[ramendlo] \n\t"
  147. #if (FLASHEND>131071)
  148. "ldi r18 , %[bootaddrhi] \n\t"
  149. "st Y+, r18 \n\t"
  150. #endif
  151. "ldi r18 , %[bootaddrme] \n\t"
  152. "st Y+, r18 \n\t"
  153. "ldi r18 , %[bootaddrlo] \n\t"
  154. "st Y+, r18 \n\t"
  155. "out %[mcucsrio], __zero_reg__ \n\t"
  156. "bootloader_startup_loop%=: \n\t"
  157. "rjmp bootloader_startup_loop%= \n\t"
  158. :
  159. : [mcucsrio] "I" (_SFR_IO_ADDR(MCUCSR)),
  160. #if (FLASHEND>131071)
  161. [ramendhi] "M" (((RAMEND - 2) >> 8) & 0xff),
  162. [ramendlo] "M" (((RAMEND - 2) >> 0) & 0xff),
  163. [bootaddrhi] "M" ((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >>16) & 0xff),
  164. #else
  165. [ramendhi] "M" (((RAMEND - 1) >> 8) & 0xff),
  166. [ramendlo] "M" (((RAMEND - 1) >> 0) & 0xff),
  167. #endif
  168. [bootaddrme] "M" ((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 8) & 0xff),
  169. [bootaddrlo] "M" ((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 0) & 0xff)
  170. );
  171. #else // Assume remaining boards are DFU, even if the flag isn't set
  172. #if !(defined(__AVR_ATmega32A__) || defined(__AVR_ATmega328P__)) // no USB - maybe BOOTLOADER_BOOTLOADHID instead though?
  173. UDCON = 1;
  174. USBCON = (1<<FRZCLK); // disable USB
  175. UCSR1B = 0;
  176. _delay_ms(5); // 5 seems to work fine
  177. #endif
  178. #ifdef BOOTLOADER_BOOTLOADHID
  179. // force bootloadHID to stay in bootloader mode, so that it waits
  180. // for a new firmware to be flashed
  181. eeprom_write_byte((uint8_t *)1, 0x00);
  182. #endif
  183. // watchdog reset
  184. reset_key = BOOTLOADER_RESET_KEY;
  185. wdt_enable(WDTO_250MS);
  186. for (;;);
  187. #endif
  188. }
  189. /* this runs before main() */
  190. void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3")));
  191. void bootloader_jump_after_watchdog_reset(void)
  192. {
  193. #ifndef BOOTLOADER_HALFKAY
  194. if ((MCUCSR & (1<<WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
  195. reset_key = 0;
  196. // My custom USBasploader requires this to come up.
  197. MCUCSR = 0;
  198. // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog.
  199. MCUCSR &= ~(1<<WDRF);
  200. wdt_disable();
  201. // This is compled into 'icall', address should be in word unit, not byte.
  202. #ifdef BOOTLOADER_SIZE
  203. ((void (*)(void))( (FLASH_SIZE - BOOTLOADER_SIZE) >> 1))();
  204. #else
  205. asm("ijmp" :: "z" (bootloader_start));
  206. #endif
  207. }
  208. #endif
  209. }