Skip to content

Commit 55b8f81

Browse files
committed
[USB CDC] Init interface as soon as possible
Signed-off-by: Frederic Pillon <[email protected]>
1 parent 1e059c6 commit 55b8f81

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

cores/arduino/stm32/hw_config.c

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
*/
3838
#include "stm32_def.h"
3939
#include "hw_config.h"
40+
#include "usbd_if.h"
4041

4142
#ifdef __cplusplus
4243
extern "C" {
@@ -54,6 +55,10 @@ void hw_config_init(void)
5455

5556
// Configure the system clock
5657
SystemClock_Config();
58+
59+
#if defined (USBCON) && defined(USBD_USE_CDC)
60+
USBD_CDC_init();
61+
#endif
5762
}
5863
#ifdef __cplusplus
5964
}

cores/arduino/stm32/usb/cdc/usbd_cdc_if.c

+19-11
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
/* USB Device Core CDC handle declaration */
3737
USBD_HandleTypeDef hUSBD_Device_CDC;
3838

39+
static bool CDC_initialized = false;
40+
3941
/* Received Data over USB are stored in this buffer */
4042
CDC_TransmitQueue_TypeDef TransmitQueue;
4143
CDC_ReceiveQueue_TypeDef ReceiveQueue;
@@ -217,23 +219,29 @@ static int8_t USBD_CDC_Transferred (void) {
217219
}
218220

219221
void CDC_init(void) {
220-
/* Init Device Library */
221-
if (USBD_Init(&hUSBD_Device_CDC, &CDC_Desc, 0) == USBD_OK) {
222-
/* Add Supported Class */
223-
if (USBD_RegisterClass(&hUSBD_Device_CDC, USBD_CDC_CLASS) == USBD_OK) {
224-
/* Add CDC Interface Class */
225-
if (USBD_CDC_RegisterInterface(&hUSBD_Device_CDC, &USBD_CDC_fops) == USBD_OK) {
226-
/* Start Device Process */
227-
USBD_Start(&hUSBD_Device_CDC);
222+
if (!CDC_initialized) {
223+
/* Init Device Library */
224+
if (USBD_Init(&hUSBD_Device_CDC, &CDC_Desc, 0) == USBD_OK) {
225+
/* Add Supported Class */
226+
if (USBD_RegisterClass(&hUSBD_Device_CDC, USBD_CDC_CLASS) == USBD_OK) {
227+
/* Add CDC Interface Class */
228+
if (USBD_CDC_RegisterInterface(&hUSBD_Device_CDC, &USBD_CDC_fops) == USBD_OK) {
229+
/* Start Device Process */
230+
USBD_Start(&hUSBD_Device_CDC);
231+
CDC_initialized = true;
232+
}
228233
}
229234
}
230235
}
231236
}
232237

233238
void CDC_deInit(void) {
234-
USBD_Stop(&hUSBD_Device_CDC);
235-
USBD_CDC_DeInit();
236-
USBD_DeInit(&hUSBD_Device_CDC);
239+
if (CDC_initialized) {
240+
USBD_Stop(&hUSBD_Device_CDC);
241+
USBD_CDC_DeInit();
242+
USBD_DeInit(&hUSBD_Device_CDC);
243+
CDC_initialized = false;
244+
}
237245
}
238246

239247
void CDC_continue_transmit(void) {

cores/arduino/stm32/usb/usbd_if.c

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#ifdef USBCON
99

1010
#include "usbd_if.h"
11+
#include "usbd_cdc_if.h"
12+
1113
/**
1214
* @brief Force to re-enumerate USB
1315
* @param None
@@ -38,4 +40,10 @@ void USBD_reenumerate(void)
3840
#endif /* USBD_REENUM_DISABLED */
3941
}
4042

43+
#ifdef USBD_USE_CDC
44+
void USBD_CDC_init(void)
45+
{
46+
CDC_init();
47+
}
48+
#endif /* USBD_USE_CDC */
4149
#endif /* USBCON */

cores/arduino/stm32/usb/usbd_if.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
#endif
2525

2626
void USBD_reenumerate(void);
27-
27+
#ifdef USBD_USE_CDC
28+
void USBD_CDC_init(void);
29+
#endif
2830
#ifdef __cplusplus
2931
}
3032
#endif

0 commit comments

Comments
 (0)