Skip to content

STM32 USB connect procedure update #13522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 50 additions & 20 deletions targets/TARGET_STM/USBPhy_STM32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -202,36 +213,51 @@ 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)

#elif (MBED_CONF_TARGET_USB_SPEED == USE_USB_HS_IN_FS)
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)
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;
hpcd.Init.speed = PCD_SPEED_FULL;

__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++;
Expand Down Expand Up @@ -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;

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
// 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

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);
}

Expand Down