Keyboard firmwares for Atmel AVR and Cortex-M
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

bootloader.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. /* Boot Section Size in bytes
  12. * Teensy halfKay 512
  13. * Atmel DFU loader 4096
  14. * LUFA bootloader 4096
  15. */
  16. #ifndef BOOT_SIZE
  17. #define BOOT_SIZE 512
  18. #endif
  19. #define FLASH_SIZE (FLASHEND + 1)
  20. #define BOOTLOADER_START (FLASH_SIZE - BOOT_SIZE)
  21. /*
  22. * Entering the Bootloader via Software
  23. * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
  24. */
  25. #define BOOTLOADER_RESET_KEY 0xB007B007
  26. uint32_t reset_key __attribute__ ((section (".noinit")));
  27. /* initialize MCU status by watchdog reset */
  28. void bootloader_jump(void) {
  29. #ifdef PROTOCOL_LUFA
  30. USB_Disable();
  31. cli();
  32. _delay_ms(2000);
  33. #endif
  34. #ifdef PROTOCOL_PJRC
  35. cli();
  36. UDCON = 1;
  37. USBCON = (1<<FRZCLK);
  38. UCSR1B = 0;
  39. _delay_ms(5);
  40. #endif
  41. // watchdog reset
  42. reset_key = BOOTLOADER_RESET_KEY;
  43. wdt_enable(WDTO_250MS);
  44. for (;;);
  45. }
  46. /* this runs before main() */
  47. void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3")));
  48. void bootloader_jump_after_watchdog_reset(void)
  49. {
  50. if ((MCUSR & (1<<WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
  51. #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
  52. // This makes custom USBasploader come up.
  53. MCUSR = 0;
  54. #endif
  55. reset_key = 0;
  56. ((void (*)(void))BOOTLOADER_START)();
  57. }
  58. }
  59. #if 0
  60. /* Jumping To The Bootloader
  61. * http://www.pjrc.com/teensy/jump_to_bootloader.html
  62. *
  63. * This method doen't work when using LUFA. idk why.
  64. * - needs to initialize more regisers or interrupt setting?
  65. */
  66. void bootloader_jump(void) {
  67. #ifdef PROTOCOL_LUFA
  68. USB_Disable();
  69. cli();
  70. _delay_ms(2000);
  71. #endif
  72. #ifdef PROTOCOL_PJRC
  73. cli();
  74. UDCON = 1;
  75. USBCON = (1<<FRZCLK);
  76. UCSR1B = 0;
  77. _delay_ms(5);
  78. #endif
  79. /*
  80. * Initialize
  81. */
  82. #if defined(__AVR_AT90USB162__)
  83. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
  84. TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
  85. DDRB = 0; DDRC = 0; DDRD = 0;
  86. PORTB = 0; PORTC = 0; PORTD = 0;
  87. #elif defined(__AVR_ATmega32U4__)
  88. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  89. TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
  90. DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
  91. PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  92. #elif defined(__AVR_AT90USB646__)
  93. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  94. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  95. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  96. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  97. #elif defined(__AVR_AT90USB1286__)
  98. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  99. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  100. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  101. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  102. #endif
  103. /*
  104. * USBaspLoader
  105. */
  106. #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
  107. // This makes custom USBasploader come up.
  108. MCUSR = 0;
  109. // initialize ports
  110. PORTB = 0; PORTC= 0; PORTD = 0;
  111. DDRB = 0; DDRC= 0; DDRD = 0;
  112. // disable interrupts
  113. EIMSK = 0; EECR = 0; SPCR = 0;
  114. ACSR = 0; SPMCSR = 0; WDTCSR = 0; PCICR = 0;
  115. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0;
  116. ADCSRA = 0; TWCR = 0; UCSR0B = 0;
  117. #endif
  118. // start Bootloader
  119. ((void (*)(void))BOOTLOADER_START)();
  120. }
  121. #endif