Skip to main content
Associate III
August 8, 2024
Solved

GPIO Pin Initilization for USB Port

  • August 8, 2024
  • 8 replies
  • 2819 views

I Created a New Project for the Following MCU's

  • STM32L072CZY
  • STM32F103C8T6
  • STM32F401RE

I enabled the USB as Device and Also Selected Virtual Com Port as the Middleware and Generated the Project.

Upon Loading the Respective Projects to the MCU's The USB Ports were Identified as Virtual Com Port(Serial Port) on Windows OS. Thereby Confirming Device is rightly Configured and Identified on the Host System.

On Inspecting the Code in Detail, Neither in any of the Functions mentioned below, Do I see the Pin Assignment of PA11 and PA12 done. i.e. GPIO Mode, GPIO Speed, GPIO Alternating Functions, etc.

  • USBD_Init
  • USBD_LL_Init
  • HAL_PCD_Init
  • HAL_PCD_MspInit

So Where Exactly are the Pins Assigned for their roles in the MX Generated Code. or what am I missing in my Understanding?

If the same is generated for lets say UART/SPI, Such Assignments are Done in HAL_UART_MspInit() Fn. respectively.

This topic has been closed for replies.
Best answer by FBL

Hi @Pavan_LohiaGroup @Pavel A. 

 

We typically configure AF10 but AF10 programming is not strictly necessary. The GPIO side is not in input/output mode. So, when we put GPIO into analog, this makes the IO work in additional function mode. So, it should not be the issue in your case!

8 replies

TDK
August 8, 2024

It depends on the chip. USB modes are not always alternate function modes.

If they need to be done, such as on the F401, they are done in HAL_PCD_MspInit. Here is what a new F401 project with VCP enabled generates:

/*******************************************************************************
 LL Driver Callbacks (PCD -> USB Device Library)
*******************************************************************************/
/* MSP Init */

void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle)
{
 GPIO_InitTypeDef GPIO_InitStruct = {0};
 if(pcdHandle->Instance==USB_OTG_FS)
 {
 /* USER CODE BEGIN USB_OTG_FS_MspInit 0 */

 /* USER CODE END USB_OTG_FS_MspInit 0 */

 __HAL_RCC_GPIOA_CLK_ENABLE();
 /**USB_OTG_FS GPIO Configuration
 PA11 ------> USB_OTG_FS_DM
 PA12 ------> USB_OTG_FS_DP
 */
 GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 /* Peripheral clock enable */
 __HAL_RCC_USB_OTG_FS_CLK_ENABLE();

 /* Peripheral interrupt init */
 HAL_NVIC_SetPriority(OTG_FS_IRQn, 0, 0);
 HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
 /* USER CODE BEGIN USB_OTG_FS_MspInit 1 */

 /* USER CODE END USB_OTG_FS_MspInit 1 */
 }
}

 

If that's not happening for you, attach your IOC.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Associate III
August 12, 2024

Hello @TDK ,

My Bad, I Checked the Code again and Yes You were Correct for F401RE.

Unfortunately The same Doesn't Apply for F103 and L072.

Though Pins are not Initialized or Configured, It Puzzles me how does the USB Port Still work.

I would Humble Request you to Try the same for F103 and L072 and Share me your Thoughts and Opinions too

ST Technical Moderator
August 8, 2024

Hi @Pavan_LohiaGroup 

It seems the code generated by MX for STM32L072CZY is not complete, the HAL_PCD_MspInit is missing the GPIO configuration for the USB pins PA11 and PA12. An internal ticket 188390 is submitted to review code generation. By manually adding the GPIO configuration for PA11 and PA12, you can ensure that the USB is properly initialized.

To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL
Associate III
August 12, 2024

Hi @FBL ,

Can You please Elaborate and Explain me, If Pins aren't Configured/Initialized, How does the VCP Code even Work?

ST Technical Moderator
August 9, 2024

Hi @Pavan_LohiaGroup 

Thank you @TDK I missed your point ! Indeed, USB data signals are not always used in alternate function modes.

F103 and L072 should be used as Additional function

 

To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL
Pavel A.
August 9, 2024

@FBL How Additional functions should be configured? If not as GPIO AFs, then via syscfg registers?

 

FBLBest answer
ST Technical Moderator
August 12, 2024

Hi @Pavan_LohiaGroup @Pavel A. 

 

We typically configure AF10 but AF10 programming is not strictly necessary. The GPIO side is not in input/output mode. So, when we put GPIO into analog, this makes the IO work in additional function mode. So, it should not be the issue in your case!

To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL