cancel
Showing results for 
Search instead for 
Did you mean: 

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

Kyoun.4
Associate II

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?

1 ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

7 REPLIES 7
TDK
Guru

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.

If you feel a post has answered your question, please click "Accept as Solution".

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.4
Associate II

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

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

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

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

Thanks!

Works well.