2022-06-27 10:25 AM
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:
2 . 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ł
Solved! Go to Solution.
2022-06-27 11:09 AM
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
2022-06-27 10:33 AM
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
2022-06-27 10:40 AM
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)?
2022-06-27 11:09 AM
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
2022-06-27 11:37 AM
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.
2022-06-27 12:30 PM
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.
2022-06-28 03:17 AM
@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:
2022-06-28 03:35 AM
@PReka.2 Do you know that STM32 pins have internal pullup and you can enable it in the Cube?
2022-06-28 12:28 PM
yes, but aren't the internal pull-ups too weak (by an order of magnitude) in typical scenarios?
KnarfB
2022-06-28 02:35 PM
@KnarfB @Andrew Neil @Pavel A. Thank you all for your answers. It helped me to solve my problem.