cancel
Showing results for 
Search instead for 
Did you mean: 

I2C Loopback Test using STM32F429ZI

maya16
Visitor

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.

10 REPLIES 10
KnarfB
Super User

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

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.

maya16
Visitor

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
  }

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 

I am sorry, it didnt work...

 

Hello @maya16 

Is the slave adresse = 0x42 << 1?

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Saket_Om

attach the .ioc file.

yes

 

Done