usbd_cdc_if.c 8.9 KB

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