usb_dfu.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2022, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef USB_DFU_H
  7. #define USB_DFU_H
  8. /**\addtogroup USB_MODULE_DFU USB DFU class
  9. * \brief This module contains USB Device Firmware Upgrade class definitions.
  10. * \details This module based on
  11. * + [USB Device Firmware Upgrade Specification, Revision 1.1]
  12. * (https://www.usb.org/sites/default/files/DFU_1.1.pdf)
  13. * @{ */
  14. /** DFU Specification release */
  15. #define DFU_VERSION 0x0110
  16. /** DFU Class Subclass */
  17. #define DFU_SUBCLASS_DFU 0x01
  18. /** DFU Class runtime Protocol */
  19. #define DFU_PROTOCOL_RUNTIME 0x01
  20. /** DFU Class DFU mode Protocol */
  21. #define DFU_PROTOCOL_MODE 0x02
  22. /**
  23. * @brief DFU Class Specific Requests
  24. */
  25. #define DFU_REQUEST_DETACH 0x00
  26. #define DFU_REQUEST_DNLOAD 0x01
  27. #define DFU_REQUEST_UPLOAD 0x02
  28. #define DFU_REQUEST_GETSTATUS 0x03
  29. #define DFU_REQUEST_CLRSTATUS 0x04
  30. #define DFU_REQUEST_GETSTATE 0x05
  31. #define DFU_REQUEST_ABORT 0x06
  32. /** DFU FUNCTIONAL descriptor type */
  33. #define DFU_FUNC_DESC 0x21
  34. /** DFU attributes DFU Functional Descriptor */
  35. #define DFU_ATTR_WILL_DETACH 0x08
  36. #define DFU_ATTR_MANIFESTATION_TOLERANT 0x04
  37. #define DFU_ATTR_CAN_UPLOAD 0x02
  38. #define DFU_ATTR_CAN_DNLOAD 0x01
  39. /** bStatus values for the DFU_GETSTATUS response */
  40. #define DFU_STATUS_OK 0x00U
  41. #define DFU_STATUS_ERR_TARGET 0x01U
  42. #define DFU_STATUS_ERR_FILE 0x02U
  43. #define DFU_STATUS_ERR_WRITE 0x03U
  44. #define DFU_STATUS_ERR_ERASE 0x04U
  45. #define DFU_STATUS_ERR_CHECK_ERASED 0x05U
  46. #define DFU_STATUS_ERR_PROG 0x06U
  47. #define DFU_STATUS_ERR_VERIFY 0x07U
  48. #define DFU_STATUS_ERR_ADDRESS 0x08U
  49. #define DFU_STATUS_ERR_NOTDONE 0x09U
  50. #define DFU_STATUS_ERR_FIRMWARE 0x0AU
  51. #define DFU_STATUS_ERR_VENDOR 0x0BU
  52. #define DFU_STATUS_ERR_USB 0x0CU
  53. #define DFU_STATUS_ERR_POR 0x0DU
  54. #define DFU_STATUS_ERR_UNKNOWN 0x0EU
  55. #define DFU_STATUS_ERR_STALLEDPKT 0x0FU
  56. /** bState values for the DFU_GETSTATUS response */
  57. #define DFU_STATE_APP_IDLE 0U
  58. #define DFU_STATE_APP_DETACH 1U
  59. #define DFU_STATE_DFU_IDLE 2U
  60. #define DFU_STATE_DFU_DNLOAD_SYNC 3U
  61. #define DFU_STATE_DFU_DNLOAD_BUSY 4U
  62. #define DFU_STATE_DFU_DNLOAD_IDLE 5U
  63. #define DFU_STATE_DFU_MANIFEST_SYNC 6U
  64. #define DFU_STATE_DFU_MANIFEST 7U
  65. #define DFU_STATE_DFU_MANIFEST_WAIT_RESET 8U
  66. #define DFU_STATE_DFU_UPLOAD_IDLE 9U
  67. #define DFU_STATE_DFU_ERROR 10U
  68. /** DFU Manifestation State */
  69. #define DFU_MANIFEST_COMPLETE 0U
  70. #define DFU_MANIFEST_IN_PROGRESS 1U
  71. /** Special Commands with Download Request */
  72. #define DFU_CMD_GETCOMMANDS 0U
  73. #define DFU_CMD_SETADDRESSPOINTER 0x21U
  74. #define DFU_CMD_ERASE 0x41U
  75. #define DFU_MEDIA_ERASE 0x00U
  76. #define DFU_MEDIA_PROGRAM 0x01U
  77. /** Other defines */
  78. /* Bit Detach capable = bit 3 in bmAttributes field */
  79. #define DFU_DETACH_MASK (1U << 3)
  80. #define DFU_MANIFEST_MASK (1U << 2)
  81. /** Run-Time Functional Descriptor */
  82. struct dfu_runtime_descriptor {
  83. uint8_t bLength; /**<\brief Descriptor length in bytes.*/
  84. uint8_t bDescriptorType; /**<\brief DFU functional descriptor type.*/
  85. uint8_t bmAttributes; /**<\brief USB DFU capabilities \ref USB_DFU_CAPAB*/
  86. uint16_t wDetachTimeout; /**<\brief USB DFU detach timeout in ms.*/
  87. uint16_t wTransferSize; /**<\brief USB DFU maximum transfer block size in bytes.*/
  88. uint16_t bcdDFUVersion; /**<\brief USB DFU version \ref VERSION_BCD utility macro.*/
  89. } __PACKED;
  90. /**\brief Payload packet to response in DFU_GETSTATUS request */
  91. struct dfu_info {
  92. uint8_t bStatus; /**<\brief An indication of the status resulting from the
  93. * execution of the most recent request.*/
  94. uint8_t bPollTimeout; /**<\brief Minimum time (LSB) in ms, that the host should wait
  95. * before sending a subsequent DFU_GETSTATUS request.*/
  96. uint16_t wPollTimeout; /**<\brief Minimum time (MSB) in ms, that the host should wait
  97. * before sending a subsequent DFU_GETSTATUS request.*/
  98. uint8_t bState; /**<\brief An indication of the state that the device is going
  99. * to enter immediately following transmission of this response.*/
  100. uint8_t iString; /**<\brief Index of the status string descriptor.*/
  101. };
  102. // clang-format off
  103. #define DFU_DESCRIPTOR_INIT() \
  104. 0x09, /* bLength */ \
  105. USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \
  106. 0x00, /* bInterfaceNumber */ \
  107. 0x00, /* bAlternateSetting */ \
  108. 0x00, /* bNumEndpoints Default Control Pipe only */ \
  109. USB_DEVICE_CLASS_APP_SPECIFIC, /* bInterfaceClass */ \
  110. 0x01, /* bInterfaceSubClass Device Firmware Upgrade */ \
  111. 0x02, /* bInterfaceProtocol DFU mode */ \
  112. 0x04, /* iInterface */ /*!< Device Firmware Update Functional Descriptor */ \
  113. 0x09, /* bLength */ \
  114. 0x21, /* DFU Functional Descriptor */ \
  115. 0x0B, /* bmAttributes */ \
  116. WBVAL(0x00ff), /* wDetachTimeOut */ \
  117. WBVAL(USBD_DFU_XFER_SIZE), /* wTransferSize */ \
  118. WBVAL(0x011a) /* bcdDFUVersion */
  119. // clang-format on
  120. #endif /* USB_DFU_H */