@@ -173,6 +173,15 @@ USBPhyHw::~USBPhyHw()
173
173
174
174
}
175
175
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
+
176
185
void USBPhyHw::init (USBPhyEvents *events)
177
186
{
178
187
const PinMap *map = NULL ;
@@ -190,10 +199,12 @@ void USBPhyHw::init(USBPhyEvents *events)
190
199
hpcd.Init .dev_endpoints = NB_ENDPOINT;
191
200
hpcd.Init .ep0_mps = MAX_PACKET_SIZE_EP0;
192
201
hpcd.Init .low_power_enable = DISABLE;
202
+
193
203
#if !defined(TARGET_STM32F2)
194
204
hpcd.Init .lpm_enable = DISABLE;
195
205
hpcd.Init .battery_charging_enable = DISABLE;
196
206
#endif
207
+
197
208
#if (MBED_CONF_TARGET_USB_SPEED == USE_USB_OTG_HS)
198
209
hpcd.Instance = USB_OTG_HS;
199
210
hpcd.Init .phy_itface = PCD_PHY_ULPI;
@@ -202,36 +213,51 @@ void USBPhyHw::init(USBPhyEvents *events)
202
213
hpcd.Init .vbus_sensing_enable = ENABLE;
203
214
hpcd.Init .use_external_vbus = DISABLE;
204
215
hpcd.Init .speed = PCD_SPEED_HIGH;
216
+
205
217
__HAL_RCC_USB_OTG_HS_CLK_ENABLE ();
206
218
__HAL_RCC_USB_OTG_HS_ULPI_CLK_ENABLE ();
207
219
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)
209
222
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;
214
229
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)
215
234
hpcd.Instance = USB_OTG_FS;
216
235
hpcd.Init .phy_itface = PCD_PHY_EMBEDDED;
217
236
hpcd.Init .Sof_enable = 1 ;
218
237
hpcd.Init .speed = PCD_SPEED_FULL;
238
+
219
239
__HAL_RCC_USB_OTG_FS_CLK_ENABLE ();
220
240
map = PinMap_USB_FS;
241
+
221
242
#elif (MBED_CONF_TARGET_USB_SPEED == USE_USB_NO_OTG)
222
243
hpcd.Instance = USB;
223
244
hpcd.Init .phy_itface = PCD_PHY_EMBEDDED;
224
245
hpcd.Init .speed = PCD_SPEED_FULL;
225
246
226
247
__HAL_RCC_USB_CLK_ENABLE ();
227
248
map = PinMap_USB_FS;
249
+
250
+ #if defined(TARGET_STM32F1)
251
+ // USB_DevConnect is empty
252
+ USB_reenumerate ();
253
+ #endif
254
+
228
255
#endif
229
256
230
257
// Pass instance for usage inside call back
231
258
instance = this ;
232
259
233
- // Configure USB pins and other clocks
234
-
260
+ // Configure USB pins
235
261
while (map->pin != NC) {
236
262
pin_function (map->pin , map->function );
237
263
map++;
@@ -334,25 +360,29 @@ bool USBPhyHw::powered()
334
360
void USBPhyHw::connect ()
335
361
{
336
362
#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
+
348
373
HAL_StatusTypeDef ret = HAL_PCD_Start (&hpcd);
349
374
MBED_ASSERT (ret == HAL_OK);
350
- #endif
375
+
376
+ wait_us (10000 );
351
377
}
352
378
353
379
void USBPhyHw::disconnect ()
354
380
{
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
356
386
MBED_ASSERT (ret == HAL_OK);
357
387
}
358
388
0 commit comments