Keyboard firmwares for Atmel AVR and Cortex-M
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.

bootloader.c 2.6KB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <util/delay.h>
  4. #include "bootloader.h"
  5. /* Start Bootloader from Application
  6. * See
  7. * http://www.pjrc.com/teensy/jump_to_bootloader.html
  8. * http://www.fourwalledcubicle.com/files/LUFA/Doc/120219/html/_page__software_bootloader_start.html
  9. */
  10. // TODO: support usbasp
  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 (FLASHEND - BOOT_SIZE)
  21. void bootloader_jump(void) {
  22. cli();
  23. //
  24. //Teensy
  25. //
  26. #if defined(__AVR_AT90USB162__) || defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  27. // disable watchdog, if enabled
  28. // disable all peripherals
  29. UDCON = 1;
  30. USBCON = (1<<FRZCLK); // disable USB
  31. UCSR1B = 0;
  32. _delay_ms(5);
  33. #else
  34. // This makes custom USBasploader come up.
  35. MCUSR = 0;
  36. #endif
  37. #if defined(__AVR_AT90USB162__)
  38. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
  39. TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
  40. DDRB = 0; DDRC = 0; DDRD = 0;
  41. PORTB = 0; PORTC = 0; PORTD = 0;
  42. #elif defined(__AVR_ATmega32U4__)
  43. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  44. TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
  45. DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
  46. PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  47. #elif defined(__AVR_AT90USB646__)
  48. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  49. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  50. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  51. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  52. #elif defined(__AVR_AT90USB1286__)
  53. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  54. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  55. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  56. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  57. #endif
  58. //
  59. //USBasp
  60. //
  61. #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
  62. // This makes custom USBasploader come up.
  63. MCUSR = 0;
  64. // initialize ports
  65. PORTB = 0; PORTC= 0; PORTD = 0;
  66. DDRB = 0; DDRC= 0; DDRD = 0;
  67. // disable interrupts
  68. EIMSK = 0; EECR = 0; SPCR = 0;
  69. ACSR = 0; SPMCSR = 0; WDTCSR = 0; PCICR = 0;
  70. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0;
  71. ADCSRA = 0; TWCR = 0; UCSR0B = 0;
  72. #endif
  73. // start Bootloader
  74. ((void (*)(void))BOOTLOADER_START)();
  75. }