Interrupt phase shift - how to?
-
- Posts: 27
- Joined: Sun Jan 14, 2018 7:47 pm
Interrupt phase shift - how to?
For controlling a wall clock i need a precise pulse-per-second signal. While this is usually done with a pps fed by GPS, i want to implement a fallback pps by using the precise 1Hz squarewave signal of an RTC chip (DS3231). So there is the need to synchronize the RTC pps with the last pps received by the GPS. The phase of the RTC squarewave signal cannot be set, so i need a software solution for this.
Hardware setup is:
- PPS by GPS is connected to GPIO and triggers a pps interrupt
- Squarewave output by RTC is connected to another GPIO and triggers the pps interrupt as fallback, if no signal comes from GPS
How can i sync the fallback pps on the GPS pps, while the GPS pps is present?
Hardware setup is:
- PPS by GPS is connected to GPIO and triggers a pps interrupt
- Squarewave output by RTC is connected to another GPIO and triggers the pps interrupt as fallback, if no signal comes from GPS
How can i sync the fallback pps on the GPS pps, while the GPS pps is present?
-
- Posts: 166
- Joined: Wed Aug 01, 2018 12:06 pm
Re: Interrupt phase shift - how to?
You can get uSec resolution from the hardware timer and using a semaphore to sync the 1pps from the GPS with the hardware timer set up as a watchdog.
The ESP has a nano second clock you can use: https://www.instructables.com/id/625-Na ... icrocontr/, which works for the ESP32 as well.
The ESP has a nano second clock you can use: https://www.instructables.com/id/625-Na ... icrocontr/, which works for the ESP32 as well.
-
- Posts: 27
- Joined: Sun Jan 14, 2018 7:47 pm
Re: Interrupt phase shift - how to?
Thanks for the hints. My current code https://github.com/cyberman54/ESP32-Pax ... keeper.cpp feeds the GPS PPS in an interrupt which fires a semaphore to a routine which controls the clock. But how could i sync with a second fallback PPS from the RTC DS3231 chip? Both, GPS and RTC, are programmed to give 1 pulse per second. I am wondering how i can use the more precise PPS from GPS to dynamically (re-)calculate the phase shift / drift of the RTC pps, and then set the timeofday by time of RTC, regarding the phase shift.idahowalker wrote: ↑Mon Feb 25, 2019 6:38 pmYou can get uSec resolution from the hardware timer and using a semaphore to sync the 1pps from the GPS with the hardware timer set up as a watchdog.
Could this, maybe, done with hardware timers of ESP32? Or a routine in the ULP?
-
- Posts: 166
- Joined: Wed Aug 01, 2018 12:06 pm
Re: Interrupt phase shift - how to?
I figure your not going to get control of the ESP32 PLL to cause it to phase shift as per your desire.
The ESP32 has a built in RTC (real time clock), why not use the GPS clock to set the ESP32 clock? Once the ESP32 clock is set, measure the offset between the two clocks and update the ESP32's clock, including the offset your code finds. This can be done on a periodic basis and can be made into a fine tuning routine that just runs as a task.
The ESP32 has a built in RTC (real time clock), why not use the GPS clock to set the ESP32 clock? Once the ESP32 clock is set, measure the offset between the two clocks and update the ESP32's clock, including the offset your code finds. This can be done on a periodic basis and can be made into a fine tuning routine that just runs as a task.
-
- Posts: 27
- Joined: Sun Jan 14, 2018 7:47 pm
Re: Interrupt phase shift - how to?
Thanks again for the hints.
The concept of using a hardware watchdog timer in conjunction with the pps pulse sounds reasonable. I thought on it, but it is not yet fully clear to me what the role of the watchdog should be: should it be fed by the pps pulse? How can i synchronize the time of RTC this way?
Could you, please, explain your idea some further? Tx.
The concept of using a hardware watchdog timer in conjunction with the pps pulse sounds reasonable. I thought on it, but it is not yet fully clear to me what the role of the watchdog should be: should it be fed by the pps pulse? How can i synchronize the time of RTC this way?
Could you, please, explain your idea some further? Tx.
-
- Posts: 166
- Joined: Wed Aug 01, 2018 12:06 pm
Re: Interrupt phase shift - how to?
No need for a watchdog if the ESP32 internal RTC is used. Set the internal RTC to the GPS time, allow the internal RTC to run and keep track of the drift, periodic set the internal RCT to the to the GPS time and refine the drift, over time the drift can be used as an offset calibration for the ESP32
Re: Interrupt phase shift - how to?
Apologies for bringing an old thread back, I'm curious: how an internal HW timer in ESP32 boards can be calibrated - do you mean adding the estimated time drift every elapsed minute or an hour? Something likeidahowalker wrote: ↑Fri Mar 01, 2019 9:51 pmover time the drift can be used as an offset calibration for the ESP32
Code: Select all
#define DRIFT_MINUTE_US 15
tick = timer_get_tick();
tick += DRIFT_MINUTE_US
------
Update.
I think what was really meant is the
Code: Select all
/**
* @brief Update the calibration value of RTC slow clock
*
* The value has to be in the same format as returned by rtc_clk_cal (microseconds,
* in Q13.19 fixed-point format).
* This value is used by timekeeping functions (such as gettimeofday) to
* calculate current time based on RTC counter value.
* @param value calibration value obtained using rtc_clk_cal
*/
void esp_clk_slowclk_cal_set(uint32_t value);
Re: Interrupt phase shift - how to?
Maybe i misunderstood what you are trying to do, but I would use the 32kHz square wave output from the DS3231.
You could keep a counter running at 32kHz interrupt on the ESP32 that is reset by the GPS pps signal.
If it goes above the 32kHz count => gps pps signal is lost, reset counter and fire the wall clock from the 32kHz interrupt.
You could keep a counter running at 32kHz interrupt on the ESP32 that is reset by the GPS pps signal.
If it goes above the 32kHz count => gps pps signal is lost, reset counter and fire the wall clock from the 32kHz interrupt.
Re: Interrupt phase shift - how to?
I was asking about how one could tune an internal RTC of ESP32 boards and then about the precision of this internal RTC standalone, once it's calibrated, without any external components - GPS, external RTCs like DS3231...
Do you mean incrementing a counter variable inside a GPIO interrupts handler? I doubt that the GPIO substem of ESP32 could handle interrupts with such a high frequency without some interrupts getting lost. Especially in a typical scenario when other tasks, notably including WiFi/Bluetooth, are running aside, even when placed on a second core.
Who is online
Users browsing this forum: Majestic-12 [Bot] and 77 guests