Skip to main content
Visitor II
August 24, 2021
Solved

How can i switch gpio function from USB to normal GPIO?

  • August 24, 2021
  • 4 replies
  • 2168 views

I set stm32f401 to use USB and deinit USB to dP/dN to use normal GPIO.

But I can not control dP/dN for GPIO.

How can I switch GPIO function from USB to normal GPIO?

    This topic has been closed for replies.
    Best answer by waclawek.jan

    What is this, SPL?

    I don't use SPL nor Cube. As I've said, make sure OTG_GCCFG.PWRDWN gets cleared. Read the post I've linked to above, and the posts linked from there.

    JW

    4 replies

    Super User
    August 24, 2021

    HAL_GPIO_Init can be used to set the target mode, same as any other GPIO pin. Ensure the clock is enabled. CubeMX will generate the code for you.

    USB pins are not set up in USB mode by default.

    Kyoun.4Author
    Visitor II
    August 24, 2021

    I set the following process to switch USB DP/DN port to normal GPIO.

    But​ it still in DP/DN mode and can not control as normal GPIO.

     // USB Device Off (DP/DN Port Off ???)

      if (USBH_Stop(&hUsbHostFS) != USBH_OK)

      {

       Error_Handler();

      }

      if (USBH_DeInit(&hUsbHostFS) != USBH_OK)

      {

       Error_Handler();

      }

      // DP/DN Port GPIO Input Mode

      GPIO_InitTypeDef GPIO_InitStruct = {0};

      GPIO_InitStruct.Pin = USB_DN_Pin|USB_DP_Pin;

      GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

      GPIO_InitStruct.Pull = GPIO_NOPULL;

      HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    Super User
    August 24, 2021

    I don't know what is "deinit USB", but in 'F407 - and most probably also in 'F401 - USB PHY overrides the GPIO settings, so you have to switch it off by clearing OTG_GCCFG.PWRDWN.

    This is not documented by ST.

    JW

    Kyoun.4Author
    Visitor II
    August 24, 2021

    I set the following process to switch USB DP/DN port to normal GPIO.

    But​ it still in DP/DN mode and can not control as normal GPIO.

     // USB Device Off (DP/DN Port Off ???)

      if (USBH_Stop(&hUsbHostFS) != USBH_OK)

      {

       Error_Handler();

      }

      if (USBH_DeInit(&hUsbHostFS) != USBH_OK)

      {

       Error_Handler();

      }

      // DP/DN Port GPIO Input Mode

      GPIO_InitTypeDef GPIO_InitStruct = {0};

      GPIO_InitStruct.Pin = USB_DN_Pin|USB_DP_Pin;

      GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

      GPIO_InitStruct.Pull = GPIO_NOPULL;

      HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    Kyoun.4Author
    Visitor II
    August 24, 2021

    I set the following process to switch USB DP/DN port to normal GPIO.

    But​ it still in DP/DN mode and can not control as normal GPIO.

     // USB Device Off (DP/DN Port Off ???)

      if (USBH_Stop(&hUsbHostFS) != USBH_OK)

      {

       Error_Handler();

      }

      if (USBH_DeInit(&hUsbHostFS) != USBH_OK)

      {

       Error_Handler();

      }

      // DP/DN Port GPIO Input Mode

      GPIO_InitTypeDef GPIO_InitStruct = {0};

      GPIO_InitStruct.Pin = USB_DN_Pin|USB_DP_Pin;

      GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

      GPIO_InitStruct.Pull = GPIO_NOPULL;

      HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    Super User
    August 24, 2021

    What is this, SPL?

    I don't use SPL nor Cube. As I've said, make sure OTG_GCCFG.PWRDWN gets cleared. Read the post I've linked to above, and the posts linked from there.

    JW

    Kyoun.4Author
    Visitor II
    August 25, 2021

    Thanks!

    Works well.