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.

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