usbd_cdc_if.c 9.3 KB

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