Skip to content

Commit bf9f6a6

Browse files
committed
[USB] Use pin map for GPIO configuration
Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 1398b02 commit bf9f6a6

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();
@@ -116,29 +93,13 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
11693
#endif /* USB */
11794
#if defined (USB_OTG_FS)
11895
if(hpcd->Instance == USB_OTG_FS) {
96+
11997
/* Configure USB FS GPIOs */
120-
__HAL_RCC_GPIOA_CLK_ENABLE();
121-
122-
/* Configure DM DP Pins */
123-
GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
124-
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
125-
GPIO_InitStruct.Pull = GPIO_NOPULL;
126-
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
127-
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
128-
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
129-
130-
/* Configure VBUS Pin */
131-
GPIO_InitStruct.Pin = GPIO_PIN_9;
132-
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
133-
GPIO_InitStruct.Pull = GPIO_NOPULL;
134-
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
135-
136-
/* Configure ID pin */
137-
GPIO_InitStruct.Pin = GPIO_PIN_10;
138-
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
139-
GPIO_InitStruct.Pull = GPIO_PULLUP;
140-
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
141-
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
98+
map = PinMap_USB_OTG_FS;
99+
while (map->pin != NC) {
100+
pin_function(map->pin, map->function);
101+
map++;
102+
}
142103

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

@@ -229,7 +143,7 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
229143
/* Set USBHS Interrupt to the lowest priority */
230144
HAL_NVIC_SetPriority(OTG_HS_IRQn, 5, 0);
231145

232-
/* Enable USBHS Interrupt */
146+
/* Enable USB HS Interrupt */
233147
HAL_NVIC_EnableIRQ(OTG_HS_IRQn);
234148

235149
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)