Skip to content

times reset after power cycle #32

Closed
@paulporto

Description

@paulporto

the time its lost after power cycling, although LSE_CLOCK is selected, and the vbat is connected
im using the epoch example with some modifications to be able to the set time via UART

arduino 1.8.13
stm32f103 bluepill
library.properties:
name=STM32duino RTC
version=1.1.0
author=STMicroelectronics, Wi6Labs
maintainer=stm32duino ........

/*
  Epoch

  This sketch shows how to manage the RTC using Epoch time

  Creation 12 Dec 2017
  by Wi6Labs
  Modified 03 Jul 2020
  by Frederic Pillon for STMicroelectronics

  This example code is in the public domain.

  https://github.com/stm32duino/STM32RTC
*/

#include <STM32RTC.h>
#include <time.h>

/* Get the rtc object */
STM32RTC& rtc = STM32RTC::getInstance();

long NewEpoch = 0;

void setup() {
  delay(1000);
  Serial.begin(115200);

  // 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(1094747232); // 
}

void loop() {
  uint32_t ss = rtc.getSubSeconds();
  uint32_t epoch = rtc.getEpoch();
  time_t rawtime = epoch;
  struct tm ts;
  char buf[80];

  Serial.print("Unix time = ");
  Serial.println(epoch);

  Serial.print("Seconds since Jan 1 2000 = ");
  Serial.println(rtc.getY2kEpoch());

  // Format time, "ddd yyyy-mm-dd hh:mm:ss zzz"
  ts = *localtime(&rawtime);
  strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S", &ts);
  Serial.print(buf);
  Serial.print(".");
  print2digits(ss);
  Serial.println();


  //the following code is for setting time via UART
  long t = millis();
  while(millis()-t < 3000) {
    delay(100);
    while(Serial.available()){
            
      char c = Serial.read();
      if(c == 'e' and NewEpoch>1094747232){
        rtc.setEpoch(NewEpoch);
        Serial.print("> ");
        Serial.println(NewEpoch);
        NewEpoch = 0;
        while(Serial.available()){ Serial.read(); }
        return;
        }
      NewEpoch *= 10;
      NewEpoch += String(c).toInt();
      }
    }

  //delay(10000);
}

void print2digits(uint32_t number) {
  if (number < 100) {
    Serial.print("0");
  }
  if (number < 10) {
    Serial.print("0");
  }
  Serial.print(number);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions