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