Skip to main content
Visitor II
October 9, 2022
Solved

About GPIO

  • October 9, 2022
  • 5 replies
  • 2412 views

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?

    This topic has been closed for replies.
    Best answer by Myasu.1

    @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.

    5 replies

    Super User
    October 9, 2022

    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

    Graduate
    October 9, 2022

    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.1AuthorAnswer
    Visitor II
    October 9, 2022

    @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.

    Graduate II
    October 9, 2022

    No.

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

    Watch post power-on / standby operation

    Super User
    October 9, 2022

    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

    Graduate II
    October 9, 2022

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