Skip to main content
Associate II
July 15, 2025
Solved

I2C Loopback Test using STM32F429ZI

  • July 15, 2025
  • 2 replies
  • 820 views

Hello, 
I am trying do a loopback test, i.e I2C1 as master and I2C2 as slave on the same board(STM32F429ZI DISC1).

I am giving the address for slave as 0x42 left shifted by one, and storing the same in oar1 register for I2C2. Now the problem is the master is not getting acknowledgement from the slave. I am using HAL to test this.
I have also turned on/set the pull up and open drain for both the I2Cs. And i have also tried with external 4.7k ohm resistors, for the pull up resistance. But to no avail.
Can somebody please suggest what can i do.

Best answer by KnarfB

the I2C2 slave address is not set here?

KnarfB_0-1752583222139.png

 

2 replies

KnarfB
Super User
July 15, 2025

More details are needed, especially code, please see

How to write your question to maximize your chance... - STMicroelectronics Community

For a 2 board example see Getting started with I2C - stm32mcu HAL_I2C_Slave_Receive_IT

When simultaneously transmitting and receiving on the same board, use non-blocking functions like HAL_I2C_Slave_Receive_IT or  HAL_I2C_Slave_Receive_DMA.

A $10 USB logic analyzer comes in handy for checking the transmission on the wires. 

hth

KnarfB

maya16Author
Associate II
July 15, 2025

Thank you, But i am using only one board...
I will try again with non blocking functions.

Let me add the main part of the code.

maya16Author
Associate II
July 15, 2025

Here is the code, a very simple one, yet i am not able to find why the slave is not responding.
i am trying through polling method. I hope this provides more clarity.

uint8_t txData = 0x69;
 uint8_t rxData;

 while (1) {

 // Master Transmit
 if (HAL_I2C_Master_Transmit(&hi2c1, 0x42 << 1, &txData, 1, HAL_MAX_DELAY) != HAL_OK) {
 // Handle error
 }

 // Slave Receive
 if (HAL_I2C_Slave_Receive(&hi2c2, &rxData, 1, HAL_MAX_DELAY) != HAL_OK) {
 // Handle error
 }

 // Compare sent and received data
 if (txData == rxData) {
 // Success
 } else {
 // Failure
 }

 HAL_Delay(1000); // Delay before next transmission
 }
KnarfB
Super User
July 15, 2025

You are using the blocking functions which cannot work. When HAL_I2C_Master_Transmit returns, all bits were already sent out on the wires and are gone.

Try:

 uint8_t txData = 0x69;
 uint8_t rxData;

 while (1) {

 // prepare Slave Receive
 if (HAL_I2C_Slave_Receive_IT(&hi2c2, &rxData, 1) != HAL_OK) {
 // Handle error
 }

 // Master Transmit
 if (HAL_I2C_Master_Transmit(&hi2c1, 0x42 << 1, &txData, 1, HAL_MAX_DELAY) != HAL_OK) {
 // Handle error
 }

 // Compare sent and received data
 if (txData == rxData) {
 // Success
 } else {
 // Failure
 }

 HAL_Delay(1000); // Delay before next transmission
 }

Enable the I2C Interrupt in STM32CubeMX.

Next steps: add a receive interrupt callback, try DMA, ...

hth

KnarfB 

maya16Author
Associate II
July 15, 2025

I am sorry, it didnt work...