dap_main.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2023 ~ 2025, sakumisu
  3. * Copyright (c) 2023 ~ 2025, HalfSweet
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. #ifndef DAP_MAIN_H
  8. #define DAP_MAIN_H
  9. #include "usbd_core.h"
  10. #include "usbd_cdc.h"
  11. #include "usbd_msc.h"
  12. #include "chry_ringbuffer.h"
  13. #include "DAP_config.h"
  14. #include "DAP.h"
  15. #define DAP_IN_EP 0x81
  16. #define DAP_OUT_EP 0x02
  17. #define CDC_IN_EP 0x83
  18. #define CDC_OUT_EP 0x04
  19. #define CDC_INT_EP 0x85
  20. #define MSC_IN_EP 0x86
  21. #define MSC_OUT_EP 0x07
  22. #define USBD_VID 0x0D28
  23. #define USBD_PID 0x0204
  24. #define USBD_MAX_POWER 500
  25. #define USBD_LANGID_STRING 1033
  26. #define CMSIS_DAP_INTERFACE_SIZE (9 + 7 + 7)
  27. #ifdef CONFIG_CHERRYDAP_USE_MSC
  28. #define CONFIG_MSC_DESCRIPTOR_LEN CDC_ACM_DESCRIPTOR_LEN
  29. #define CONFIG_MSC_INTF_NUM 1
  30. #define MSC_INTF_NUM (0x02 + 1)
  31. #else
  32. #define CONFIG_MSC_DESCRIPTOR_LEN 0
  33. #define CONFIG_MSC_INTF_NUM 0
  34. #define MSC_INTF_NUM (0x02)
  35. #endif
  36. #ifdef CONFIG_USB_HS
  37. #if DAP_PACKET_SIZE != 512
  38. #error "DAP_PACKET_SIZE must be 512 in hs"
  39. #endif
  40. #else
  41. #if DAP_PACKET_SIZE != 64
  42. #error "DAP_PACKET_SIZE must be 64 in fs"
  43. #endif
  44. #endif
  45. #define USBD_WINUSB_VENDOR_CODE 0x20
  46. #define USBD_WEBUSB_ENABLE 0
  47. #define USBD_BULK_ENABLE 1
  48. #define USBD_WINUSB_ENABLE 1
  49. /* WinUSB Microsoft OS 2.0 descriptor sizes */
  50. #define WINUSB_DESCRIPTOR_SET_HEADER_SIZE 10
  51. #define WINUSB_FUNCTION_SUBSET_HEADER_SIZE 8
  52. #define WINUSB_FEATURE_COMPATIBLE_ID_SIZE 20
  53. #define FUNCTION_SUBSET_LEN 160
  54. #define DEVICE_INTERFACE_GUIDS_FEATURE_LEN 132
  55. #define USBD_WINUSB_DESC_SET_LEN (WINUSB_DESCRIPTOR_SET_HEADER_SIZE + USBD_WEBUSB_ENABLE * FUNCTION_SUBSET_LEN + USBD_BULK_ENABLE * FUNCTION_SUBSET_LEN)
  56. #define USBD_NUM_DEV_CAPABILITIES (USBD_WEBUSB_ENABLE + USBD_WINUSB_ENABLE)
  57. #define USBD_WEBUSB_DESC_LEN 24
  58. #define USBD_WINUSB_DESC_LEN 28
  59. #define USBD_BOS_WTOTALLENGTH (0x05 + \
  60. USBD_WEBUSB_DESC_LEN * USBD_WEBUSB_ENABLE + \
  61. USBD_WINUSB_DESC_LEN * USBD_WINUSB_ENABLE)
  62. #define CONFIG_UARTRX_RINGBUF_SIZE (8 * 1024)
  63. #define CONFIG_USBRX_RINGBUF_SIZE (8 * 1024)
  64. #ifdef __cplusplus
  65. extern "C"
  66. {
  67. #endif
  68. extern USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t uartrx_ringbuffer[CONFIG_UARTRX_RINGBUF_SIZE];
  69. extern USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t usbrx_ringbuffer[CONFIG_USBRX_RINGBUF_SIZE];
  70. extern USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t usb_tmpbuffer[DAP_PACKET_SIZE];
  71. extern const struct usb_descriptor cmsisdap_descriptor;
  72. extern __ALIGN_BEGIN const uint8_t USBD_WinUSBDescriptorSetDescriptor[];
  73. extern __ALIGN_BEGIN const uint8_t USBD_BinaryObjectStoreDescriptor[];
  74. extern char *string_descriptors[];
  75. extern struct usbd_interface dap_intf;
  76. extern struct usbd_interface intf1;
  77. extern struct usbd_interface intf2;
  78. extern struct usbd_interface intf3;
  79. extern struct usbd_interface hid_intf;
  80. extern struct usbd_endpoint dap_out_ep;
  81. extern struct usbd_endpoint dap_in_ep;
  82. extern struct usbd_endpoint cdc_out_ep;
  83. extern struct usbd_endpoint cdc_in_ep;
  84. extern chry_ringbuffer_t g_uartrx;
  85. extern chry_ringbuffer_t g_usbrx;
  86. __STATIC_INLINE void chry_dap_init(uint8_t busid, uint32_t reg_base)
  87. {
  88. chry_ringbuffer_init(&g_uartrx, uartrx_ringbuffer, CONFIG_UARTRX_RINGBUF_SIZE);
  89. chry_ringbuffer_init(&g_usbrx, usbrx_ringbuffer, CONFIG_USBRX_RINGBUF_SIZE);
  90. DAP_Setup();
  91. usbd_desc_register(0, &cmsisdap_descriptor);
  92. /*!< winusb */
  93. usbd_add_interface(0, &dap_intf);
  94. usbd_add_endpoint(0, &dap_out_ep);
  95. usbd_add_endpoint(0, &dap_in_ep);
  96. /*!< cdc acm */
  97. usbd_add_interface(0, usbd_cdc_acm_init_intf(0, &intf1));
  98. usbd_add_interface(0, usbd_cdc_acm_init_intf(0, &intf2));
  99. usbd_add_endpoint(0, &cdc_out_ep);
  100. usbd_add_endpoint(0, &cdc_in_ep);
  101. #ifdef CONFIG_CHERRYDAP_USE_MSC
  102. usbd_add_interface(0, usbd_msc_init_intf(0, &intf3, MSC_OUT_EP, MSC_IN_EP));
  103. #endif
  104. extern void usbd_event_handler(uint8_t busid, uint8_t event);
  105. usbd_initialize(busid, reg_base, usbd_event_handler);
  106. }
  107. void chry_dap_handle(void);
  108. void chry_dap_usb2uart_handle(void);
  109. void chry_dap_usb2uart_uart_config_callback(struct cdc_line_coding *line_coding);
  110. void chry_dap_usb2uart_uart_send_bydma(uint8_t *data, uint16_t len);
  111. void chry_dap_usb2uart_uart_send_complete(uint32_t size);
  112. #ifdef __cplusplus
  113. }
  114. #endif
  115. #endif