Skip to content

RTClock running very very slow #572

@BNNorman

Description

@BNNorman

I've got some STM32F103C8's from Alieexpress. I have a project to monitor air quality and originally used a DS3232 to provide a timestamp. Then I thought I'd try to use the STM32's built in rtc to free up the DS3232 for other things but I noticed some strange behaviour - the rtc in the stm32s I have appear to be running very very slowly.

To check this out I have a simple stripboard with a socket for the Blue Pill, a 3v battery connected to vbat and a DS3232 connected using I2C.

My Sketch sets the STM32 rtc and the system time from the DS3232 during setup. So far so good.

The sketch then outputs the times every 5 seconds using the DS3232 as the loop time control. The System clock keeps step with the DS3232 but the STM32 rtc seems to have a mind of it's own. I have attached a screen shot - in column H you can see that the difference between each 'error' in column G is, on average, an accumulating 3 second lag. I don't know why the System,RTC and DS3232 aren't exactly the same at the very beginning but the STM32 rtc is clearly running slow.

image

I read, on the net, that the soldering of the 32.768kHz crystal might need rework on these boards so I've done that - and the two capacitors (C9/C12 under the board). But it made no difference.

I've been side tracked by this for two weeks - no-one else (on the web) appears to have a problem but I can't see any fault with my sketch (below). I have 'played' with the prescaler value and can improve the situation using a value of 12781 instead of 32768. I calculated the pre-scaler based on the time error as a fraction of 32768.

It's difficult to scope the XTAL output without upsetting the frequency so I'm not able to accurately assess if the crystal is running at the correct frequency - but it's way way off if not. I mean losing tens of seconds in as many minutes.

My sketch is below. If anyone can help me resolve this I'd be eternally grateful as will my maker buddies looking to use the BluePills for LoRa/LoRaWan applications.

Regards
Brian

`/* STM32_RTC_VS_DS1307
*

  • Uses modified DS1307RTC library to define RTC2 to avoid
  • conflict with RTClock

*/

#include <Wire.h>
#include <TimeLib.h>

#define LED PC13 // blink during loop

// Using the DS1307 library - works fine reading DS3232
// original DS1307RTC clashes with RTClock - renamed RTC to RTC2
#include <DS1307_RTC.h>

// STM32 built in clock
#include <RTClock.h>
RTClock stm32rtc(RTCSEL_LSE); // initialise the RTClock using the 32kHz xtal source

char logMsg[100]; // message buffer
tmElements_t tm; // used by

uint8_t LED_STATE=LOW;
time_t startTime=0;

/***

  • setup

*/
void setup()
{
pinMode(LED,OUTPUT);
digitalWrite(LED,LED_STATE);

Serial.begin(9600);

delay(400); //STM32 seems to need this

Serial.println("\nSTM32_RTC_VS_DS1307 starting");

// set systime and stm32 rtc using the DS3232
Serial.println("fetching current date & time from DS3232");

time_t RTCnow=0;

if (!RTC2.isRunning())
{
Serial.println("RTC2 is not running");
}
else
{
RTCnow=RTC2.get();
snprintf(logMsg,sizeof logMsg,"DS3232 Now=%d",RTCnow);
Serial.println(logMsg);
}

// set the STM32 system time and rtc time
stm32rtc.setTime(RTCnow);
setTime(RTCnow);

snprintf(logMsg,sizeof logMsg,"\nTime & Date set to %0.4d/%0.2d/%0.2d %0.2d:%0.2d:%0.2d",year(),month(),day(),hour(),minute(),second());
Serial.println(logMsg);

}

/***

  • loop()
    */

void loop()
{
LED_STATE=!LED_STATE; // LED flasher
digitalWrite(LED,LED_STATE);

time_t t0=RTC2.get();

while((RTC2.get()-t0)<5) yield();

RTC2.read(tm);

// output a status message
snprintf(logMsg,sizeof logMsg,"SYS Time is %0.2d:%0.2d:%0.2d, RTC time is %0.2d:%0.2d:%0.2d, DS3232 time is %0.2d:%0.2d:%0.2d",
hour(),minute(),second(),
stm32rtc.hour(),stm32rtc.minute(),stm32rtc.second(),
tm.Hour,tm.Minute,tm.Second);

Serial.println(logMsg);
}

`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions