usbd_cdc_if.c 9.7 KB

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