dfu_template.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2022 ~ 2026, 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 USB_CONFIG_SIZE (9 + DFU_DESCRIPTOR_LEN)
  13. #define FLASH_DESC_STR "@Internal Flash /0x08000000/16*128Kg"
  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(4)
  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. FLASH_DESC_STR
  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 >= (sizeof(string_descriptors) / sizeof(char *))) {
  58. return NULL;
  59. }
  60. return string_descriptors[index];
  61. }
  62. const struct usb_descriptor dfu_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. static void usbd_event_handler(uint8_t busid, uint8_t event)
  69. {
  70. switch (event) {
  71. case USBD_EVENT_RESET:
  72. break;
  73. case USBD_EVENT_CONNECTED:
  74. break;
  75. case USBD_EVENT_DISCONNECTED:
  76. break;
  77. case USBD_EVENT_RESUME:
  78. break;
  79. case USBD_EVENT_SUSPEND:
  80. break;
  81. case USBD_EVENT_CONFIGURED:
  82. break;
  83. case USBD_EVENT_SET_REMOTE_WAKEUP:
  84. break;
  85. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  86. break;
  87. default:
  88. break;
  89. }
  90. }
  91. struct usbd_interface intf0;
  92. void dfu_init(uint8_t busid, uintptr_t reg_base)
  93. {
  94. usbd_desc_register(busid, &dfu_descriptor);
  95. usbd_add_interface(busid, usbd_dfu_init_intf(&intf0));
  96. usbd_initialize(busid, reg_base, usbd_event_handler);
  97. }
  98. volatile uint32_t flash_start_address;
  99. void usbd_dfu_begin_load(void)
  100. {
  101. flash_start_address = 0x08000000;
  102. }
  103. void usbd_dfu_end_load(void)
  104. {
  105. }
  106. void usbd_dfu_reset(void)
  107. {
  108. //NVIC_SystemReset();
  109. }
  110. int usbd_dfu_write(uint16_t value, const uint8_t *data, uint16_t length)
  111. {
  112. //usb_hexdump(data, length);
  113. // patch for stm32 special command
  114. #if 1
  115. if (value == 0) {
  116. if (data[0] == DFU_SPECIAL_CMD_SET_ADDRESS_POINTER) {
  117. memcpy((uint8_t *)&flash_start_address, data, 4);
  118. } else if (data[0] == DFU_SPECIAL_CMD_ERASE) {
  119. memcpy((uint8_t *)&flash_start_address, data, 4);
  120. }
  121. } else if (value > 1) {
  122. uint32_t addr = (value - 2) * CONFIG_USBDEV_REQUEST_BUFFER_LEN + flash_start_address;
  123. }
  124. #else
  125. flash_start_address += length;
  126. #endif
  127. return 0;
  128. }
  129. int usbd_dfu_read(uint16_t value, const uint8_t *data, uint16_t length, uint16_t *actual_length)
  130. {
  131. return 0;
  132. }