esp_timer: Fix System time jumps back ~54secs

Closes: https://github.com/espressif/esp-idf/issues/2513
This commit is contained in:
KonstantinKondrashov
2019-09-06 20:17:25 +08:00
parent 09cc922b42
commit f1e8a49836
2 changed files with 31 additions and 41 deletions

View File

@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "sys/param.h"
#include "esp_err.h" #include "esp_err.h"
#include "esp_timer.h" #include "esp_timer.h"
#include "esp_system.h" #include "esp_system.h"
@@ -127,13 +128,6 @@ static uint32_t s_timer_us_per_overflow;
// will not increment s_time_base_us if this flag is set. // will not increment s_time_base_us if this flag is set.
static bool s_mask_overflow; static bool s_mask_overflow;
//The timer_overflow_happened read alarm register to tell if overflow happened.
//However, there is a monent that overflow happens, and before ISR function called
//alarm register is set to another value, then you call timer_overflow_happened,
//it will return false.
//So we store the overflow value when new alarm is to be set.
static bool s_overflow_happened;
#ifdef CONFIG_PM_DFS_USE_RTC_TIMER_REF #ifdef CONFIG_PM_DFS_USE_RTC_TIMER_REF
// If DFS is enabled, upon the first frequency change this value is set to the // If DFS is enabled, upon the first frequency change this value is set to the
// difference between esp_timer value and RTC timer value. On every subsequent // difference between esp_timer value and RTC timer value. On every subsequent
@@ -152,10 +146,6 @@ portMUX_TYPE s_time_update_lock = portMUX_INITIALIZER_UNLOCKED;
// Check if timer overflow has happened (but was not handled by ISR yet) // Check if timer overflow has happened (but was not handled by ISR yet)
static inline bool IRAM_ATTR timer_overflow_happened() static inline bool IRAM_ATTR timer_overflow_happened()
{ {
if (s_overflow_happened) {
return true;
}
return ((REG_READ(FRC_TIMER_CTRL_REG(1)) & FRC_TIMER_INT_STATUS) != 0 && return ((REG_READ(FRC_TIMER_CTRL_REG(1)) & FRC_TIMER_INT_STATUS) != 0 &&
((REG_READ(FRC_TIMER_ALARM_REG(1)) == ALARM_OVERFLOW_VAL && TIMER_IS_AFTER_OVERFLOW(REG_READ(FRC_TIMER_COUNT_REG(1))) && !s_mask_overflow) || ((REG_READ(FRC_TIMER_ALARM_REG(1)) == ALARM_OVERFLOW_VAL && TIMER_IS_AFTER_OVERFLOW(REG_READ(FRC_TIMER_COUNT_REG(1))) && !s_mask_overflow) ||
(!TIMER_IS_AFTER_OVERFLOW(REG_READ(FRC_TIMER_ALARM_REG(1))) && TIMER_IS_AFTER_OVERFLOW(REG_READ(FRC_TIMER_COUNT_REG(1)))))); (!TIMER_IS_AFTER_OVERFLOW(REG_READ(FRC_TIMER_ALARM_REG(1))) && TIMER_IS_AFTER_OVERFLOW(REG_READ(FRC_TIMER_COUNT_REG(1))))));
@@ -222,35 +212,31 @@ uint64_t IRAM_ATTR esp_timer_impl_get_time()
void IRAM_ATTR esp_timer_impl_set_alarm(uint64_t timestamp) void IRAM_ATTR esp_timer_impl_set_alarm(uint64_t timestamp)
{ {
portENTER_CRITICAL(&s_time_update_lock); portENTER_CRITICAL(&s_time_update_lock);
// Alarm time relative to the moment when counter was 0
uint64_t time_after_timebase_us = timestamp - s_time_base_us;
// Adjust current time if overflow has happened
bool overflow = timer_overflow_happened();
uint64_t cur_count = REG_READ(FRC_TIMER_COUNT_REG(1));
if (overflow) {
assert(time_after_timebase_us > s_timer_us_per_overflow);
time_after_timebase_us -= s_timer_us_per_overflow;
s_overflow_happened = true;
}
// Calculate desired timer compare value (may exceed 2^32-1)
uint64_t compare_val = time_after_timebase_us * s_timer_ticks_per_us;
uint32_t alarm_reg_val = ALARM_OVERFLOW_VAL;
// Use calculated alarm value if it is less than ALARM_OVERFLOW_VAL. // Use calculated alarm value if it is less than ALARM_OVERFLOW_VAL.
// Note that if by the time we update ALARM_REG, COUNT_REG value is higher, // Note that if by the time we update ALARM_REG, COUNT_REG value is higher,
// interrupt will not happen for another ALARM_OVERFLOW_VAL timer ticks, // interrupt will not happen for another ALARM_OVERFLOW_VAL timer ticks,
// so need to check if alarm value is too close in the future (e.g. <2 us away). // so need to check if alarm value is too close in the future (e.g. <2 us away).
const uint32_t offset = s_timer_ticks_per_us * 2; const uint32_t offset = s_timer_ticks_per_us * 2;
do {
// Adjust current time if overflow has happened
if (timer_overflow_happened()) {
timer_count_reload();
s_time_base_us += s_timer_us_per_overflow;
}
s_mask_overflow = false;
uint64_t cur_count = REG_READ(FRC_TIMER_COUNT_REG(1));
// Alarm time relative to the moment when counter was 0
int64_t time_after_timebase_us = (int64_t)timestamp - s_time_base_us;
// Calculate desired timer compare value (may exceed 2^32-1)
int64_t compare_val = time_after_timebase_us * s_timer_ticks_per_us;
compare_val = MAX(compare_val, cur_count + offset);
uint32_t alarm_reg_val = ALARM_OVERFLOW_VAL;
if (compare_val < ALARM_OVERFLOW_VAL) { if (compare_val < ALARM_OVERFLOW_VAL) {
if (compare_val < cur_count + offset) {
compare_val = cur_count + offset;
if (compare_val > ALARM_OVERFLOW_VAL) {
compare_val = ALARM_OVERFLOW_VAL;
}
}
alarm_reg_val = (uint32_t) compare_val; alarm_reg_val = (uint32_t) compare_val;
} }
REG_WRITE(FRC_TIMER_ALARM_REG(1), alarm_reg_val); REG_WRITE(FRC_TIMER_ALARM_REG(1), alarm_reg_val);
} while (REG_READ(FRC_TIMER_ALARM_REG(1)) <= REG_READ(FRC_TIMER_COUNT_REG(1)));
portEXIT_CRITICAL(&s_time_update_lock); portEXIT_CRITICAL(&s_time_update_lock);
} }
@@ -261,7 +247,6 @@ static void IRAM_ATTR timer_alarm_isr(void *arg)
if (timer_overflow_happened()) { if (timer_overflow_happened()) {
timer_count_reload(); timer_count_reload();
s_time_base_us += s_timer_us_per_overflow; s_time_base_us += s_timer_us_per_overflow;
s_overflow_happened = false;
} }
s_mask_overflow = false; s_mask_overflow = false;
// Clear interrupt status // Clear interrupt status
@@ -336,7 +321,6 @@ void esp_timer_impl_advance(int64_t time_us)
REG_WRITE(FRC_TIMER_ALARM_REG(1), 0); REG_WRITE(FRC_TIMER_ALARM_REG(1), 0);
REG_WRITE(FRC_TIMER_LOAD_REG(1), 0); REG_WRITE(FRC_TIMER_LOAD_REG(1), 0);
s_time_base_us += count / s_timer_ticks_per_us + time_us; s_time_base_us += count / s_timer_ticks_per_us + time_us;
s_overflow_happened = false;
portEXIT_CRITICAL(&s_time_update_lock); portEXIT_CRITICAL(&s_time_update_lock);
} }

