Search code examples
cembeddedstm32

How to reinitialize USB enumeration in STM32 Microcontrollers using only Software?


I am working on a USB Msc project that requires different USB sticks to be able to connect to the stm32f429. I am using HAL libraries.

Some USB sticks require restarting or re-enumeration for File read and write operation.

I want to restart the USB process like disconnecting and connecting a USB stick but using only software.

I have tried resetting the USB port using the below functions but it didn't result in the enumeration of the USB.

    static uint8_t  USBMNG_ResetUsbPort(void)
    {
      uint32_t curState = ((USBx_HPRT0 ) & 0x01UL);
      
      if(curState != 0)
      {
        USBx_HPRT0 &= ~(1UL << 12);
      }
      else
      {
        USBx_HPRT0 &= ~(1UL << 12);
        USBx_HPRT0 |= (1UL << 12);
      }
      return 1;
    }


    uint8_t  USBMNG_USBDevDisconnect(void)
    {
      
      /* In case phy is stopped, ensure to ungate and restore the phy CLK */
      USBx_PCGCCTL &= ~(USB_OTG_PCGCCTL_STOPCLK | USB_OTG_PCGCCTL_GATECLK);

      USBx_DEVICE->DCTL |= USB_OTG_DCTL_SDIS;
      return 1;
    }

    uint8_t  USBMNG_USBDevConnect(void)
    {
      /* In case phy is stopped, ensure to ungate and restore the phy CLK */
      USBx_PCGCCTL &= ~(USB_OTG_PCGCCTL_STOPCLK | USB_OTG_PCGCCTL_GATECLK);

      USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_SDIS;
      return 1;
    }

Solution

  • I found out how to ReEnumerate USB.

    USBH_ReEnumerate function in the usbh_core.c does the job: