Skip to content

Commit f084faa

Browse files
committed
fix(usb): generalize HAL v2 device adapter
1 parent 69d48fe commit f084faa

3 files changed

Lines changed: 31 additions & 29 deletions

File tree

‎libraries/SrcWrapper/inc/stm32_def.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,13 @@ __STATIC_INLINE void LL_RTC_SetBinMixBCDU(RTC_TypeDef *RTCx, uint32_t BinMixBcdU
203203
}
204204
#endif // STM32U0xx
205205

206-
/* STM32G0xx, STM32U0xx and some STM32U5xx defined USB_DRD_FS */
206+
/* Some STM32 series define USB_DRD_FS instead of the legacy USB instance. */
207207
#if !defined(USB) && defined(USB_DRD_FS)
208208
#define USB USB_DRD_FS
209209
#define PinMap_USB PinMap_USB_DRD_FS
210-
#if defined(STM32H5xx) || defined(STM32U0xx) ||\
210+
#if defined(STM32C5xx)
211+
#define USB_BASE USB_DRD_FS_BASE
212+
#elif defined(STM32H5xx) || defined(STM32U0xx) ||\
211213
defined(STM32U3xx) || defined(STM32U5xx)
212214
#define USB_BASE USB_DRD_BASE
213215
#if !defined(__HAL_RCC_USB_CLK_ENABLE)

