usbd_usbtmc.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* Define to prevent recursive inclusion -------------------------------------*/
  2. #ifndef __USB_USBTMC_H
  3. #define __USB_USBTMC_H
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. //----------------------------------------------------------------
  8. // Refer to:
  9. // [1] USBTMC Standard, rev. 1.0, 14/04/2003
  10. // "Universal Serial Bus Test and Measurement Class Specification (USBTMC)"
  11. // [2] USBTMC-USB488 Standard, rev. 1.0, 14/04/2003
  12. // "Universal Serial Bus Test and Measurement Class, Subclass USB488 Specification (USBTMC-USB488)
  13. //----------------------------------------------------------------
  14. /* Includes ------------------------------------------------------------------*/
  15. #include "usbd_ioreq.h"
  16. #include "usb/usb_hooks.h"
  17. #include "usb/usb_config.h"
  18. #include "core/config.h"
  19. #include "drivers/usb/class/common/class_common.h"
  20. #define USBTMC_BULKIO_EP 0x01 /* Bulk_IN_OUT */
  21. #define USBTMC_INTIO_EP 0x02 /* Interrupt_IN */
  22. #define USBTMC_BULKIN_EP (0x80 | USBTMC_BULKIO_EP) /* EP1 for data IN */
  23. #define USBTMC_BULKOUT_EP (0x7F & USBTMC_BULKIO_EP) /* EP1 for data OUT */
  24. #define USBTMC_INTERRUPTIN_EP (0x80 | USBTMC_INTIO_EP) /* EP2 for data IN */
  25. #define USBTMC_BENCHMARK_EP USBTMC_BULKIO_EP
  26. #define USBTMC_DATAIN_MAX_PACKET_SIZE 64 // Control-EP size
  27. #define USBTMC_DATAOUT_MAX_PACKET_SIZE 64 // Control-EP size
  28. // Refer [1], 3.4 Interrupt-IN.
  29. // The Host, after an Interrupt-IN transfer has completed, must interpret the first byte in the next Interrupt-
  30. // IN DATA payload as a new notification, beginning with bNotify1. Note that the USB 2.0 specification,
  31. // section 5.7.3, defines when an interrupt transfer must be considered complete. If no subclass specification
  32. // applies (bInterfaceProtocol = 0, see Table 44), or if the notification is a vendor specific request, the last
  33. // Interrupt-IN transaction must be a short packet (< wMaxPacketSize).
  34. #define USBTMC_INTIN_MAX_PACKET_SIZE 2 // INT-EP size: in accordance to [2]
  35. #define USBTMC_USE_COMMON_EP0RXREADY 1
  36. #define USBTMC_USE_COMMON_EP0TXSENT 1
  37. #if USB_CONTROL_USBTMC_RX_BUFFER < USB_MAX_EP0_SIZE
  38. #error Invalid USB_CONTROL_USBTMC_RX_BUFFER buffer size. Must be at least USB_MAX_EP0_SIZE bytes.
  39. #endif
  40. #if USB_CONTROL_USBTMC_TX_BUFFER < USB_MAX_EP0_SIZE
  41. #error Invalid USB_CONTROL_USBTMC_TX_BUFFER buffer size. Must be at least USB_MAX_EP0_SIZE bytes.
  42. #endif
  43. /*---------------------------------------------------------------------*/
  44. /* USBTMC interface definitions */
  45. /*---------------------------------------------------------------------*/
  46. typedef struct
  47. {
  48. USBD_HandleTypeDef * pdev; // for @USBD_USBTMC_DataIn_BeginSend()
  49. sUSBTransfer_t ep0_tx_transfer; // Control-IN EP
  50. sUSBTransfer_t ep0_rx_transfer; // Control-OUT EP
  51. sUSBTransfer_t ep1_tx_transfer; // Bulk-IN EP
  52. sUSBTransfer_t ep1_rx_transfer; // Bulk-OUT EP
  53. sUSBTransfer_t ep2_tx_transfer; // Interrupt-IN EP
  54. uint8_t ep0_tx_buffer[ USB_CONTROL_USBTMC_TX_BUFFER ]; // Control-IN EP
  55. uint8_t ep0_rx_buffer[ USB_CONTROL_USBTMC_RX_BUFFER ]; // Control-OUT EP
  56. uint8_t ep1_tx_buffer[ USB_BULK_USBTMC_TX_BUFFER ]; // Bulk-IN EP
  57. uint8_t ep1_rx_buffer[ USB_BULK_USBTMC_RX_BUFFER ]; // Bulk-OUT EP
  58. uint8_t ep2_tx_buffer[ USB_INTERRUPT_USBTMC_TX_BUFFER ]; // Interrupt-IN EP
  59. }
  60. USBD_USBTMC_HandleTypeDef;
  61. static inline size_t usbd_usbtmc_get_control_tx_transfer_size()
  62. {
  63. return sizeof(((USBD_USBTMC_HandleTypeDef*)0)->ep0_tx_buffer);
  64. }
  65. static inline size_t usbd_usbtmc_get_control_rx_transfer_size()
  66. {
  67. return sizeof(((USBD_USBTMC_HandleTypeDef*)0)->ep0_rx_buffer);
  68. }
  69. static inline size_t usbd_usbtmc_get_bulk_tx_transfer_size()
  70. {
  71. return sizeof(((USBD_USBTMC_HandleTypeDef*)0)->ep1_tx_buffer);
  72. }
  73. static inline size_t usbd_usbtmc_get_bulk_rx_transfer_size()
  74. {
  75. return sizeof(((USBD_USBTMC_HandleTypeDef*)0)->ep1_rx_buffer);
  76. }
  77. static inline size_t usbd_usbtmc_get_interrupt_tx_transfer_size()
  78. {
  79. return sizeof(((USBD_USBTMC_HandleTypeDef*)0)->ep2_tx_buffer);
  80. }
  81. #if CONFIG_USB_USBTMC_ENABLE
  82. extern const USBD_COMMON_ItfTypeDef USBD_Interface_Usbtmc_UsbInterface;
  83. #endif
  84. extern const USBD_ClassTypeDef USBD_USBTMC;
  85. #define USBD_USBTMC_CLASS &USBD_USBTMC
  86. sUSBTransfer_t * USBD_USBTMC_GetDataTxTransferHandle(); // Bulk-IN (USBTMC)
  87. sUSBTransfer_t * USBD_USBTMC_GetDataRxTransferHandle(); // Bulk-OUT (USBTMC)
  88. sUSBTransfer_t * USBD_USBTMC_GetInterruptTxTransferHandle(); // Interrupt-IN (USBTMC)
  89. const USBD_DescriptorsTypeDef * USBD_USBTMC_GetDescriptorHandlers();
  90. // USBD_USBTMC_DataIn_BeginSend
  91. // Initiates the first packet sending using virtual DataIN event
  92. // @bTxAlreadyPrepared - if the TX-transfer is already prepared to transmission, do not reset transfer, do not call TX-handler
  93. uint8_t USBD_USBTMC_DataIn_BeginSend( uint8_t epnum, bool bTxAlreadyPrepared );
  94. // USBD_USBTMC_DataIn_ZeroSend
  95. // Queue zero packet in EP IN
  96. uint8_t USBD_USBTMC_DataIn_ZeroSend( uint8_t epnum );
  97. // USBD_USBTMC_ClearEP()
  98. // Restore specfied halted data-endpoint by it's address and reset the buffer
  99. // Recover the Rx-transfer for specified endpoint
  100. // Restarts the receiving for the specified endpoint
  101. void USBD_USBTMC_ClearEP( uint8_t epaddr );
  102. // USBD_USBTMC_StallEP()
  103. // Halt specfied data-endpoint by it's address and reset the buffer
  104. void USBD_USBTMC_StallEP( uint8_t epaddr );
  105. // USBD_USBTMC_NakEP()
  106. // Reset hardware EP buffer and setup the end-point to Not-acknowlage mode
  107. // No data will be returned by EP util the data will be queued again
  108. void USBD_USBTMC_NakEP( uint8_t epaddr );
  109. // USBD_USBTMC_GetTxQueuedSize
  110. // Returns amound of bytes queued in USBTMC IN endpoint.
  111. // @epaddr - USBTMC EP-IN number
  112. // @pCount - pointer to the value to store the packet length, can not be NULL
  113. // Return: true if there is a queued packet in EP-IN, the length is stored in @pCount
  114. // false if there no queued packet in EP-IN, @pCount has not been modified
  115. bool USBD_USBTMC_GetTxQueuedSize( uint8_t epaddr, uint32_t * pCount );
  116. #ifdef __cplusplus
  117. }
  118. #endif
  119. #endif /* __USB_USBTMC_H */