usbd_cdc_if.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. {CDC_Init_FS, CDC_DeInit_FS, CDC_Control_FS, CDC_Receive_FS, CDC_TransmitCplt_FS};
  106. /* Private functions ---------------------------------------------------------*/
  107. /**
  108. * @brief Initializes the CDC media low layer over the FS USB IP
  109. * @retval USBD_OK if all operations are OK else USBD_FAIL
  110. */
  111. static int8_t CDC_Init_FS(void) {
  112. /* USER CODE BEGIN 3 */
  113. /* Set Application Buffers */
  114. USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0);
  115. USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS);
  116. return (USBD_OK);
  117. /* USER CODE END 3 */
  118. }
  119. /**
  120. * @brief DeInitializes the CDC media low layer
  121. * @retval USBD_OK if all operations are OK else USBD_FAIL
  122. */
  123. static int8_t CDC_DeInit_FS(void) {
  124. /* USER CODE BEGIN 4 */
  125. return (USBD_OK);
  126. /* USER CODE END 4 */
  127. }
  128. /**
  129. * @brief Manage the CDC class requests
  130. * @param cmd: Command code
  131. * @param pbuf: Buffer containing command data (request parameters)
  132. * @param length: Number of data to be sent (in bytes)
  133. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  134. */
  135. static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length) {
  136. /* USER CODE BEGIN 5 */
  137. switch(cmd) {
  138. case CDC_SEND_ENCAPSULATED_COMMAND:
  139. break;
  140. case CDC_GET_ENCAPSULATED_RESPONSE:
  141. break;
  142. case CDC_SET_COMM_FEATURE:
  143. break;
  144. case CDC_GET_COMM_FEATURE:
  145. break;
  146. case CDC_CLEAR_COMM_FEATURE:
  147. break;
  148. /*******************************************************************************/
  149. /* Line Coding Structure */
  150. /*-----------------------------------------------------------------------------*/
  151. /* Offset | Field | Size | Value | Description */
  152. /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/
  153. /* 4 | bCharFormat | 1 | Number | Stop bits */
  154. /* 0 - 1 Stop bit */
  155. /* 1 - 1.5 Stop bits */
  156. /* 2 - 2 Stop bits */
  157. /* 5 | bParityType | 1 | Number | Parity */
  158. /* 0 - None */
  159. /* 1 - Odd */
  160. /* 2 - Even */
  161. /* 3 - Mark */
  162. /* 4 - Space */
  163. /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
  164. /*******************************************************************************/
  165. case CDC_SET_LINE_CODING:
  166. break;
  167. case CDC_GET_LINE_CODING:
  168. break;
  169. case CDC_SET_CONTROL_LINE_STATE:
  170. break;
  171. case CDC_SEND_BREAK:
  172. break;
  173. default:
  174. break;
  175. }
  176. return (USBD_OK);
  177. /* USER CODE END 5 */
  178. }
  179. /**
  180. * @brief Data received over USB OUT endpoint are sent over CDC interface
  181. * through this function.
  182. *
  183. * @note
  184. * This function will issue a NAK packet on any OUT packet received on
  185. * USB endpoint until exiting this function. If you exit this function
  186. * before transfer is complete on CDC interface (ie. using DMA controller)
  187. * it will result in receiving more data while previous ones are still
  188. * not sent.
  189. *
  190. * @param Buf: Buffer of data to be received
  191. * @param Len: Number of data received (in bytes)
  192. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  193. */
  194. static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t* Len) {
  195. /* USER CODE BEGIN 6 */
  196. USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
  197. USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  198. return (USBD_OK);
  199. /* USER CODE END 6 */
  200. }
  201. /**
  202. * @brief CDC_Transmit_FS
  203. * Data to send over USB IN endpoint are sent over CDC interface
  204. * through this function.
  205. * @note
  206. *
  207. *
  208. * @param Buf: Buffer of data to be sent
  209. * @param Len: Number of data to be sent (in bytes)
  210. * @retval USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY
  211. */
  212. uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len) {
  213. uint8_t result = USBD_OK;
  214. /* USER CODE BEGIN 7 */
  215. USBD_CDC_HandleTypeDef* hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
  216. if(hcdc->TxState != 0) {
  217. return USBD_BUSY;
  218. }
  219. USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
  220. result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
  221. /* USER CODE END 7 */
  222. return result;
  223. }
  224. /**
  225. * @brief CDC_TransmitCplt_FS
  226. * Data transmitted callback
  227. *
  228. * @note
  229. * This function is IN transfer complete callback used to inform user that
  230. * the submitted Data is successfully sent over USB.
  231. *
  232. * @param Buf: Buffer of data to be received
  233. * @param Len: Number of data received (in bytes)
  234. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  235. */
  236. static int8_t CDC_TransmitCplt_FS(uint8_t* Buf, uint32_t* Len, uint8_t epnum) {
  237. uint8_t result = USBD_OK;
  238. /* USER CODE BEGIN 13 */
  239. UNUSED(Buf);
  240. UNUSED(Len);
  241. UNUSED(epnum);
  242. /* USER CODE END 13 */
  243. return result;
  244. }
  245. /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
  246. /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
  247. /**
  248. * @}
  249. */
  250. /**
  251. * @}
  252. */
  253. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/