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.

BootloaderHID.c 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. *
  28. * Main source file for the HID class bootloader. This file contains the complete bootloader logic.
  29. */
  30. #include "BootloaderHID.h"
  31. /** Flag to indicate if the bootloader should be running, or should exit and allow the application code to run
  32. * via a soft reset. When cleared, the bootloader will abort, the USB interface will shut down and the application
  33. * started via a forced watchdog reset.
  34. */
  35. static bool RunBootloader = true;
  36. /** Magic lock for forced application start. If the HWBE fuse is programmed and BOOTRST is unprogrammed, the bootloader
  37. * will start if the /HWB line of the AVR is held low and the system is reset. However, if the /HWB line is still held
  38. * low when the application attempts to start via a watchdog reset, the bootloader will re-start. If set to the value
  39. * \ref MAGIC_BOOT_KEY the special init function \ref Application_Jump_Check() will force the application to start.
  40. */
  41. uint16_t MagicBootKey ATTR_NO_INIT;
  42. /** Special startup routine to check if the bootloader was started via a watchdog reset, and if the magic application
  43. * start key has been loaded into \ref MagicBootKey. If the bootloader started via the watchdog and the key is valid,
  44. * this will force the user application to start via a software jump.
  45. */
  46. void Application_Jump_Check(void)
  47. {
  48. /* If the reset source was the bootloader and the key is correct, clear it and jump to the application */
  49. if ((MCUSR & (1 << WDRF)) && (MagicBootKey == MAGIC_BOOT_KEY))
  50. {
  51. MagicBootKey = 0;
  52. // cppcheck-suppress constStatement
  53. ((void (*)(void))0x0000)();
  54. }
  55. }
  56. /** Main program entry point. This routine configures the hardware required by the bootloader, then continuously
  57. * runs the bootloader processing routine until instructed to soft-exit.
  58. */
  59. int main(void)
  60. {
  61. /* Setup hardware required for the bootloader */
  62. SetupHardware();
  63. /* Enable global interrupts so that the USB stack can function */
  64. GlobalInterruptEnable();
  65. while (RunBootloader)
  66. USB_USBTask();
  67. /* Disconnect from the host - USB interface will be reset later along with the AVR */
  68. USB_Detach();
  69. /* Unlock the forced application start mode of the bootloader if it is restarted */
  70. MagicBootKey = MAGIC_BOOT_KEY;
  71. /* Enable the watchdog and force a timeout to reset the AVR */
  72. wdt_enable(WDTO_250MS);
  73. for (;;);
  74. }
  75. /** Configures all hardware required for the bootloader. */
  76. static void SetupHardware(void)
  77. {
  78. /* Disable watchdog if enabled by bootloader/fuses */
  79. MCUSR &= ~(1 << WDRF);
  80. wdt_disable();
  81. /* Relocate the interrupt vector table to the bootloader section */
  82. MCUCR = (1 << IVCE);
  83. MCUCR = (1 << IVSEL);
  84. /* Initialize USB subsystem */
  85. USB_Init();
  86. }
  87. /** Event handler for the USB_ConfigurationChanged event. This configures the device's endpoints ready
  88. * to relay data to and from the attached USB host.
  89. */
  90. void EVENT_USB_Device_ConfigurationChanged(void)
  91. {
  92. /* Setup HID Report Endpoint */
  93. Endpoint_ConfigureEndpoint(HID_IN_EPADDR, EP_TYPE_INTERRUPT, HID_IN_EPSIZE, 1);
  94. }
  95. /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
  96. * the device from the USB host before passing along unhandled control requests to the library for processing
  97. * internally.
  98. */
  99. void EVENT_USB_Device_ControlRequest(void)
  100. {
  101. /* Ignore any requests that aren't directed to the HID interface */
  102. if ((USB_ControlRequest.bmRequestType & (CONTROL_REQTYPE_TYPE | CONTROL_REQTYPE_RECIPIENT)) !=
  103. (REQTYPE_CLASS | REQREC_INTERFACE))
  104. {
  105. return;
  106. }
  107. /* Process HID specific control requests */
  108. switch (USB_ControlRequest.bRequest)
  109. {
  110. case HID_REQ_SetReport:
  111. Endpoint_ClearSETUP();
  112. /* Wait until the command has been sent by the host */
  113. while (!(Endpoint_IsOUTReceived()));
  114. /* Read in the write destination address */
  115. #if (FLASHEND > 0xFFFF)
  116. uint32_t PageAddress = ((uint32_t)Endpoint_Read_16_LE() << 8);
  117. #else
  118. uint16_t PageAddress = Endpoint_Read_16_LE();
  119. #endif
  120. /* Check if the command is a program page command, or a start application command */
  121. #if (FLASHEND > 0xFFFF)
  122. if ((uint16_t)(PageAddress >> 8) == COMMAND_STARTAPPLICATION)
  123. #else
  124. if (PageAddress == COMMAND_STARTAPPLICATION)
  125. #endif
  126. {
  127. RunBootloader = false;
  128. }
  129. else
  130. {
  131. /* Erase the given FLASH page, ready to be programmed */
  132. boot_page_erase(PageAddress);
  133. boot_spm_busy_wait();
  134. /* Write each of the FLASH page's bytes in sequence */
  135. for (uint8_t PageWord = 0; PageWord < (SPM_PAGESIZE / 2); PageWord++)
  136. {
  137. /* Check if endpoint is empty - if so clear it and wait until ready for next packet */
  138. if (!(Endpoint_BytesInEndpoint()))
  139. {
  140. Endpoint_ClearOUT();
  141. while (!(Endpoint_IsOUTReceived()));
  142. }
  143. /* Write the next data word to the FLASH page */
  144. boot_page_fill(PageAddress + ((uint16_t)PageWord << 1), Endpoint_Read_16_LE());
  145. }
  146. /* Write the filled FLASH page to memory */
  147. boot_page_write(PageAddress);
  148. boot_spm_busy_wait();
  149. /* Re-enable RWW section */
  150. boot_rww_enable();
  151. }
  152. Endpoint_ClearOUT();
  153. Endpoint_ClearStatusStage();
  154. break;
  155. }
  156. }