Skip to main content
Associate II
Jul 16, 2025 12:08 PM UTC
Question

Clock / Timing problem in I2C

I am trying to run a loopback test with 2 i2c's on the same board. The onboard leds need to light up on successful transmission and reception, the problem is the leds light up when in debug mode, going line by line, but not in stand

#define I2C1_ADD (0x15<<1)
#define I2C2_ADD (0x42<<1)

uint8_t I2C1_tx='1';
uint8_t I2C2_tx='2';
uint8_t I2C1_rx,I2C2_rx;

HAL_I2C_Slave_Receive_IT(&hi2c2,&I2C2_rx,sizeof(I2C1_tx));
HAL_Delay(100);
HAL_I2C_Master_Transmit(&hi2c1,I2C2_ADD,&I2C1_tx,sizeof(I2C1_tx),1000);


HAL_I2C_Slave_Receive_IT(&hi2c1,&I2C1_rx,sizeof(I2C2_tx));
HAL_Delay(100);
HAL_I2C_Master_Transmit(&hi2c2,I2C1_ADD,&I2C2_tx,sizeof(I2C2_tx),1000);


while(1)
{
 if(I2C2_rx==I2C1_tx)
 {
	 HAL_GPIO_WritePin(GPIOG,GPIO_PIN_13,SET);
 HAL_Delay(100);
	
 }
 if(I2C1_rx==I2C2_tx)
 {
 	 HAL_GPIO_WritePin(GPIOG,GPIO_PIN_14,SET);
 	 HAL_Delay(500);
 }
}

alone. Can someone plz suggest what might be wrong.

This topic has been closed for replies.

3 replies

TDK
Jul 16, 2025 1:28 PM UTC

Don't switch between slave and master. Leave I2C1 as master and I2C2 as slave.

 

Try declaring I2C1_rx I2C2_rx as volatile, since they are being modified by the DMA.

volatile uint8_t I2C1_rx;
volatile uint8_t I2C2_rx;

 

"If you feel a post has answered your question, please click ""Accept as Solution""."
ST Technical Moderator
Jul 17, 2025 1:46 PM UTC

Hello @maya16 

Please ensure that the interrupt priorities are configured correctly. The interrupt priority for receive should be higher (i.e., numerically lower) than the interrupt priority for transmit.

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Saket_Om
Andrew Neil
Super User
Jul 21, 2025 9:30 AM UTC

You already have a thread on this - marked as solved:

I2C Loopback Test using STM32F429ZI

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.