dfu_with_st_tool_template.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  15. static const uint8_t device_descriptor[] = {
  16. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0200, 0x01)
  17. };
  18. static const uint8_t config_descriptor[] = {
  19. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  20. DFU_DESCRIPTOR_INIT()
  21. };
  22. static const uint8_t device_quality_descriptor[] = {
  23. ///////////////////////////////////////
  24. /// device qualifier descriptor
  25. ///////////////////////////////////////
  26. 0x0a,
  27. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  28. 0x00,
  29. 0x02,
  30. 0x00,
  31. 0x00,
  32. 0x00,
  33. 0x40,
  34. 0x00,
  35. 0x00,
  36. };
  37. static const char *string_descriptors[] = {
  38. (const char[]){ 0x09, 0x04 }, /* Langid */
  39. "CherryUSB", /* Manufacturer */
  40. "CherryUSB DFU DEMO", /* Product */
  41. "2022123456", /* Serial Number */
  42. };
  43. static const uint8_t *device_descriptor_callback(uint8_t speed)
  44. {
  45. return device_descriptor;
  46. }
  47. static const uint8_t *config_descriptor_callback(uint8_t speed)
  48. {
  49. return config_descriptor;
  50. }
  51. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  52. {
  53. return device_quality_descriptor;
  54. }
  55. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  56. {
  57. if (index > 3) {
  58. return NULL;
  59. }
  60. return string_descriptors[index];
  61. }
  62. const struct usb_descriptor dfu_flash_descriptor = {
  63. .device_descriptor_callback = device_descriptor_callback,
  64. .config_descriptor_callback = config_descriptor_callback,
  65. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  66. .string_descriptor_callback = string_descriptor_callback
  67. };
  68. #else
  69. const uint8_t dfu_flash_descriptor[] = {
  70. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0200, 0x01),
  71. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  72. DFU_DESCRIPTOR_INIT(),
  73. ///////////////////////////////////////
  74. /// string0 descriptor
  75. ///////////////////////////////////////
  76. USB_LANGID_INIT(USBD_LANGID_STRING),
  77. ///////////////////////////////////////
  78. /// string1 descriptor
  79. ///////////////////////////////////////
  80. 0x14, /* bLength */
  81. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  82. 'C', 0x00, /* wcChar0 */
  83. 'h', 0x00, /* wcChar1 */
  84. 'e', 0x00, /* wcChar2 */
  85. 'r', 0x00, /* wcChar3 */
  86. 'r', 0x00, /* wcChar4 */
  87. 'y', 0x00, /* wcChar5 */
  88. 'U', 0x00, /* wcChar6 */
  89. 'S', 0x00, /* wcChar7 */
  90. 'B', 0x00, /* wcChar8 */
  91. ///////////////////////////////////////
  92. /// string2 descriptor
  93. ///////////////////////////////////////
  94. 0x1e, /* bLength */
  95. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  96. 'D', 0x00, /* wcChar0 */
  97. 'F', 0x00, /* wcChar1 */
  98. 'U', 0x00, /* wcChar2 */
  99. 'W', 0x00, /* wcChar3 */
  100. 'i', 0x00, /* wcChar4 */
  101. 't', 0x00, /* wcChar5 */
  102. 'h', 0x00, /* wcChar6 */
  103. 's', 0x00, /* wcChar7 */
  104. 't', 0x00, /* wcChar8 */
  105. ' ', 0x00, /* wcChar9 */
  106. 't', 0x00, /* wcChar10 */
  107. 'o', 0x00, /* wcChar11 */
  108. 'o', 0x00, /* wcChar12 */
  109. 'l', 0x00, /* wcChar13 */
  110. ///////////////////////////////////////
  111. /// string3 descriptor
  112. ///////////////////////////////////////
  113. 0x16, /* bLength */
  114. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  115. '2', 0x00, /* wcChar0 */
  116. '0', 0x00, /* wcChar1 */
  117. '2', 0x00, /* wcChar2 */
  118. '2', 0x00, /* wcChar3 */
  119. '1', 0x00, /* wcChar4 */
  120. '2', 0x00, /* wcChar5 */
  121. '3', 0x00, /* wcChar6 */
  122. '4', 0x00, /* wcChar7 */
  123. '5', 0x00, /* wcChar8 */
  124. '6', 0x00, /* wcChar9 */
  125. ///////////////////////////////////////
  126. /// string4 descriptor
  127. ///////////////////////////////////////
  128. 0x60, /* bLength */
  129. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  130. '@', 0x00, /* wcChar0 */
  131. 'I', 0x00, /* wcChar1 */
  132. 'n', 0x00, /* wcChar2 */
  133. 't', 0x00, /* wcChar3 */
  134. 'e', 0x00, /* wcChar4 */
  135. 'r', 0x00, /* wcChar5 */
  136. 'n', 0x00, /* wcChar6 */
  137. 'a', 0x00, /* wcChar7 */
  138. 'l', 0x00, /* wcChar8 */
  139. ' ', 0x00, /* wcChar9 */
  140. 'F', 0x00, /* wcChar10 */
  141. 'l', 0x00, /* wcChar11*/
  142. 'a', 0x00, /* wcChar12 */
  143. 's', 0x00, /* wcChar13 */
  144. 'h', 0x00, /* wcChar14 */
  145. ' ', 0x00, /* wcChar15 */
  146. ' ', 0x00, /* wcChar16 */
  147. ' ', 0x00, /* wcChar17 */
  148. '/', 0x00, /* wcChar18 */
  149. '0', 0x00, /* wcChar19 */
  150. 'x', 0x00, /* wcChar20 */
  151. '0', 0x00, /* wcChar21*/
  152. '8', 0x00, /* wcChar22 */
  153. '0', 0x00, /* wcChar23 */
  154. '0', 0x00, /* wcChar24 */
  155. '0', 0x00, /* wcChar25 */
  156. '0', 0x00, /* wcChar26 */
  157. '0', 0x00, /* wcChar27 */
  158. '0', 0x00, /* wcChar28 */
  159. '/', 0x00, /* wcChar29 */
  160. '1', 0x00, /* wcChar30 */
  161. '6', 0x00, /* wcChar31*/
  162. '*', 0x00, /* wcChar32 */
  163. '0', 0x00, /* wcChar33 */
  164. '0', 0x00, /* wcChar34 */
  165. '1', 0x00, /* wcChar35 */
  166. 'K', 0x00, /* wcChar36 */
  167. 'a', 0x00, /* wcChar37 */
  168. ',', 0x00, /* wcChar38 */
  169. '1', 0x00, /* wcChar39 */
  170. '1', 0x00, /* wcChar40 */
  171. '2', 0x00, /* wcChar41*/
  172. '*', 0x00, /* wcChar42 */
  173. '0', 0x00, /* wcChar43 */
  174. '1', 0x00, /* wcChar44 */
  175. 'K', 0x00, /* wcChar45 */
  176. 'g', 0x00, /* wcChar46 */
  177. #ifdef CONFIG_USB_HS
  178. ///////////////////////////////////////
  179. /// device qualifier descriptor
  180. ///////////////////////////////////////
  181. 0x0a,
  182. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  183. 0x00,
  184. 0x02,
  185. 0x00,
  186. 0x00,
  187. 0x00,
  188. 0x40,
  189. 0x00,
  190. 0x00,
  191. #endif
  192. 0x00
  193. };
  194. #endif
  195. static void usbd_event_handler(uint8_t busid, uint8_t event)
  196. {
  197. switch (event) {
  198. case USBD_EVENT_RESET:
  199. break;
  200. case USBD_EVENT_CONNECTED:
  201. break;
  202. case USBD_EVENT_DISCONNECTED:
  203. break;
  204. case USBD_EVENT_RESUME:
  205. break;
  206. case USBD_EVENT_SUSPEND:
  207. break;
  208. case USBD_EVENT_CONFIGURED:
  209. break;
  210. case USBD_EVENT_SET_REMOTE_WAKEUP:
  211. break;
  212. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  213. break;
  214. default:
  215. break;
  216. }
  217. }
  218. struct usbd_interface intf0;
  219. void dfu_flash_init(uint8_t busid, uintptr_t reg_base)
  220. {
  221. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  222. usbd_desc_register(busid, &dfu_flash_descriptor);
  223. #else
  224. usbd_desc_register(busid, dfu_flash_descriptor);
  225. #endif
  226. usbd_add_interface(busid, usbd_dfu_init_intf(&intf0));
  227. usbd_initialize(busid, reg_base, usbd_event_handler);
  228. }