2022-01-06 17:02:09 +00:00
|
|
|
/*
|
2024-02-27 02:00:06 +00:00
|
|
|
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
2022-01-06 17:02:09 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2016-10-25 14:12:07 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
2016-11-02 09:17:28 +00:00
|
|
|
#include <time.h>
|
2023-07-12 02:03:03 +00:00
|
|
|
#include <limits.h>
|
2016-11-02 09:17:28 +00:00
|
|
|
#include <reent.h>
|
2017-08-16 06:58:30 +00:00
|
|
|
#include <unistd.h>
|
2016-10-25 14:12:07 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/reent.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/times.h>
|
2020-08-24 03:33:57 +00:00
|
|
|
#include <sys/lock.h>
|
2020-06-23 03:53:58 +00:00
|
|
|
|
2020-06-23 08:46:06 +00:00
|
|
|
#include "esp_system.h"
|
2016-10-25 14:12:07 +00:00
|
|
|
#include "esp_attr.h"
|
2020-11-06 04:00:07 +00:00
|
|
|
#include "esp_rom_sys.h"
|
2016-11-02 09:17:28 +00:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2020-06-23 03:53:58 +00:00
|
|
|
|
2020-08-11 09:49:48 +00:00
|
|
|
#include "esp_private/system_internal.h"
|
|
|
|
|
2020-06-23 03:53:58 +00:00
|
|
|
#include "soc/rtc.h"
|
|
|
|
|
|
|
|
#include "esp_time_impl.h"
|
|
|
|
|
2019-06-06 02:57:29 +00:00
|
|
|
#include "sdkconfig.h"
|
2016-10-25 14:12:07 +00:00
|
|
|
|
2020-10-20 06:09:32 +00:00
|
|
|
#if !CONFIG_ESP_TIME_FUNCS_USE_NONE
|
2020-06-23 03:53:58 +00:00
|
|
|
#define IMPL_NEWLIB_TIME_FUNCS 1
|
2016-11-02 09:17:28 +00:00
|
|
|
#endif
|
2016-10-25 14:12:07 +00:00
|
|
|
|
2020-06-23 03:53:58 +00:00
|
|
|
#if IMPL_NEWLIB_TIME_FUNCS
|
2018-05-28 12:36:04 +00:00
|
|
|
// stores the start time of the slew
|
2020-06-23 03:53:58 +00:00
|
|
|
static uint64_t s_adjtime_start_us;
|
2018-05-28 12:36:04 +00:00
|
|
|
// is how many microseconds total to slew
|
2020-06-23 03:53:58 +00:00
|
|
|
static int64_t s_adjtime_total_correction_us;
|
2016-11-02 09:17:28 +00:00
|
|
|
|
2020-08-24 03:33:57 +00:00
|
|
|
static _lock_t s_time_lock;
|
2018-05-28 12:36:04 +00:00
|
|
|
|
|
|
|
// This function gradually changes boot_time to the correction value and immediately updates it.
|
2019-07-16 09:33:30 +00:00
|
|
|
static uint64_t adjust_boot_time(void)
|
2018-05-28 12:36:04 +00:00
|
|
|
{
|
2024-02-27 02:00:06 +00:00
|
|
|
#define ADJTIME_CORRECTION_FACTOR 6
|
2020-06-23 03:53:58 +00:00
|
|
|
|
|
|
|
uint64_t boot_time = esp_time_impl_get_boot_time();
|
|
|
|
if ((boot_time == 0) || (esp_time_impl_get_time_since_boot() < s_adjtime_start_us)) {
|
|
|
|
s_adjtime_start_us = 0;
|
2018-05-28 12:36:04 +00:00
|
|
|
}
|
2020-06-23 03:53:58 +00:00
|
|
|
if (s_adjtime_start_us > 0) {
|
|
|
|
uint64_t since_boot = esp_time_impl_get_time_since_boot();
|
|
|
|
// If to call this function once per second, then (since_boot - s_adjtime_start_us) will be 1_000_000 (1 second),
|
2018-05-28 12:36:04 +00:00
|
|
|
// and the correction will be equal to (1_000_000us >> 6) = 15_625 us.
|
|
|
|
// The minimum possible correction step can be (64us >> 6) = 1us.
|
|
|
|
// Example: if the time error is 1 second, then it will be compensate for 1 sec / 0,015625 = 64 seconds.
|
2020-06-23 03:53:58 +00:00
|
|
|
int64_t correction = (since_boot >> ADJTIME_CORRECTION_FACTOR) - (s_adjtime_start_us >> ADJTIME_CORRECTION_FACTOR);
|
2018-05-28 12:36:04 +00:00
|
|
|
if (correction > 0) {
|
2020-06-23 03:53:58 +00:00
|
|
|
s_adjtime_start_us = since_boot;
|
|
|
|
if (s_adjtime_total_correction_us < 0) {
|
|
|
|
if ((s_adjtime_total_correction_us + correction) >= 0) {
|
|
|
|
boot_time = boot_time + s_adjtime_total_correction_us;
|
|
|
|
s_adjtime_start_us = 0;
|
2018-05-28 12:36:04 +00:00
|
|
|
} else {
|
2020-06-23 03:53:58 +00:00
|
|
|
s_adjtime_total_correction_us += correction;
|
2018-05-28 12:36:04 +00:00
|
|
|
boot_time -= correction;
|
|
|
|
}
|
|
|
|
} else {
|
2020-06-23 03:53:58 +00:00
|
|
|
if ((s_adjtime_total_correction_us - correction) <= 0) {
|
|
|
|
boot_time = boot_time + s_adjtime_total_correction_us;
|
|
|
|
s_adjtime_start_us = 0;
|
2018-05-28 12:36:04 +00:00
|
|
|
} else {
|
2020-06-23 03:53:58 +00:00
|
|
|
s_adjtime_total_correction_us -= correction;
|
2018-05-28 12:36:04 +00:00
|
|
|
boot_time += correction;
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 03:53:58 +00:00
|
|
|
esp_time_impl_set_boot_time(boot_time);
|
2018-05-28 12:36:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return boot_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the adjusted boot time.
|
2020-06-23 03:53:58 +00:00
|
|
|
static uint64_t get_adjusted_boot_time(void)
|
2018-05-28 12:36:04 +00:00
|
|
|
{
|
2020-08-24 03:33:57 +00:00
|
|
|
_lock_acquire(&s_time_lock);
|
2018-05-28 12:36:04 +00:00
|
|
|
uint64_t adjust_time = adjust_boot_time();
|
2020-08-24 03:33:57 +00:00
|
|
|
_lock_release(&s_time_lock);
|
2018-05-28 12:36:04 +00:00
|
|
|
return adjust_time;
|
|
|
|
}
|
|
|
|
|
2020-06-23 03:53:58 +00:00
|
|
|
// Applying the accumulated correction to base_time and stopping the smooth time adjustment.
|
2024-02-27 02:00:06 +00:00
|
|
|
static void adjtime_corr_stop(void)
|
2018-05-28 12:36:04 +00:00
|
|
|
{
|
2020-08-24 03:33:57 +00:00
|
|
|
_lock_acquire(&s_time_lock);
|
2024-02-27 02:00:06 +00:00
|
|
|
if (s_adjtime_start_us != 0) {
|
2018-05-28 12:36:04 +00:00
|
|
|
adjust_boot_time();
|
2020-06-23 03:53:58 +00:00
|
|
|
s_adjtime_start_us = 0;
|
2018-05-28 12:36:04 +00:00
|
|
|
}
|
2020-08-24 03:33:57 +00:00
|
|
|
_lock_release(&s_time_lock);
|
2018-05-28 12:36:04 +00:00
|
|
|
}
|
2020-06-23 03:53:58 +00:00
|
|
|
#endif
|
2017-01-11 09:28:09 +00:00
|
|
|
|
2018-05-28 12:36:04 +00:00
|
|
|
int adjtime(const struct timeval *delta, struct timeval *outdelta)
|
|
|
|
{
|
2020-06-23 03:53:58 +00:00
|
|
|
#if IMPL_NEWLIB_TIME_FUNCS
|
2024-02-27 02:00:06 +00:00
|
|
|
if (outdelta != NULL) {
|
2020-08-24 03:33:57 +00:00
|
|
|
_lock_acquire(&s_time_lock);
|
2020-06-08 14:57:38 +00:00
|
|
|
adjust_boot_time();
|
2020-06-23 03:53:58 +00:00
|
|
|
if (s_adjtime_start_us != 0) {
|
|
|
|
outdelta->tv_sec = s_adjtime_total_correction_us / 1000000L;
|
|
|
|
outdelta->tv_usec = s_adjtime_total_correction_us % 1000000L;
|
2020-06-08 14:57:38 +00:00
|
|
|
} else {
|
|
|
|
outdelta->tv_sec = 0;
|
|
|
|
outdelta->tv_usec = 0;
|
|
|
|
}
|
2020-08-24 03:33:57 +00:00
|
|
|
_lock_release(&s_time_lock);
|
2020-06-08 14:57:38 +00:00
|
|
|
}
|
2024-02-27 02:00:06 +00:00
|
|
|
if (delta != NULL) {
|
2018-05-28 12:36:04 +00:00
|
|
|
int64_t sec = delta->tv_sec;
|
|
|
|
int64_t usec = delta->tv_usec;
|
2024-02-27 02:00:06 +00:00
|
|
|
if (llabs(sec) > ((INT_MAX / 1000000L) - 1L)) {
|
2022-12-08 22:32:31 +00:00
|
|
|
errno = EINVAL;
|
2018-05-28 12:36:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If adjusting the system clock by adjtime () is already done during the second call adjtime (),
|
|
|
|
* and the delta of the second call is not NULL, the earlier tuning is stopped,
|
|
|
|
* but the already completed part of the adjustment is not canceled.
|
|
|
|
*/
|
2020-08-24 03:33:57 +00:00
|
|
|
_lock_acquire(&s_time_lock);
|
2020-06-23 03:53:58 +00:00
|
|
|
// If correction is already in progress (s_adjtime_start_time_us != 0), then apply accumulated corrections.
|
2018-05-28 12:36:04 +00:00
|
|
|
adjust_boot_time();
|
2020-06-23 03:53:58 +00:00
|
|
|
s_adjtime_start_us = esp_time_impl_get_time_since_boot();
|
|
|
|
s_adjtime_total_correction_us = sec * 1000000L + usec;
|
2020-08-24 03:33:57 +00:00
|
|
|
_lock_release(&s_time_lock);
|
2018-05-28 12:36:04 +00:00
|
|
|
}
|
2017-09-22 15:04:16 +00:00
|
|
|
return 0;
|
2020-06-23 03:53:58 +00:00
|
|
|
#else
|
2022-12-08 22:29:01 +00:00
|
|
|
errno = ENOSYS;
|
2020-06-23 03:53:58 +00:00
|
|
|
return -1;
|
2017-09-22 15:04:16 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-11-02 09:17:28 +00:00
|
|
|
clock_t IRAM_ATTR _times_r(struct _reent *r, struct tms *ptms)
|
|
|
|
{
|
|
|
|
clock_t t = xTaskGetTickCount() * (portTICK_PERIOD_MS * CLK_TCK / 1000);
|
2016-11-08 02:33:21 +00:00
|
|
|
ptms->tms_cstime = 0;
|
2016-11-02 09:17:28 +00:00
|
|
|
ptms->tms_cutime = 0;
|
|
|
|
ptms->tms_stime = t;
|
|
|
|
ptms->tms_utime = 0;
|
|
|
|
struct timeval tv = {0, 0};
|
|
|
|
_gettimeofday_r(r, &tv, NULL);
|
|
|
|
return (clock_t) tv.tv_sec;
|
|
|
|
}
|
|
|
|
|
2016-11-03 04:46:46 +00:00
|
|
|
int IRAM_ATTR _gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz)
|
|
|
|
{
|
|
|
|
(void) tz;
|
2020-06-23 03:53:58 +00:00
|
|
|
|
|
|
|
#if IMPL_NEWLIB_TIME_FUNCS
|
2016-11-02 09:17:28 +00:00
|
|
|
if (tv) {
|
2020-06-23 03:53:58 +00:00
|
|
|
uint64_t microseconds = get_adjusted_boot_time() + esp_time_impl_get_time_since_boot();
|
2017-01-11 09:28:09 +00:00
|
|
|
tv->tv_sec = microseconds / 1000000;
|
2016-11-02 09:17:28 +00:00
|
|
|
tv->tv_usec = microseconds % 1000000;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#else
|
2016-10-25 14:12:07 +00:00
|
|
|
__errno_r(r) = ENOSYS;
|
2016-10-27 03:47:41 +00:00
|
|
|
return -1;
|
2020-06-23 03:53:58 +00:00
|
|
|
#endif
|
2016-10-25 14:12:07 +00:00
|
|
|
}
|
2016-11-03 04:46:46 +00:00
|
|
|
|
|
|
|
int settimeofday(const struct timeval *tv, const struct timezone *tz)
|
|
|
|
{
|
|
|
|
(void) tz;
|
2020-06-23 03:53:58 +00:00
|
|
|
#if IMPL_NEWLIB_TIME_FUNCS
|
2016-11-03 04:46:46 +00:00
|
|
|
if (tv) {
|
2018-05-28 12:36:04 +00:00
|
|
|
adjtime_corr_stop();
|
2016-11-03 04:46:46 +00:00
|
|
|
uint64_t now = ((uint64_t) tv->tv_sec) * 1000000LL + tv->tv_usec;
|
2020-06-23 03:53:58 +00:00
|
|
|
uint64_t since_boot = esp_time_impl_get_time_since_boot();
|
|
|
|
esp_time_impl_set_boot_time(now - since_boot);
|
2016-11-03 04:46:46 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#else
|
2016-11-04 02:55:25 +00:00
|
|
|
errno = ENOSYS;
|
2016-11-03 04:46:46 +00:00
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
2016-11-21 09:15:37 +00:00
|
|
|
|
2017-08-16 06:58:30 +00:00
|
|
|
int usleep(useconds_t us)
|
|
|
|
{
|
|
|
|
const int us_per_tick = portTICK_PERIOD_MS * 1000;
|
|
|
|
if (us < us_per_tick) {
|
2020-07-21 05:07:34 +00:00
|
|
|
esp_rom_delay_us((uint32_t) us);
|
2017-08-16 06:58:30 +00:00
|
|
|
} else {
|
|
|
|
/* since vTaskDelay(1) blocks for anywhere between 0 and portTICK_PERIOD_MS,
|
|
|
|
* round up to compensate.
|
|
|
|
*/
|
|
|
|
vTaskDelay((us + us_per_tick - 1) / us_per_tick);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-31 23:18:14 +00:00
|
|
|
unsigned int sleep(unsigned int seconds)
|
|
|
|
{
|
2024-02-27 02:00:06 +00:00
|
|
|
usleep(seconds * 1000000UL);
|
2017-08-31 23:18:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-23 03:53:58 +00:00
|
|
|
int clock_settime(clockid_t clock_id, const struct timespec *tp)
|
2016-11-21 09:15:37 +00:00
|
|
|
{
|
2020-06-23 03:53:58 +00:00
|
|
|
#if IMPL_NEWLIB_TIME_FUNCS
|
2018-06-25 06:12:12 +00:00
|
|
|
if (tp == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
struct timeval tv;
|
|
|
|
switch (clock_id) {
|
2024-02-27 02:00:06 +00:00
|
|
|
case CLOCK_REALTIME:
|
|
|
|
tv.tv_sec = tp->tv_sec;
|
|
|
|
tv.tv_usec = tp->tv_nsec / 1000L;
|
|
|
|
settimeofday(&tv, NULL);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
2018-06-25 06:12:12 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-02-27 02:00:06 +00:00
|
|
|
int clock_gettime(clockid_t clock_id, struct timespec *tp)
|
2018-06-25 06:12:12 +00:00
|
|
|
{
|
2020-06-23 03:53:58 +00:00
|
|
|
#if IMPL_NEWLIB_TIME_FUNCS
|
2018-06-25 06:12:12 +00:00
|
|
|
if (tp == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
struct timeval tv;
|
|
|
|
uint64_t monotonic_time_us = 0;
|
|
|
|
switch (clock_id) {
|
2024-02-27 02:00:06 +00:00
|
|
|
case CLOCK_REALTIME:
|
|
|
|
_gettimeofday_r(NULL, &tv, NULL);
|
|
|
|
tp->tv_sec = tv.tv_sec;
|
|
|
|
tp->tv_nsec = tv.tv_usec * 1000L;
|
|
|
|
break;
|
|
|
|
case CLOCK_MONOTONIC:
|
|
|
|
monotonic_time_us = esp_time_impl_get_time();
|
|
|
|
tp->tv_sec = monotonic_time_us / 1000000LL;
|
|
|
|
tp->tv_nsec = (monotonic_time_us % 1000000LL) * 1000L;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
2018-06-25 06:12:12 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-02-27 02:00:06 +00:00
|
|
|
int clock_getres(clockid_t clock_id, struct timespec *res)
|
2018-06-25 06:12:12 +00:00
|
|
|
{
|
2020-06-23 03:53:58 +00:00
|
|
|
#if IMPL_NEWLIB_TIME_FUNCS
|
2018-06-25 06:12:12 +00:00
|
|
|
if (res == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2020-06-23 03:53:58 +00:00
|
|
|
|
2018-06-25 06:12:12 +00:00
|
|
|
res->tv_sec = 0;
|
2020-10-07 09:39:11 +00:00
|
|
|
res->tv_nsec = esp_system_get_time_resolution();
|
2020-06-23 03:53:58 +00:00
|
|
|
|
2018-06-25 06:12:12 +00:00
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
2020-06-23 03:53:58 +00:00
|
|
|
|
|
|
|
void esp_newlib_time_init(void)
|
|
|
|
{
|
|
|
|
esp_time_impl_init();
|
2020-11-10 07:40:01 +00:00
|
|
|
}
|