Skip to main content
Graduate
July 16, 2025
Question

Clock / Timing problem in I2C

  • July 16, 2025
  • 3 replies
  • 352 views

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

    Super User
    July 16, 2025

    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;

     

    Technical Moderator
    July 17, 2025

    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.

    Super User
    July 21, 2025

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

    I2C Loopback Test using STM32F429ZI