Skip to content

Commit 9ecef7c

Browse files
committed
[USB] Fix across series
GPIO config will be handle thanks Peripherals Pins Signed-off-by: Frederic.Pillon <[email protected]>
1 parent a984c84 commit 9ecef7c

File tree

2 files changed

+175
-44
lines changed

2 files changed

+175
-44
lines changed

cores/arduino/stm32/usb/usbd_conf.c

+146-44
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,68 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
5353
HAL_PWREx_EnableVddUSB();
5454
}
5555
#endif
56+
#if defined (USB)
57+
if(hpcd->Instance == USB) {
58+
/* Configure USB FS GPIOs */
59+
__HAL_RCC_GPIOA_CLK_ENABLE();
60+
61+
/* Configure DM DP Pins */
62+
GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
63+
#ifdef GPIO_MODE_AF_INPUT
64+
GPIO_InitStruct.Mode = GPIO_MODE_AF_INPUT;
65+
#else
66+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
67+
#endif
68+
GPIO_InitStruct.Pull = GPIO_PULLUP;
69+
#ifdef GPIO_SPEED_FREQ_VERY_HIGH
70+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
71+
#else
72+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
73+
#endif
74+
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
75+
76+
/* Configure VBUS Pin */
77+
GPIO_InitStruct.Pin = GPIO_PIN_9;
78+
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
79+
GPIO_InitStruct.Pull = GPIO_NOPULL;
80+
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
81+
82+
/* Configure ID pin */
83+
GPIO_InitStruct.Pin = GPIO_PIN_10;
84+
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
85+
GPIO_InitStruct.Pull = GPIO_PULLUP;
86+
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
5687

88+
/* Enable USB FS Clock */
89+
__HAL_RCC_USB_CLK_ENABLE();
90+
91+
#if defined (USE_USB_INTERRUPT_REMAPPED)
92+
/*USB interrupt remapping enable */
93+
__HAL_REMAPINTERRUPT_USB_ENABLE();
94+
#endif
95+
96+
/* Set USB FS Interrupt priority */
97+
HAL_NVIC_SetPriority(USB_IRQn, 5, 0);
98+
99+
/* Enable USB FS Interrupt */
100+
HAL_NVIC_EnableIRQ(USB_IRQn);
101+
102+
if(hpcd->Init.low_power_enable == 1) {
103+
/* Enable EXTI for USB wakeup */
104+
__HAL_USB_WAKEUP_EXTI_CLEAR_FLAG();
105+
__HAL_USB_WAKEUP_EXTI_ENABLE_RISING_EDGE();
106+
__HAL_USB_WAKEUP_EXTI_ENABLE_IT();
107+
#if defined(STM32F1xx) || defined(STM32F3xx)
108+
/* USB Wakeup Interrupt */
109+
HAL_NVIC_EnableIRQ(USBWakeUp_IRQn);
110+
111+
/* Enable USB Wake-up interrupt */
112+
HAL_NVIC_SetPriority(USBWakeUp_IRQn, 0, 0);
113+
#endif
114+
}
115+
}
116+
#endif /* USB */
117+
#if defined (USB_OTG_FS)
57118
if(hpcd->Instance == USB_OTG_FS) {
58119
/* Configure USB FS GPIOs */
59120
__HAL_RCC_GPIOA_CLK_ENABLE();
@@ -93,10 +154,21 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
93154
__HAL_USB_OTG_FS_WAKEUP_EXTI_CLEAR_FLAG();
94155
__HAL_USB_OTG_FS_WAKEUP_EXTI_ENABLE_RISING_EDGE();
95156
__HAL_USB_OTG_FS_WAKEUP_EXTI_ENABLE_IT();
157+
#if !defined(STM32L4xx)
158+
/* Set EXTI Wakeup Interrupt priority */
159+
HAL_NVIC_SetPriority(OTG_FS_WKUP_IRQn, 0, 0);
160+
161+
/* Enable EXTI Interrupt */
162+
HAL_NVIC_EnableIRQ(OTG_FS_WKUP_IRQn);
163+
#endif
96164
}
97165
}
166+
#endif /* USB_OTG_FS */
98167
#if defined (USB_OTG_HS)
99-
else if(hpcd->Instance == USB_OTG_HS) {
168+
if(hpcd->Instance == USB_OTG_HS) {
169+
#ifdef USE_USB_HS_IN_FS
170+
171+
#else
100172
/* Configure USB FS GPIOs */
101173
__HAL_RCC_GPIOA_CLK_ENABLE();
102174
__HAL_RCC_GPIOB_CLK_ENABLE();
@@ -149,6 +221,7 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
149221
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
150222

151223
__HAL_RCC_USB_OTG_HS_ULPI_CLK_ENABLE();
224+
#endif /* USE_USB_HS_IN_FS */
152225

153226
/* Enable USB HS Clocks */
154227
__HAL_RCC_USB_OTG_HS_CLK_ENABLE();
@@ -160,12 +233,12 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
160233
HAL_NVIC_EnableIRQ(OTG_HS_IRQn);
161234

162235
if(hpcd->Init.low_power_enable == 1) {
163-
/* Enable EXTI Line 20 for USB wakeup*/
236+
/* Enable EXTI Line 20 for USB wakeup */
164237
__HAL_USB_OTG_HS_WAKEUP_EXTI_CLEAR_FLAG();
165238
__HAL_USB_OTG_HS_WAKEUP_EXTI_ENABLE_RISING_EDGE();
166239
__HAL_USB_OTG_HS_WAKEUP_EXTI_ENABLE_IT();
167240

168-
/* Set EXTI Wakeup Interrupt priority*/
241+
/* Set EXTI Wakeup Interrupt priority */
169242
HAL_NVIC_SetPriority(OTG_HS_WKUP_IRQn, 0, 0);
170243

171244
/* Enable EXTI Interrupt */
@@ -182,18 +255,26 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
182255
*/
183256
void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
184257
{
258+
/* Disable USB FS Clock */
259+
#if defined (USB)
260+
if(hpcd->Instance == USB) {
261+
__HAL_RCC_USB_CLK_DISABLE();
262+
}
263+
#endif
264+
#if defined (USB_OTG_FS)
185265
if(hpcd->Instance == USB_OTG_FS) {
186266
/* Disable USB FS Clock */
187267
__HAL_RCC_USB_OTG_FS_CLK_DISABLE();
188-
__HAL_RCC_SYSCFG_CLK_DISABLE();
189268
}
269+
#endif
190270
#if defined (USB_OTG_HS)
191-
else if(hpcd->Instance == USB_OTG_HS) {
271+
if(hpcd->Instance == USB_OTG_HS) {
192272
/* Disable USB HS Clocks */
193273
__HAL_RCC_USB_OTG_HS_CLK_DISABLE();
194-
__HAL_RCC_SYSCFG_CLK_DISABLE();
195274
}
196275
#endif /* USB_OTG_HS */
276+
/* Disable SYSCFG Clock */
277+
__HAL_RCC_SYSCFG_CLK_DISABLE();
197278
}
198279

199280
/*******************************************************************************
@@ -284,9 +365,8 @@ void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
284365
USBD_LL_Suspend(hpcd->pData);
285366
__HAL_PCD_GATE_PHYCLOCK(hpcd);
286367

287-
/*Enter in STOP mode */
288-
if (hpcd->Init.low_power_enable)
289-
{
368+
/*Enter in STOP mode */
369+
if (hpcd->Init.low_power_enable) {
290370
/* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register */
291371
SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
292372
}
@@ -299,6 +379,12 @@ void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
299379
*/
300380
void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
301381
{
382+
if (hpcd->Init.low_power_enable) {
383+
SystemClock_Config();
384+
385+
/* Reset SLEEPDEEP bit of Cortex System Control Register */
386+
SCB->SCR &= (uint32_t)~((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
387+
}
302388
__HAL_PCD_UNGATE_PHYCLOCK(hpcd);
303389
USBD_LL_Resume(hpcd->pData);
304390
}
@@ -354,13 +440,17 @@ void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
354440
*/
355441
#ifdef USE_USB_HS
356442
void OTG_HS_IRQHandler(void)
357-
#else
443+
#elif defined(USB_OTG_FS)
358444
void OTG_FS_IRQHandler(void)
445+
#else /* USB */
446+
void USB_IRQHandler(void)
359447
#endif
360448
{
361449
HAL_PCD_IRQHandler(&g_hpcd);
362450
}
363451

452+
453+
364454
/**
365455
* @brief This function handles USB OTG FS Wakeup IRQ Handler.
366456
* @param None
@@ -369,8 +459,10 @@ void OTG_FS_IRQHandler(void)
369459

370460
#ifdef USE_USB_HS
371461
void OTG_HS_WKUP_IRQHandler(void)
372-
#else
462+
#elif defined(USB_OTG_FS)
373463
void OTG_FS_WKUP_IRQHandler(void)
464+
#else
465+
void USBWakeUp_IRQHandler(void)
374466
#endif
375467
{
376468
if((&g_hpcd)->Init.low_power_enable)
@@ -386,11 +478,13 @@ void OTG_FS_WKUP_IRQHandler(void)
386478
__HAL_PCD_UNGATE_PHYCLOCK((&g_hpcd));
387479
}
388480
#ifdef USE_USB_HS
389-
/* Clear EXTI pending Bit*/
481+
/* Clear EXTI pending Bit*/
390482
__HAL_USB_OTG_HS_WAKEUP_EXTI_CLEAR_FLAG();
391-
#else
483+
#elif defined(USB_OTG_FS)
392484
/* Clear EXTI pending Bit*/
393485
__HAL_USB_OTG_FS_WAKEUP_EXTI_CLEAR_FLAG();
486+
#else
487+
__HAL_USB_WAKEUP_EXTI_CLEAR_FLAG();
394488
#endif
395489
}
396490
/*******************************************************************************
@@ -403,64 +497,72 @@ void OTG_FS_WKUP_IRQHandler(void)
403497
*/
404498
USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
405499
{
500+
/* Set common LL Driver parameters */
501+
g_hpcd.Init.dev_endpoints = 4;
502+
g_hpcd.Init.ep0_mps = DEP0CTL_MPS_64;
503+
#if !defined(STM32F1xx) && !defined(STM32F2xx) || defined(USB)
504+
g_hpcd.Init.lpm_enable = DISABLE;
505+
g_hpcd.Init.battery_charging_enable = DISABLE;
506+
#endif
507+
g_hpcd.Init.low_power_enable = DISABLE;
508+
g_hpcd.Init.Sof_enable = DISABLE;
509+
510+
/* Set specific LL Driver parameters */
406511
#ifdef USE_USB_HS
407-
/* Set LL Driver parameters */
408512
g_hpcd.Instance = USB_OTG_HS;
409-
g_hpcd.Init.dev_endpoints = 3;
410513
g_hpcd.Init.use_dedicated_ep1 = DISABLE;
411-
g_hpcd.Init.ep0_mps = DEP0CTL_MPS_64;
412514
g_hpcd.Init.dma_enable = DISABLE;
413-
g_hpcd.Init.low_power_enable = DISABLE;
414-
g_hpcd.Init.lpm_enable = DISABLE;
415-
g_hpcd.Init.battery_charging_enable = DISABLE;
515+
#ifdef USE_USB_HS_IN_FS
516+
g_hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
517+
#else
416518
g_hpcd.Init.phy_itface = PCD_PHY_ULPI;
417-
g_hpcd.Init.Sof_enable = ENABLE;
519+
#endif
418520
g_hpcd.Init.speed = PCD_SPEED_HIGH;
419521
g_hpcd.Init.vbus_sensing_enable = ENABLE;
420522
g_hpcd.Init.use_external_vbus = DISABLE;
421-
/* Link The driver to the stack */
422-
g_hpcd.pData = pdev;
423-
pdev->pData = &g_hpcd;
424-
/* Initialize LL Driver */
425-
if (HAL_PCD_Init(&g_hpcd) != HAL_OK) {
426-
Error_Handler();
427-
}
428-
429-
/* configure EPs FIFOs */
430-
HAL_PCDEx_SetRxFiFo(&g_hpcd, 0x200);
431-
HAL_PCDEx_SetTxFiFo(&g_hpcd, 0, 0x80);
432-
HAL_PCDEx_SetTxFiFo(&g_hpcd, 1, 0x40);
433-
HAL_PCDEx_SetTxFiFo(&g_hpcd, 2, 0x160);
434-
435523
#else /* USE_USB_FS */
436-
/* Set LL Driver parameters */
524+
#ifdef USB_OTG_FS
437525
g_hpcd.Instance = USB_OTG_FS;
438-
g_hpcd.Init.dev_endpoints = 3;
439526
g_hpcd.Init.use_dedicated_ep1 = DISABLE;
440-
g_hpcd.Init.ep0_mps = DEP0CTL_MPS_64;
441527
g_hpcd.Init.dma_enable = DISABLE;
442-
g_hpcd.Init.low_power_enable = DISABLE;
443-
g_hpcd.Init.lpm_enable = DISABLE;
444-
g_hpcd.Init.battery_charging_enable = DISABLE;
445-
g_hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
446-
g_hpcd.Init.Sof_enable = DISABLE;
447-
g_hpcd.Init.speed = PCD_SPEED_FULL;
448528
g_hpcd.Init.vbus_sensing_enable = DISABLE;
449529
g_hpcd.Init.use_external_vbus = DISABLE;
530+
#else
531+
g_hpcd.Instance = USB;
532+
#endif
533+
g_hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
534+
g_hpcd.Init.speed = PCD_SPEED_FULL;
535+
#endif /* USE_USB_HS */
536+
450537
/* Link The driver to the stack */
451538
g_hpcd.pData = pdev;
452539
pdev->pData = &g_hpcd;
540+
453541
/* Initialize LL Driver */
454542
if (HAL_PCD_Init(&g_hpcd) != HAL_OK) {
455543
Error_Handler();
456544
}
457545

546+
#ifdef USE_USB_HS
547+
/* configure EPs FIFOs */
548+
HAL_PCDEx_SetRxFiFo(&g_hpcd, 0x200);
549+
HAL_PCDEx_SetTxFiFo(&g_hpcd, 0, 0x80);
550+
HAL_PCDEx_SetTxFiFo(&g_hpcd, 1, 0x40);
551+
HAL_PCDEx_SetTxFiFo(&g_hpcd, 2, 0x160);
552+
#else /* USE_USB_FS */
553+
#ifdef USB_OTG_FS
458554
/* configure EPs FIFOs */
459555
HAL_PCDEx_SetRxFiFo(&g_hpcd, 0x80);
460556
HAL_PCDEx_SetTxFiFo(&g_hpcd, 0, 0x40);
461557
HAL_PCDEx_SetTxFiFo(&g_hpcd, 1, 0x40);
462558
HAL_PCDEx_SetTxFiFo(&g_hpcd, 2, 0x40);
463-
#endif /* USE_USB_(F|H)S */
559+
#else
560+
HAL_PCDEx_PMAConfig(&g_hpcd, 0x00 , PCD_SNG_BUF, 0x18);
561+
HAL_PCDEx_PMAConfig(&g_hpcd, 0x80 , PCD_SNG_BUF, 0x58);
562+
HAL_PCDEx_PMAConfig(&g_hpcd, 0x81 , PCD_SNG_BUF, 0x100);
563+
HAL_PCDEx_PMAConfig(&g_hpcd, 0x82 , PCD_SNG_BUF, 0x100);
564+
#endif
565+
#endif /* USE_USB_HS */
464566
return USBD_OK;
465567
}
466568

cores/arduino/stm32/usb/usbd_conf.h

+29
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,35 @@
4040
#include <stdlib.h>
4141
#include <string.h>
4242

43+
#if defined(USB_BASE)
44+
45+
#if defined(STM32F1xx)
46+
#define USB_IRQn USB_LP_CAN1_RX0_IRQn
47+
#define USB_IRQHandler USB_LP_CAN1_RX0_IRQHandler
48+
#elif defined(STM32F3xx)
49+
/* ToDo: Check remap on USB_LP_IRQn */
50+
#ifndef USE_USB_INTERRUPT_REMAPPED
51+
#define USB_IRQn USB_LP_CAN_RX0_IRQn
52+
#define USB_IRQHandler USB_LP_CAN_RX0_IRQHandler
53+
#else
54+
#define USB_IRQn USB_LP_IRQn
55+
#define USB_IRQHandler USB_LP_IRQHandler
56+
#endif /* USE_USB_INTERRUPT_REMAPPED */
57+
#elif defined(STM32L1xx)
58+
#define USB_IRQn USB_LP_IRQn
59+
#define USB_IRQHandler USB_LP_IRQHandler
60+
#endif
61+
62+
#endif /* USB_BASE */
63+
64+
#ifndef __HAL_PCD_GATE_PHYCLOCK
65+
#define __HAL_PCD_GATE_PHYCLOCK(_DUMMY_)
66+
#endif
67+
68+
#ifndef __HAL_PCD_UNGATE_PHYCLOCK
69+
#define __HAL_PCD_UNGATE_PHYCLOCK(_DUMMY_)
70+
#endif
71+
4372
#ifndef USBD_MAX_NUM_INTERFACES
4473
#define USBD_MAX_NUM_INTERFACES 2U
4574
#endif /* USBD_MAX_NUM_INTERFACES */

0 commit comments

Comments
 (0)