You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
8.5 KiB

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