cancel
Showing results for 
Search instead for 
Did you mean: 

I2C sending wrong and incomplete data

fal.sch
Associate II

Hello,

I have a STM32F301K6 based board and it seems that the I2C HAL driver is not working properly. I have a little test software running which should receive one byte (0x00) and send back 5 bytes (0x01 to 0x05) periodically.

Receive works fine but transmit sends wrong data on the first transmit and only 4 bytes on the second transmit.

Here some captures from the oscilloscope:

Both rx/tx cycles. Clock line is kept low after 2nd tx because of the missing last byte I guess.0693W000003Oh1mQAC.jpg

Data of first rx/tx. Notice the incorrect two bytes.0693W000003Oh3SQAS.jpg

Data of second rx/tx.0693W000003Oh5EQAS.jpg

My Code is quite simple:

#define I2C_RX_SIZE 1
#define I2C_TX_SIZE 5
 
uint8_t i2c_rx_buf[I2C_RX_SIZE] = {0};
uint8_t i2c_tx_buf[I2C_TX_SIZE] = {1,2,3,4,5};
 
//...
 
void main() {
    //...
    while(1) {
	HAL_I2C_Slave_Receive(&hi2c1, i2c_rx_buf, I2C_RX_SIZE, 200);
	HAL_I2C_Slave_Transmit(&hi2c1, i2c_tx_buf, I2C_TX_SIZE, 200);
    }
    //...
}

Hardware configuration:0693W000003Oh6gQAC.png0693W000003Oh75QAC.png

3 REPLIES 3
TDK
Guru

That plot doesn't look right. You have a low level, a high level, and a mid level. Almost like two things are trying to drive the bus at the same time. Maybe something is configured in push-pull instead of open-drain. What else is connected to SDA/SCL?

If you feel a post has answered your question, please click "Accept as Solution".
fal.sch
Associate II

@TDK​ Only the master, which is a STM32H743 based board. I probably should mention that on the F301-Slave-Board there is a ISO1540 to isolate the bus. But this design has work in previous projects.

However, I have not changed my code to wait for ready state before sending and receiving.

while(HAL_I2C_Slave_Receive(&hi2c1, i2c_rx_buf, I2C_RX_SIZE, 200) != HAL_OK);
while(HAL_I2C_Slave_Transmit(&hi2c1, i2c_tx_buf, I2C_TX_SIZE, 200) != HAL_OK);

Now is sometimes works for some seconds before hanging with the same problem (clock line on mid/low level).

The problem seems to be with the  F301-Slave-Board. As soon as I press the reset button the clock line goes high again and the master starts trying to write.