RTC Calendar update issue in NCS

Hello Team,

     I am using the nRF52833 with the nRF Connect SDK (v2.9.1) in VS Code, and I am experiencing an issue with the RTC calendar update.

     I initially set the RTC calendar to 30th October 2025, 09:00:00 AM. The RTC was set successfully and worked correctly throughout the same day. However, at midnight, I observed that the RTC tick counter advanced by 86,400 seconds (one full day), causing the calendar to update directly to 1st November 2025 instead of 31st October 2025.

    Could you please help me understand why this is happening and how to prevent the RTC from skipping October 31st or other months too?

Parents
  • Hi,

    The RTC of the nRF52833 is a Real Time Counter (not a Real Time Clock). Therefore, in order to implement calendar functionality (time and date) on the nRF52833, one must either use an external Real Time Clock, or build time and date functionality on top of the RTC of the nRF52833. For both there are several alternatives, but it sounds like you use a software solution, either from existing solutions already present in nRF Connect SDK, or from external libraries (either from us or from third parties.) What exact sample, application and/or library are you using for your project?

    Regards,
    Terje

  • I am using rtc.h for setting RTC calendar as follows:

    void init_rtc_calender(void) {
         if (!device_is_ready(rtc_dev)) {
             LOG_ERR("RTC device not ready");
             return;
         }
     
         time_t epoch = 1761917512;
         struct tm *tm = gmtime(&epoch);
         struct rtc_time set_time = {
             .tm_year = tm->tm_year + 1900,
             .tm_mon = tm->tm_mon + 1,
             .tm_mday = tm->tm_mday,
             .tm_hour = tm->tm_hour,
             .tm_min = tm->tm_min,
             .tm_sec = tm->tm_sec
         };
              
         if (rtc_set_time(rtc_dev, &set_time) != 0) {
             LOG_ERR("Failed to set RTC time");
         }
     }
    AND
    Readback  RTC calendar as :
    // Get RTC time
         struct rtc_time current_time;
         if (rtc_get_time(rtc_dev, &current_time) != 0) {
             epoch_send_in_progress = false;
             return;
         }
     
         // Convert to Unix epoch
         struct tm tm_time = {
             .tm_year = current_time.tm_year - 1900,  // Years since 1900
             .tm_mon  = current_time.tm_mon - 1,     // 0-11
             .tm_mday = current_time.tm_mday,
             .tm_hour = current_time.tm_hour,
             .tm_min  = current_time.tm_min,
             .tm_sec  = current_time.tm_sec
         };
         time_t epoch_time = mktime(&tm_time);
         //uint32_t epoch_32bit = (uint32_t)epoch_time;
         flash_state.rtc_calender_epoch = (uint32_t)epoch_time;
         LOG_INF(" -------------- CURRENT RTC CAL in Scanner : %d  -------------------",flash_state.rtc_calender_epoch);
    
  • Hi,

    I see that you convert manually there, between the tm and rtc_time data structures. Please note the following:

    • Both of those structures use the same underlying representation. That is: Both hold in .tm_year a value equal to the current year - 1900, and both store in .tm_mon a value of the month, where January is 0, February is 1, and so on, up to December which is 11. Since both structures use the same representation, there is no need to do arithmetic on the numbers when converting between the two. Adding or subtracting 1 will convert to the next or previous month respectively, and in the case of December or January most likely fail in some undefined way.
    • Both structures have additional fields, which needs to be updated correctly when other fields update. Therefore, you shouldn't really do manual changes such as the ones that you do, or you risk that the structure holds an internally inconsistent state.

    There is for instance the function rtc_time_to_tm(), for converting from an rtc_time structure to a tm structure. I highly recommend using that one, instead of filling the values manually yourself. I did not find a corresponding function for the reverse conversion, but by looking at the implementation of rtc_time_to_tm(), you should be able to easily implement the reverse cast.

    Since you add and subtract 1 to the month when you convert between the two structures, what you experienced is most likely that:

    • With the erroneous manual conversion, the 30th of October is converted into the 30th of a neighboring month, which is a month with only 30 days.
    • Adding seconds to the corresponding timestamp, it now represents the 1st of the following month, not the 31st of the same month.
    • With the erroneous manual conversion back the other way, the date is now the 1st of the month following October: November 1st.

    To summarize, the error is not in additional seconds gets added to the timestamp, but rather that the date structure conversions are wrong.

    Regards,
    Terje

  •    Thanks, for your support!

      Issue Resolved, Now RTC Calendar updated successfully, when set RTC calendar directly from epoch without updating structure.

Reply Children
No Data
Related