BootloaderDFU.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2018.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2018 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. * Header file for BootloaderDFU.c.
  29. */
  30. #ifndef _BOOTLOADER_H_
  31. #define _BOOTLOADER_H_
  32. /* Includes: */
  33. #include <avr/io.h>
  34. #include <avr/wdt.h>
  35. #include <avr/boot.h>
  36. #include <avr/pgmspace.h>
  37. #include <avr/eeprom.h>
  38. #include <avr/power.h>
  39. #include <avr/interrupt.h>
  40. #include <util/delay.h>
  41. #include <stdbool.h>
  42. #include "Descriptors.h"
  43. #include "BootloaderAPI.h"
  44. #include "Config/AppConfig.h"
  45. #include <LUFA/Drivers/USB/USB.h>
  46. #include <LUFA/Drivers/Board/LEDs.h>
  47. #include <LUFA/Platform/Platform.h>
  48. /* Preprocessor Checks: */
  49. #if !defined(__OPTIMIZE_SIZE__)
  50. #error This bootloader requires that it be optimized for size, not speed, to fit into the target device. Change optimization settings and try again.
  51. #endif
  52. /* Macros: */
  53. /** Major bootloader version number. */
  54. #define BOOTLOADER_VERSION_MINOR 2
  55. /** Minor bootloader version number. */
  56. #define BOOTLOADER_VERSION_REV 0
  57. /** Magic bootloader key to unlock forced application start mode. */
  58. #define MAGIC_BOOT_KEY 0xDC42
  59. /** Complete bootloader version number expressed as a packed byte, constructed from the
  60. * two individual bootloader version macros.
  61. */
  62. #define BOOTLOADER_VERSION ((BOOTLOADER_VERSION_MINOR << 4) | BOOTLOADER_VERSION_REV)
  63. /** First byte of the bootloader identification bytes, used to identify a device's bootloader. */
  64. #define BOOTLOADER_ID_BYTE1 0xDC
  65. /** Second byte of the bootloader identification bytes, used to identify a device's bootloader. */
  66. #define BOOTLOADER_ID_BYTE2 0xFB
  67. /** Convenience macro, used to determine if the issued command is the given one-byte long command.
  68. *
  69. * \param[in] dataarr Command byte array to check against
  70. * \param[in] cb1 First command byte to check
  71. */
  72. #define IS_ONEBYTE_COMMAND(dataarr, cb1) (dataarr[0] == (cb1))
  73. /** Convenience macro, used to determine if the issued command is the given two-byte long command.
  74. *
  75. * \param[in] dataarr Command byte array to check against
  76. * \param[in] cb1 First command byte to check
  77. * \param[in] cb2 Second command byte to check
  78. */
  79. #define IS_TWOBYTE_COMMAND(dataarr, cb1, cb2) ((dataarr[0] == (cb1)) && (dataarr[1] == (cb2)))
  80. /** Length of the DFU file suffix block, appended to the end of each complete memory write command.
  81. * The DFU file suffix is currently unused (but is designed to give extra file information, such as
  82. * a CRC of the complete firmware for error checking) and so is discarded.
  83. */
  84. #define DFU_FILE_SUFFIX_SIZE 16
  85. /** Length of the DFU file filler block, appended to the start of each complete memory write command.
  86. * Filler bytes are added to the start of each complete memory write command, and must be discarded.
  87. */
  88. #define DFU_FILLER_BYTES_SIZE 26
  89. /** DFU class command request to detach from the host. */
  90. #define DFU_REQ_DETATCH 0x00
  91. /** DFU class command request to send data from the host to the bootloader. */
  92. #define DFU_REQ_DNLOAD 0x01
  93. /** DFU class command request to send data from the bootloader to the host. */
  94. #define DFU_REQ_UPLOAD 0x02
  95. /** DFU class command request to get the current DFU status and state from the bootloader. */
  96. #define DFU_REQ_GETSTATUS 0x03
  97. /** DFU class command request to reset the current DFU status and state variables to their defaults. */
  98. #define DFU_REQ_CLRSTATUS 0x04
  99. /** DFU class command request to get the current DFU state of the bootloader. */
  100. #define DFU_REQ_GETSTATE 0x05
  101. /** DFU class command request to abort the current multi-request transfer and return to the dfuIDLE state. */
  102. #define DFU_REQ_ABORT 0x06
  103. /** DFU command to begin programming the device's memory. */
  104. #define COMMAND_PROG_START 0x01
  105. /** DFU command to begin reading the device's memory. */
  106. #define COMMAND_DISP_DATA 0x03
  107. /** DFU command to issue a write command. */
  108. #define COMMAND_WRITE 0x04
  109. /** DFU command to issue a read command. */
  110. #define COMMAND_READ 0x05
  111. /** DFU command to issue a memory base address change command, to set the current 64KB flash page
  112. * that subsequent flash operations should use. */
  113. #define COMMAND_CHANGE_BASE_ADDR 0x06
  114. /* Type Defines: */
  115. /** Type define for a non-returning function pointer to the loaded application. */
  116. typedef void (*AppPtr_t)(void) ATTR_NO_RETURN;
  117. /** Type define for a structure containing a complete DFU command issued by the host. */
  118. typedef struct
  119. {
  120. uint8_t Command; /**< Single byte command to perform, one of the \c COMMAND_* macro values */
  121. uint8_t Data[5]; /**< Command parameters */
  122. uint16_t DataSize; /**< Size of the command parameters */
  123. } DFU_Command_t;
  124. /* Enums: */
  125. /** DFU bootloader states. Refer to the DFU class specification for information on each state. */
  126. enum DFU_State_t
  127. {
  128. appIDLE = 0,
  129. appDETACH = 1,
  130. dfuIDLE = 2,
  131. dfuDNLOAD_SYNC = 3,
  132. dfuDNBUSY = 4,
  133. dfuDNLOAD_IDLE = 5,
  134. dfuMANIFEST_SYNC = 6,
  135. dfuMANIFEST = 7,
  136. dfuMANIFEST_WAIT_RESET = 8,
  137. dfuUPLOAD_IDLE = 9,
  138. dfuERROR = 10
  139. };
  140. /** DFU command status error codes. Refer to the DFU class specification for information on each error code. */
  141. enum DFU_Status_t
  142. {
  143. OK = 0,
  144. errTARGET = 1,
  145. errFILE = 2,
  146. errWRITE = 3,
  147. errERASE = 4,
  148. errCHECK_ERASED = 5,
  149. errPROG = 6,
  150. errVERIFY = 7,
  151. errADDRESS = 8,
  152. errNOTDONE = 9,
  153. errFIRMWARE = 10,
  154. errVENDOR = 11,
  155. errUSBR = 12,
  156. errPOR = 13,
  157. errUNKNOWN = 14,
  158. errSTALLEDPKT = 15
  159. };
  160. /* Function Prototypes: */
  161. static void SetupHardware(void);
  162. static void ResetHardware(void);
  163. void EVENT_USB_Device_ControlRequest(void);
  164. #if defined(INCLUDE_FROM_BOOTLOADER_C)
  165. static void DiscardFillerBytes(uint8_t NumberOfBytes);
  166. static void ProcessBootloaderCommand(void);
  167. static void LoadStartEndAddresses(void);
  168. static void ProcessMemProgCommand(void);
  169. static void ProcessMemReadCommand(void);
  170. static void ProcessWriteCommand(void);
  171. static void ProcessReadCommand(void);
  172. #endif
  173. void Application_Jump_Check(void) ATTR_INIT_SECTION(3);
  174. #endif