cancel
Showing results for 
Search instead for 
Did you mean: 

About GPIO

Myasu.1
Senior

I am using a nucleo l4r5zi.

I would like to use the GPIOs on this board as inputs and get the value of those inputs from software.Specifically, I would like to use PG2 to accomplish this.

I have the following setup and am inputting 3.3v into PG2, but for some reason I cannot read the value of the input.

・Pin initilization​

static void set_pin(void) 
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
	
    __HAL_RCC_GPIOG_CLK_ENABLE();
    HAL_PWREx_EnableVddIO2();
 
     /*Configure GPIO pin : PG2 */
    GPIO_InitStruct.Pin = GPIO_PIN_2;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOG, &GPIO_InitStruct)
}

​・Read

HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_2)

Are there any settings that are missing?

1 ACCEPTED SOLUTION

Accepted Solutions
Myasu.1
Senior

@Community member​ 

thank you for quick reply.

I added the following code before calling HAL_PWREx_EnableVddIO2, and it worked as expected.

__HAL_RCC_PWR_CLK_ENABLE

​However, I understand that this is a bit to enable the power interface clock.

Does this mean that I have to call the above function when using GPIO?

I had not called it before, but peripheral functions like UART were working.

View solution in original post

6 REPLIES 6

If in doubts, always start with reading out and checking all the relevant registers' content.

> HAL_PWREx_EnableVddIO2();

Have you enabled PWR clock in RCC before calling this function?

JW

Johi
Senior III

Hello,

You did not initialize: GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

If things do not work out the right way at first: use the MX configuration tool to generate initialization code and compare with what you have.

The MX code generator is not perfect but for getting your board up and running, it can save you a lot of time.

And for logic 0 do not forget to connect to the ground as you did not provide pullup or pulldown.

Best regards,

Johi.

Myasu.1
Senior

@Community member​ 

thank you for quick reply.

I added the following code before calling HAL_PWREx_EnableVddIO2, and it worked as expected.

__HAL_RCC_PWR_CLK_ENABLE

​However, I understand that this is a bit to enable the power interface clock.

Does this mean that I have to call the above function when using GPIO?

I had not called it before, but peripheral functions like UART were working.

No.

Usually just do it once. I usually do it in SystemClock_Config()

Watch post power-on / standby operation

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

To be able to use pins which are supplied from VDDIO2, i.e. PG2..PG15, you have to set PWR_CR2.IOSV. And to be able to do that, you have to enable PWR clock in RCC.

JW

Piranha
Chief II

And one can disable that clock after the PWR is configured.