5 Replies Latest reply on Apr 2, 2015 6:54 PM by ArvindS_76 Branched to a new discussion.

    Resetting RTC internal counter

      When I try to set new time to RTC by rtc_setRTCTime(), the internal counter looks remaining.

      Then rtc_getRTCTime() returns unexpected time.

      Is there any way to reset the internal counter?

       

      For example, when I use Jul/15/2014 12pm at first and 1 hour past, time will be 1pm.

      I can get correct time(it's 1pm) from rtc_getRTCTime();

      Then I set current time like Mar/10/2015 3pm and 1 more hour past, it should be 4pm.

      But rtc_getRTCTime() returns 5pm. (totally 2hour past from the first rtc_setRTCTime().)

      Does anybody know the solution?

       

      [Environment]

      SDK: 2.2.0

      Target: BCM920737TAG

      Deep sleep used with internal 128K

        • 1. Re: Resetting RTC internal counter

          Hello Daisuke,

           

          From the developers:

          1.  include rtc_api.a patch.

          2.  Then use rtc_setReferenceTime() to set the time instead of rtc_setRTCTime()

           

          Thanks

          JT

          • 2. Re: Resetting RTC internal counter

            Hello Jonathan,

             

            Thank you for your help.

            But rtc_setReferenceTime() doesn't change the result.

            I still get future time.

             

            Anyway, I'll prepare reproducible code from rtc_sample.c

             

            Regards,

            • 3. Re: Resetting RTC internal counter

              Attached is reproducible code based on rtc_sample.

              Please check it and tell me any advice.

              • 4. Re: Resetting RTC internal counter

                Hello Daisuke,

                 

                We are working with the developers on this RTC function question but do not have an immediate suggestion.

                 

                Thanks,

                JT

                • 5. Re: Resetting RTC internal counter
                  ArvindS_76

                  There are two bugs in your code:

                   

                  1. I think you meant to increment time by 1hr:

                   

                  #if 1    // 2015/Mar/11 -d.miya

                  if (0x05 == num_timeouts_since_boot) {

                      UINT8 hours = 0x00;

                      UINT8 readbyte = bleprofile_ReadNVRAM(0x10, 0x01, &hours);

                      current_time.hour += ++hours;     /////// ----> Notice you increment hours by whats in NV

                   

                      rtc_setReferenceTime(&current_time);    // I'd like to adjust 1 hour.

                                                              // But it changes only reference time.

                  //            rtc_setRTCTime(&current_time);            // I'd like to adjust 1 hour.

                                                              // But it doesn't clear internal counter

                   

                      UINT8 writtenbyte = bleprofile_WriteNVRAM(0x10, 0x01, &hours);   //// ---> and then you write the incremented value to NV. First iteration, you write 1, the next time, you add 2 and write 2. Next time you add 3hrs and write 3 in NV and so on. This is why your current time skips by one extra hour on the 5th second.

                  }

                  #endif    // 2015/Mar/11 -d.miya

                   

                  2. When you increment/decrement the time by x, you have increment/decrement the reference time on boot from deep-sleep by the same amount. Your time reference shifts, and since the HW RTC continues to run in deep-sleep, you have to tell the driver that you moved things around.

                   

                  Your fixes are simple - In the create function, I changed this:

                   

                  // If this is a power-on reset, we need to set up the reference time.

                  if (mia_isResetReasonPor())

                  {

                      //// .... The same code that you have.....

                  }

                  else

                  {

                      // Set up reference offset by the updates.

                      UINT8 hours = 0x00;

                      UINT8 readbyte = bleprofile_ReadNVRAM(0x10, 0x01, &hours);

                      // Increment the reference by the same amount as the previous increment before entering deep sleep.

                      current_time.hour += hours;

                      rtc_setReferenceTime(&current_time);

                  }

                   

                   

                  Before entering deep sleep, increment by 1hr and set time, but write to NV how many times (cumulative) the RTC has been updated from the start:

                   

                  if (0x05 == num_timeouts_since_boot) {

                      UINT8 hours = 0x00;

                      UINT8 readbyte = bleprofile_ReadNVRAM(0x10, 0x01, &hours);

                      current_time.hour += 1;    ///// ------>>>> Add 1 hr.

                      hours++;      ///// ------>>>> But remember how many times with respect to cold boot.

                      //rtc_setReferenceTime(&current_time);    // I'd like to adjust 1 hour.

                                                              // But it changes only reference time.

                      rtc_setRTCTime(&current_time);            // I'd like to adjust 1 hour.

                                                              // But it doesn't clear internal counter

                   

                      UINT8 writtenbyte = bleprofile_WriteNVRAM(0x10, 0x01, &hours);

                  }

                   

                  Of course, all this code above treats time as plain integers which is incorrect. I will leave it to you as an exercise to use datetime and update appropriately.