STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
usbd_cdc_if.c
Go to the documentation of this file.
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 * 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#include <string.h>
26#include <stdbool.h>
27#include "stm32h7xx.h"
28#include "mode.h"
29/* USER CODE END INCLUDE */
30
31/* Private typedef -----------------------------------------------------------*/
32/* Private define ------------------------------------------------------------*/
33/* Private macro -------------------------------------------------------------*/
34
35/* USER CODE BEGIN PV */
36/* Global status flag to check if a PC serial terminal is actively open */
37volatile bool g_usb_terminal_connected = false;
38static uint8_t line_coding[7] = {0x00, 0xC2, 0x01, 0x00, 0, 0, 8}; /* 115200 8N1 */
39/* USER CODE END PV */
40
41/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
42 * @brief Usb device library.
43 * @{
44 */
45
46/** @addtogroup USBD_CDC_IF
47 * @{
48 */
49
50/** @defgroup USBD_CDC_IF_Private_TypesDefinitions USBD_CDC_IF_Private_TypesDefinitions
51 * @brief Private types.
52 * @{
53 */
54
55/* USER CODE BEGIN PRIVATE_TYPES */
56
57/* USER CODE END PRIVATE_TYPES */
58
59/**
60 * @}
61 */
62
63/** @defgroup USBD_CDC_IF_Private_Defines USBD_CDC_IF_Private_Defines
64 * @brief Private defines.
65 * @{
66 */
67
68/* USER CODE BEGIN PRIVATE_DEFINES */
69#define BOOTLOADER_ADDR 0x1FF09800UL /* ST's H723 example; confirm in AN2606 */
70#define BOOTLOADER_MAGIC 0xB007100DUL /* RTC->BKP0R, survives the reset */
71/* USER CODE END PRIVATE_DEFINES */
72
73/**
74 * @}
75 */
76
77/** @defgroup USBD_CDC_IF_Private_Macros USBD_CDC_IF_Private_Macros
78 * @brief Private macros.
79 * @{
80 */
81
82/* USER CODE BEGIN PRIVATE_MACRO */
83
84/* USER CODE END PRIVATE_MACRO */
85
86/**
87 * @}
88 */
89
90/** @defgroup USBD_CDC_IF_Private_Variables USBD_CDC_IF_Private_Variables
91 * @brief Private variables.
92 * @{
93 */
94
95/* Create buffer for reception and transmission */
96/* It's up to user to redefine and/or remove those define */
97/** Received data over USB are stored in this buffer */
99
100/** Data to send over USB CDC are stored in this buffer */
102
103/* USER CODE BEGIN PRIVATE_VARIABLES */
104
105/* USER CODE END PRIVATE_VARIABLES */
106
107/**
108 * @}
109 */
110
111/** @defgroup USBD_CDC_IF_Exported_Variables USBD_CDC_IF_Exported_Variables
112 * @brief Public variables.
113 * @{
114 */
115
117
118/* USER CODE BEGIN EXPORTED_VARIABLES */
119
120/* USER CODE END EXPORTED_VARIABLES */
121
122/**
123 * @}
124 */
125
126/** @defgroup USBD_CDC_IF_Private_FunctionPrototypes USBD_CDC_IF_Private_FunctionPrototypes
127 * @brief Private functions declaration.
128 * @{
129 */
130
131static int8_t CDC_Init_HS(void);
132static int8_t CDC_DeInit_HS(void);
133static int8_t CDC_Control_HS(uint8_t cmd, uint8_t* pbuf, uint16_t length);
134static int8_t CDC_Receive_HS(uint8_t* pbuf, uint32_t *Len);
135static int8_t CDC_TransmitCplt_HS(uint8_t *pbuf, uint32_t *Len, uint8_t epnum);
136
137/* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */
138static bool is_bootloader_cmd(const uint8_t *b, uint32_t n);
139/* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */
140
141/**
142 * @}
143 */
144
153
154/* Private functions ---------------------------------------------------------*/
155
156/**
157 * @brief Initializes the CDC media low layer over the USB HS IP
158 * @retval USBD_OK if all operations are OK else USBD_FAIL
159 */
160static int8_t CDC_Init_HS(void)
161{
162 /* USER CODE BEGIN 8 */
163 /* Set Application Buffers */
166 return (USBD_OK);
167 /* USER CODE END 8 */
168}
169
170/**
171 * @brief DeInitializes the CDC media low layer
172 * @param None
173 * @retval USBD_OK if all operations are OK else USBD_FAIL
174 */
175static int8_t CDC_DeInit_HS(void)
176{
177 /* USER CODE BEGIN 9 */
179 return (USBD_OK);
180 /* USER CODE END 9 */
181}
182
183/**
184 * @brief Manage the CDC class requests
185 * @param cmd: Command code
186 * @param pbuf: Buffer containing command data (request parameters)
187 * @param length: Number of data to be sent (in bytes)
188 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
189 */
190static int8_t CDC_Control_HS(uint8_t cmd, uint8_t* pbuf, uint16_t length)
191{
192 /* USER CODE BEGIN 10 */
193 switch(cmd)
194 {
196
197 break;
198
200
201 break;
202
204
205 break;
206
208
209 break;
210
212
213 break;
214
216 memcpy(line_coding, pbuf, sizeof line_coding);
217 break;
218
220 memcpy(pbuf, line_coding, sizeof line_coding);
221 break;
222
224 {
225 /* Level 3: Check DTR bit (Bit 0) to verify if host serial terminal is open */
227 g_usb_terminal_connected = (req->wValue & 0x0001) ? true : false;
228 }
229 break;
230
231 case CDC_SEND_BREAK:
232
233 break;
234
235 default:
236 break;
237 }
238
239 return (USBD_OK);
240 /* USER CODE END 10 */
241}
242
243/**
244 * @brief Data received over USB OUT endpoint are sent over CDC interface
245 * through this function.
246 *
247 * @note
248 * This function will issue a NAK packet on any OUT packet received on
249 * USB endpoint until exiting this function. If you exit this function
250 * before transfer is complete on CDC interface (ie. using DMA controller)
251 * it will result in receiving more data while previous ones are still
252 * not sent.
253 *
254 * @param Buf: Buffer of data to be received
255 * @param Len: Number of data received (in bytes)
256 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAILL
257 */
258static int8_t CDC_Receive_HS(uint8_t* Buf, uint32_t *Len)
259{
260 /* USER CODE BEGIN 11 */
263
264 /* Only in IDLE. Reset, and main() jumps before anything else starts. */
265 if (app_state() == FLIGHT_IDLE && is_bootloader_cmd(Buf, *Len))
266 {
268 RTC->BKP0R = BOOTLOADER_MAGIC;
270 }
271
272 return (USBD_OK);
273 /* USER CODE END 11 */
274}
275
276/**
277 * @brief Data to send over USB IN endpoint are sent over CDC interface
278 * through this function.
279 * @param Buf: Buffer of data to be sent
280 * @param Len: Number of data to be sent (in bytes)
281 * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY
282 */
283uint8_t CDC_Transmit_HS(uint8_t* Buf, uint16_t Len)
284{
285 uint8_t result = USBD_OK;
286 /* USER CODE BEGIN 12 */
288 if (!usb_terminal_open() || hcdc == NULL)
289 {
290 return USBD_FAIL;
291 }
292 if (hcdc->TxState != 0){
293 return USBD_BUSY;
294 }
297 /* USER CODE END 12 */
298 return result;
299}
300
301/**
302 * @brief CDC_TransmitCplt_HS
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_HS(uint8_t *Buf, uint32_t *Len, uint8_t epnum)
314{
315 uint8_t result = USBD_OK;
316 /* USER CODE BEGIN 14 */
317 UNUSED(Buf);
318 UNUSED(Len);
319 UNUSED(epnum);
320 /* USER CODE END 14 */
321 return result;
322}
323
324/* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
326{
327 return hUsbDeviceHS.dev_state == USBD_STATE_CONFIGURED; /* unplug reads as suspended */
328}
329
331{
333}
334
335/* "BOOTLOADER" exactly, with an optional line ending. */
336static bool is_bootloader_cmd(const uint8_t *b, uint32_t n)
337{
338 while (n > 0 && (b[n - 1] == '\r' || b[n - 1] == '\n')) n--;
339 return n == 10 && memcmp(b, "BOOTLOADER", 10) == 0;
340}
341
342/* First thing in main(): straight from reset, so nothing needs undoing. */
344{
346 if (RTC->BKP0R != BOOTLOADER_MAGIC) return;
348 RTC->BKP0R = 0;
349 __set_MSP(*(volatile uint32_t *)BOOTLOADER_ADDR);
350 ((void (*)(void))(*(volatile uint32_t *)(BOOTLOADER_ADDR + 4)))();
351}
352/* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
353
354/**
355 * @}
356 */
357
358/**
359 * @}
360 */
@ FLIGHT_IDLE
#define NVIC_SystemReset
__STATIC_INLINE void __set_MSP(uint32_t topOfMainStack)
Set Main Stack Pointer.
void HAL_PWR_EnableBkUpAccess(void)
#define RTC
#define __HAL_RCC_RTC_CLK_ENABLE()
#define APP_RX_DATA_SIZE
Definition usbd_cdc_if.h:52
#define APP_TX_DATA_SIZE
Definition usbd_cdc_if.h:53
volatile bool g_usb_terminal_connected
Definition usbd_cdc_if.c:37
USBD_HandleTypeDef hUsbDeviceHS
Definition usb_device.c:44
#define BOOTLOADER_ADDR
Definition usbd_cdc_if.c:69
#define BOOTLOADER_MAGIC
Definition usbd_cdc_if.c:70
static bool is_bootloader_cmd(const uint8_t *b, uint32_t n)
static int8_t CDC_Receive_HS(uint8_t *pbuf, uint32_t *Len)
Data received over USB OUT endpoint are sent over CDC interface through this function.
static int8_t CDC_Control_HS(uint8_t cmd, uint8_t *pbuf, uint16_t length)
Manage the CDC class requests.
static int8_t CDC_DeInit_HS(void)
DeInitializes the CDC media low layer.
static int8_t CDC_Init_HS(void)
Initializes the CDC media low layer over the USB HS IP.
static int8_t CDC_TransmitCplt_HS(uint8_t *pbuf, uint32_t *Len, uint8_t epnum)
CDC_TransmitCplt_HS Data transmitted callback.
uint8_t UserTxBufferHS[APP_TX_DATA_SIZE]
Data to send over USB CDC are stored in this buffer.
uint8_t UserRxBufferHS[APP_RX_DATA_SIZE]
Received data over USB are stored in this buffer.
Definition usbd_cdc_if.c:98
uint8_t CDC_Transmit_HS(uint8_t *Buf, uint16_t Len)
Data to send over USB IN endpoint are sent over CDC interface through this function.
bool usb_configured(void)
USBD_CDC_ItfTypeDef USBD_Interface_fops_HS
CDC Interface callback.
void usb_bootloader_check(void)
bool usb_terminal_open(void)
struct _USBD_HandleTypeDef USBD_HandleTypeDef
struct usb_setup_req USBD_SetupReqTypedef
@ USBD_FAIL
Definition usbd_def.h:284
@ USBD_OK
Definition usbd_def.h:281
@ USBD_BUSY
Definition usbd_def.h:282
uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev)
USBD_CDC_ReceivePacket prepare OUT Endpoint for reception.
Definition usbd_cdc.c:856
uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff)
USBD_CDC_SetRxBuffer.
Definition usbd_cdc.c:789
uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev)
USBD_CDC_TransmitPacket Transmit packet on IN endpoint.
Definition usbd_cdc.c:816
uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff, uint32_t length)
USBD_CDC_SetTxBuffer.
Definition usbd_cdc.c:766
#define NULL
Definition usbd_def.h:44
#define USBD_STATE_CONFIGURED
Definition usbd_def.h:162
#define CDC_GET_LINE_CODING
Definition usbd_cdc.h:86
#define CDC_SET_CONTROL_LINE_STATE
Definition usbd_cdc.h:87
#define CDC_GET_ENCAPSULATED_RESPONSE
Definition usbd_cdc.h:81
#define CDC_CLEAR_COMM_FEATURE
Definition usbd_cdc.h:84
#define CDC_SEND_ENCAPSULATED_COMMAND
Definition usbd_cdc.h:80
#define CDC_SEND_BREAK
Definition usbd_cdc.h:88
#define CDC_SET_COMM_FEATURE
Definition usbd_cdc.h:82
#define CDC_SET_LINE_CODING
Definition usbd_cdc.h:85
#define CDC_GET_COMM_FEATURE
Definition usbd_cdc.h:83
struct _USBD_CDC_Itf USBD_CDC_ItfTypeDef
*brief create JSON pointer *sa reference_tokens const string_t const string_t & b
Definition json.hpp:13878
json_pointer result
Definition json.hpp:14064
flight_state_t app_state(void)
Definition mode.c:48
*brief user defined string literal for JSON values *sa std::size_t n
Definition json.hpp:24474
CMSIS STM32H7xx Device Peripheral Access Layer Header File.
#define UNUSED(x)
__IO uint32_t TxState
Definition usbd_cdc.h:130
static uint8_t line_coding[7]
Definition usbd_cdc_if.c:38
: Header for usbd_cdc_if.c file.