Skip to content

Commit 9577fdb

Browse files
committed
[USB] Use pin map for GPIO configuration
Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 243de08 commit 9577fdb

File tree

3 files changed

+27
-107
lines changed

3 files changed

+27
-107
lines changed

cores/arduino/stm32/PeripheralPins.h

+5
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,10 @@ extern const PinMap PinMap_Ethernet[];
7171
//*** QUADSPI ***
7272
extern const PinMap PinMap_QUADSPI[];
7373

74+
//*** USB ***
75+
extern const PinMap PinMap_USB[];
76+
extern const PinMap PinMap_USB_OTG_FS[];
77+
extern const PinMap PinMap_USB_OTG_HS[];
78+
7479
#endif
7580

cores/arduino/stm32/usb/usbd_conf.c

+21-107
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ PCD_HandleTypeDef g_hpcd;
4141
*/
4242
void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
4343
{
44-
GPIO_InitTypeDef GPIO_InitStruct;
45-
44+
const PinMap *map = NULL;
4645
#if defined(PWR_CR2_USV)
4746
/* Enable VDDUSB on Pwrctrl CR2 register*/
4847
if(__HAL_RCC_PWR_IS_CLK_DISABLED()) {
@@ -55,35 +54,13 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
5554
#endif
5655
#if defined (USB)
5756
if(hpcd->Instance == USB) {
58-
/* Configure USB FS GPIOs */
59-
__HAL_RCC_GPIOA_CLK_ENABLE();
6057

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);
58+
/* Configure USB FS GPIOs */
59+
map = PinMap_USB;
60+
while (map->pin != NC) {
61+
pin_function(map->pin, map->function);
62+
map++;
63+
}
8764

8865
/* Enable USB FS Clock */
8966
__HAL_RCC_USB_CLK_ENABLE();
@@ -111,29 +88,13 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
11188
#endif /* USB */
11289
#if defined (USB_OTG_FS)
11390
if(hpcd->Instance == USB_OTG_FS) {
91+
11492
/* Configure USB FS GPIOs */
115-
__HAL_RCC_GPIOA_CLK_ENABLE();
116-
117-
/* Configure DM DP Pins */
118-
GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
119-
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
120-
GPIO_InitStruct.Pull = GPIO_NOPULL;
121-
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
122-
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
123-
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
124-
125-
/* Configure VBUS Pin */
126-
GPIO_InitStruct.Pin = GPIO_PIN_9;
127-
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
128-
GPIO_InitStruct.Pull = GPIO_NOPULL;
129-
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
130-
131-
/* Configure ID pin */
132-
GPIO_InitStruct.Pin = GPIO_PIN_10;
133-
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
134-
GPIO_InitStruct.Pull = GPIO_PULLUP;
135-
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
136-
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
93+
map = PinMap_USB_OTG_FS;
94+
while (map->pin != NC) {
95+
pin_function(map->pin, map->function);
96+
map++;
97+
}
13798

13899
/* Enable USB FS Clock */
139100
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();
@@ -161,60 +122,13 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
161122
#endif /* USB_OTG_FS */
162123
#if defined (USB_OTG_HS)
163124
if(hpcd->Instance == USB_OTG_HS) {
164-
#ifdef USE_USB_HS_IN_FS
165-
166-
#else
167-
/* Configure USB FS GPIOs */
168-
__HAL_RCC_GPIOA_CLK_ENABLE();
169-
__HAL_RCC_GPIOB_CLK_ENABLE();
170-
__HAL_RCC_GPIOC_CLK_ENABLE();
171-
__HAL_RCC_GPIOH_CLK_ENABLE();
172-
173-
/* CLK */
174-
GPIO_InitStruct.Pin = GPIO_PIN_5;
175-
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
176-
GPIO_InitStruct.Pull = GPIO_NOPULL;
177-
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
178-
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
179-
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
180-
181-
/* D0 */
182-
GPIO_InitStruct.Pin = GPIO_PIN_3;
183-
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
184-
GPIO_InitStruct.Pull = GPIO_NOPULL;
185-
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
186-
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
187-
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
188-
189-
/* D1 D2 D3 D4 D5 D6 D7 */
190-
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_5 |\
191-
GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
192-
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
193-
GPIO_InitStruct.Pull = GPIO_NOPULL;
194-
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
195-
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
196-
197-
/* STP */
198-
GPIO_InitStruct.Pin = GPIO_PIN_0;
199-
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
200-
GPIO_InitStruct.Pull = GPIO_NOPULL;
201-
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
202-
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
203-
204-
/* NXT */
205-
GPIO_InitStruct.Pin = GPIO_PIN_4;
206-
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
207-
GPIO_InitStruct.Pull = GPIO_NOPULL;
208-
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
209-
HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
210-
211-
/* DIR */
212-
GPIO_InitStruct.Pin = GPIO_PIN_2;
213-
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
214-
GPIO_InitStruct.Pull = GPIO_NOPULL;
215-
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
216-
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
217-
125+
/* Configure USB HS GPIOs */
126+
map = PinMap_USB_OTG_HS;
127+
while (map->pin != NC) {
128+
pin_function(map->pin, map->function);
129+
map++;
130+
}
131+
#ifndef USE_USB_HS_IN_FS
218132
__HAL_RCC_USB_OTG_HS_ULPI_CLK_ENABLE();
219133
#endif /* USE_USB_HS_IN_FS */
220134

@@ -224,7 +138,7 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
224138
/* Set USBHS Interrupt to the lowest priority */
225139
HAL_NVIC_SetPriority(OTG_HS_IRQn, 5, 0);
226140

227-
/* Enable USBHS Interrupt */
141+
/* Enable USB HS Interrupt */
228142
HAL_NVIC_EnableIRQ(OTG_HS_IRQn);
229143

230144
if(hpcd->Init.low_power_enable == 1) {

cores/arduino/stm32/usb/usbd_conf.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#ifdef USBCON
2929
/* Includes ------------------------------------------------------------------*/
3030
#include "stm32_def.h"
31+
#include "variant.h"
3132

3233
#if !defined(USB_BASE) && !defined(USB_OTG_DEVICE_BASE)
3334
#error "This board does not support USB! Select 'None' in the 'Tools->USB interface' menu"

0 commit comments

Comments
 (0)