2025-07-14 11:46 PM
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.
2025-07-15 12:22 AM - edited 2025-07-15 4:54 AM
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
2025-07-15 12:24 AM - edited 2025-07-15 12:29 AM
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.
2025-07-15 12:28 AM
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
}
2025-07-15 12:52 AM
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
2025-07-15 3:13 AM
I am sorry, it didnt work...
2025-07-15 3:40 AM
Hello @maya16
Is the slave adresse = 0x42 << 1?
2025-07-15 4:54 AM
attach the .ioc file.
2025-07-15 5:17 AM
yes
2025-07-15 5:27 AM - edited 2025-07-15 5:27 AM