View File

@@ -126,12 +126,15 @@ esp_err_t IRAM_ATTR esp_timer_start_once(esp_timer_handle_t timer, uint64_t time
if (!is_initialized() || timer_armed(timer)) { if (!is_initialized() || timer_armed(timer)) {
return ESP_ERR_INVALID_STATE; return ESP_ERR_INVALID_STATE;
} }
timer_list_lock();
timer->alarm = esp_timer_get_time() + timeout_us; timer->alarm = esp_timer_get_time() + timeout_us;
timer->period = 0; timer->period = 0;
#if WITH_PROFILING #if WITH_PROFILING
timer->times_armed++; timer->times_armed++;
#endif #endif
return timer_insert(timer); esp_err_t err = timer_insert(timer);
timer_list_unlock();
return err;
} }
esp_err_t IRAM_ATTR esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period_us) esp_err_t IRAM_ATTR esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period_us)
@@ -139,13 +142,16 @@ esp_err_t IRAM_ATTR esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t
if (!is_initialized() || timer_armed(timer)) { if (!is_initialized() || timer_armed(timer)) {
return ESP_ERR_INVALID_STATE; return ESP_ERR_INVALID_STATE;
} }
timer_list_lock();
period_us = MAX(period_us, esp_timer_impl_get_min_period_us()); period_us = MAX(period_us, esp_timer_impl_get_min_period_us());
timer->alarm = esp_timer_get_time() + period_us; timer->alarm = esp_timer_get_time() + period_us;
timer->period = period_us; timer->period = period_us;
#if WITH_PROFILING #if WITH_PROFILING
timer->times_armed++; timer->times_armed++;
#endif #endif
return timer_insert(timer); esp_err_t err = timer_insert(timer);
timer_list_unlock();
return err;
} }
esp_err_t IRAM_ATTR esp_timer_stop(esp_timer_handle_t timer) esp_err_t IRAM_ATTR esp_timer_stop(esp_timer_handle_t timer)
@@ -164,16 +170,17 @@ esp_err_t esp_timer_delete(esp_timer_handle_t timer)
if (timer_armed(timer)) { if (timer_armed(timer)) {
return ESP_ERR_INVALID_STATE; return ESP_ERR_INVALID_STATE;
} }
timer_list_lock();
timer->event_id = EVENT_ID_DELETE_TIMER; timer->event_id = EVENT_ID_DELETE_TIMER;
timer->alarm = esp_timer_get_time() + 50; timer->alarm = esp_timer_get_time();
timer->period = 0; timer->period = 0;
timer_insert(timer); timer_insert(timer);
timer_list_unlock();
return ESP_OK; return ESP_OK;
} }
static IRAM_ATTR esp_err_t timer_insert(esp_timer_handle_t timer) static IRAM_ATTR esp_err_t timer_insert(esp_timer_handle_t timer)
{ {
timer_list_lock();
#if WITH_PROFILING #if WITH_PROFILING
timer_remove_inactive(timer); timer_remove_inactive(timer);
#endif #endif
@@ -196,7 +203,6 @@ static IRAM_ATTR esp_err_t timer_insert(esp_timer_handle_t timer)
if (timer == LIST_FIRST(&s_timers)) { if (timer == LIST_FIRST(&s_timers)) {
esp_timer_impl_set_alarm(timer->alarm); esp_timer_impl_set_alarm(timer->alarm);
} }
timer_list_unlock();
return ESP_OK; return ESP_OK;
} }