Rtc.setcompilertime() not setting right time on redboard artemis

Hello,

I was working with my redboard artemis and testing it with the rtc examples. I am using the arduino IDE and a prerelease version of the library for the apollo3 boards. setting the rtc to the compiler time was working for a while but recently when i set compiler time, sometimes it would work (but be a couple minutes behind) and then others it would not change be stuck on the last successful time/date. any ideas on this issue and solutions?

Thank you

…do they work on not pre-release versions?

I just tried and it seems to not. The first upload seemed to work, still only a minute behind. but then I tried reuploading and compiling the code and it would just stick to that initial compile time.

edit: I’m using the pre-release as it had a working String(float).

This is a known issue with TIME and DATE.
When source code is compiled these macros are translated to real values during pre_processing of the source file by the compiler. When nothing changed to the source file, on a next compile it will continue to use the already pre-processed version (and thus the TIME that was set during the previous pre-processing).

A sketch is always compiled and thus will get the TIME of the latest compile.

In the setToCompilerTime() function it will use the TIME when it was pre-processed. Because no change was in the RTC.cpp library, it will use the pre-processed files it depends on for subsequent sketch compiles. Hence you will see the function continue to use the initial compile time.

In the Arduino IDE there is NO flag for the compiler (that I am aware off) to change to automatically rebuild all source files. BUT you can switch to a different board, compile - verify, then switch back to the previous board and compile again.

P.s. the compile on the different board is allowed to fail, it is only to make sure the pre-processed files are removed by selecting another board.

this might be a dumb question, but could you also like make a change, compile, and then undo do that get it to use the current TIME and DATE?

Thank you for the insight, I was testing it to figure out exactly what was happening but was so confused why sometimes it would but other times it wouldn’t. I will try what you have suggested.

Making a change to the Sketch does not change the source code of the library or the files where the RTC library depends on. You could try to make a change in the library RTC.cpp and save it. With the next compile it will discard the previous pre-processed files. But that is a long way around. Better to use the proposed selecting of a different board and then back-again.

tried swapping the listed board and back and it seems to work, thank you for the suggestion. its a couple seconds behind, and I was wondering if there was a way to get it to put pretty much exact if not a couple milliseconds off?

1 Like

Set the RTC from the sketch instead of library :

/*
  Author: Nathan Seidle
  Created: Septempter 27th, 2019

  This example demonstrates how to initialize and read from the on-board RTC.

  Most SparkFun Artemis boards have the necessary external 32kHz crystal to
  enable the RTC. If you are using the Artemis module bare you will either
  need an external 32kHz xtal or use the internal LFRC. Read the datasheet
  section 12.1 for more information.

  This example is based on the Ambiq SDK EVB2 RTC example.
*/



/*
// This file is subject to the terms and conditions defined in
// file 'LICENSE.md', which is part of this source code package.
*/

#include "RTC.h" // Include RTC library included with the Aruino_Apollo3 core
//APM3_RTC rtc; // Create instance of RTC class ( V1.2.3)

char const *pcMonths[] =
    {
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December",
        "Invalid month"};
        
void setup()
{
  Serial.begin(115200);
  Serial.println("SparkFun RTC Example to set __TIME__");

  // Easily set RTC using the system __DATE__ and __TIME__ macros from compiler
  setToCompilerTime();

  // Manually set RTC date and time
  // rtc.setTime(0, 50, 59, 12, 3, 6, 20); // 12:59:50.000, June 3rd, 2020 (hund, ss, mm, hh, dd, mm, yy)
}

void loop()
{
  rtc.getTime();

  Serial.printf("It is now ");
  Serial.printf("%d:", rtc.hour);
  Serial.printf("%02d:", rtc.minute);
  Serial.printf("%02d.", rtc.seconds);
  Serial.printf("%02d", rtc.hundredths);

  Serial.printf(" %02d/", rtc.month);
  Serial.printf("%02d/", rtc.dayOfMonth);
  Serial.printf("%02d", rtc.year);

  Serial.printf(" Day of week: %d =", rtc.weekday);
  Serial.printf(" %s", rtc.textWeekday);

  Serial.println();

  delay(1000);
}

int mthToIndex(char const *pcMon)
{
  int idx;
  for (idx = 0; idx < 12; idx++)
  {
    if (am_util_string_strnicmp(pcMonths[idx], pcMon, 3) == 0)
      return idx;
  }
  return 12; //Error
}

// toVal() converts a string to an ASCII value.
int toVal(char const *pcAsciiStr) {
  int iRetVal = 0;
  iRetVal += pcAsciiStr[1] - '0';
  iRetVal += pcAsciiStr[0] == ' ' ? 0 : (pcAsciiStr[0] - '0') * 10;
  return iRetVal;
}

void setToCompilerTime()
{
  am_hal_rtc_time_t hal_time;
  
  hal_time.ui32Weekday = am_util_time_computeDayofWeek(2000 + toVal(&__DATE__[9]), mthToIndex(&__DATE__[0]) + 1, toVal(&__DATE__[4]));
  hal_time.ui32Century = 0;
  hal_time.ui32Year = toVal(&__DATE__[9]);
  hal_time.ui32Month = mthToIndex(&__DATE__[0]) + 1; //Compiler ouputs months in 0-11.
  hal_time.ui32DayOfMonth = toVal(&__DATE__[4]);
  hal_time.ui32Hour = toVal(&__TIME__[0]);
  hal_time.ui32Minute = toVal(&__TIME__[3]);
  hal_time.ui32Second = toVal(&__TIME__[6]);
  hal_time.ui32Hundredths = 00;

  am_hal_rtc_time_set(&hal_time); //Initialize the RTC with this date/time
}
1 Like