1
0

usb_dfu.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* This file is the part of the LUS32 project
  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](http://www.usb.org/developers/docs/devclass_docs/DFU_1.1.pdf)
  24. * @{ */
  25. /**\name USB DFU class subclass and protocol definitions
  26. * @{ */
  27. #define USB_CLASS_DFU 0xFE /**< USB DFU class is an application-specified class */
  28. #define USB_DFU_SUBCLASS_DFU 0x01 /**< USB DFU subclass code */
  29. #define USB_DFU_PROTO_RUNTIME 0x01 /**< USB DFU runtime-mode protocol */
  30. #define USB_DFU_PROTO_DFU 0x02 /**< USB DFU DFU-mode protocol */
  31. /** @{ */
  32. /**\name USB DFU descriptor types */
  33. #define USB_DTYPE_DFU_FUNCTIONAL 0x21 /**< USB DFU functional descriptor type */
  34. /**\name USB DFU class-specific requests
  35. * @{ */
  36. #define USB_DFU_DETACH 0x00 /**< Initiates a detach-attach sequence on the bus when it sees this request. */
  37. #define USB_DFU_DNLOAD 0x01 /**< Initiates firmware image downloading */
  38. #define USB_DFU_UPLOAD 0x02 /**< This request is employed by the host to solicit firmware from the device.*/
  39. #define USB_DFU_GETSTATUS 0x03 /**< The host employs this request to facilitate synchronization with the device. \see usb_dfu_status */
  40. #define USB_DFU_CLRSTATAUS 0x04 /**< This request resets DFU machine state to DFU_IDLE*/
  41. #define USB_DFU_GETSTATE 0x05 /**< This request solicits a report about the state of the device. */
  42. #define USB_DFU_ABORT 0x06 /**< This request enables the host to exit from certain states and return to the DFU_IDLE state*/
  43. /** @} */
  44. /**\anchor USB_DFU_CAPAB
  45. * \name USB DFU capabilities
  46. * @{ */
  47. #define USB_DFU_ATTR_WILL_DETACH 0x08 /**< Device will perform a bus detach-attach sequence when it receives a DFU_DETACH request. */
  48. #define USB_DFU_ATTR_MANIF_TOL 0x04 /**< Device is able to communicate via USB after Manifestation phase. */
  49. #define USB_DFU_ATTR_CAN_UPLOAD 0x02 /**< Upload capable. */
  50. #define USB_DFU_ATTR_CAN_DNLOAD 0x01 /**< Download capable. */
  51. /** @} */
  52. /**\name USB DFU status codes
  53. * @{ */
  54. #define USB_DFU_STATUS_OK 0x00 /**< No error condition is present. */
  55. #define USB_DFU_STATUS_ERR_TARGET 0x01 /**< File is not targeted for use by this device. */
  56. #define USB_DFU_STATUS_ERR_FILE 0x02 /**< File is for this device but fails some vendor specific verification test. */
  57. #define USB_DFU_STATUS_ERR_WRITE 0x03 /**< Device is unable to write memory. */
  58. #define USB_DFU_STATUS_ERR_ERASE 0x04 /**< Memory erase function failed. */
  59. #define USB_DFU_STATUS_ERR_CHECK_ERASED 0x05 /**< Memory erase check failed. */
  60. #define USB_DFU_STATUS_ERR_PROG 0x06 /**< Program memory function failed. */
  61. #define USB_DFU_STATUS_ERR_VERIFY 0x07 /**< Programmed memory failed verification. */
  62. #define USB_DFU_STATUS_ERR_ADDRESS 0x08 /**< Cannot program memory due to received address that is out of range. */
  63. #define USB_DFU_STATUS_ERR_NOTDONE 0x09 /**< Received DFU_DNLOAD with wLength = 0, but device does not think it has all of the data yet. */
  64. #define USB_DFU_STATUS_ERR_FIRMWARE 0x0A /**< Device's firmware is corrupt. It cannot return to run-time (non-DFU) operations. */
  65. #define USB_DFU_STATUS_ERR_VENDOR 0x0B /**< iString indicates a vendor-specific error. */
  66. #define USB_DFU_STATUS_ERR_USBR 0x0C /**< Device detected unexpected USB reset signaling. */
  67. #define USB_DFU_STATUS_ERR_POR 0x0D /**< Device detected unexpected power on reset. */
  68. #define USB_DFU_STATUS_ERR_UNKNOWN 0x0E /**< Something went wrong, but the device does not know what it was. */
  69. #define USB_DFU_STATUS_ERR_STALLEDPKT 0x0F /**< Device stalled an unexpected request. */
  70. /** @} */
  71. /**\name USB DFU state codes
  72. * @{ */
  73. #define USB_DFU_STATE_APP_IDLE 0x00 /**< Device is running its normal application. */
  74. #define USB_DFU_STATE_APP_DETACH 0x01 /**< Device is running its normal application, has received the DFU_DETACH request, and is waiting for a USB reset. */
  75. #define USB_DFU_STATE_DFU_IDLE 0x02 /**< Device is operating in the DFU mode and is waiting for requests. */
  76. #define USB_DFU_STATE_DFU_DNLOADSYNC 0x03 /**< Device has received a block and is waiting for the host to solicit the status via DFU_GETSTATUS. */
  77. #define USB_DFU_STATE_DFU_DNBUSY 0x04 /**< Device is programming a control-write block into its nonvolatile memories. */
  78. #define USB_DFU_STATE_DFU_DNLOADIDLE 0x05 /**< Device is processing a download operation. Expecting DFU_DNLOAD requests. */
  79. #define USB_DFU_STATE_DFU_MANIFESTSYNC 0x06 /**< Device has received the final block of firmware from the host
  80. * and is waiting for receipt of DFU_GETSTATUS to begin the Manifestation
  81. * phase; or device has completed the Manifestation phase and is waiting
  82. * for receipt of DFU_GETSTATUS. (Devices that can enter this state
  83. * after the Manifestation phase set bmAttributes bit bitManifestationTolerant to 1.) */
  84. #define USB_DFU_STATE_DFU_MANIFEST 0x07 /**< Device is in the Manifestation phase. (Not all devices will be able to respond to DFU_GETSTATUS when in this state.) */
  85. #define USB_DFU_STATE_DFU_MANIFESTWR 0x08 /**< Device has programmed its memories and is waiting for a USB reset or a
  86. * power on reset. (Devices that must enter this state clear bitManifestationTolerant to 0.) */
  87. #define USB_DFU_STATE_DFU_UPLOADIDLE 0x09 /**< The device is processing an upload operation. Expecting DFU_UPLOAD requests. */
  88. #define USB_DFU_STATE_DFU_ERROR 0x0A /**< An error has occurred. Awaiting the DFU_CLRSTATUS request. */
  89. /** @} */
  90. /** \brief USB DFU functional descriptor */
  91. struct usb_dfu_func_desc {
  92. uint8_t bLength; /**< Descriptor length in bytes. */
  93. uint8_t bDescriptorType; /**< Descriptor type, set it to \ref USB_DTYPE_DFU_FUNCTIONAL */
  94. uint8_t bmAttributes; /**< Bitmap field of the \ref USB_DFU_CAPAB "USB DFU capabilities"*/
  95. uint16_t wDetachTimeout; /**< USB DFU detach timeout in ms */
  96. uint16_t wTransferSize; /**< USB DFU maximum transfer block size in bytes */
  97. uint16_t bcdDFUVersion; /**< USB DFU version \ref VERSION_BCD utility macro */
  98. }__attribute__((packed));
  99. /** \brief Payload packet to response in DFU_GETSTATUS request */
  100. struct usb_dfu_status {
  101. uint8_t bStatus; /**< An indication of the status resulting from the execution of the most recent request.*/
  102. uint8_t bPollTimeout;
  103. uint16_t wPollTimeout; /**< Minimum time, (ms), that the host should wait before sending a subsequent \ref USB_DFU_GETSTATUS "DFU_GETSTATUS" request. */
  104. uint8_t bState; /**< An indication of the state that the device is going to enter immediately following transmission of this response. */
  105. uint8_t iString; /**< Index of the status \ref usb_string_descriptor "string descriptor". */
  106. };
  107. /** @} */
  108. #if defined(__cplusplus)
  109. }
  110. #endif
  111. #endif /* _USB_DFU_H_ */