2025-10-06 6:52 AM - last edited on 2025-10-09 1:18 AM by Saket_Om
I'm learning about the I2C interface. The polling method works properly, but the interrupt method is not working correctly.I enabled I2C listen mode, but when I send the address on the I2C line, the address match callback is not triggered.
Solved! Go to Solution.
2025-10-10 3:11 AM
It sounds like you're facing an issue with I2C interrupt handling. Here are a few things to check:
I2C Interrupt Enable: Ensure that the interrupt for address match is enabled. In many systems, there's a register or flag that needs to be set to enable interrupt generation when an address match occurs.
Interrupt Priority: Check if the interrupt priority is configured correctly. Sometimes interrupts may not trigger if the priority isn’t set right, especially if there are other higher-priority interrupts.
Address Mode: Verify that you're configuring the I2C peripheral in the correct address mode (7bit or 10bit) and that the address you’re sending matches the configured address in the slave mode.
Address Matching Configuration: Some I2C interfaces have different options for matching the address. For instance, you may need to configure whether the slave is listening for a specific address or a general call.
Master/Slave Mode: Double-check that you're properly initializing the I2C interface in slave mode and that the master is sending the correct address.
Callback Registration: Make sure that the callback function for address match is properly registered and associated with the interrupt. Also, check that no other interrupt sources are interfering with this callback.
Peripheral Clock: Ensure the I2C peripheral clock is enabled and running. Sometimes the interrupt might not trigger if the peripheral is not clocked.
2025-10-06 7:04 AM
Welcome to the forum.
You need to give some more details - please see: How to write your question to maximize your chances to find a solution
See also: How to insert source code
2025-10-08 7:13 AM
1. Part Number: STM32L071KUZ
2. Environment: IDE: STM32CubeIDE [Version: 1.17.0] and Custom Flasher.
3. Hardware: Custom board using STM32L071KUZ (internal clock only)
4. Details / Symptoms:
5. Expected Behavior:
7. Occurrence: Always
8. Sanity Checks / What Has Been Tried:
Additional Notes:
Question:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2025 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 "stm32l0xx_hal_i2c.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
/* 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 ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef hlpuart1;
TIM_HandleTypeDef htim3;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_LPUART1_UART_Init(void);
static void MX_TIM3_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void PrintChar(char c) {
HAL_UART_Transmit(&hlpuart1, (uint8_t*)&c, 1, HAL_MAX_DELAY);
}
int _write(int file, char *ptr, int len) {
for(int i=0; i<len; i++) PrintChar(ptr[i]);
return len;
}
/* Convert I2C state to string for debug */
uint8_t RxData[3];
void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t dir, uint16_t addr)
{
if (dir == I2C_DIRECTION_TRANSMIT)
HAL_I2C_Slave_Sequential_Receive_IT(hi2c, RxData, 3, I2C_FIRST_AND_LAST_FRAME);
}
void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
printf("Data received: %02X %02X %02X\n", RxData[0], RxData[1], RxData[2]);
}
void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
{
HAL_I2C_EnableListen_IT(hi2c);
}
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
{
printf("I2C Error: 0x%08lX\n", HAL_I2C_GetError(hi2c));
__HAL_I2C_DISABLE(hi2c);
__HAL_I2C_ENABLE(hi2c);
HAL_I2C_EnableListen_IT(hi2c);
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
MX_LPUART1_UART_Init();
MX_TIM3_Init();
/* USER CODE BEGIN 2 */
if (HAL_I2C_EnableListen_IT(&hi2c1) == HAL_OK)
{
printf("I2C1 Listen Mode Enabled\r\n");
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1){
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_LPUART1|RCC_PERIPHCLK_I2C1;
PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief I2C1 Initialization Function
* @PAram None
* @retval None
*/
static void MX_I2C1_Init(void)
{
/* USER CODE BEGIN I2C1_Init 0 */
/* USER CODE END I2C1_Init 0 */
/* USER CODE BEGIN I2C1_Init 1 */
/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x0060112F;
hi2c1.Init.OwnAddress1 = 132;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_ENABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
/** Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
Error_Handler();
}
/** Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C1_Init 2 */
HAL_NVIC_SetPriority(I2C1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(I2C1_IRQn);
/* USER CODE END I2C1_Init 2 */
}
/**
* @brief LPUART1 Initialization Function
* @PAram None
* @retval None
*/
static void MX_LPUART1_UART_Init(void)
{
/* USER CODE BEGIN LPUART1_Init 0 */
/* USER CODE END LPUART1_Init 0 */
/* USER CODE BEGIN LPUART1_Init 1 */
/* USER CODE END LPUART1_Init 1 */
hlpuart1.Instance = LPUART1;
hlpuart1.Init.BaudRate = 115200;
hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;
hlpuart1.Init.StopBits = UART_STOPBITS_1;
hlpuart1.Init.Parity = UART_PARITY_NONE;
hlpuart1.Init.Mode = UART_MODE_TX_RX;
hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&hlpuart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN LPUART1_Init 2 */
/* USER CODE END LPUART1_Init 2 */
}
/**
* @brief TIM3 Initialization Function
* @PAram None
* @retval None
*/
static void MX_TIM3_Init(void)
{
/* USER CODE BEGIN TIM3_Init 0 */
/* USER CODE END TIM3_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_SlaveConfigTypeDef sSlaveConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
/* USER CODE BEGIN TIM3_Init 1 */
/* USER CODE END TIM3_Init 1 */
htim3.Instance = TIM3;
htim3.Init.Prescaler = 0;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 65535;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_OC_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER;
sSlaveConfig.InputTrigger = TIM_TS_ITR0;
if (HAL_TIM_SlaveConfigSynchro(&htim3, &sSlaveConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_TIMING;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_OC_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIMEx_RemapConfig(&htim3, TIM3_TI1_GPIO) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_OC_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM3_Init 2 */
/* USER CODE END TIM3_Init 2 */
HAL_TIM_MspPostInit(&htim3);
}
/**
* @brief GPIO Initialization Function
* @PAram None
* @retval None
*/
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/* 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.
* @PAram file: pointer to the source file name
* @PAram 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 */
2025-10-09 1:14 AM
Hello @Gnanavel
Is HAL_Delay work fine when using pooling mode?
Please add enable irq in the beginning of the main function and check again. See the code below:
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
__enable_irq();
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
MX_LPUART1_UART_Init();
MX_TIM3_Init();
/* USER CODE BEGIN 2 */
if (HAL_I2C_EnableListen_IT(&hi2c1) == HAL_OK)
{
printf("I2C1 Listen Mode Enabled\r\n");
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1){
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
2025-10-10 1:02 AM
Hi @Saket_Om
Thank you for your replay!
If add __enable_irq() in the main function then it called again main function/starting of the program.
This is the observation of the slave detected (MCU addr 0x42):
Before __enable_irq() observation
nvidia@ubuntu:~/Desktop/MCU$ i2cdetect -y -r 2
0 1 2 3 4 5 6 7 8 9 a b c d e f
40: -- -- 42 -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- 54 -- -- -- -- -- -- -- -- -- -- --
70: 70 -- -- -- -- -- -- -- Address detected
After __enable_irq() observation
nvidia@ubuntu:~/Desktop/MCU$ i2cdetect -y -r 2
0 1 2 3 4 5 6 7 8 9 a b c d e f
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- 54 -- -- -- -- -- -- -- -- -- -- --
70: 70 -- -- -- -- -- -- -- Address not detected
How I confirmed this is when I add a printf and then re-enable the interrupt, it keeps printing continuously.
if(HAL_I2C_Slave_Receive(&hi2c1, RX_Buffer, 1, 1000) == HAL_OK)
{
if(RX_Buffer[0] != 0) // Use index 0, not 1
{
printf("Received data: 0x%02X\r\n", RX_Buffer[0]);
}
}nvidia@ubuntu:~/Desktop/MCU$ i2cset -y 2 0x42 0x78
Output: Received data: 0x78
if( HAL_I2C_Slave_Receive_IT(&hi2c1 ,(uint8_t *)RX_Buffer, 1) == HAL_OK)
{
if(RX_Buffer[0] != 0) // Use index 0, not 1
{
printf("Received data: 0x%02X\r\n", RX_Buffer[0]);
}
}nvidia@ubuntu:~/Desktop/MCU$ i2cset -y 2 0x42 0x78
Output: Nothing — the callback is not triggered at all.
Can you share any example for this.
2025-10-10 1:26 AM - edited 2025-10-10 1:29 AM
Hello @Gnanavel
Please refer to the example below.
Note that the slave should be ready before the master sends the address.
Could you share your project please?
2025-10-10 3:11 AM
It sounds like you're facing an issue with I2C interrupt handling. Here are a few things to check:
I2C Interrupt Enable: Ensure that the interrupt for address match is enabled. In many systems, there's a register or flag that needs to be set to enable interrupt generation when an address match occurs.
Interrupt Priority: Check if the interrupt priority is configured correctly. Sometimes interrupts may not trigger if the priority isn’t set right, especially if there are other higher-priority interrupts.
Address Mode: Verify that you're configuring the I2C peripheral in the correct address mode (7bit or 10bit) and that the address you’re sending matches the configured address in the slave mode.
Address Matching Configuration: Some I2C interfaces have different options for matching the address. For instance, you may need to configure whether the slave is listening for a specific address or a general call.
Master/Slave Mode: Double-check that you're properly initializing the I2C interface in slave mode and that the master is sending the correct address.
Callback Registration: Make sure that the callback function for address match is properly registered and associated with the interrupt. Also, check that no other interrupt sources are interfering with this callback.
Peripheral Clock: Ensure the I2C peripheral clock is enabled and running. Sometimes the interrupt might not trigger if the peripheral is not clocked.