usb_dfu.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* This file is the part of the Lightweight USB device Stack for STM32 microcontrollers
  2. *
  3. * Copyright ©2016 Dmitry Filimonchuk <dmitrystu[at]gmail[dot]com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #ifndef _USB_DFU_H_
  16. #define _USB_DFU_H_
  17. #if defined(__cplusplus)
  18. extern "C" {
  19. #endif
  20. /**\addtogroup USB_MODULE_DFU USB DFU class
  21. * \brief This module contains USB Device Firmware Upgrade class definitions.
  22. * \details This module based on
  23. * + [USB Device Firmware Upgrade Specification, Revision 1.1]
  24. * (https://www.usb.org/sites/default/files/DFU_1.1.pdf)
  25. * @{ */
  26. /**\name USB DFU class subclass and protocol definitions
  27. * @{ */
  28. #define USB_CLASS_DFU 0xFE /**<\brief USB DFU class.*/
  29. #define USB_DFU_SUBCLASS_DFU 0x01 /**<\brief USB DFU subclass code.*/
  30. #define USB_DFU_PROTO_RUNTIME 0x01 /**<\brief USB DFU runtime-mode protocol.*/
  31. #define USB_DFU_PROTO_DFU 0x02 /**<\brief USB DFU DFU-mode protocol.*/
  32. /** @{ */
  33. /**\name USB DFU descriptor types */
  34. #define USB_DTYPE_DFU_FUNCTIONAL 0x21 /**<\brief USB DFU functional descriptor type.*/
  35. /**\name USB DFU class-specific requests
  36. * @{ */
  37. #define USB_DFU_DETACH 0x00 /**<\brief Initiates a detach-attach sequence on the
  38. * bus when it sees this request. */
  39. #define USB_DFU_DNLOAD 0x01 /**<\brief Initiates firmware image downloading */
  40. #define USB_DFU_UPLOAD 0x02 /**<\brief This request is employed by the host to
  41. * solicit firmware from the device.*/
  42. #define USB_DFU_GETSTATUS 0x03 /**<\brief The host employs this request to facilitate
  43. * synchronization with the device.*/
  44. #define USB_DFU_CLRSTATUS 0x04 /**<\brief This request resets DFU machine state to
  45. * DFU_IDLE.*/
  46. #define USB_DFU_GETSTATE 0x05 /**<\brief This request solicits a report about the
  47. * state of the device.*/
  48. #define USB_DFU_ABORT 0x06 /**<\brief This request enables the host to exit from
  49. * certain states and return to the DFU_IDLE state.*/
  50. /** @} */
  51. /**\anchor USB_DFU_CAPAB
  52. * \name USB DFU capabilities
  53. * @{ */
  54. #define USB_DFU_ATTR_WILL_DETACH 0x08 /**<\brief Device will perform a bus detach-attach
  55. * sequence when it receives a DFU_DETACH request.*/
  56. #define USB_DFU_ATTR_MANIF_TOL 0x04 /**<\brief Device is able to communicate via USB
  57. * after Manifestation phase.*/
  58. #define USB_DFU_ATTR_CAN_UPLOAD 0x02 /**<\brief Upload capable.*/
  59. #define USB_DFU_ATTR_CAN_DNLOAD 0x01 /**<\brief Download capable.*/
  60. /** @} */
  61. /**\name USB DFU status codes
  62. * @{ */
  63. #define USB_DFU_STATUS_OK 0x00 /**<\brief No error condition is present.*/
  64. #define USB_DFU_STATUS_ERR_TARGET 0x01 /**<\brief File is not targeted for use by this device.*/
  65. #define USB_DFU_STATUS_ERR_FILE 0x02 /**<\brief File is for this device but fails some
  66. * vendor specific verification test.*/
  67. #define USB_DFU_STATUS_ERR_WRITE 0x03 /**<\brief Device is unable to write memory.*/
  68. #define USB_DFU_STATUS_ERR_ERASE 0x04 /**<\brief Memory erase function failed.*/
  69. #define USB_DFU_STATUS_ERR_CHECK_ERASED 0x05 /**<\brief Memory erase check failed.*/
  70. #define USB_DFU_STATUS_ERR_PROG 0x06 /**<\brief Program memory function failed.*/
  71. #define USB_DFU_STATUS_ERR_VERIFY 0x07 /**<\brief Programmed memory failed verification.*/
  72. #define USB_DFU_STATUS_ERR_ADDRESS 0x08 /**<\brief Cannot program memory due to received
  73. * address that is out of range. */
  74. #define USB_DFU_STATUS_ERR_NOTDONE 0x09 /**<\brief Received DFU_DNLOAD with wLength = 0, but
  75. * device does not think it has all of the data yet.*/
  76. #define USB_DFU_STATUS_ERR_FIRMWARE 0x0A /**<\brief Device's firmware is corrupt. It cannot
  77. * return to run-time (non-DFU) operations.*/
  78. #define USB_DFU_STATUS_ERR_VENDOR 0x0B /**<\brief iString indicates a vendor-specific error.*/
  79. #define USB_DFU_STATUS_ERR_USBR 0x0C /**<\brief Device detected unexpected USB reset signaling.*/
  80. #define USB_DFU_STATUS_ERR_POR 0x0D /**<\brief Device detected unexpected power on reset. */
  81. #define USB_DFU_STATUS_ERR_UNKNOWN 0x0E /**<\brief Something went wrong, but the device does
  82. * not know what it was.*/
  83. #define USB_DFU_STATUS_ERR_STALLEDPKT 0x0F /**<\brief Device stalled an unexpected request.*/
  84. /** @} */
  85. /**\name USB DFU state codes
  86. * @{ */
  87. #define USB_DFU_STATE_APP_IDLE 0x00 /**<\brief Device is running its normal application.*/
  88. #define USB_DFU_STATE_APP_DETACH 0x01 /**<\brief Device is running its normal application,
  89. * has received the DFU_DETACH request, and is
  90. * waiting for a USB reset. */
  91. #define USB_DFU_STATE_DFU_IDLE 0x02 /**<\brief Device is operating in the DFU mode and
  92. * is waiting for requests. */
  93. #define USB_DFU_STATE_DFU_DNLOADSYNC 0x03 /**<\brief Device has received a block and is waiting
  94. * for the host to solicit the status via DFU_GETSTATUS. */
  95. #define USB_DFU_STATE_DFU_DNBUSY 0x04 /**<\brief Device is programming a control-write block
  96. * into its nonvolatile memories. */
  97. #define USB_DFU_STATE_DFU_DNLOADIDLE 0x05 /**<\brief Device is processing a download operation.
  98. * Expecting DFU_DNLOAD requests. */
  99. #define USB_DFU_STATE_DFU_MANIFESTSYNC 0x06 /**<\brief Device has received the final block of
  100. * firmware from the host and is waiting for receipt
  101. * of DFU_GETSTATUS to begin the Manifestation phase;
  102. * or device has completed the Manifestation phase
  103. * and is waiting for receipt of DFU_GETSTATUS.
  104. * \note Devices that can enter this state after the
  105. * Manifestation phase set bmAttributes bit
  106. * bitManifestationTolerant to 1. */
  107. #define USB_DFU_STATE_DFU_MANIFEST 0x07 /**<\brief Device is in the Manifestation phase.
  108. * \note Not all devices will be able to respond to
  109. * DFU_GETSTATUS when in this state.*/
  110. #define USB_DFU_STATE_DFU_MANIFESTWR 0x08 /**<\brief Device has programmed its memories and is
  111. * waiting for a USB reset or a power on reset.
  112. * \note Devices that must enter this state clear
  113. * bitManifestationTolerant to 0.*/
  114. #define USB_DFU_STATE_DFU_UPLOADIDLE 0x09 /**<\brief The device is processing an upload operation.*/
  115. #define USB_DFU_STATE_DFU_ERROR 0x0A /**<\brief An error has occurred. */
  116. /** @} */
  117. /**\brief USB DFU functional descriptor */
  118. struct usb_dfu_func_desc {
  119. uint8_t bLength; /**<\brief Descriptor length in bytes.*/
  120. uint8_t bDescriptorType; /**<\brief DFU functional descriptor type.*/
  121. uint8_t bmAttributes; /**<\brief USB DFU capabilities \ref USB_DFU_CAPAB*/
  122. uint16_t wDetachTimeout; /**<\brief USB DFU detach timeout in ms.*/
  123. uint16_t wTransferSize; /**<\brief USB DFU maximum transfer block size in bytes.*/
  124. uint16_t bcdDFUVersion; /**<\brief USB DFU version \ref VERSION_BCD utility macro.*/
  125. }__attribute__((packed));
  126. /**\brief Payload packet to response in DFU_GETSTATUS request */
  127. struct usb_dfu_status {
  128. uint8_t bStatus; /**<\brief An indication of the status resulting from the
  129. * execution of the most recent request.*/
  130. uint8_t bPollTimeout; /**<\brief Minimum time (LSB) in ms, that the host should wait
  131. * before sending a subsequent DFU_GETSTATUS request.*/
  132. uint16_t wPollTimeout; /**<\brief Minimum time (MSB) in ms, that the host should wait
  133. * before sending a subsequent DFU_GETSTATUS request.*/
  134. uint8_t bState; /**<\brief An indication of the state that the device is going
  135. * to enter immediately following transmission of this response.*/
  136. uint8_t iString; /**<\brief Index of the status string descriptor.*/
  137. };
  138. /** @} */
  139. #if defined(__cplusplus)
  140. }
  141. #endif
  142. #endif /* _USB_DFU_H_ */