usbd_cdc_if.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : usbd_cdc_if.c
  5. * @version : v2.0_Cube
  6. * @brief : Usb device for Virtual Com Port.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
  11. * All rights reserved.</center></h2>
  12. *
  13. * This software component is licensed by ST under Ultimate Liberty license
  14. * SLA0044, the "License"; You may not use this file except in compliance with
  15. * the License. You may obtain a copy of the License at:
  16. * www.st.com/SLA0044
  17. *
  18. ******************************************************************************
  19. */
  20. /* USER CODE END Header */
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "usbd_cdc_if.h"
  23. /* USER CODE BEGIN INCLUDE */
  24. #include "fifo.h"
  25. /* USER CODE END INCLUDE */
  26. /* Private typedef -----------------------------------------------------------*/
  27. /* Private define ------------------------------------------------------------*/
  28. /* Private macro -------------------------------------------------------------*/
  29. /* USER CODE BEGIN PV */
  30. /* Private variables ---------------------------------------------------------*/
  31. /* USER CODE END PV */
  32. /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
  33. * @brief Usb device library.
  34. * @{
  35. */
  36. /** @addtogroup USBD_CDC_IF
  37. * @{
  38. */
  39. /** @defgroup USBD_CDC_IF_Private_TypesDefinitions USBD_CDC_IF_Private_TypesDefinitions
  40. * @brief Private types.
  41. * @{
  42. */
  43. /* USER CODE BEGIN PRIVATE_TYPES */
  44. /* USER CODE END PRIVATE_TYPES */
  45. /**
  46. * @}
  47. */
  48. /** @defgroup USBD_CDC_IF_Private_Defines USBD_CDC_IF_Private_Defines
  49. * @brief Private defines.
  50. * @{
  51. */
  52. /* USER CODE BEGIN PRIVATE_DEFINES */
  53. /* USER CODE END PRIVATE_DEFINES */
  54. /**
  55. * @}
  56. */
  57. /** @defgroup USBD_CDC_IF_Private_Macros USBD_CDC_IF_Private_Macros
  58. * @brief Private macros.
  59. * @{
  60. */
  61. /* USER CODE BEGIN PRIVATE_MACRO */
  62. #define RX_FIFO_SIZE 500
  63. /* USER CODE END PRIVATE_MACRO */
  64. /**
  65. * @}
  66. */
  67. /** @defgroup USBD_CDC_IF_Private_Variables USBD_CDC_IF_Private_Variables
  68. * @brief Private variables.
  69. * @{
  70. */
  71. /* Create buffer for reception and transmission */
  72. /* It's up to user to redefine and/or remove those define */
  73. /** Received data over USB are stored in this buffer */
  74. uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
  75. /** Data to send over USB CDC are stored in this buffer */
  76. uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
  77. /* USER CODE BEGIN PRIVATE_VARIABLES */
  78. FIFO_HandleTypeDef RxFIFO;
  79. uint8_t RxFIFOBuffer[RX_FIFO_SIZE];
  80. /* USER CODE END PRIVATE_VARIABLES */
  81. /**
  82. * @}
  83. */
  84. /** @defgroup USBD_CDC_IF_Exported_Variables USBD_CDC_IF_Exported_Variables
  85. * @brief Public variables.
  86. * @{
  87. */
  88. extern USBD_HandleTypeDef hUsbDeviceFS;
  89. /* USER CODE BEGIN EXPORTED_VARIABLES */
  90. /* USER CODE END EXPORTED_VARIABLES */
  91. /**
  92. * @}
  93. */
  94. /** @defgroup USBD_CDC_IF_Private_FunctionPrototypes USBD_CDC_IF_Private_FunctionPrototypes
  95. * @brief Private functions declaration.
  96. * @{
  97. */
  98. static int8_t CDC_Init_FS(void);
  99. static int8_t CDC_DeInit_FS(void);
  100. static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length);
  101. static int8_t CDC_Receive_FS(uint8_t* pbuf, uint32_t *Len);
  102. /* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */
  103. /* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */
  104. /**
  105. * @}
  106. */
  107. USBD_CDC_ItfTypeDef USBD_Interface_fops_FS =
  108. {
  109. CDC_Init_FS,
  110. CDC_DeInit_FS,
  111. CDC_Control_FS,
  112. CDC_Receive_FS
  113. };
  114. /* Private functions ---------------------------------------------------------*/
  115. /**
  116. * @brief Initializes the CDC media low layer over the FS USB IP
  117. * @retval USBD_OK if all operations are OK else USBD_FAIL
  118. */
  119. static int8_t CDC_Init_FS(void)
  120. {
  121. /* USER CODE BEGIN 3 */
  122. /* Set Application Buffers */
  123. USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0);
  124. USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS);
  125. fifo_init(&RxFIFO, RxFIFOBuffer, RX_FIFO_SIZE);
  126. return (USBD_OK);
  127. /* USER CODE END 3 */
  128. }
  129. /**
  130. * @brief DeInitializes the CDC media low layer
  131. * @retval USBD_OK if all operations are OK else USBD_FAIL
  132. */
  133. static int8_t CDC_DeInit_FS(void)
  134. {
  135. /* USER CODE BEGIN 4 */
  136. return (USBD_OK);
  137. /* USER CODE END 4 */
  138. }
  139. /**
  140. * @brief Manage the CDC class requests
  141. * @param cmd: Command code
  142. * @param pbuf: Buffer containing command data (request parameters)
  143. * @param length: Number of data to be sent (in bytes)
  144. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  145. */
  146. static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length)
  147. {
  148. /* USER CODE BEGIN 5 */
  149. switch(cmd)
  150. {
  151. case CDC_SEND_ENCAPSULATED_COMMAND:
  152. break;
  153. case CDC_GET_ENCAPSULATED_RESPONSE:
  154. break;
  155. case CDC_SET_COMM_FEATURE:
  156. break;
  157. case CDC_GET_COMM_FEATURE:
  158. break;
  159. case CDC_CLEAR_COMM_FEATURE:
  160. break;
  161. /*******************************************************************************/
  162. /* Line Coding Structure */
  163. /*-----------------------------------------------------------------------------*/
  164. /* Offset | Field | Size | Value | Description */
  165. /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/
  166. /* 4 | bCharFormat | 1 | Number | Stop bits */
  167. /* 0 - 1 Stop bit */
  168. /* 1 - 1.5 Stop bits */
  169. /* 2 - 2 Stop bits */
  170. /* 5 | bParityType | 1 | Number | Parity */
  171. /* 0 - None */
  172. /* 1 - Odd */
  173. /* 2 - Even */
  174. /* 3 - Mark */
  175. /* 4 - Space */
  176. /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
  177. /*******************************************************************************/
  178. case CDC_SET_LINE_CODING:
  179. break;
  180. case CDC_GET_LINE_CODING:
  181. break;
  182. case CDC_SET_CONTROL_LINE_STATE:
  183. break;
  184. case CDC_SEND_BREAK:
  185. break;
  186. default:
  187. break;
  188. }
  189. return (USBD_OK);
  190. /* USER CODE END 5 */
  191. }
  192. /**
  193. * @brief Data received over USB OUT endpoint are sent over CDC interface
  194. * through this function.
  195. *
  196. * @note
  197. * This function will issue a NAK packet on any OUT packet received on
  198. * USB endpoint until exiting this function. If you exit this function
  199. * before transfer is complete on CDC interface (ie. using DMA controller)
  200. * it will result in receiving more data while previous ones are still
  201. * not sent.
  202. *
  203. * @param Buf: Buffer of data to be received
  204. * @param Len: Number of data received (in bytes)
  205. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  206. */
  207. static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
  208. {
  209. /* USER CODE BEGIN 6 */
  210. USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS);
  211. USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  212. if(fifo_write(&RxFIFO, UserRxBufferFS, *Len) > 0)
  213. return (USBD_OK);
  214. else
  215. return (USBD_FAIL);
  216. /* USER CODE END 6 */
  217. }
  218. /**
  219. * @brief CDC_Transmit_FS
  220. * Data to send over USB IN endpoint are sent over CDC interface
  221. * through this function.
  222. * @note
  223. *
  224. *
  225. * @param Buf: Buffer of data to be sent
  226. * @param Len: Number of data to be sent (in bytes)
  227. * @retval USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY
  228. */
  229. uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
  230. {
  231. uint8_t result = USBD_OK;
  232. /* USER CODE BEGIN 7 */
  233. USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
  234. if (hcdc->TxState != 0){
  235. return USBD_BUSY;
  236. }
  237. memcpy(UserTxBufferFS, Buf, Len);
  238. USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, Len);
  239. result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
  240. /* USER CODE END 7 */
  241. return result;
  242. }
  243. /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
  244. uint32_t CDC_DataAvailable(void)
  245. {
  246. return fifo_size(&RxFIFO);
  247. }
  248. int CDC_WriteData(uint8_t* buf, uint32_t len)
  249. {
  250. if(CDC_Transmit_FS(buf, len) == USBD_OK)
  251. return len;
  252. else
  253. return (-1);
  254. }
  255. int CDC_ReadData(uint8_t* buf, uint32_t len)
  256. {
  257. return fifo_read(&RxFIFO, buf, len);
  258. }
  259. /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
  260. /**
  261. * @}
  262. */
  263. /**
  264. * @}
  265. */