usb_dfu.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2021, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef USB_DFU_H
  7. #define USB_DFU_H
  8. #include <stdint.h>
  9. #include <drivers/usb_device.h>
  10. #define DFU_DESCRIPTOR_TYPE 0x21U
  11. /* Max DFU Packet Size = 1024 bytes */
  12. #define USBD_DFU_XFER_SIZE 1024U
  13. #define TRANSFER_SIZE_BYTES(size) \
  14. ((uint8_t)((size) & 0xFF)), /* XFERSIZEB0 */\
  15. ((uint8_t)((size) >> 8)) /* XFERSIZEB1 */
  16. /*
  17. * helper for descriptor of DFU interface 0 Alternate setting n
  18. * with iInterface = index of string descriptor, assumed Nth user string
  19. */
  20. #define USBD_DFU_IF_DESC(n) 0x09U, /* Interface Descriptor size */\
  21. USB_DESC_TYPE_INTERFACE, /* descriptor type */\
  22. 0x00U, /* Number of Interface */\
  23. (n), /* Alternate setting */\
  24. 0x00U, /* bNumEndpoints*/\
  25. 0xFEU, /* Application Specific Class Code */\
  26. 0x01U, /* Device Firmware Upgrade Code */\
  27. 0x02U, /* DFU mode protocol */ \
  28. USBD_IDX_USER0_STR + (n) /* iInterface */
  29. /* DFU1.1 Standard */
  30. #define USB_DFU_VERSION 0x0110U
  31. #define USB_DFU_ITF_SIZ 9U
  32. #define USB_DFU_DESC_SIZ(itf) (USB_DFU_ITF_SIZ * ((itf) + 2U))
  33. /*
  34. * bmAttribute value for DFU:
  35. * bitCanDnload = 1(bit 0)
  36. * bitCanUpload = 1(bit 1)
  37. * bitManifestationTolerant = 1 (bit 2)
  38. * bitWillDetach = 1(bit 3)
  39. * Reserved (bit4-6)
  40. * bitAcceleratedST = 0(bit 7)
  41. */
  42. #define DFU_BM_ATTRIBUTE 0x0FU
  43. #define DFU_STATUS_SIZE 6U
  44. /* Callback for media access */
  45. struct usb_dfu_media {
  46. int (*upload)(uint8_t alt, uintptr_t *buffer, uint32_t *len,
  47. void *user_data);
  48. int (*download)(uint8_t alt, uintptr_t *buffer, uint32_t *len,
  49. void *user_data);
  50. int (*manifestation)(uint8_t alt, void *user_data);
  51. };
  52. /* Internal DFU handle */
  53. struct usb_dfu_handle {
  54. uint8_t status[DFU_STATUS_SIZE];
  55. uint8_t dev_state;
  56. uint8_t dev_status;
  57. uint8_t alt_setting;
  58. const struct usb_dfu_media *callback;
  59. };
  60. void usb_dfu_register(struct usb_handle *pdev, struct usb_dfu_handle *phandle);
  61. int usb_dfu_loop(struct usb_handle *pdev, const struct usb_dfu_media *pmedia);
  62. /* Function provided by plat */
  63. struct usb_handle *usb_dfu_plat_init(void);
  64. #endif /* USB_DFU_H */