cancel
Showing results for 
Search instead for 
Did you mean: 

Data transmited with I2C is garbled on STM32L476

QuickSilver127
Associate

Hello, I wanted to try I2C communication on my Nucleo board since I'm planning to use it in one of my designs. I connected together pins of I2C1 and I2C2, turned on internal pull-ups on both sides for all the pins and wanted to transmit "HELLO" from one to the other.

QuickSilver127_0-1738423363995.png

QuickSilver127_1-1738423374631.png

Code for transmitting:

HAL_I2C_Slave_Receive_IT(&hi2c1 ,(uint8_t *)RX_Buffer, 5);

status = HAL_I2C_Master_Transmit(&hi2c2, 72 << 1, *data, 5, 80000000);

After receiving the data however, even though status is set to HAL_OK it is completely garbled: 

expressions.png

I looked at the signal waveforms on my osciloscope with the logic analyzer enabled, and it shows that it's exactly what was transmitted:

RigolDS1.png

After that I thought that it might be an issue of the internal pull-ups not being strong enough, so I added 1K resistors on both data and clock lines, but the data in the receiving buffer is exactly the same. Does anybody know what am I doing wrong?

 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

Seems like a code bug rather than a hardware issue.

> status = HAL_I2C_Master_Transmit(&hi2c2, 72 << 1, *data, 5, 80000000);

How is data defined? Should be something like this:

char data[] = "Hello";
status = HAL_I2C_Master_Transmit(&hi2c2, 72 << 1, (uint8_t*)data, strlen(data), 80000000);

 

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

View solution in original post

5 REPLIES 5
TDK
Guru

Seems like a code bug rather than a hardware issue.

> status = HAL_I2C_Master_Transmit(&hi2c2, 72 << 1, *data, 5, 80000000);

How is data defined? Should be something like this:

char data[] = "Hello";
status = HAL_I2C_Master_Transmit(&hi2c2, 72 << 1, (uint8_t*)data, strlen(data), 80000000);

 

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

Yes, that was it, data is an array of chars, so I thouht that using it as an argument would automatically take a pointer to it. Thanks for helping.

It will if you don't dereference it (data instead of *data). The latter should also be giving a compilation warning.

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

> data is an array of chars, so I thouht that using it as an argument would automatically take a pointer to it

 

Yes it would, would you use data as parameter. But you used *data, i.e. you dereferenced that pointer, i.e. you took the value of the first member of the array and used it as a pointer. In other words, you transmitted whatever was at address 0x00000072.

I'm quite sure the compiler earned you about this. 

JW

Now that I looked at it did. I'll remember to look at compiler warnings from now on.