2025-04-03 8:25 AM - last edited on 2025-04-03 8:53 AM by mƎALLEm
I connected a external button on my PCB board and am trying to use it to toggle another GPIO Pin (PA8) with HAL_GPIO_EXTI_Callback()
I connected the button to PC4 on my NUCLEO board and is configured as follow:
/*Configure GPIO pin : PC4 */
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
In the User Code 4 Section:
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == GPIO_PIN_4){
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_8);
}
}
In stm32g0xx_it.c:
void EXTI4_15_IRQHandler(void)
{
/* USER CODE BEGIN EXTI4_15_IRQn 0 */
/* USER CODE END EXTI4_15_IRQn 0 */
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_4); // PC4
HAL_GPIO_EXTI_IRQHandler(B1_Pin);
/* USER CODE BEGIN EXTI4_15_IRQn 1 */
/* USER CODE END EXTI4_15_IRQn 1 */
}
if(triggerDetected==1)
{
if((HAL_GetTick()-triggerTick)>debounceTime)
{
// Toggle relay (if energy units > 0)
if (units_left > 0.0f) {
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_8); // drives relay and D2
} else {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); // force off if no units
}
//Reset trigger
triggerDetected=0;
}
}
void EXTI4_15_IRQHandler(void)
{
/* USER CODE BEGIN EXTI4_15_IRQn 0 */
triggerDetected = 1;
triggerTick = HAL_GetTick();
/* USER CODE END EXTI4_15_IRQn 0 */
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_4); // PC4
HAL_GPIO_EXTI_IRQHandler(B1_Pin);
/* USER CODE BEGIN EXTI4_15_IRQn 1 */
/* USER CODE END EXTI4_15_IRQn 1 */
}
Solved! Go to Solution.
2025-04-03 8:50 AM - edited 2025-04-03 9:06 AM
Hello @blade_runner_2004 and welcome to the ST community,
First, you need to use </> button to share your code. See this post. (updated your post accordingly).
Second, on G0 product there are separated callbacks for Falling and rising edges:
void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
You can refer to this example: https://github.com/STMicroelectronics/STM32CubeG0/blob/master/Projects/NUCLEO-G0B1RE/Examples/GPIO/GPIO_EXTI/Src/main.c
You didn't mention if you enabled the NVIC.
PS: this thread has no relation with CubeIDE it will be moved to the embedded software forum board.
Hope that helps.
2025-04-03 8:50 AM - edited 2025-04-03 9:06 AM
Hello @blade_runner_2004 and welcome to the ST community,
First, you need to use </> button to share your code. See this post. (updated your post accordingly).
Second, on G0 product there are separated callbacks for Falling and rising edges:
void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
You can refer to this example: https://github.com/STMicroelectronics/STM32CubeG0/blob/master/Projects/NUCLEO-G0B1RE/Examples/GPIO/GPIO_EXTI/Src/main.c
You didn't mention if you enabled the NVIC.
PS: this thread has no relation with CubeIDE it will be moved to the embedded software forum board.
Hope that helps.
2025-04-03 9:02 AM
Thank You
2025-04-03 9:05 AM
If your question has been answered please accept my comment where I answered your question as solution.
Thanks