cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103C8T6 RTC losing date

PDack.1
Associate

Hi, I am working on a simple datalogger project using the STM32F103C8T6, I am currently using the STM32RTC library to access the RTC peripheral via a custom-built board. I originally prototyped using Blue Pill but quickly migrated away from that once I started adding sensors and OLED.

What I find is that on power cycle I lose both the date and Epoch but not the time. I know that they're being set correctly because the getDay/Month/Year/Epoch() return correct values until power cycle.

Things I have checked so far:

  • My external 32.768K xtal is scoped and working.
  • Clock source is set correctly to LSE.
  • VBAT voltage is applied and is not lost on power cycle.

It seems other people have had the same issue, and someone within a different IDE/Compiler seemed to solve it but they are using a different library and they seem to be able to get Epoch time to restore! Source: https://community.st.com/s/question/0D50X00009XkeH7SAJ/store-rtc-date-into-backup-register

Of course it's not critical I get the Date method to restore, quite happy to convert from Epoch time if necessary.

I have chosen the Arduino IDE route purely because I had the necessary libraries for the OLED, USB Keyboard, flash memory IC, temperature, humidity and light level sensors I'm using so I'm reluctant to rewrite all these for some other IDE/Compiler after spending a couple of weeks of work getting everything else to work perfectly. My background is 8 bit PIC/Holtek MCUs and hardware so I'm not used to digging through so many levels of abstraction to find the necessary registers and find out what's going on!

If anyone has successfully got the RTC working within Arduino IDE I'd appreciate any advice / tips!

As it's a paying job I can't really post the whole code, but I've posted the relevant sections below. Of course I commented / uncommented relevant sections from set_time() to test them.

//RTC library
#include <STM32RTC.h>
 
/* Get the rtc object */
STM32RTC& rtc = STM32RTC::getInstance();
 
/* Change these values to set the current initial time */
const byte seconds = 0;
const byte minutes = 12;
const byte hours = 12;
 
/* Change these values to set the current initial date */
const byte weekDay = 1;
const byte day = 23;
const byte month = 3;
const byte year = 20;
 
//set time called on button press
void set_time(void){
  // Set the time
  //rtc.setTime(hours, minutes, seconds);
  rtc.setEpoch(1451606400); // Jan 1, 2016
  // Set the date
  //rtc.setDate(day, month, year);
}
 
 
 
void setup() {
  rtc.setClockSource(STM32RTC::LSE_CLOCK);
  rtc.begin(); // initialize RTC 24H format
 
 
}
 
void loop() {
    if (!digitalRead(DOWN)) {
      display.clearDisplay();
      display.setCursor(10,10);             // Start at top-left corner
      display.println(F("DOWN"));
      display.display();
      digitalWrite(RED, 0);
      digitalWrite(GREEN, 1);
      digitalWrite(BLUE, 0);
      set_time();
  }
 
    if (!digitalRead(OK)) {
      Keyboard.begin();
      delay(500); //wait for keyboard to connect
      Keyboard.println("Time is currently:");
      Keyboard.write(0x0d);  // that's a CR
      Keyboard.write(0x0a);  // that's a LF
      Keyboard.println("Epoch:");
      Keyboard.print(rtc.getEpoch());
      Keyboard.write(0x0d);  // that's a CR
      Keyboard.write(0x0a);  // that's a LF
      Keyboard.println("Hours:");
      Keyboard.print(rtc.getHours());
      Keyboard.write(0x0d);  // that's a CR
      Keyboard.write(0x0a);  // that's a LF
      Keyboard.println("Minutes:");
      Keyboard.print(rtc.getMinutes());
      Keyboard.write(0x0d);  // that's a CR
      Keyboard.write(0x0a);  // that's a LF
      Keyboard.println("Seconds:");
      Keyboard.print(rtc.getSeconds());
      Keyboard.write(0x0d);  // that's a CR
      Keyboard.write(0x0a);  // that's a LF
      Keyboard.println("Day:");
      Keyboard.print(rtc.getDay());
      Keyboard.write(0x0d);  // that's a CR
      Keyboard.write(0x0a);  // that's a LF
      Keyboard.println("Month:");
      Keyboard.print(rtc.getMonth());
      Keyboard.write(0x0d);  // that's a CR
      Keyboard.write(0x0a);  // that's a LF
      Keyboard.println("Year:");
      Keyboard.print(rtc.getYear());
      Keyboard.write(0x0d);  // that's a CR
      Keyboard.write(0x0a);  // that's a LF
      Keyboard.end();
      display.clearDisplay();
      display.setCursor(10,10);             // Start at top-left corner
      display.println(F("OK"));
      display.display();
      digitalWrite(RED, 1);
      digitalWrite(GREEN, 1);
      digitalWrite(BLUE, 1);
  }
 
}

Edit: Meant to mention - I am using the newer STM core not the stm32duino so the other RTC library for Maple Leaf will not work, already tried it.

Regards,

Paul

0 REPLIES 0