Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4803

General • On-chip RTC weirdness - follow up

$
0
0
Thanks for the notes in the previous posting viewtopic.php?t=348730

I too was wondering where the time went. In my case I am reading a DS3231 and then setting the PICO RTC time. The PICO time would almost immediately become one second ahead of the DS3231 time.

I have created the following function to update the PICO time following the pattern created by 'hippy'. I don't need microsecond accuracy but +- a few ms is fine.

On my system I send a TimeSet command from PC to PICO. PICO sets the DS3231 time and periodically synchronises the PICO time to the DS3231. Before the "synchronisation" was always a second out. Now, so far, the two time stamps are very close and certainly equate at the whole second.

Code:

//also based on Extended library for Pi Pico (lib2) https://forums.raspberrypi.com/viewtopic.php?t=313774#define CHECK_DT(tag, min, max)  if ((dt->tag < min) || (dt->tag > max)) return falsebool sys_check_datetime(datetime_t *dt) {    CHECK_DT(year, 0, 4095);    CHECK_DT(month, 1, 12);    CHECK_DT(day, 1, 31);    CHECK_DT(dotw, 0, 6);    CHECK_DT(hour, 0, 23);    CHECK_DT(min, 0, 59);    CHECK_DT(sec, 0, 59);    return true;}bool sys_setrtc(datetime_t *dt){  // ref to PICO Forum https://forums.raspberrypi.com/viewtopic.php?t=348730// PCO RTC will by default increment almost immediately after a write to time registers    if (!sys_check_datetime(dt)) {        return false;    }    // Set as recommended to reset seconds tick timer                rtc_hw->ctrl = 0;                        // [CTRL] = DISABLE                while (rtc_running()) {//wait for rtc to stop                    tight_loop_contents();                }                                   // Write to rtc_hw setup registers from SDK code    rtc_hw->setup_0 = (((uint32_t)dt->year)  << RTC_SETUP_0_YEAR_LSB ) |                      (((uint32_t)dt->month) << RTC_SETUP_0_MONTH_LSB) |                      (((uint32_t)dt->day)   << RTC_SETUP_0_DAY_LSB);    rtc_hw->setup_1 = (((uint32_t)dt->dotw)  << RTC_SETUP_1_DOTW_LSB) |                      (((uint32_t)dt->hour)  << RTC_SETUP_1_HOUR_LSB) |                      (((uint32_t)dt->min)   << RTC_SETUP_1_MIN_LSB)  |                      (((uint32_t)dt->sec)   << RTC_SETUP_1_SEC_LSB);                rtc_hw->ctrl = RTC_CTRL_LOAD_BITS;       // [CTRL] = LOAD                rtc_hw->ctrl = RTC_CTRL_RTC_ENABLE_BITS; // [CTRL] = ENABLE                while (!rtc_running()) {//wait for rtc to restart                   tight_loop_contents();                }                //sleep and reload the tc_hw->ctrl                sleep_us(64);                // Reload it again on the fly to make it right                rtc_hw->ctrl = RTC_CTRL_LOAD_BITS | RTC_CTRL_RTC_ENABLE_BITS;                sleep_us(64);    return true;      }
Please say if I have missed something.
Thanks for the solution.
Ted

Statistics: Posted by tfcroft4 — Tue Apr 09, 2024 3:56 pm



Viewing all articles
Browse latest Browse all 4803

Trending Articles