Skip to main content
PReka.2
Associate
June 27, 2022
Solved

I2C problem. I can't get the communication to work on STM32F0 Discovery in STM32CubeIDE. Where is the error in reasoning?

Hello, I tried to run the RTC DS1307 first using libraries available on the internet. It was not successful. Later I tried to run the communication "manually" but it doesn't work either. Please help. 

The module with DS1307 works on Arduino. Through the Arduino I have set the output from the SQW Pin to flash every 1 Hz. Using STM I try to disable this function by typing

HAL_I2C_Mem_Write(&hi2c1, 0x68<<1, 0x07, 1, 0x00, 1, 1000);

but nothing happens. Please help me because I can't stand it anymore.

To sum up:

1 . I set configuration:

0693W00000NspqmQAB.jpg2 . Generate the code via Cube.

3 . Add one line of code:

 /* USER CODE BEGIN 2 */

 HAL_I2C_Mem_Write(&hi2c1, 0x68<<1, 0x07, 1, 0x00, 1, 1000);

 /* USER CODE END 2 */

and LED connected to SQW output of DS1307 is still blinking after uplode code to STM32F0 Discovery. What I do wrong?

Sincerely

Paweł

This topic has been closed for replies.
KnarfB
KnarfBBest answer
Super User
June 27, 2022

The HAL_I2C_Mem_Write gives you a return code. From that you can tell if the STM has successfully talked to the I2C target device (HAL_OK) or not. If not, check the electrical connections, pull-ups? A Logic Analyzer could also be usedful to check the I2C signalling.

hth

KnarfB

10 replies

KnarfB
Super User
June 27, 2022

Haven'T looked deep into it, but the 5th parameter is a buffer (pointer), not a value. So you have to define a variable/array/... and pass its address to the function.

And, for the 4th parameter, constants like I2C_MEMADD_SIZE_8BIT are common.

hth

KnarfB

PReka.2
PReka.2Author
Associate
June 27, 2022

Thank You for your answer.

I tried this one:

 /* USER CODE BEGIN 2 */

 const uint8_t DS1307_adress = 0x68;

 const uint8_t DS1307_config = 0x00;

  

 HAL_I2C_Mem_Write(&hi2c1, (uint16_t)(DS1307_adress<<1), 0x07, 1, &DS1307_config, 1, 1000);

 /* USER CODE END 2 */

Should I set something else to start I2C, or is there any possibility that Cube made a mistake in generating the code (I had a problem with Cube when it set the wrong AF of pins - I believe TIM3 CH 3 should set AF1, but cube set AF0)?

KnarfB
KnarfBBest answer
Super User
June 27, 2022

The HAL_I2C_Mem_Write gives you a return code. From that you can tell if the STM has successfully talked to the I2C target device (HAL_OK) or not. If not, check the electrical connections, pull-ups? A Logic Analyzer could also be usedful to check the I2C signalling.

hth

KnarfB

PReka.2
PReka.2Author
Associate
June 27, 2022

The HAL_I2C_Mem_Write gives me the value "HAL_ERROR". Unfortenly I don't have Logic Analyzer - I ordered the device but don't get it yet - that's why I tray with SQW output :-).

I use a module with pull-ups. Module work with Arduino.

PReka.2
PReka.2Author
Associate
June 27, 2022

Oh, for bats's snacks, it was the pull-up resistors. It cost me a few days :-(. One thing to note - Arduino works well with the module, STM does not :-(.

R2 was missing.0693W00000Nsq4UQAR.png0693W00000Nsq4PQAR.jpg

Andrew Neil
Super User
June 28, 2022

@PReka.2​  "it was the pull-up resistors"

That would have been immediately obvious with an oscilloscope (or logic analyser).

When working at this level, the hardware is a key part of the system - you need hardware tools as well as software tools!

"Arduino works well with the module, STM does not"

Presumably because Arduino provides pullups?

Anyhow, now that it's solved, please mark the solution:

0693W000008y9fZQAQ.png

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
PReka.2
PReka.2Author
Associate
June 28, 2022

@KnarfB​  @Andrew Neil​ @Pavel A.​ Thank you all for your answers. It helped me to solve my problem.

  1. I know that an Oscilloscope or Logic Analyzer are helpful. As I mentioned - I ordered the device but don't get it yet - that's why I try with SQW output.
  2. Is it ok to use inernal pullups to I2C communications?
  3. Thanks for showing the steps to marking the solution. I will close it after answers about I2C and internal pullups.
Andrew Neil
Super User
June 29, 2022

@PReka.2​  "Is it ok to use inernal pullups to I2C communications?"

As noted earlier, they will generally be too weak - but you might "get away" with it ...

A thread on I2C pullups:

https://electronics.stackexchange.com/questions/1849/is-there-a-correct-resistance-value-for-i2c-pull-up-resistors

In that thread, this picture shows the effects of the pullup value:

0693W00000Nt1crQAB.png 

Note that marking a solution doesn't close the thread - further replies can still be added.

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.