usbd_cdc_if.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. /* 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. /* Define size for the receive and transmit buffer over CDC */
  53. /* It's up to user to redefine and/or remove those define */
  54. #define APP_RX_DATA_SIZE 2048
  55. #define APP_TX_DATA_SIZE 2048
  56. /* USER CODE END PRIVATE_DEFINES */
  57. /**
  58. * @}
  59. */
  60. /** @defgroup USBD_CDC_IF_Private_Macros USBD_CDC_IF_Private_Macros
  61. * @brief Private macros.
  62. * @{
  63. */
  64. /* USER CODE BEGIN PRIVATE_MACRO */
  65. /* USER CODE END PRIVATE_MACRO */
  66. /**
  67. * @}
  68. */
  69. /** @defgroup USBD_CDC_IF_Private_Variables USBD_CDC_IF_Private_Variables
  70. * @brief Private variables.
  71. * @{
  72. */
  73. /* Create buffer for reception and transmission */
  74. /* It's up to user to redefine and/or remove those define */
  75. /** Received data over USB are stored in this buffer */
  76. uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
  77. /** Data to send over USB CDC are stored in this buffer */
  78. uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
  79. /* USER CODE BEGIN PRIVATE_VARIABLES */
  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. {CDC_Init_FS, CDC_DeInit_FS, CDC_Control_FS, CDC_Receive_FS};
  109. /* Private functions ---------------------------------------------------------*/
  110. /**
  111. * @brief Initializes the CDC media low layer over the FS USB IP
  112. * @retval USBD_OK if all operations are OK else USBD_FAIL
  113. */
  114. static int8_t CDC_Init_FS(void) {
  115. /* USER CODE BEGIN 3 */
  116. /* Set Application Buffers */
  117. USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0);
  118. USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS);
  119. return (USBD_OK);
  120. /* USER CODE END 3 */
  121. }
  122. /**
  123. * @brief DeInitializes the CDC media low layer
  124. * @retval USBD_OK if all operations are OK else USBD_FAIL
  125. */
  126. static int8_t CDC_DeInit_FS(void) {
  127. /* USER CODE BEGIN 4 */
  128. return (USBD_OK);
  129. /* USER CODE END 4 */
  130. }
  131. /**
  132. * @brief Manage the CDC class requests
  133. * @param cmd: Command code
  134. * @param pbuf: Buffer containing command data (request parameters)
  135. * @param length: Number of data to be sent (in bytes)
  136. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  137. */
  138. static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length) {
  139. /* USER CODE BEGIN 5 */
  140. switch(cmd) {
  141. case CDC_SEND_ENCAPSULATED_COMMAND:
  142. break;
  143. case CDC_GET_ENCAPSULATED_RESPONSE:
  144. break;
  145. case CDC_SET_COMM_FEATURE:
  146. break;
  147. case CDC_GET_COMM_FEATURE:
  148. break;
  149. case CDC_CLEAR_COMM_FEATURE:
  150. break;
  151. /*******************************************************************************/
  152. /* Line Coding Structure */
  153. /*-----------------------------------------------------------------------------*/
  154. /* Offset | Field | Size | Value | Description */
  155. /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/
  156. /* 4 | bCharFormat | 1 | Number | Stop bits */
  157. /* 0 - 1 Stop bit */
  158. /* 1 - 1.5 Stop bits */
  159. /* 2 - 2 Stop bits */
  160. /* 5 | bParityType | 1 | Number | Parity */
  161. /* 0 - None */
  162. /* 1 - Odd */
  163. /* 2 - Even */
  164. /* 3 - Mark */
  165. /* 4 - Space */
  166. /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
  167. /*******************************************************************************/
  168. case CDC_SET_LINE_CODING:
  169. break;
  170. case CDC_GET_LINE_CODING:
  171. break;
  172. case CDC_SET_CONTROL_LINE_STATE:
  173. break;
  174. case CDC_SEND_BREAK:
  175. break;
  176. default:
  177. break;
  178. }
  179. return (USBD_OK);
  180. /* USER CODE END 5 */
  181. }
  182. /**
  183. * @brief Data received over USB OUT endpoint are sent over CDC interface
  184. * through this function.
  185. *
  186. * @note
  187. * This function will block any OUT packet reception on USB endpoint
  188. * untill exiting this function. If you exit this function before transfer
  189. * is complete on CDC interface (ie. using DMA controller) it will result
  190. * in receiving more data while previous ones are still not sent.
  191. *
  192. * @param Buf: Buffer of data to be received
  193. * @param Len: Number of data received (in bytes)
  194. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  195. */
  196. static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t* Len) {
  197. /* USER CODE BEGIN 6 */
  198. USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
  199. USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  200. return (USBD_OK);
  201. /* USER CODE END 6 */
  202. }
  203. /**
  204. * @brief CDC_Transmit_FS
  205. * Data to send over USB IN endpoint are sent over CDC interface
  206. * through this function.
  207. * @note
  208. *
  209. *
  210. * @param Buf: Buffer of data to be sent
  211. * @param Len: Number of data to be sent (in bytes)
  212. * @retval USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY
  213. */
  214. uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len) {
  215. uint8_t result = USBD_OK;
  216. /* USER CODE BEGIN 7 */
  217. USBD_CDC_HandleTypeDef* hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
  218. if(hcdc->TxState != 0) {
  219. return USBD_BUSY;
  220. }
  221. USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
  222. result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
  223. /* USER CODE END 7 */
  224. return result;
  225. }
  226. /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
  227. /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
  228. /**
  229. * @}
  230. */
  231. /**
  232. * @}
  233. */
  234. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/