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 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. // disable watchdog, if enabled
  24. // disable all peripherals
  25. UDCON = 1;
  26. USBCON = (1<<FRZCLK); // disable USB
  27. UCSR1B = 0;
  28. _delay_ms(5);
  29. #if defined(__AVR_AT90USB162__)
  30. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
  31. TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
  32. DDRB = 0; DDRC = 0; DDRD = 0;
  33. PORTB = 0; PORTC = 0; PORTD = 0;
  34. #elif defined(__AVR_ATmega32U4__)
  35. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  36. TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
  37. DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
  38. PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  39. #elif defined(__AVR_AT90USB646__)
  40. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  41. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  42. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  43. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  44. #elif defined(__AVR_AT90USB1286__)
  45. EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
  46. TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
  47. DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
  48. PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
  49. #endif
  50. // start Bootloader
  51. ((void (*)(void))BOOTLOADER_START)();
  52. }