upload
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

bootloader.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include <avr/io.h>
  4. #include <avr/interrupt.h>
  5. #include <avr/wdt.h>
  6. #include <util/delay.h>
  7. #include "bootloader.h"
  8. #ifdef PROTOCOL_LUFA
  9. #include <LUFA/Drivers/USB/USB.h>
  10. #endif
  11. /* Bootloader Size in *bytes*
  12. *
  13. * AVR Boot section size are defined by setting BOOTSZ fuse in fact. Consult with your MCU datasheet.
  14. * Note that 'Word'(2 bytes) size and address are used in datasheet while TMK uses 'Byte'.
  15. *
  16. *
  17. * Size of Bootloaders in bytes:
  18. * Atmel DFU loader(ATmega32U4) 4096
  19. * Atmel DFU loader(AT90USB128) 8192
  20. * LUFA bootloader(ATmega32U4) 4096
  21. * Arduino Caterina(ATmega32U4) 4096
  22. * USBaspLoader(ATmega***) 2048
  23. * Teensy halfKay(ATmega32U4) 512
  24. * Teensy++ halfKay(AT90USB128) 1024
  25. *
  26. *
  27. * AVR Boot section is located at the end of Flash memory like the followings.
  28. *
  29. *
  30. * byte Atmel/LUFA(ATMega32u4) byte Atmel(AT90SUB128)
  31. * 0x0000 +---------------+ 0x00000 +---------------+
  32. * | | | |
  33. * | | | |
  34. * | Application | | Application |
  35. * | | | |
  36. * = = = =
  37. * | | 32KB-4KB | | 128KB-8KB
  38. * 0x6000 +---------------+ 0x1FC00 +---------------+
  39. * | Bootloader | 4KB | Bootloader | 8KB
  40. * 0x7FFF +---------------+ 0x1FFFF +---------------+
  41. *
  42. *
  43. * byte Teensy(ATMega32u4) byte Teensy++(AT90SUB128)
  44. * 0x0000 +---------------+ 0x00000 +---------------+
  45. * | | | |
  46. * | | | |
  47. * | Application | | Application |
  48. * | | | |
  49. * = = = =
  50. * | | 32KB-512B | | 128KB-1KB
  51. * 0x7E00 +---------------+ 0x1FC00 +---------------+
  52. * | Bootloader | 512B | Bootloader | 1KB
  53. * 0x7FFF +---------------+ 0x1FFFF +---------------+
  54. */
  55. #ifndef BOOTLOADER_SIZE
  56. #warning To use bootloader_jump() you need to define BOOTLOADER_SIZE in config.h.
  57. #define BOOTLOADER_SIZE 4096
  58. #endif
  59. #define FLASH_SIZE (FLASHEND + 1L)
  60. #define BOOTLOADER_START (FLASH_SIZE - BOOTLOADER_SIZE)
  61. /*
  62. * Entering the Bootloader via Software
  63. * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
  64. */
  65. #define BOOTLOADER_RESET_KEY 0xB007B007
  66. uint32_t reset_key __attribute__ ((section (".noinit")));
  67. /* initialize MCU status by watchdog reset */
  68. void bootloader_jump(void) {
  69. #ifdef PROTOCOL_LUFA
  70. USB_Disable();
  71. cli();
  72. _delay_ms(2000);
  73. #endif
  74. #ifdef PROTOCOL_PJRC
  75. cli();
  76. UDCON = 1;
  77. USBCON = (1<<FRZCLK);
  78. UCSR1B = 0;
  79. _delay_ms(5);
  80. #endif
  81. // watchdog reset
  82. reset_key = BOOTLOADER_RESET_KEY;
  83. wdt_enable(WDTO_250MS);
  84. for (;;);
  85. }
  86. /* this runs before main() */
  87. void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3")));
  88. void bootloader_jump_after_watchdog_reset(void)
  89. {
  90. if ((MCUSR & (1<<WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
  91. reset_key = 0;
  92. // My custom USBasploader requires this to come up.
  93. MCUSR = 0;
  94. // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog.
  95. MCUSR &= ~(1<<WDRF);
  96. wdt_disable();
  97. // This is compled into 'icall', address should be in word unit, not byte.
  98. ((void (*)(void))(BOOTLOADER_START/2))();
  99. }
  100. }
  101. #if 0
  102. /* Jumping To The Bootloader
  103. * http://www.pjrc.com/teensy/jump_to_bootloader.html
  104. *
  105. * This method doen't work when using LUFA. idk why.
  106. * - needs to initialize more regisers or interrupt setting?
  107. */
  108. void bootloader_jump(void) {
  109. #ifdef PROTOCOL_LUFA
  110. USB_Disable();
  111. cli();
  112. _delay_ms(2000);
  113. #endif
  114. #ifdef PROTOCOL_PJRC
  115. cli();
  116. UDCON = 1;
  117. USBCON = (1<<FRZCLK);
  118. UCSR1B = 0;
  119. _delay_ms(5);
  120. #endif
  121. /*
  122. * Initialize
  123. */
  124. #if defined(__AVR_AT90USB162__)
  125. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
  126. TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
  127. DDRB = 0; DDRC = 0; DDRD = 0;
  128. PORTB = 0; PORTC = 0; PORTD = 0;
  129. #elif defined(__AVR_ATmega32U4__)
  130. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  131. TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
  132. DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
  133. PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  134. #elif defined(__AVR_AT90USB646__)
  135. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  136. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  137. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  138. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  139. #elif defined(__AVR_AT90USB1286__)
  140. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  141. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  142. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  143. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  144. #endif
  145. /*
  146. * USBaspLoader
  147. */
  148. #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
  149. // This makes custom USBasploader come up.
  150. MCUSR = 0;
  151. // initialize ports
  152. PORTB = 0; PORTC= 0; PORTD = 0;
  153. DDRB = 0; DDRC= 0; DDRD = 0;
  154. // disable interrupts
  155. EIMSK = 0; EECR = 0; SPCR = 0;
  156. ACSR = 0; SPMCSR = 0; WDTCSR = 0; PCICR = 0;
  157. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0;
  158. ADCSRA = 0; TWCR = 0; UCSR0B = 0;
  159. #endif
  160. // This is compled into 'icall', address should be in word unit, not byte.
  161. ((void (*)(void))(BOOTLOADER_START/2))();
  162. }
  163. #endif