Skip to content

Commit 861cd07

Browse files
committed
[USB] Introduce USB HS
Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 8917c9d commit 861cd07

File tree

2 files changed

+203
-72
lines changed

2 files changed

+203
-72
lines changed

cores/arduino/board.h

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ extern "C"{
2424
#if !defined(USB_BASE) && !defined(USB_OTG_DEVICE_BASE)
2525
#error "This board does not support USB! Select 'None' in the 'Tools->USB interface' menu"
2626
#endif
27+
#if defined(USE_USB_HS) && !defined(USB_OTG_HS)
28+
#error "This board does not support USB High Speed! Select 'Full Speed' in the 'Tools->USB interface' menu"
29+
#endif
2730
#include "usb_interface.h"
2831
#endif /* USBCON */
2932

cores/arduino/stm32/usbd_conf.c

+200-72
Original file line numberDiff line numberDiff line change
@@ -39,59 +39,136 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
3939
{
4040
GPIO_InitTypeDef GPIO_InitStruct;
4141

42-
/* Enable USB power on Pwrctrl CR2 register */
43-
HAL_PWREx_EnableVddUSB();
44-
45-
/* Configure USB FS GPIOs */
46-
__HAL_RCC_GPIOA_CLK_ENABLE();
47-
48-
/* Configure DM DP Pins */
49-
GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
50-
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
51-
GPIO_InitStruct.Pull = GPIO_NOPULL;
52-
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
53-
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
54-
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
55-
56-
/* Configure VBUS Pin */
57-
GPIO_InitStruct.Pin = GPIO_PIN_9;
58-
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
59-
GPIO_InitStruct.Pull = GPIO_NOPULL;
60-
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
61-
62-
/* Configure ID pin */
63-
GPIO_InitStruct.Pin = GPIO_PIN_10;
64-
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
65-
GPIO_InitStruct.Pull = GPIO_PULLUP;
66-
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
67-
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
68-
69-
/* Configure power enable pin (USB_OTG_FS_PWR_EN) */
70-
__HAL_RCC_GPIOD_CLK_ENABLE();
71-
GPIO_InitStruct.Pin = GPIO_PIN_12;
72-
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
73-
GPIO_InitStruct.Pull = GPIO_PULLUP;
74-
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
75-
76-
/* USB power output is disabled in device mode */
77-
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
78-
79-
/* Enable USB FS Clock */
80-
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();
81-
82-
/* Set USB FS Interrupt priority */
83-
HAL_NVIC_SetPriority(OTG_FS_IRQn, 5, 0);
84-
85-
/* Enable USB FS Interrupt */
86-
HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
87-
88-
if(hpcd->Init.low_power_enable == 1)
89-
{
90-
/* Enable EXTI Line 18 for USB wakeup */
91-
__HAL_USB_OTG_FS_WAKEUP_EXTI_CLEAR_FLAG();
92-
__HAL_USB_OTG_FS_WAKEUP_EXTI_ENABLE_RISING_EDGE();
93-
__HAL_USB_OTG_FS_WAKEUP_EXTI_ENABLE_IT();
42+
#if defined(PWR_CR2_USV)
43+
/* Enable VDDUSB on Pwrctrl CR2 register*/
44+
if(__HAL_RCC_PWR_IS_CLK_DISABLED()) {
45+
__HAL_RCC_PWR_CLK_ENABLE();
46+
HAL_PWREx_EnableVddUSB();
47+
__HAL_RCC_PWR_CLK_DISABLE();
48+
} else {
49+
HAL_PWREx_EnableVddUSB();
50+
}
51+
#endif
52+
53+
if(hpcd->Instance == USB_OTG_FS) {
54+
/* Configure USB FS GPIOs */
55+
__HAL_RCC_GPIOA_CLK_ENABLE();
56+
57+
/* Configure DM DP Pins */
58+
GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
59+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
60+
GPIO_InitStruct.Pull = GPIO_NOPULL;
61+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
62+
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
63+
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
64+
65+
/* Configure VBUS Pin */
66+
GPIO_InitStruct.Pin = GPIO_PIN_9;
67+
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
68+
GPIO_InitStruct.Pull = GPIO_NOPULL;
69+
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
70+
71+
/* Configure ID pin */
72+
GPIO_InitStruct.Pin = GPIO_PIN_10;
73+
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
74+
GPIO_InitStruct.Pull = GPIO_PULLUP;
75+
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
76+
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
77+
78+
/* Enable USB FS Clock */
79+
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();
80+
81+
/* Set USB FS Interrupt priority */
82+
HAL_NVIC_SetPriority(OTG_FS_IRQn, 5, 0);
83+
84+
/* Enable USB FS Interrupt */
85+
HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
86+
87+
if(hpcd->Init.low_power_enable == 1) {
88+
/* Enable EXTI Line 18 for USB wakeup */
89+
__HAL_USB_OTG_FS_WAKEUP_EXTI_CLEAR_FLAG();
90+
__HAL_USB_OTG_FS_WAKEUP_EXTI_ENABLE_RISING_EDGE();
91+
__HAL_USB_OTG_FS_WAKEUP_EXTI_ENABLE_IT();
92+
}
9493
}
94+
#if defined (USB_OTG_HS)
95+
else if(hpcd->Instance == USB_OTG_HS) {
96+
/* Configure USB FS GPIOs */
97+
__HAL_RCC_GPIOA_CLK_ENABLE();
98+
__HAL_RCC_GPIOB_CLK_ENABLE();
99+
__HAL_RCC_GPIOC_CLK_ENABLE();
100+
__HAL_RCC_GPIOH_CLK_ENABLE();
101+
102+
/* CLK */
103+
GPIO_InitStruct.Pin = GPIO_PIN_5;
104+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
105+
GPIO_InitStruct.Pull = GPIO_NOPULL;
106+
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
107+
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
108+
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
109+
110+
/* D0 */
111+
GPIO_InitStruct.Pin = GPIO_PIN_3;
112+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
113+
GPIO_InitStruct.Pull = GPIO_NOPULL;
114+
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
115+
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
116+
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
117+
118+
/* D1 D2 D3 D4 D5 D6 D7 */
119+
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_5 |\
120+
GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
121+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
122+
GPIO_InitStruct.Pull = GPIO_NOPULL;
123+
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
124+
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
125+
126+
/* STP */
127+
GPIO_InitStruct.Pin = GPIO_PIN_0;
128+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
129+
GPIO_InitStruct.Pull = GPIO_NOPULL;
130+
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
131+
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
132+
133+
/* NXT */
134+
GPIO_InitStruct.Pin = GPIO_PIN_4;
135+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
136+
GPIO_InitStruct.Pull = GPIO_NOPULL;
137+
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
138+
HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
139+
140+
/* DIR */
141+
GPIO_InitStruct.Pin = GPIO_PIN_2;
142+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
143+
GPIO_InitStruct.Pull = GPIO_NOPULL;
144+
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
145+
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
146+
147+
__HAL_RCC_USB_OTG_HS_ULPI_CLK_ENABLE();
148+
149+
/* Enable USB HS Clocks */
150+
__HAL_RCC_USB_OTG_HS_CLK_ENABLE();
151+
152+
/* Set USBHS Interrupt to the lowest priority */
153+
HAL_NVIC_SetPriority(OTG_HS_IRQn, 5, 0);
154+
155+
/* Enable USBHS Interrupt */
156+
HAL_NVIC_EnableIRQ(OTG_HS_IRQn);
157+
158+
if(hpcd->Init.low_power_enable == 1) {
159+
/* Enable EXTI Line 20 for USB wakeup*/
160+
__HAL_USB_OTG_HS_WAKEUP_EXTI_CLEAR_FLAG();
161+
__HAL_USB_OTG_HS_WAKEUP_EXTI_ENABLE_RISING_EDGE();
162+
__HAL_USB_OTG_HS_WAKEUP_EXTI_ENABLE_IT();
163+
164+
/* Set EXTI Wakeup Interrupt priority*/
165+
HAL_NVIC_SetPriority(OTG_HS_WKUP_IRQn, 0, 0);
166+
167+
/* Enable EXTI Interrupt */
168+
HAL_NVIC_EnableIRQ(OTG_HS_WKUP_IRQn);
169+
}
170+
}
171+
#endif /* USB_OTG_HS */
95172
}
96173

97174
/**
@@ -101,11 +178,18 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
101178
*/
102179
void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
103180
{
104-
UNUSED(hpcd);
105-
106-
/* Disable USB FS Clock */
107-
__HAL_RCC_USB_OTG_FS_CLK_DISABLE();
108-
__HAL_RCC_SYSCFG_CLK_DISABLE();
181+
if(hpcd->Instance == USB_OTG_FS) {
182+
/* Disable USB FS Clock */
183+
__HAL_RCC_USB_OTG_FS_CLK_DISABLE();
184+
__HAL_RCC_SYSCFG_CLK_DISABLE();
185+
}
186+
#if defined (USB_OTG_HS)
187+
else if(hpcd->Instance == USB_OTG_HS) {
188+
/* Disable USB HS Clocks */
189+
__HAL_RCC_USB_OTG_HS_CLK_DISABLE();
190+
__HAL_RCC_SYSCFG_CLK_DISABLE();
191+
}
192+
#endif /* USB_OTG_HS */
109193
}
110194

111195
/*******************************************************************************
@@ -193,8 +277,8 @@ void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
193277
*/
194278
void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
195279
{
196-
__HAL_PCD_GATE_PHYCLOCK(hpcd);
197280
USBD_LL_Suspend(hpcd->pData);
281+
__HAL_PCD_GATE_PHYCLOCK(hpcd);
198282

199283
/*Enter in STOP mode */
200284
if (hpcd->Init.low_power_enable)
@@ -260,11 +344,15 @@ void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
260344

261345

262346
/**
263-
* @brief This function handles USB-On-The-Go FS global interrupt request.
347+
* @brief This function handles USB-On-The-Go FS/HS global interrupt request.
264348
* @param None
265349
* @retval None
266350
*/
351+
#ifdef USE_USB_HS
352+
void OTG_HS_IRQHandler(void)
353+
#else
267354
void OTG_FS_IRQHandler(void)
355+
#endif
268356
{
269357
HAL_PCD_IRQHandler(&g_hpcd);
270358
}
@@ -275,7 +363,11 @@ void OTG_FS_IRQHandler(void)
275363
* @retval None
276364
*/
277365

366+
#ifdef USE_USB_HS
367+
void OTG_HS_WKUP_IRQHandler(void)
368+
#else
278369
void OTG_FS_WKUP_IRQHandler(void)
370+
#endif
279371
{
280372
if((&g_hpcd)->Init.low_power_enable)
281373
{
@@ -289,9 +381,13 @@ void OTG_FS_WKUP_IRQHandler(void)
289381
/* ungate PHY clock */
290382
__HAL_PCD_UNGATE_PHYCLOCK((&g_hpcd));
291383
}
292-
384+
#ifdef USE_USB_HS
385+
/* Clear EXTI pending Bit*/
386+
__HAL_USB_OTG_HS_WAKEUP_EXTI_CLEAR_FLAG();
387+
#else
293388
/* Clear EXTI pending Bit*/
294389
__HAL_USB_OTG_FS_WAKEUP_EXTI_CLEAR_FLAG();
390+
#endif
295391
}
296392
/*******************************************************************************
297393
LL Driver Interface (USB Device Library --> PCD)
@@ -303,32 +399,64 @@ void OTG_FS_WKUP_IRQHandler(void)
303399
*/
304400
USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
305401
{
402+
#ifdef USE_USB_HS
403+
/* Set LL Driver parameters */
404+
g_hpcd.Instance = USB_OTG_HS;
405+
g_hpcd.Init.dev_endpoints = 3;
406+
g_hpcd.Init.use_dedicated_ep1 = DISABLE;
407+
g_hpcd.Init.ep0_mps = DEP0CTL_MPS_64;
408+
g_hpcd.Init.dma_enable = DISABLE;
409+
g_hpcd.Init.low_power_enable = DISABLE;
410+
g_hpcd.Init.lpm_enable = DISABLE;
411+
g_hpcd.Init.battery_charging_enable = DISABLE;
412+
g_hpcd.Init.phy_itface = PCD_PHY_ULPI;
413+
g_hpcd.Init.Sof_enable = ENABLE;
414+
g_hpcd.Init.speed = PCD_SPEED_HIGH;
415+
g_hpcd.Init.vbus_sensing_enable = ENABLE;
416+
g_hpcd.Init.use_external_vbus = DISABLE;
417+
/* Link The driver to the stack */
418+
g_hpcd.pData = pdev;
419+
pdev->pData = &g_hpcd;
420+
/* Initialize LL Driver */
421+
if (HAL_PCD_Init(&g_hpcd) != HAL_OK) {
422+
Error_Handler();
423+
}
424+
425+
/* configure EPs FIFOs */
426+
HAL_PCDEx_SetRxFiFo(&g_hpcd, 0x200);
427+
HAL_PCDEx_SetTxFiFo(&g_hpcd, 0, 0x80);
428+
HAL_PCDEx_SetTxFiFo(&g_hpcd, 1, 0x40);
429+
HAL_PCDEx_SetTxFiFo(&g_hpcd, 2, 0x160);
430+
431+
#else /* USE_USB_FS */
306432
/* Set LL Driver parameters */
307433
g_hpcd.Instance = USB_OTG_FS;
308434
g_hpcd.Init.dev_endpoints = 3;
309-
g_hpcd.Init.use_dedicated_ep1 = 0;
435+
g_hpcd.Init.use_dedicated_ep1 = DISABLE;
310436
g_hpcd.Init.ep0_mps = DEP0CTL_MPS_64;
311-
g_hpcd.Init.dma_enable = 0;
312-
g_hpcd.Init.low_power_enable = 0;
313-
g_hpcd.Init.lpm_enable = 0;
314-
g_hpcd.Init.battery_charging_enable = 0;
437+
g_hpcd.Init.dma_enable = DISABLE;
438+
g_hpcd.Init.low_power_enable = DISABLE;
439+
g_hpcd.Init.lpm_enable = DISABLE;
440+
g_hpcd.Init.battery_charging_enable = DISABLE;
315441
g_hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
316-
g_hpcd.Init.Sof_enable = 0;
442+
g_hpcd.Init.Sof_enable = DISABLE;
317443
g_hpcd.Init.speed = PCD_SPEED_FULL;
318-
g_hpcd.Init.vbus_sensing_enable = 1;
319-
g_hpcd.Init.lpm_enable = 0;
320-
g_hpcd.Init.use_external_vbus = 0;
444+
g_hpcd.Init.vbus_sensing_enable = DISABLE;
445+
g_hpcd.Init.use_external_vbus = DISABLE;
321446
/* Link The driver to the stack */
322447
g_hpcd.pData = pdev;
323448
pdev->pData = &g_hpcd;
324449
/* Initialize LL Driver */
325-
HAL_PCD_Init(&g_hpcd);
450+
if (HAL_PCD_Init(&g_hpcd) != HAL_OK) {
451+
Error_Handler();
452+
}
326453

327454
/* configure EPs FIFOs */
328455
HAL_PCDEx_SetRxFiFo(&g_hpcd, 0x80);
329456
HAL_PCDEx_SetTxFiFo(&g_hpcd, 0, 0x40);
330-
HAL_PCDEx_SetTxFiFo(&g_hpcd, 1, 0x80);
331-
HAL_PCDEx_SetTxFiFo(&g_hpcd, 2, 0x80);
457+
HAL_PCDEx_SetTxFiFo(&g_hpcd, 1, 0x40);
458+
HAL_PCDEx_SetTxFiFo(&g_hpcd, 2, 0x40);
459+
#endif /* USE_USB_(F|H)S */
332460
return USBD_OK;
333461
}
334462

0 commit comments

Comments
 (0)