2024-12-13 04:38 AM - last edited on 2024-12-13 05:06 AM by SofLit
Hello,
I am currently working with the STM32H7S78-DK board using STM32CubeIDE for setup. I have connected four external buttons to the following pins: D2 (PF1), D4 (PF2), D7 (PF3), and D8 (PF4) on the board.
I have configured the buttons as external interrupts. However, while I am able to detect button presses in the main loop by polling the GPIO pins, the interrupt does not seem to trigger the HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) function.
Steps I have followed:
- Configured GPIO pins (PF1, PF2, PF3, PF4) as interrupt inputs in STM32CubeMX.
- Enabled the interrupts in the NVIC configuration.
- Implemented the HAL_GPIO_EXTI_Callback function in the code.
- Used GPIO_MODE_IT_RISING for interrupt triggering.
Despite the above configurations, the interrupt Callback is not being invoked when the buttons are pressed.
Here I attached some snapshot of configuration and code i have write.
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stdio.h"
#include <string.h>
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart4;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
static void MX_GPIO_Init(void);
static void MX_FLASH_Init(void);
static void MX_UART4_Init(void);
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
GPIO_PinState buttonState[4] = {GPIO_PIN_RESET, GPIO_PIN_RESET, GPIO_PIN_RESET, GPIO_PIN_RESET}; // Variables to store button states
/* USER CODE BEGIN PV */
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
void UART_Print(const char* message);
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
void UART_Print(const char* message)
{
// Transmit the string data over UART
HAL_UART_Transmit(&huart4, (uint8_t*)message, strlen(message), HAL_MAX_DELAY);
}
/* USER CODE BEGIN 0 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == BUTTON_1_Pin)
{
UART_Print("BUTTON_1 pressed\r\n");
}
else if (GPIO_Pin == BUTTON_2_Pin)
{
UART_Print("BUTTON_2 pressed\r\n");
}
else if (GPIO_Pin == BUTTON_3_Pin)
{
UART_Print("BUTTON_3 pressed\r\n");
}
else if (GPIO_Pin == BUTTON_4_Pin)
{
UART_Print("BUTTON_4 pressed\r\n");
}
HAL_GPIO_TogglePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin);
}
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Update SystemCoreClock variable according to RCC registers values. */
SystemCoreClockUpdate();
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_FLASH_Init();
MX_UART4_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
buttonState[0] = HAL_GPIO_ReadPin(GPIOF, BUTTON_1_Pin);
buttonState[1] = HAL_GPIO_ReadPin(GPIOF, BUTTON_2_Pin);
buttonState[2] = HAL_GPIO_ReadPin(GPIOF, BUTTON_3_Pin);
buttonState[3] = HAL_GPIO_ReadPin(GPIOF, BUTTON_4_Pin);
// UART_Print("Hello from STM32!\n\r");
// printf("Hello_STM32!\n\r");
// HAL_Delay(1000);
for (int i = 0; i < 4; i++)
{
if (buttonState[i] == GPIO_PIN_SET) // If the button is pressed
{
printf("Buttton %d is pressed\n\r",i);
HAL_GPIO_TogglePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin);
}
}
HAL_Delay(100);
}
/* USER CODE END 3 */
}
/**
* @brief FLASH Initialization Function
* None
* @retval None
*/
static void MX_FLASH_Init(void)
{
/* USER CODE BEGIN FLASH_Init 0 */
/* USER CODE END FLASH_Init 0 */
/* USER CODE BEGIN FLASH_Init 1 */
/* USER CODE END FLASH_Init 1 */
/* USER CODE BEGIN FLASH_Init 2 */
/* USER CODE END FLASH_Init 2 */
}
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the LPUART1 and Loop until the end of transmission */
HAL_UART_Transmit(&huart4, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
/**
* @brief UART4 Initialization Function
* None
* @retval None
*/
static void MX_UART4_Init(void)
{
/* USER CODE BEGIN UART4_Init 0 */
/* USER CODE END UART4_Init 0 */
/* USER CODE BEGIN UART4_Init 1 */
/* USER CODE END UART4_Init 1 */
huart4.Instance = UART4;
huart4.Init.BaudRate = 115200;
huart4.Init.WordLength = UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart4.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart4) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart4, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart4, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart4) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN UART4_Init 2 */
/* USER CODE END UART4_Init 2 */
}
/**
* @brief GPIO Initialization Function
* None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOO_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin, GPIO_PIN_RESET);
/*Configure GPIO pins : BUTTON_3_Pin BUTTON_2_Pin BUTTON_4_Pin BUTTON_1_Pin */
GPIO_InitStruct.Pin = BUTTON_3_Pin|BUTTON_2_Pin|BUTTON_4_Pin|BUTTON_1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
/*Configure GPIO pin : GREEN_LED_Pin */
GPIO_InitStruct.Pin = GREEN_LED_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GREEN_LED_GPIO_Port, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(BUTTON_1_EXTI_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(BUTTON_1_EXTI_IRQn);
HAL_NVIC_SetPriority(BUTTON_2_EXTI_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(BUTTON_2_EXTI_IRQn);
HAL_NVIC_SetPriority(BUTTON_3_EXTI_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(BUTTON_3_EXTI_IRQn);
HAL_NVIC_SetPriority(BUTTON_4_EXTI_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(BUTTON_4_EXTI_IRQn);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* file: pointer to the source file name
* line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
Could you please help me resolve this issue or provide guidance on what might be missing from the configuration?
Thank you for your support.
Best regards,
Mehul
2024-12-13 05:43 AM
Hello,
Did you set break points in EXTI IRQ handlers? are they reached?
2024-12-13 06:33 AM
Hi,
Thanks for answering.
No it is not reached to the EXTI IRQ handlers.
/Mehul
2024-12-13 07:38 AM
I'm not seeing an issue with what you shared (except I'm missing something)!
Is that possible to zip your project and share it here?
2024-12-13 05:30 PM
Show a schematic of how the buttons are connected to the STM32.
Also try testing the USER button on PC13.