source: trunk/fw_g473rct/USB_Device/App/usbd_cdc_if.c

Last change on this file was 69, checked in by f.jahn, 4 weeks ago

Started implementing Modbus on USB CDC channel.

File size: 9.2 KB
Line 
1/* USER CODE BEGIN Header */
2/**
3 ******************************************************************************
4 * @file : usbd_cdc_if.c
5 * @version : v3.0_Cube
6 * @brief : Usb device for Virtual Com Port.
7 ******************************************************************************
8 * @attention
9 *
10 * Copyright (c) 2026 STMicroelectronics.
11 * All rights reserved.
12 *
13 * This software is licensed under terms that can be found in the LICENSE file
14 * in the root directory of this software component.
15 * If no LICENSE file comes with this software, it is provided AS-IS.
16 *
17 ******************************************************************************
18 */
19/* USER CODE END Header */
20
21/* Includes ------------------------------------------------------------------*/
22#include "usbd_cdc_if.h"
23
24/* USER CODE BEGIN INCLUDE */
25
26#include "ring.h"
27
28/* USER CODE END INCLUDE */
29
30/* Private typedef -----------------------------------------------------------*/
31/* Private define ------------------------------------------------------------*/
32/* Private macro -------------------------------------------------------------*/
33
34/* USER CODE BEGIN PV */
35/* Private variables ---------------------------------------------------------*/
36
37/* USER CODE END PV */
38
39/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
40 * @brief Usb device library.
41 * @{
42 */
43
44/** @addtogroup USBD_CDC_IF
45 * @{
46 */
47
48/** @defgroup USBD_CDC_IF_Private_TypesDefinitions USBD_CDC_IF_Private_TypesDefinitions
49 * @brief Private types.
50 * @{
51 */
52
53/* USER CODE BEGIN PRIVATE_TYPES */
54
55/* USER CODE END PRIVATE_TYPES */
56
57/**
58 * @}
59 */
60
61/** @defgroup USBD_CDC_IF_Private_Defines USBD_CDC_IF_Private_Defines
62 * @brief Private defines.
63 * @{
64 */
65
66/* USER CODE BEGIN PRIVATE_DEFINES */
67/* USER CODE END PRIVATE_DEFINES */
68
69/**
70 * @}
71 */
72
73/** @defgroup USBD_CDC_IF_Private_Macros USBD_CDC_IF_Private_Macros
74 * @brief Private macros.
75 * @{
76 */
77
78/* USER CODE BEGIN PRIVATE_MACRO */
79
80/* USER CODE END PRIVATE_MACRO */
81
82/**
83 * @}
84 */
85
86/** @defgroup USBD_CDC_IF_Private_Variables USBD_CDC_IF_Private_Variables
87 * @brief Private variables.
88 * @{
89 */
90/* Create buffer for reception and transmission */
91/* It's up to user to redefine and/or remove those define */
92/** Received data over USB are stored in this buffer */
93uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
94
95/** Data to send over USB CDC are stored in this buffer */
96uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
97
98/* USER CODE BEGIN PRIVATE_VARIABLES */
99
100/* USER CODE END PRIVATE_VARIABLES */
101
102/**
103 * @}
104 */
105
106/** @defgroup USBD_CDC_IF_Exported_Variables USBD_CDC_IF_Exported_Variables
107 * @brief Public variables.
108 * @{
109 */
110
111extern USBD_HandleTypeDef hUsbDeviceFS;
112
113/* USER CODE BEGIN EXPORTED_VARIABLES */
114
115/* USER CODE END EXPORTED_VARIABLES */
116
117/**
118 * @}
119 */
120
121/** @defgroup USBD_CDC_IF_Private_FunctionPrototypes USBD_CDC_IF_Private_FunctionPrototypes
122 * @brief Private functions declaration.
123 * @{
124 */
125
126static int8_t CDC_Init_FS(void);
127static int8_t CDC_DeInit_FS(void);
128static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length);
129static int8_t CDC_Receive_FS(uint8_t* pbuf, uint32_t *Len);
130static int8_t CDC_TransmitCplt_FS(uint8_t *pbuf, uint32_t *Len, uint8_t epnum);
131
132/* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */
133
134/* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */
135
136/**
137 * @}
138 */
139
140USBD_CDC_ItfTypeDef USBD_Interface_fops_FS =
141{
142 CDC_Init_FS,
143 CDC_DeInit_FS,
144 CDC_Control_FS,
145 CDC_Receive_FS,
146 CDC_TransmitCplt_FS
147};
148
149/* Private functions ---------------------------------------------------------*/
150/**
151 * @brief Initializes the CDC media low layer over the FS USB IP
152 * @retval USBD_OK if all operations are OK else USBD_FAIL
153 */
154static int8_t CDC_Init_FS(void)
155{
156 /* USER CODE BEGIN 3 */
157 /* Set Application Buffers */
158 USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0);
159 USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS);
160 return (USBD_OK);
161 /* USER CODE END 3 */
162}
163
164/**
165 * @brief DeInitializes the CDC media low layer
166 * @retval USBD_OK if all operations are OK else USBD_FAIL
167 */
168static int8_t CDC_DeInit_FS(void)
169{
170 /* USER CODE BEGIN 4 */
171 return (USBD_OK);
172 /* USER CODE END 4 */
173}
174
175/**
176 * @brief Manage the CDC class requests
177 * @param cmd: Command code
178 * @param pbuf: Buffer containing command data (request parameters)
179 * @param length: Number of data to be sent (in bytes)
180 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
181 */
182static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length)
183{
184 /* USER CODE BEGIN 5 */
185 switch(cmd)
186 {
187 case CDC_SEND_ENCAPSULATED_COMMAND:
188
189 break;
190
191 case CDC_GET_ENCAPSULATED_RESPONSE:
192
193 break;
194
195 case CDC_SET_COMM_FEATURE:
196
197 break;
198
199 case CDC_GET_COMM_FEATURE:
200
201 break;
202
203 case CDC_CLEAR_COMM_FEATURE:
204
205 break;
206
207 /*******************************************************************************/
208 /* Line Coding Structure */
209 /*-----------------------------------------------------------------------------*/
210 /* Offset | Field | Size | Value | Description */
211 /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/
212 /* 4 | bCharFormat | 1 | Number | Stop bits */
213 /* 0 - 1 Stop bit */
214 /* 1 - 1.5 Stop bits */
215 /* 2 - 2 Stop bits */
216 /* 5 | bParityType | 1 | Number | Parity */
217 /* 0 - None */
218 /* 1 - Odd */
219 /* 2 - Even */
220 /* 3 - Mark */
221 /* 4 - Space */
222 /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
223 /*******************************************************************************/
224 case CDC_SET_LINE_CODING:
225
226 break;
227
228 case CDC_GET_LINE_CODING:
229
230 break;
231
232 case CDC_SET_CONTROL_LINE_STATE:
233
234 break;
235
236 case CDC_SEND_BREAK:
237
238 break;
239
240 default:
241 break;
242 }
243
244 return (USBD_OK);
245 /* USER CODE END 5 */
246}
247
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 extern ring_buffer_t usb_in_buff;
267 for (uint32_t i = 0U; i < *Len; ++i)
268 rb_push(&usb_in_buff, Buf[i]);
269
270 USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
271 USBD_CDC_ReceivePacket(&hUsbDeviceFS);
272 return (USBD_OK);
273 /* USER CODE END 6 */
274}
275
276/**
277 * @brief CDC_Transmit_FS
278 * Data to send over USB IN endpoint are sent over CDC interface
279 * through this function.
280 * @note
281 *
282 *
283 * @param Buf: Buffer of data to be sent
284 * @param Len: Number of data to be sent (in bytes)
285 * @retval USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY
286 */
287uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
288{
289 uint8_t result = USBD_OK;
290 /* USER CODE BEGIN 7 */
291 USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
292 if (hcdc->TxState != 0){
293 return USBD_BUSY;
294 }
295 USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
296 result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
297 /* USER CODE END 7 */
298 return result;
299}
300
301/**
302 * @brief CDC_TransmitCplt_FS
303 * Data transmitted callback
304 *
305 * @note
306 * This function is IN transfer complete callback used to inform user that
307 * the submitted Data is successfully sent over USB.
308 *
309 * @param Buf: Buffer of data to be received
310 * @param Len: Number of data received (in bytes)
311 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
312 */
313static int8_t CDC_TransmitCplt_FS(uint8_t *Buf, uint32_t *Len, uint8_t epnum)
314{
315 uint8_t result = USBD_OK;
316 /* USER CODE BEGIN 13 */
317 UNUSED(Buf);
318 UNUSED(Len);
319 UNUSED(epnum);
320 /* USER CODE END 13 */
321 return result;
322}
323
324/* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
325
326/* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
327
328/**
329 * @}
330 */
331
332/**
333 * @}
334 */
Note: See TracBrowser for help on using the repository browser.