usbd_cdc_if.c 9.0 KB

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