Merge pull request #13522 from jeromecoutant/PR_USB_PULLUP

STM32 USB connect procedure update
pull/13544/head
Martin Kojtal 2020-09-03 13:24:10 +01:00 committed by GitHub
commit 2eb2fe4184
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 50 additions and 20 deletions

View File

@ -173,6 +173,15 @@ USBPhyHw::~USBPhyHw()
}
#if defined(TARGET_STM32F1)
void USB_reenumerate()
{
// Force USB_DP pin (with external pull up) to 0
DigitalOut usb_dp_pin(USB_DP, 0) ;
wait_us(10000); // 10ms
}
#endif
void USBPhyHw::init(USBPhyEvents *events)
{
const PinMap *map = NULL;
@ -190,10 +199,12 @@ void USBPhyHw::init(USBPhyEvents *events)
hpcd.Init.dev_endpoints = NB_ENDPOINT;
hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0;
hpcd.Init.low_power_enable = DISABLE;
#if !defined(TARGET_STM32F2)
hpcd.Init.lpm_enable = DISABLE;
hpcd.Init.battery_charging_enable = DISABLE;
#endif
#if (MBED_CONF_TARGET_USB_SPEED == USE_USB_OTG_HS)
hpcd.Instance = USB_OTG_HS;
hpcd.Init.phy_itface = PCD_PHY_ULPI;
@ -202,22 +213,32 @@ void USBPhyHw::init(USBPhyEvents *events)
hpcd.Init.vbus_sensing_enable = ENABLE;
hpcd.Init.use_external_vbus = DISABLE;
hpcd.Init.speed = PCD_SPEED_HIGH;
__HAL_RCC_USB_OTG_HS_CLK_ENABLE();
__HAL_RCC_USB_OTG_HS_ULPI_CLK_ENABLE();
map = PinMap_USB_HS;
#elif (MBED_CONF_TARGET_USB_SPEED == USE_USB_OTG_HS)
hpcd.Instance = USB_OTG_HS;
hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
hpcd.Init.Sof_enable = 1;
hpcd.Init.speed = PCD_SPEED_HIGH;
#elif (MBED_CONF_TARGET_USB_SPEED == USE_USB_OTG_FS)
#elif (MBED_CONF_TARGET_USB_SPEED == USE_USB_HS_IN_FS)
hpcd.Instance = USB_OTG_HS;
hpcd.Init.phy_itface = USB_OTG_EMBEDDED_PHY;
hpcd.Init.Sof_enable = ENABLE;
hpcd.Init.speed = PCD_SPEED_FULL;
hpcd.Init.dma_enable = DISABLE;
hpcd.Init.vbus_sensing_enable = DISABLE;
hpcd.Init.use_external_vbus = DISABLE;
__HAL_RCC_USB_OTG_HS_CLK_ENABLE();
map = PinMap_USB_HS;
#elif (MBED_CONF_TARGET_USB_SPEED == USE_USB_OTG_FS)
hpcd.Instance = USB_OTG_FS;
hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
hpcd.Init.Sof_enable = 1;
hpcd.Init.speed = PCD_SPEED_FULL;
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();
map = PinMap_USB_FS;
#elif (MBED_CONF_TARGET_USB_SPEED == USE_USB_NO_OTG)
hpcd.Instance = USB;
hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
@ -225,13 +246,18 @@ void USBPhyHw::init(USBPhyEvents *events)
__HAL_RCC_USB_CLK_ENABLE();
map = PinMap_USB_FS;
#if defined(TARGET_STM32F1)
// USB_DevConnect is empty
USB_reenumerate();
#endif
#endif
// Pass instance for usage inside call back
instance = this;
// Configure USB pins and other clocks
// Configure USB pins
while (map->pin != NC) {
pin_function(map->pin, map->function);
map++;
@ -334,25 +360,29 @@ bool USBPhyHw::powered()
void USBPhyHw::connect()
{
#if (MBED_CONF_TARGET_USB_SPEED == USE_USB_NO_OTG)
DigitalOut usb_disc_pin(USB_DP, 1) ;
wait_ns(1000);
usb_disc_pin = 0;
// Initializes the USB controller registers
USB_DevInit(hpcd.Instance, hpcd.Init); // hpcd.Init not used
#if defined(TARGET_STM32F1)
// USB_DevConnect is empty
USB_reenumerate();
#endif
#endif
uint32_t wInterrupt_Mask = USB_CNTR_CTRM | USB_CNTR_WKUPM | USB_CNTR_SUSPM | USB_CNTR_ERRM |
USB_CNTR_SOFM | USB_CNTR_ESOFM | USB_CNTR_RESETM;
/*Set interrupt mask*/
hpcd.Instance->CNTR = wInterrupt_Mask;
HAL_PCD_DevConnect(&hpcd); // HAL_PCD_DevConnect always return HAL_OK
wait_us(10000);
#else
HAL_StatusTypeDef ret = HAL_PCD_Start(&hpcd);
MBED_ASSERT(ret == HAL_OK);
#endif
wait_us(10000);
}
void USBPhyHw::disconnect()
{
HAL_StatusTypeDef ret = HAL_PCD_Stop(&hpcd);
/* Disable DP Pull-Up bit to disconnect the Internal PU resistor on USB DP line */
USB_DevDisconnect(hpcd.Instance);
wait_us(10000);
HAL_StatusTypeDef ret = HAL_PCD_Stop(&hpcd); // USB_DisableGlobalInt + USB_StopDevice
MBED_ASSERT(ret == HAL_OK);
}