Skip to main content
Shilpashree Madhu
Associate III
December 1, 2021
Solved

How to switch GPIO PORT's individual pin into analog input mode without disabling whole GPIO port before going into Stop mode, how to do it?

  • December 1, 2021
  • 3 replies
  • 4261 views

..

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

For example, to set PA5 to analog mode:

GPIOA_MODER |= (3 << (5 * 2));

where 3 is 0b11 for Analog Mode as given by GPIOx_MODER description in the GPIO chapter of RM; 5 is for PA5; 2 is because there are 2 bits per pin in GPIOx_MODER.

JW

3 replies

TDK
Super User
December 1, 2021

Please include your chip number in posts.

Set the appropriate bits within GPIOx->MODER to analog mode.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Shilpashree Madhu
Associate III
December 1, 2021

The chip I'm using is STM32L4R5ZI-P.

My question is what if I want to switch only a particular bit ​to Analog mode without switching whole GPIO PORT into Analog mode before entering to Stop2mode..

TDK
Super User
December 1, 2021

> My question is what if I want to switch only a particular bit ​to Analog mode without switching whole GPIO PORT into Analog mode before entering to Stop2mode..

Yes, that is the question I answered.

Set the bits associated with that pin to analog mode and leave the bits associated with other pins unmodified.

You can also use HAL_GPIO_Init to do this.

"If you feel a post has answered your question, please click ""Accept as Solution""."
waclawek.jan
waclawek.janBest answer
Super User
December 1, 2021

For example, to set PA5 to analog mode:

GPIOA_MODER |= (3 << (5 * 2));

where 3 is 0b11 for Analog Mode as given by GPIOx_MODER description in the GPIO chapter of RM; 5 is for PA5; 2 is because there are 2 bits per pin in GPIOx_MODER.

JW

Shilpashree Madhu
Associate III
December 2, 2021

Much appreciated for the explanation, Thank you very much@waclawek.jan

TDK
Super User
December 1, 2021

If you're not comfortable with register-level manipulation, I would suggest sticking with HAL.

To change PA5 to analog mode:

 GPIO_InitTypeDef GPIO_InitStruct = {0};
 GPIO_InitStruct.Pin = GPIO_PIN_5;
 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

"If you feel a post has answered your question, please click ""Accept as Solution""."
Shilpashree Madhu
Associate III
December 2, 2021

Thank you soo much @TDK​ (ST Employee)​ 

Your explanation was very informative.