dfu_with_st_tool_template.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #include "usbd_dfu.h"
  8. #define USBD_VID 0x0483
  9. #define USBD_PID 0xDF11
  10. #define USBD_MAX_POWER 100
  11. #define USBD_LANGID_STRING 1033
  12. #define FLASH_DESC_STR "@Internal Flash /0x08000000/16*001Ka,112*01Kg"
  13. #define USB_CONFIG_SIZE (9 + 9 + 9)
  14. static const uint8_t device_descriptor[] = {
  15. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0200, 0x01)
  16. };
  17. static const uint8_t config_descriptor[] = {
  18. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  19. DFU_DESCRIPTOR_INIT()
  20. };
  21. static const uint8_t device_quality_descriptor[] = {
  22. ///////////////////////////////////////
  23. /// device qualifier descriptor
  24. ///////////////////////////////////////
  25. 0x0a,
  26. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  27. 0x00,
  28. 0x02,
  29. 0x00,
  30. 0x00,
  31. 0x00,
  32. 0x40,
  33. 0x00,
  34. 0x00,
  35. };
  36. static const char *string_descriptors[] = {
  37. (const char[]){ 0x09, 0x04 }, /* Langid */
  38. "CherryUSB", /* Manufacturer */
  39. "CherryUSB DFU DEMO", /* Product */
  40. "2022123456", /* Serial Number */
  41. };
  42. static const uint8_t *device_descriptor_callback(uint8_t speed)
  43. {
  44. return device_descriptor;
  45. }
  46. static const uint8_t *config_descriptor_callback(uint8_t speed)
  47. {
  48. return config_descriptor;
  49. }
  50. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  51. {
  52. return device_quality_descriptor;
  53. }
  54. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  55. {
  56. if (index > 3) {
  57. return NULL;
  58. }
  59. return string_descriptors[index];
  60. }
  61. const struct usb_descriptor dfu_flash_descriptor = {
  62. .device_descriptor_callback = device_descriptor_callback,
  63. .config_descriptor_callback = config_descriptor_callback,
  64. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  65. .string_descriptor_callback = string_descriptor_callback
  66. };
  67. static void usbd_event_handler(uint8_t busid, uint8_t event)
  68. {
  69. switch (event) {
  70. case USBD_EVENT_RESET:
  71. break;
  72. case USBD_EVENT_CONNECTED:
  73. break;
  74. case USBD_EVENT_DISCONNECTED:
  75. break;
  76. case USBD_EVENT_RESUME:
  77. break;
  78. case USBD_EVENT_SUSPEND:
  79. break;
  80. case USBD_EVENT_CONFIGURED:
  81. break;
  82. case USBD_EVENT_SET_REMOTE_WAKEUP:
  83. break;
  84. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  85. break;
  86. default:
  87. break;
  88. }
  89. }
  90. struct usbd_interface intf0;
  91. void dfu_flash_init(uint8_t busid, uintptr_t reg_base)
  92. {
  93. usbd_desc_register(busid, &dfu_flash_descriptor);
  94. usbd_add_interface(busid, usbd_dfu_init_intf(&intf0));
  95. usbd_initialize(busid, reg_base, usbd_event_handler);
  96. }