Skip to main content
Graduate
February 4, 2025
Solved

Internal Pull up STM32G4 series

  • February 4, 2025
  • 1 reply
  • 1781 views

Hello,

I am using the internal pull up for the input pins, but when read the port status using the program, it always reads 0 instead of 1. I expected it to read 1 and if make that pin grounded it shall read 0. 

 

 

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

    I couldn´t see anything wrong in your code. I tried to do some easy example and it works right and pin input value is also seen in debugger. Do you have connected anything externally to MCU pins? 

     

     

    int main(void)
    {
     /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
     HAL_Init();
    
     /* Configure the system clock */
     SystemClock_Config();
    
     /* Initialize all configured peripherals */
     MX_GPIO_Init();
     /* USER CODE BEGIN 2 */
     /* USER CODE END 2 */
    
     /* Infinite loop */
     while (1)
     {
     HAL_GPIO_WritePin(USER_LED_GPIO_Port, USER_LED_Pin, 
     HAL_GPIO_ReadPin(INPUT_PULL_UP_GPIO_Port, INPUT_PULL_UP_Pin));
     }
    }
    
    static void MX_GPIO_Init(void)
    {
     GPIO_InitTypeDef GPIO_InitStruct = {0};
    
     /* GPIO Ports Clock Enable */
     __HAL_RCC_GPIOC_CLK_ENABLE();
     __HAL_RCC_GPIOF_CLK_ENABLE();
     __HAL_RCC_GPIOA_CLK_ENABLE();
    
     /*Configure GPIO pin Output Level */
     HAL_GPIO_WritePin(USER_LED_GPIO_Port, USER_LED_Pin, GPIO_PIN_RESET);
    
     /*Configure GPIO pin : INPUT_PULL_UP_Pin */
     GPIO_InitStruct.Pin = INPUT_PULL_UP_Pin;
     GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
     GPIO_InitStruct.Pull = GPIO_PULLUP;
     HAL_GPIO_Init(INPUT_PULL_UP_GPIO_Port, &GPIO_InitStruct);
    
     /*Configure GPIO pin : USER_LED_Pin */
     GPIO_InitStruct.Pin = USER_LED_Pin;
     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
     GPIO_InitStruct.Pull = GPIO_NOPULL;
     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
     HAL_GPIO_Init(USER_LED_GPIO_Port, &GPIO_InitStruct);
    }

     

     

     

    1 reply

    ST Employee
    February 4, 2025

    Hello,

    can you also share complete configuration of these pins?

    Thank you

    STUser34Author
    Graduate
    February 4, 2025

    This is the code

    #define ON 1
    
    static void MX_GPIO_Init(void)
    {
    	GPIO_InitTypeDef GPIO_InitStruct = {0};
    
     /* GPIO Ports Clock Enable */
     __HAL_RCC_GPIOC_CLK_ENABLE();
     __HAL_RCC_GPIOF_CLK_ENABLE();
     __HAL_RCC_GPIOG_CLK_ENABLE();
     __HAL_RCC_GPIOA_CLK_ENABLE();
     __HAL_RCC_GPIOB_CLK_ENABLE();
     
     /* USER CODE BEGIN */
    
     /*Configure GPIO pin Output Level */
     // HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_RESET);
    
     /*Configure GPIO pin : PB11 */
     GPIO_InitStruct.Pin = GPIO_PIN_11;
     GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
     GPIO_InitStruct.Pull = GPIO_PULLUP;
     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    
     GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_15;
     GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
     GPIO_InitStruct.Pull = GPIO_PULLUP;
     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    
     /*Configure GPIO pins : PC6 PC7 PC8 */
     GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8;
     GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
     GPIO_InitStruct.Pull = GPIO_PULLUP;
     HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
    
     /*Configure GPIO pins : PA8 PA9 PA10 */
     GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;
     GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
     GPIO_InitStruct.Pull = GPIO_PULLUP;
     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
     /* USER CODE END */
    }
    MC_VehicleMode_Status_t SetVehicleMode(MCI_Handle_t *pHandle)
    {
     uint8_t l_portb_pin11_state_u8=0;
     uint8_t l_portb_pin12_state_u8=0;
     uint8_t l_porta_pin8_state_u8=0;
     uint8_t l_portc_pin7_state_u8 = 0;
     uint8_t l_portb_pin15_state_u8 = 0;
     uint8_t l_porta_pin9_state_u8 = 0;
    
     l_portb_pin11_state_u8 = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_11);
     l_portb_pin12_state_u8 = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12);
     
     if(l_portb_pin11_state_u8 == ON)
     {
     pHandle->VehicleMode = MCM_REVERSE_MODE;
     }
     else if(l_portb_pin12_state_u8 == ON)
     {
     pHandle->VehicleMode = MCM_CITY_MODE;
     }
    }

    But in the code when i debug it always reads as 0 instead of 1. I have added only the main section of the code. Please let me know if anything else to be shared or added.

     

    Explorer
    February 4, 2025

    I suppose the values are consistent with the input register values of the GPIO in the debugger.

    Have you tried a, say, 5k external pull-up resistor ?
    And have you physically measured the voltage on the respective pins ?