‎libraries/USBDevice/inc/usbd_conf.h‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,15 @@ extern "C" {
3131

3232
#if defined(USE_HALV2_DRIVER)
3333
#if !defined(STM32C5xx)
34-
#error "USB library is not yet compatible with this HALv2 driver."
34+
#warning "USB device support is currently implemented only for STM32C5xx HAL v2."
35+
#endif
3536
#endif
3637

37-
/* STM32C5 uses the HAL v2 USB DRD FS PCD. Keep the legacy USB name and
38-
* PMA buffer-kind names available to the common endpoint configuration. */
39-
#define USB USB_DRD_FS
38+
#if defined(STM32C5xx)
4039
#define PCD_SNG_BUF HAL_PCD_SNG_BUF
4140
#define PCD_DBL_BUF HAL_PCD_DBL_BUF
42-
#define USB_IRQn USB_DRD_FS_IRQn
43-
#define USB_IRQHandler USB_DRD_FS_IRQHandler
44-
#else
41+
#endif
42+
4543
#if !defined(USB_BASE) && !defined(USB_OTG_DEVICE_BASE)
4644
#error "This board does not support USB! Select 'None' in the 'Tools->USB interface' menu"
4745
#endif
@@ -50,8 +48,6 @@ extern "C" {
5048
#endif
5149
#if !defined(USB_BASE) && !defined(USB_OTG_FS) && defined(USB_OTG_HS) && !defined(USE_USB_HS)
5250
#error "This board support only USB High Speed! Select 'High Speed' or 'High Speed in Full Speed mode' in the 'Tools->USB interface' menu"
53-
#endif
54-
5551
#endif
5652
#include <stdio.h>
5753
#include <stdlib.h>
@@ -87,7 +83,7 @@ extern "C" {
8783
#elif defined(STM32G0B1xx) || defined(STM32G0C1xx)
8884
#define USB_IRQn USB_UCPD1_2_IRQn
8985
#define USB_IRQHandler USB_UCPD1_2_IRQHandler
90-
#elif defined(STM32C0xx) || defined(STM32H5xx) || defined(STM32U0xx)
86+
#elif defined(STM32C0xx) || defined(STM32C5xx) || defined(STM32H5xx) || defined(STM32U0xx)
9187
#define USB_IRQn USB_DRD_FS_IRQn
9288
#define USB_IRQHandler USB_DRD_FS_IRQHandler
9389
#elif defined(STM32U5xx) && !defined(USB_DRD_FS)
Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
/**
22
******************************************************************************
3-
* @file usbd_conf_c5.c
4-
* @brief STM32C5 HAL v2 USB device low-level adapter.
3+
* @file usbd_confv2.c
4+
* @brief HAL v2 USB device low-level adapter.
55
******************************************************************************
66
*/
77

8-
#if defined(USBCON) && defined(USE_HALV2_DRIVER) && defined(STM32C5xx)
8+
#if defined(USBCON) && defined(USE_HALV2_DRIVER)
9+
10+
#if !defined(STM32C5xx)
11+
#warning "USB device support is currently implemented only for STM32C5xx HAL v2."
12+
#else
913

1014
#include "usbd_core.h"
1115
#include "usbd_if.h"
@@ -20,20 +24,19 @@ static USBD_HandleTypeDef *g_pdev;
2024
static uint8_t g_stalled_in;
2125
static uint8_t g_stalled_out;
2226

23-
static void USBD_C5_ErrorHandler(void)
27+
static void USBD_V2_ErrorHandler(void)
2428
{
2529
Error_Handler();
2630
}
2731

28-
static void USBD_C5_ConfigPinsAndIrq(void)
32+
static void USBD_V2_ConfigPinsAndIrq(void)
2933
{
30-
/* USB DRD FS owns PA11/PA12 and configures these fixed USB pins itself
31-
* during peripheral initialization. Do not configure a GPIO alternate
32-
* function here: C5 USB differs from the older USB device IPs. */
34+
/* The HAL v2 USB DRD FS PCD owns PA11/PA12 and configures these fixed USB
35+
* pins during peripheral initialization. */
3336

3437
HAL_RCC_USB_EnableClock();
35-
HAL_CORTEX_NVIC_SetPriority(USB_DRD_FS_IRQn, USBD_IRQ_PRIO, USBD_IRQ_SUBPRIO);
36-
HAL_CORTEX_NVIC_EnableIRQ(USB_DRD_FS_IRQn);
38+
HAL_CORTEX_NVIC_SetPriority(USB_IRQn, USBD_IRQ_PRIO, USBD_IRQ_SUBPRIO);
39+
HAL_CORTEX_NVIC_EnableIRQ(USB_IRQn);
3740
}
3841

3942
void HAL_PCD_SetupStageCallback(hal_pcd_handle_t *hpcd)
@@ -100,7 +103,7 @@ void HAL_PCD_DisconnectCallback(hal_pcd_handle_t *hpcd)
100103
USBD_LL_DevDisconnected(g_pdev);
101104
}
102105

103-
void USB_DRD_FS_IRQHandler(void)
106+
void USB_IRQHandler(void)
104107
{
105108
HAL_PCD_IRQHandler(&g_hpcd);
106109
}
@@ -119,24 +122,24 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
119122
};
120123

121124
USBD_reenumerate();
122-
USBD_C5_ConfigPinsAndIrq();
125+
USBD_V2_ConfigPinsAndIrq();
123126

124127
g_pdev = pdev;
125128
g_stalled_in = 0U;
126129
g_stalled_out = 0U;
127130
pdev->pData = &g_hpcd;
128131

129132
if (HAL_PCD_Init(&g_hpcd, HAL_PCD_DRD_FS) != HAL_OK) {
130-
USBD_C5_ErrorHandler();
133+
USBD_V2_ErrorHandler();
131134
}
132135
if (HAL_PCD_SetConfig(&g_hpcd, &config) != HAL_OK) {
133-
USBD_C5_ErrorHandler();
136+
USBD_V2_ErrorHandler();
134137
}
135138

136139
for (uint32_t i = 0U; i < (DEV_NUM_EP + 1U); i++) {
137140
if (HAL_PCD_PMAConfig(&g_hpcd, ep_def[i].ep_adress, ep_def[i].ep_kind,
138141
ep_def[i].ep_size) != HAL_OK) {
139-
USBD_C5_ErrorHandler();
142+
USBD_V2_ErrorHandler();
140143
}
141144
}
142145

@@ -146,7 +149,7 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
146149
USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev)
147150
{
148151
(void)pdev;
149-
HAL_CORTEX_NVIC_DisableIRQ(USB_DRD_FS_IRQn);
152+
HAL_CORTEX_NVIC_DisableIRQ(USB_IRQn);
150153
HAL_PCD_DeInit(&g_hpcd);
151154
HAL_RCC_USB_DisableClock();
152155
g_pdev = NULL;
@@ -248,4 +251,5 @@ void USBD_LL_Delay(uint32_t delay)
248251
HAL_Delay(delay);
249252
}
250253

251-
#endif /* USBCON && USE_HALV2_DRIVER && STM32C5xx */
254+
#endif /* STM32C5xx */
255+
#endif /* USBCON && USE_HALV2_DRIVER */

0 commit comments

Comments
 (0)