Skip to content

Commit 2eb2fe4

Browse files
authored
Merge pull request ARMmbed#13522 from jeromecoutant/PR_USB_PULLUP
STM32 USB connect procedure update
2 parents 4a68b8a + ac9c4b7 commit 2eb2fe4

File tree

1 file changed

+50
-20
lines changed

1 file changed

+50
-20
lines changed

targets/TARGET_STM/USBPhy_STM32.cpp

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ USBPhyHw::~USBPhyHw()
173173

174174
}
175175

176+
#if defined(TARGET_STM32F1)
177+
void USB_reenumerate()
178+
{
179+
// Force USB_DP pin (with external pull up) to 0
180+
DigitalOut usb_dp_pin(USB_DP, 0) ;
181+
wait_us(10000); // 10ms
182+
}
183+
#endif
184+
176185
void USBPhyHw::init(USBPhyEvents *events)
177186
{
178187
const PinMap *map = NULL;
@@ -190,10 +199,12 @@ void USBPhyHw::init(USBPhyEvents *events)
190199
hpcd.Init.dev_endpoints = NB_ENDPOINT;
191200
hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0;
192201
hpcd.Init.low_power_enable = DISABLE;
202+
193203
#if !defined(TARGET_STM32F2)
194204
hpcd.Init.lpm_enable = DISABLE;
195205
hpcd.Init.battery_charging_enable = DISABLE;
196206
#endif
207+
197208
#if (MBED_CONF_TARGET_USB_SPEED == USE_USB_OTG_HS)
198209
hpcd.Instance = USB_OTG_HS;
199210
hpcd.Init.phy_itface = PCD_PHY_ULPI;
@@ -202,36 +213,51 @@ void USBPhyHw::init(USBPhyEvents *events)
202213
hpcd.Init.vbus_sensing_enable = ENABLE;
203214
hpcd.Init.use_external_vbus = DISABLE;
204215
hpcd.Init.speed = PCD_SPEED_HIGH;
216+
205217
__HAL_RCC_USB_OTG_HS_CLK_ENABLE();
206218
__HAL_RCC_USB_OTG_HS_ULPI_CLK_ENABLE();
207219
map = PinMap_USB_HS;
208-
#elif (MBED_CONF_TARGET_USB_SPEED == USE_USB_OTG_HS)
220+
221+
#elif (MBED_CONF_TARGET_USB_SPEED == USE_USB_HS_IN_FS)
209222
hpcd.Instance = USB_OTG_HS;
210-
hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
211-
hpcd.Init.Sof_enable = 1;
212-
hpcd.Init.speed = PCD_SPEED_HIGH;
213-
#elif (MBED_CONF_TARGET_USB_SPEED == USE_USB_OTG_FS)
223+
hpcd.Init.phy_itface = USB_OTG_EMBEDDED_PHY;
224+
hpcd.Init.Sof_enable = ENABLE;
225+
hpcd.Init.speed = PCD_SPEED_FULL;
226+
hpcd.Init.dma_enable = DISABLE;
227+
hpcd.Init.vbus_sensing_enable = DISABLE;
228+
hpcd.Init.use_external_vbus = DISABLE;
214229

230+
__HAL_RCC_USB_OTG_HS_CLK_ENABLE();
231+
map = PinMap_USB_HS;
232+
233+
#elif (MBED_CONF_TARGET_USB_SPEED == USE_USB_OTG_FS)
215234
hpcd.Instance = USB_OTG_FS;
216235
hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
217236
hpcd.Init.Sof_enable = 1;
218237
hpcd.Init.speed = PCD_SPEED_FULL;
238+
219239
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();
220240
map = PinMap_USB_FS;
241+
221242
#elif (MBED_CONF_TARGET_USB_SPEED == USE_USB_NO_OTG)
222243
hpcd.Instance = USB;
223244
hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
224245
hpcd.Init.speed = PCD_SPEED_FULL;
225246

226247
__HAL_RCC_USB_CLK_ENABLE();
227248
map = PinMap_USB_FS;
249+
250+
#if defined(TARGET_STM32F1)
251+
// USB_DevConnect is empty
252+
USB_reenumerate();
253+
#endif
254+
228255
#endif
229256

230257
// Pass instance for usage inside call back
231258
instance = this;
232259

233-
// Configure USB pins and other clocks
234-
260+
// Configure USB pins
235261
while (map->pin != NC) {
236262
pin_function(map->pin, map->function);
237263
map++;
@@ -334,25 +360,29 @@ bool USBPhyHw::powered()
334360
void USBPhyHw::connect()
335361
{
336362
#if (MBED_CONF_TARGET_USB_SPEED == USE_USB_NO_OTG)
337-
DigitalOut usb_disc_pin(USB_DP, 1) ;
338-
wait_ns(1000);
339-
usb_disc_pin = 0;
340-
341-
uint32_t wInterrupt_Mask = USB_CNTR_CTRM | USB_CNTR_WKUPM | USB_CNTR_SUSPM | USB_CNTR_ERRM |
342-
USB_CNTR_SOFM | USB_CNTR_ESOFM | USB_CNTR_RESETM;
343-
/*Set interrupt mask*/
344-
hpcd.Instance->CNTR = wInterrupt_Mask;
345-
HAL_PCD_DevConnect(&hpcd); // HAL_PCD_DevConnect always return HAL_OK
346-
wait_us(10000);
347-
#else
363+
// Initializes the USB controller registers
364+
USB_DevInit(hpcd.Instance, hpcd.Init); // hpcd.Init not used
365+
366+
#if defined(TARGET_STM32F1)
367+
// USB_DevConnect is empty
368+
USB_reenumerate();
369+
#endif
370+
371+
#endif
372+
348373
HAL_StatusTypeDef ret = HAL_PCD_Start(&hpcd);
349374
MBED_ASSERT(ret == HAL_OK);
350-
#endif
375+
376+
wait_us(10000);
351377
}
352378

353379
void USBPhyHw::disconnect()
354380
{
355-
HAL_StatusTypeDef ret = HAL_PCD_Stop(&hpcd);
381+
/* Disable DP Pull-Up bit to disconnect the Internal PU resistor on USB DP line */
382+
USB_DevDisconnect(hpcd.Instance);
383+
wait_us(10000);
384+
385+
HAL_StatusTypeDef ret = HAL_PCD_Stop(&hpcd); // USB_DisableGlobalInt + USB_StopDevice
356386
MBED_ASSERT(ret == HAL_OK);
357387
}
358388

0 commit comments

Comments
 (0)