usb_dfu.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_DFU 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. /* Define DFU application notification signals. */
  69. #define DFU_NOTIFICATION_BEGIN_DOWNLOAD 0x1u
  70. #define DFU_NOTIFICATION_END_DOWNLOAD 0x2u
  71. #define DFU_NOTIFICATION_ABORT_DOWNLOAD 0x3u
  72. #define DFU_NOTIFICATION_BEGIN_UPLOAD 0x5u
  73. #define DFU_NOTIFICATION_END_UPLOAD 0x6u
  74. #define DFU_NOTIFICATION_ABORT_UPLOAD 0x7u
  75. /* Define DFU application notification signals. */
  76. #define DFU_MEDIA_STATUS_OK 0
  77. #define DFU_MEDIA_STATUS_BUSY 1
  78. #define DFU_MEDIA_STATUS_ERROR 2
  79. /** Special Commands with Download Request for STM32, wValue = 0 */
  80. #define DFU_SPECIAL_CMD_SET_ADDRESS_POINTER 0x21U
  81. #define DFU_SPECIAL_CMD_ERASE 0x41U
  82. #define DFU_SPECIAL_READ_UNPROTECT 0x92U
  83. /** Run-Time Functional Descriptor */
  84. struct dfu_runtime_descriptor {
  85. uint8_t bLength; /**<\brief Descriptor length in bytes.*/
  86. uint8_t bDescriptorType; /**<\brief DFU functional descriptor type.*/
  87. uint8_t bmAttributes; /**<\brief USB DFU capabilities \ref USB_DFU_CAPAB*/
  88. uint16_t wDetachTimeout; /**<\brief USB DFU detach timeout in ms.*/
  89. uint16_t wTransferSize; /**<\brief USB DFU maximum transfer block size in bytes.*/
  90. uint16_t bcdDFUVersion; /**<\brief USB DFU version \ref VERSION_BCD utility macro.*/
  91. } __PACKED;
  92. /**\brief Payload packet to response in DFU_GETSTATUS request */
  93. struct dfu_status {
  94. uint8_t bStatus; /**<\brief An indication of the status resulting from the
  95. * execution of the most recent request.*/
  96. uint32_t bwPollTimeout; /**<\brief Minimum time 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. #define DFU_DESCRIPTOR_LEN 18
  103. // clang-format off
  104. #define DFU_DESCRIPTOR_INIT(str_idx) \
  105. 0x09, /* bLength */ \
  106. USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \
  107. 0x00, /* bInterfaceNumber */ \
  108. 0x00, /* bAlternateSetting */ \
  109. 0x00, /* bNumEndpoints Default Control Pipe only */ \
  110. USB_DEVICE_CLASS_APP_SPECIFIC, /* bInterfaceClass */ \
  111. DFU_SUBCLASS_DFU, /* bInterfaceSubClass Device Firmware Upgrade */ \
  112. DFU_PROTOCOL_DFU, /* bInterfaceProtocol DFU mode */ \
  113. str_idx, /* iInterface */ \
  114. /*!< Device Firmware Update Functional Descriptor */ \
  115. 0x09, /* bLength */ \
  116. 0x21, /* DFU Functional Descriptor */ \
  117. 0x0B, /* bmAttributes */ \
  118. WBVAL(0x00ff), /* wDetachTimeOut */ \
  119. WBVAL(CONFIG_USBDEV_REQUEST_BUFFER_LEN), /* wTransferSize */ \
  120. WBVAL(DFU_VERSION) /* bcdDFUVersion */
  121. // clang-format on
  122. #endif /* USB_DFU_H */