source: S-port/trunk/USB_DEVICE/App/usbd_cdc_if.c

Last change on this file was 1, checked in by AlexLir, 3 years ago
File size: 9.3 KB
Line 
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) 2021 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#include "log_and_debug.h"
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/* USER CODE END PRIVATE_DEFINES */
67
68/**
69 * @}
70 */
71
72/** @defgroup USBD_CDC_IF_Private_Macros USBD_CDC_IF_Private_Macros
73 * @brief Private macros.
74 * @{
75 */
76
77/* USER CODE BEGIN PRIVATE_MACRO */
78
79/* USER CODE END PRIVATE_MACRO */
80
81/**
82 * @}
83 */
84
85/** @defgroup USBD_CDC_IF_Private_Variables USBD_CDC_IF_Private_Variables
86 * @brief Private variables.
87 * @{
88 */
89/* Create buffer for reception and transmission */
90/* It's up to user to redefine and/or remove those define */
91/** Received data over USB are stored in this buffer */
92uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
93
94/** Data to send over USB CDC are stored in this buffer */
95uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
96
97/* USER CODE BEGIN PRIVATE_VARIABLES */
98
99/* USER CODE END PRIVATE_VARIABLES */
100
101/**
102 * @}
103 */
104
105/** @defgroup USBD_CDC_IF_Exported_Variables USBD_CDC_IF_Exported_Variables
106 * @brief Public variables.
107 * @{
108 */
109
110extern USBD_HandleTypeDef hUsbDeviceFS;
111
112/* USER CODE BEGIN EXPORTED_VARIABLES */
113
114/* USER CODE END EXPORTED_VARIABLES */
115
116/**
117 * @}
118 */
119
120/** @defgroup USBD_CDC_IF_Private_FunctionPrototypes USBD_CDC_IF_Private_FunctionPrototypes
121 * @brief Private functions declaration.
122 * @{
123 */
124
125static int8_t CDC_Init_FS(void);
126static int8_t CDC_DeInit_FS(void);
127static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length);
128static int8_t CDC_Receive_FS(uint8_t* pbuf, uint32_t *Len);
129static int8_t CDC_TransmitCplt_FS(uint8_t *pbuf, uint32_t *Len, uint8_t epnum);
130
131/* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */
132
133/* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */
134
135/**
136 * @}
137 */
138
139USBD_CDC_ItfTypeDef USBD_Interface_fops_FS =
140{
141 CDC_Init_FS,
142 CDC_DeInit_FS,
143 CDC_Control_FS,
144 CDC_Receive_FS,
145 CDC_TransmitCplt_FS
146};
147
148/* Private functions ---------------------------------------------------------*/
149/**
150 * @brief Initializes the CDC media low layer over the FS USB IP
151 * @retval USBD_OK if all operations are OK else USBD_FAIL
152 */
153static int8_t CDC_Init_FS(void)
154{
155 /* USER CODE BEGIN 3 */
156 /* Set Application Buffers */
157 USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0);
158 USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS);
159 return (USBD_OK);
160 /* USER CODE END 3 */
161}
162
163/**
164 * @brief DeInitializes the CDC media low layer
165 * @retval USBD_OK if all operations are OK else USBD_FAIL
166 */
167static int8_t CDC_DeInit_FS(void)
168{
169 /* USER CODE BEGIN 4 */
170 return (USBD_OK);
171 /* USER CODE END 4 */
172}
173
174/**
175 * @brief Manage the CDC class requests
176 * @param cmd: Command code
177 * @param pbuf: Buffer containing command data (request parameters)
178 * @param length: Number of data to be sent (in bytes)
179 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
180 */
181static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length)
182{
183 /* USER CODE BEGIN 5 */
184 switch(cmd)
185 {
186 case CDC_SEND_ENCAPSULATED_COMMAND:
187
188 break;
189
190 case CDC_GET_ENCAPSULATED_RESPONSE:
191
192 break;
193
194 case CDC_SET_COMM_FEATURE:
195
196 break;
197
198 case CDC_GET_COMM_FEATURE:
199
200 break;
201
202 case CDC_CLEAR_COMM_FEATURE:
203
204 break;
205
206 /*******************************************************************************/
207 /* Line Coding Structure */
208 /*-----------------------------------------------------------------------------*/
209 /* Offset | Field | Size | Value | Description */
210 /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/
211 /* 4 | bCharFormat | 1 | Number | Stop bits */
212 /* 0 - 1 Stop bit */
213 /* 1 - 1.5 Stop bits */
214 /* 2 - 2 Stop bits */
215 /* 5 | bParityType | 1 | Number | Parity */
216 /* 0 - None */
217 /* 1 - Odd */
218 /* 2 - Even */
219 /* 3 - Mark */
220 /* 4 - Space */
221 /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
222 /*******************************************************************************/
223 case CDC_SET_LINE_CODING:
224
225 break;
226
227 case CDC_GET_LINE_CODING:
228
229 break;
230
231 case CDC_SET_CONTROL_LINE_STATE:
232
233 break;
234
235 case CDC_SEND_BREAK:
236
237 break;
238
239 default:
240 break;
241 }
242
243 return (USBD_OK);
244 /* USER CODE END 5 */
245}
246
247#include "log_and_debug.h"
248/**
249 * @brief Data received over USB OUT endpoint are sent over CDC interface
250 * through this function.
251 *
252 * @note
253 * This function will issue a NAK packet on any OUT packet received on
254 * USB endpoint until exiting this function. If you exit this function
255 * before transfer is complete on CDC interface (ie. using DMA controller)
256 * it will result in receiving more data while previous ones are still
257 * 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 */
263static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
264{
265 /* USER CODE BEGIN 6 */
266
267 //USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
268 debug_receive(Buf, Len);
269 USBD_CDC_ReceivePacket(&hUsbDeviceFS);
270 return (USBD_OK);
271 /* USER CODE END 6 */
272}
273
274/**
275 * @brief CDC_Transmit_FS
276 * Data to send over USB IN endpoint are sent over CDC interface
277 * through this function.
278 * @note
279 *
280 *
281 * @param Buf: Buffer of data to be sent
282 * @param Len: Number of data to be sent (in bytes)
283 * @retval USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY
284 */
285uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
286{
287 uint8_t result = USBD_OK;
288 /* USER CODE BEGIN 7 */
289 USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
290 if (hcdc->TxState != 0){
291 return USBD_BUSY;
292 }
293 USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
294 result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
295 /* USER CODE END 7 */
296 return result;
297}
298
299/**
300 * @brief CDC_TransmitCplt_FS
301 * Data transmited callback
302 *
303 * @note
304 * This function is IN transfer complete callback used to inform user that
305 * the submitted Data is successfully sent over USB.
306 *
307 * @param Buf: Buffer of data to be received
308 * @param Len: Number of data received (in bytes)
309 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
310 */
311static int8_t CDC_TransmitCplt_FS(uint8_t *Buf, uint32_t *Len, uint8_t epnum)
312{
313 uint8_t result = USBD_OK;
314 /* USER CODE BEGIN 13 */
315 UNUSED(Buf);
316 UNUSED(Len);
317 UNUSED(epnum);
318 /* USER CODE END 13 */
319 return result;
320}
321
322/* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
323
324/* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
325
326/**
327 * @}
328 */
329
330/**
331 * @}
332 */
333
334/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Note: See TracBrowser for help on using the repository browser.