usbd_cdc_if.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "usbd_cdc_if.h"
  2. #include <furi-hal-vcp_i.h>
  3. extern USBD_HandleTypeDef hUsbDeviceFS;
  4. static int8_t CDC_Init_FS(void);
  5. static int8_t CDC_DeInit_FS(void);
  6. static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length);
  7. static int8_t CDC_Receive_FS(uint8_t* pbuf, uint32_t *Len);
  8. static int8_t CDC_TransmitCplt_FS(uint8_t *pbuf, uint32_t *Len, uint8_t epnum);
  9. USBD_CDC_ItfTypeDef USBD_Interface_fops_FS =
  10. {
  11. CDC_Init_FS,
  12. CDC_DeInit_FS,
  13. CDC_Control_FS,
  14. CDC_Receive_FS,
  15. CDC_TransmitCplt_FS
  16. };
  17. uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
  18. uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
  19. /** Initializes the CDC media low layer over the FS USB IP
  20. * @retval USBD_OK if all operations are OK else USBD_FAIL
  21. */
  22. static int8_t CDC_Init_FS(void) {
  23. /* Set Application Buffers */
  24. USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0);
  25. USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS);
  26. return (USBD_OK);
  27. }
  28. /**
  29. * @brief DeInitializes the CDC media low layer
  30. * @retval USBD_OK if all operations are OK else USBD_FAIL
  31. */
  32. static int8_t CDC_DeInit_FS(void) {
  33. return (USBD_OK);
  34. }
  35. /** Manage the CDC class requests
  36. * @param cmd: Command code
  37. * @param pbuf: Buffer containing command data (request parameters)
  38. * @param length: Number of data to be sent (in bytes)
  39. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  40. */
  41. static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length) {
  42. if (cmd == CDC_SEND_ENCAPSULATED_COMMAND) {
  43. } else if (cmd == CDC_GET_ENCAPSULATED_RESPONSE) {
  44. } else if (cmd == CDC_SET_COMM_FEATURE) {
  45. } else if (cmd == CDC_GET_COMM_FEATURE) {
  46. } else if (cmd == CDC_CLEAR_COMM_FEATURE) {
  47. } else if (cmd == CDC_SET_LINE_CODING) {
  48. /*******************************************************************************/
  49. /* Line Coding Structure */
  50. /*-----------------------------------------------------------------------------*/
  51. /* Offset | Field | Size | Value | Description */
  52. /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/
  53. /* 4 | bCharFormat | 1 | Number | Stop bits */
  54. /* 0 - 1 Stop bit */
  55. /* 1 - 1.5 Stop bits */
  56. /* 2 - 2 Stop bits */
  57. /* 5 | bParityType | 1 | Number | Parity */
  58. /* 0 - None */
  59. /* 1 - Odd */
  60. /* 2 - Even */
  61. /* 3 - Mark */
  62. /* 4 - Space */
  63. /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
  64. /*******************************************************************************/
  65. } else if (cmd == CDC_GET_LINE_CODING) {
  66. } else if (cmd == CDC_SET_CONTROL_LINE_STATE) {
  67. furi_hal_vcp_on_cdc_control_line(((USBD_SetupReqTypedef*)pbuf)->wValue);
  68. } else if (cmd == CDC_SEND_BREAK) {
  69. } else {
  70. }
  71. return (USBD_OK);
  72. }
  73. /** Data received over USB OUT endpoint are sent over CDC interface through this function.
  74. *
  75. * @note
  76. * This function will issue a NAK packet on any OUT packet received on
  77. * USB endpoint until exiting this function. If you exit this function
  78. * before transfer is complete on CDC interface (ie. using DMA controller)
  79. * it will result in receiving more data while previous ones are still
  80. * not sent.
  81. *
  82. * @param Buf: Buffer of data to be received
  83. * @param Len: Number of data received (in bytes)
  84. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  85. */
  86. static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len) {
  87. if (*Len) {
  88. furi_hal_vcp_on_cdc_rx(Buf, *Len);
  89. } else {
  90. USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  91. }
  92. return (USBD_OK);
  93. }
  94. /** CDC_Transmit_FS Data to send over USB IN endpoint are sent over CDC interface
  95. * through this function.
  96. * @param Buf: Buffer of data to be sent
  97. * @param Len: Number of data to be sent (in bytes)
  98. * @retval USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY
  99. */
  100. uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
  101. {
  102. uint8_t result = USBD_OK;
  103. USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
  104. if (hcdc->TxState != 0){
  105. return USBD_BUSY;
  106. }
  107. memcpy(UserTxBufferFS, Buf, Len);
  108. USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, Len);
  109. result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
  110. return result;
  111. }
  112. /** CDC_TransmitCplt_FS Data transmited callback
  113. *
  114. * @note
  115. * This function is IN transfer complete callback used to inform user that
  116. * the submitted Data is successfully sent over USB.
  117. *
  118. * @param Buf: Buffer of data to be received
  119. * @param Len: Number of data received (in bytes)
  120. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  121. */
  122. static int8_t CDC_TransmitCplt_FS(uint8_t *Buf, uint32_t *Len, uint8_t epnum) {
  123. uint8_t result = USBD_OK;
  124. furi_hal_vcp_on_cdc_tx_complete(*Len);
  125. return result;
  126. }