cancel
Showing results for 
Search instead for 
Did you mean: 

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

PReka.2
Associate II

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ł

1 ACCEPTED SOLUTION

Accepted Solutions
KnarfB
Principal III

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

View solution in original post

10 REPLIES 10
KnarfB
Principal III

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
Associate II

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
Principal III

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
Associate II

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
Associate II

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

@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

@PReka.2​ Do you know that STM32 pins have internal pullup and you can enable it in the Cube?

yes, but aren't the internal pull-ups too weak (by an order of magnitude) in typical scenarios?

KnarfB

PReka.2
Associate II

@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.