2025-02-01 07:31 AM
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.
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:
I looked at the signal waveforms on my osciloscope with the logic analyzer enabled, and it shows that it's exactly what was transmitted:
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?
Solved! Go to Solution.
2025-02-01 07:40 AM
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);
2025-02-01 07:40 AM
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);
2025-02-01 07:46 AM
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.
2025-02-01 07:50 AM
It will if you don't dereference it (data instead of *data). The latter should also be giving a compilation warning.
2025-02-01 07:50 AM
> 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
2025-02-01 08:02 AM
Now that I looked at it did. I'll remember to look at compiler warnings from now on.