cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_GPIO_ReadPin is always returning High. Why is it not working?

sandtime
Associate II
Posted on November 21, 2014 at 19:59

I'm having problems reading the state of a GPIO pin.

If I use the HAL function to read the pin, it always returns that the pin is high. But if I read it manually, then it works properly and correctly returns the actual value of the pin state. This is the relevant code:

// Configure and enable GPIO for PA15
GPIO_InitTypeDef GPIO_InitStructure;
// Enable GPIOA clock
__GPIOA_CLK_ENABLE();
// Configure PA15 pin as input floating. Pull Down because we want to detect a high level.
GPIO_InitStructure.Pin = GPIO_PIN_15;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
// Set the orange LED to reflect the status of PA
// This does NOT work. It always returns that PA15 is high.
// if (HAL_GPIO_ReadPin(GPIOA, 15) == GPIO_PIN_SET)
// This DOES work.
if
(GPIOA->IDR & 0x8000)
{
BSP_LED_On(LED3);
}
else
{
BSP_LED_Off(LED3);
}

Any ideas as to what the problem is? For what it's worth, I'm using STM32CubeF4 1.3.0.
5 REPLIES 5
Posted on January 16, 2015 at 16:41

Hi Trace,

What STM32 board are you using ? So we can better explain it to our development team...

Regards,

Heisenberg.

seejohnleung
Associate II
Posted on January 23, 2015 at 12:03

Hi,

I am also having the same problem reading from stm32f4 discovery board.

It always returns 1.

//defines

#define SD_CARD_EXIST_PIN GPIO_PIN_14

#define SD_CARD_EXIST_PORT GPIOE

 __GPIOE_CLK_ENABLE();

 GPIO_InitStructure.Pin   = SD_CARD_EXIST_PIN;

  GPIO_InitStructure.Mode  = GPIO_MODE_INPUT;

  GPIO_InitStructure.Pull  = GPIO_PULLDOWN;

  GPIO_InitStructure.Speed = GPIO_SPEED_LOW;

  HAL_GPIO_Init(SD_CARD_EXIST_PORT, &GPIO_InitStructure);

int32_t check_sdcard_exist(void)

{

// volatile uint32_t temp;

 int32_t exist_or_not=2;

 GPIO_PinState state;

 

 state=HAL_GPIO_ReadPin(SD_CARD_EXIST_PORT,SD_CARD_EXIST_PIN);

 //temp=;

 if ( state == GPIO_PIN_SET)

 {

  exist_or_not=1; //exists

 }

 else

 {

  exist_or_not=0; //not exists

 } 

 return exist_or_not;

}

Burak Enez
Associate
Posted on January 24, 2015 at 20:51

I am having the exactly the same issue with one STM32F205RB based board. I am usingSTM32F2xx_HAL_Driver v1.1.0. I also implemented FreeRTOS with Stm32CubeMX program. I tried debugging and I can't get in the if statement. All of the other functionalities work perfectly.

void Thread3(void const * argument) {
GPIO_PinState state;
/* Infinite loop */
for(;;) {
state = Button_GetState(BUTTON_KEY1);
if(state == BUTTON_PRESSED)
LED_Toggle(LED3);
osDelay(100);
}
}

Idk how to make it work. Any help will be appreciated. Edit:

#define BUTTON_PRESSED GPIO_PIN_SET

altisclimax
Associate
Posted on March 22, 2015 at 05:47

I encounter This also.

after changing the pull option, it work fine for me.

------------------------------------------------------------------

  GPIO_InitStruct.Pin = GPIO_PIN_12;

  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

  //GPIO_InitStruct.Pull = GPIO_NOPULL (change to pull up)

    GPIO_InitStruct.Pull = GPIO_PULLUP;

  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

-----------------------------------------------------------------

You saved my day 🙂   Thanks and Good luck!