reboot.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include "usbcas.h"
  2. /* ATmega32U4 with Caterina bootloader */
  3. #define FLASH_SIZE_BYTES 32768
  4. #define BOOTSZ 0
  5. #define BOOTLOADER_SEC_SIZE (4096 >> BOOTSZ)
  6. #define MAGIC_BOOT_KEY 0xdc42acca
  7. #define BOOTLOADER_START_ADDRESS (FLASH_SIZE_BYTES - BOOTLOADER_SEC_SIZE)
  8. static uint32_t boot_key_magic ATTR_NO_INIT;
  9. static void bootloader_jump_check(void) ATTR_INIT_SECTION(3);
  10. static void bootloader_jump_check(void)
  11. {
  12. void (* const bootloader)(void) =
  13. (void (*)(void))BOOTLOADER_START_ADDRESS;
  14. /*
  15. * If the reset source was the watch dog and the boot key magic is set,
  16. * clear the magic and jump into the boot loader
  17. */
  18. if (boot_key_magic == MAGIC_BOOT_KEY) {
  19. boot_key_magic = 0;
  20. bootloader();
  21. }
  22. }
  23. /*
  24. * Host sent BREAK; reboot into bootloader
  25. */
  26. void EVENT_CDC_Device_BreakSent(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, const uint8_t duration)
  27. {
  28. /* Set the bootloader invocation key */
  29. boot_key_magic = MAGIC_BOOT_KEY;
  30. /* Arm the watchdog to force a hardware reset */
  31. wdt_enable(WDTO_250MS);
  32. /* Return to the main loop so we ack the send break correctly */
  33. }