stm32duino / Arduino_Core_STM32

STM32 core support for Arduino
https://github.com/stm32duino/Arduino_Core_STM32/wiki
Other
2.76k stars 963 forks source link

STM32RTC #621

Closed poky99 closed 4 years ago

poky99 commented 5 years ago

Good day. I tried to use the STM32RTC standard RTCClockSelection example on the BluePill stm32f103 board, but each time after the reboot only hours and minutes are saved, the date is set to 01/01/00 Arduino 1.8.9 Core 1.6.1 Win 7 x64.

#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 = 0;
const byte hours = 16;

/* Change these values to set the current initial date */
/* Monday 15th June 2015 */
const byte weekDay = 1;
const byte day = 15;
const byte month = 6;
const byte year = 15;

void setup()
{
  Serial.begin(9600);
  // Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK.
  // By default the LSI is selected as source.
  rtc.setClockSource(STM32RTC::LSE_CLOCK);
  rtc.begin(); // initialize RTC 24H format
  rtc.setEpoch(1567246986);
}

void loop()
{
  // Print date...
  print2digits(rtc.getDay());
  Serial.print("/");
  print2digits(rtc.getMonth());
  Serial.print("/");
  print2digits(rtc.getYear());
  Serial.print(" ");
  // ...and time
  print2digits(rtc.getHours());
  Serial.print(":");
  print2digits(rtc.getMinutes());
  Serial.print(":");
  print2digits(rtc.getSeconds());
  Serial.println();
  delay(1000);
}
void print2digits(int number) {
  if (number < 10) {
    Serial.print("0"); // print a 0 before if the number is < than 10
  }
  Serial.print(number);
}

then comment //rtc.setEpoch(1567246986); and flash

fpistm commented 5 years ago

Hi @poky99 in fact there is a restriction for F1 due to HAL: https://github.com/stm32duino/Arduino_Core_STM32/blob/151d737c9ec6b21f5903245ddbfc068ae91e5a09/system/Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rtc.c#L61-L64

You could try to save it manually in a backup register.

poky99 commented 5 years ago

I was thinking about that. But what if the device is without power for more than a day?

fpistm commented 5 years ago

Well I guess the best is to wake up one time per day to update.

fpistm commented 4 years ago

@poky99 any update on this?

rtek1000 commented 4 years ago

unfortunantetly there is no solution for the lost of date because it is a bug in the STM32F1 series. There is little comment in one RTC examples of the cube-library: @ note On STM32F1 families, as there are restrictions on the RTC version V1, date will be lost in all the cases.

Source: https://community.st.com/s/question/0D50X00009XkekSSAR/stm32f103-rtc-vbat-date-resets-after-power-off

Note: A great alternative is to use a DS3231 or DS3232, which already has thermal compensation. See the uRTCLib library, if it interests you: https://github.com/Naguissa/uRTCLib

fpistm commented 4 years ago

No feedback since a while