cancel
Showing results for 
Search instead for 
Did you mean: 

Internal Pull up STM32G4 series

STUser34
Associate III

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. 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

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);
}

 

 

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

11 REPLIES 11
Hl_st
ST Employee

Hello,

can you also share complete configuration of these pins?

Thank you

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

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.

 

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 ?

At present i did not give provision for external pull up resistor, i want to use the internal pull up feature of the micro. If i want to make the pin low then i provide a ground to detect switch pressed.

Switch not pressed -> micro internal pull up -> gpio reads 1

Switch pressed -> micro pin shorted to ground -> gpio shall read 0. 

The above is the idea i want to use.

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);
}

 

 

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Ok i will check and update you.

Yes there is some problem in the circuit i will try the below circuit, initially it was planned to give 12V as input now i feel it is not required.

STUser34_1-1738664004331.png

 

 

 

I know from other STM32 parts that the internal pull-up is relatively weak, usually between 40 and 50kOhm.
In connection with your external circuitry - the 4k resistor to ground - you effectively have a pull-down configuration.

Yes i will remove that 4K resistor and check, should i remove the series 1k and 10K resistor as well?