2017-04-24 06:36:47 -04:00
|
|
|
// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
|
2016-10-25 10:12:07 -04:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
2016-11-02 05:17:28 -04:00
|
|
|
#include <time.h>
|
|
|
|
#include <reent.h>
|
2017-08-16 02:58:30 -04:00
|
|
|
#include <unistd.h>
|
2016-10-25 10:12:07 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/reent.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/times.h>
|
2020-08-23 23:33:57 -04:00
|
|
|
#include <sys/lock.h>
|
2020-06-22 23:53:58 -04:00
|
|
|
|
2020-06-23 04:46:06 -04:00
|
|
|
#include "esp_system.h"
|
2016-10-25 10:12:07 -04:00
|
|
|
#include "esp_attr.h"
|
2020-11-05 23:00:07 -05:00
|
|
|
#include "esp_rom_sys.h"
|
2016-11-02 05:17:28 -04:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2020-06-22 23:53:58 -04:00
|
|
|
|
2020-08-11 05:49:48 -04:00
|
|
|
#include "esp_private/system_internal.h"
|
|
|
|
|
2020-06-22 23:53:58 -04:00
|
|
|
#include "soc/rtc.h"
|
|
|
|
|
|
|
|
#include "esp_time_impl.h"
|
|
|
|
|
2019-06-05 22:57:29 -04:00
|
|
|
#include "sdkconfig.h"
|
2016-10-25 10:12:07 -04:00
|
|
|
|
2020-01-09 23:58:54 -05:00
|
|
|
#ifdef CONFIG_SDK_TOOLCHAIN_SUPPORTS_TIME_WIDE_64_BITS
|
|
|
|
_Static_assert(sizeof(time_t) == 8, "The toolchain does not support time_t wide 64-bits");
|
|
|
|
#else
|
|
|
|
_Static_assert(sizeof(time_t) == 4, "The toolchain supports time_t wide 64-bits. Please enable CONFIG_SDK_TOOLCHAIN_SUPPORTS_TIME_WIDE_64_BITS.");
|
|
|
|
#endif
|
|
|
|
|
2020-10-20 02:09:32 -04:00
|
|
|
#if !CONFIG_ESP_TIME_FUNCS_USE_NONE
|
2020-06-22 23:53:58 -04:00
|
|
|
#define IMPL_NEWLIB_TIME_FUNCS 1
|
2016-11-02 05:17:28 -04:00
|
|
|
#endif
|
2016-10-25 10:12:07 -04:00
|
|
|
|
2020-06-22 23:53:58 -04:00
|
|
|
#if IMPL_NEWLIB_TIME_FUNCS
|
2018-05-28 08:36:04 -04:00
|
|
|
// stores the start time of the slew
|
2020-06-22 23:53:58 -04:00
|
|
|
static uint64_t s_adjtime_start_us;
|
2018-05-28 08:36:04 -04:00
|
|
|
// is how many microseconds total to slew
|
2020-06-22 23:53:58 -04:00
|
|
|
static int64_t s_adjtime_total_correction_us;
|
2016-11-02 05:17:28 -04:00
|
|
|
|
2020-08-23 23:33:57 -04:00
|
|
|
static _lock_t s_time_lock;
|
2018-05-28 08:36:04 -04:00
|
|
|
|
|
|
|
// This function gradually changes boot_time to the correction value and immediately updates it.
|
2019-07-16 05:33:30 -04:00
|
|
|
static uint64_t adjust_boot_time(void)
|
2018-05-28 08:36:04 -04:00
|
|
|
{
|
2020-06-22 23:53:58 -04:00
|
|
|
#define ADJTIME_CORRECTION_FACTOR 6
|
|
|
|
|
|
|
|
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 08:36:04 -04:00
|
|
|
}
|
2020-06-22 23:53:58 -04: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 08:36:04 -04: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-22 23:53:58 -04:00
|
|
|
int64_t correction = (since_boot >> ADJTIME_CORRECTION_FACTOR) - (s_adjtime_start_us >> ADJTIME_CORRECTION_FACTOR);
|
2018-05-28 08:36:04 -04:00
|
|
|
if (correction > 0) {
|
2020-06-22 23:53:58 -04: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 08:36:04 -04:00
|
|
|
} else {
|
2020-06-22 23:53:58 -04:00
|
|
|
s_adjtime_total_correction_us += correction;
|
2018-05-28 08:36:04 -04:00
|
|
|
boot_time -= correction;
|
|
|
|
}
|
|
|
|
} else {
|
2020-06-22 23:53:58 -04: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 08:36:04 -04:00
|
|
|
} else {
|
2020-06-22 23:53:58 -04:00
|
|
|
s_adjtime_total_correction_us -= correction;
|
2018-05-28 08:36:04 -04:00
|
|
|
boot_time += correction;
|
|
|
|
}
|
|
|
|
}
|
2020-06-22 23:53:58 -04:00
|
|
|
esp_time_impl_set_boot_time(boot_time);
|
2018-05-28 08:36:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return boot_time;
|
|
|
|
}
|
|
|
|
|
2020-06-22 23:53:58 -04:00
|
|
|
|
2018-05-28 08:36:04 -04:00
|
|
|
// Get the adjusted boot time.
|
2020-06-22 23:53:58 -04:00
|
|
|
static uint64_t get_adjusted_boot_time(void)
|
2018-05-28 08:36:04 -04:00
|
|
|
{
|
2020-08-23 23:33:57 -04:00
|
|
|
_lock_acquire(&s_time_lock);
|
2018-05-28 08:36:04 -04:00
|
|
|
uint64_t adjust_time = adjust_boot_time();
|
2020-08-23 23:33:57 -04:00
|
|
|
_lock_release(&s_time_lock);
|
2018-05-28 08:36:04 -04:00
|
|
|
return adjust_time;
|
|
|
|
}
|
|
|
|
|
2020-06-22 23:53:58 -04:00
|
|
|
// Applying the accumulated correction to base_time and stopping the smooth time adjustment.
|
2018-05-28 08:36:04 -04:00
|
|
|
static void adjtime_corr_stop (void)
|
|
|
|
{
|
2020-08-23 23:33:57 -04:00
|
|
|
_lock_acquire(&s_time_lock);
|
2020-06-22 23:53:58 -04:00
|
|
|
if (s_adjtime_start_us != 0){
|
2018-05-28 08:36:04 -04:00
|
|
|
adjust_boot_time();
|
2020-06-22 23:53:58 -04:00
|
|
|
s_adjtime_start_us = 0;
|
2018-05-28 08:36:04 -04:00
|
|
|
}
|
2020-08-23 23:33:57 -04:00
|
|
|
_lock_release(&s_time_lock);
|
2018-05-28 08:36:04 -04:00
|
|
|
}
|
2020-06-22 23:53:58 -04:00
|
|
|
#endif
|
2017-01-11 04:28:09 -05:00
|
|
|
|
2018-05-28 08:36:04 -04:00
|
|
|
int adjtime(const struct timeval *delta, struct timeval *outdelta)
|
|
|
|
{
|
2020-06-22 23:53:58 -04:00
|
|
|
#if IMPL_NEWLIB_TIME_FUNCS
|
2020-06-08 10:57:38 -04:00
|
|
|
if(outdelta != NULL){
|
2020-08-23 23:33:57 -04:00
|
|
|
_lock_acquire(&s_time_lock);
|
2020-06-08 10:57:38 -04:00
|
|
|
adjust_boot_time();
|
2020-06-22 23:53:58 -04: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 10:57:38 -04:00
|
|
|
} else {
|
|
|
|
outdelta->tv_sec = 0;
|
|
|
|
outdelta->tv_usec = 0;
|
|
|
|
}
|
2020-08-23 23:33:57 -04:00
|
|
|
_lock_release(&s_time_lock);
|
2020-06-08 10:57:38 -04:00
|
|
|
}
|
2018-05-28 08:36:04 -04:00
|
|
|
if(delta != NULL){
|
|
|
|
int64_t sec = delta->tv_sec;
|
|
|
|
int64_t usec = delta->tv_usec;
|
|
|
|
if(llabs(sec) > ((INT_MAX / 1000000L) - 1L)) {
|
2022-12-08 17:32:31 -05:00
|
|
|
errno = EINVAL;
|
2018-05-28 08:36:04 -04: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-23 23:33:57 -04:00
|
|
|
_lock_acquire(&s_time_lock);
|
2020-06-22 23:53:58 -04:00
|
|
|
// If correction is already in progress (s_adjtime_start_time_us != 0), then apply accumulated corrections.
|
2018-05-28 08:36:04 -04:00
|
|
|
adjust_boot_time();
|
2020-06-22 23:53:58 -04:00
|
|
|
s_adjtime_start_us = esp_time_impl_get_time_since_boot();
|
|
|
|
s_adjtime_total_correction_us = sec * 1000000L + usec;
|
2020-08-23 23:33:57 -04:00
|
|
|
_lock_release(&s_time_lock);
|
2018-05-28 08:36:04 -04:00
|
|
|
}
|
2017-09-22 11:04:16 -04:00
|
|
|
return 0;
|
2020-06-22 23:53:58 -04:00
|
|
|
#else
|
2022-12-08 17:29:01 -05:00
|
|
|
errno = ENOSYS;
|
2020-06-22 23:53:58 -04:00
|
|
|
return -1;
|
2017-09-22 11:04:16 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-11-02 05:17:28 -04: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-07 21:33:21 -05:00
|
|
|
ptms->tms_cstime = 0;
|
2016-11-02 05:17:28 -04: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 00:46:46 -04:00
|
|
|
int IRAM_ATTR _gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz)
|
|
|
|
{
|
|
|
|
(void) tz;
|
2020-06-22 23:53:58 -04:00
|
|
|
|
|
|
|
#if IMPL_NEWLIB_TIME_FUNCS
|
2016-11-02 05:17:28 -04:00
|
|
|
if (tv) {
|
2020-06-22 23:53:58 -04:00
|
|
|
uint64_t microseconds = get_adjusted_boot_time() + esp_time_impl_get_time_since_boot();
|
2017-01-11 04:28:09 -05:00
|
|
|
tv->tv_sec = microseconds / 1000000;
|
2016-11-02 05:17:28 -04:00
|
|
|
tv->tv_usec = microseconds % 1000000;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#else
|
2016-10-25 10:12:07 -04:00
|
|
|
__errno_r(r) = ENOSYS;
|
2016-10-26 23:47:41 -04:00
|
|
|
return -1;
|
2020-06-22 23:53:58 -04:00
|
|
|
#endif
|
2016-10-25 10:12:07 -04:00
|
|
|
}
|
2016-11-03 00:46:46 -04:00
|
|
|
|
|
|
|
int settimeofday(const struct timeval *tv, const struct timezone *tz)
|
|
|
|
{
|
|
|
|
(void) tz;
|
2020-06-22 23:53:58 -04:00
|
|
|
#if IMPL_NEWLIB_TIME_FUNCS
|
2016-11-03 00:46:46 -04:00
|
|
|
if (tv) {
|
2018-05-28 08:36:04 -04:00
|
|
|
adjtime_corr_stop();
|
2016-11-03 00:46:46 -04:00
|
|
|
uint64_t now = ((uint64_t) tv->tv_sec) * 1000000LL + tv->tv_usec;
|
2020-06-22 23:53:58 -04:00
|
|
|
uint64_t since_boot = esp_time_impl_get_time_since_boot();
|
|
|
|
esp_time_impl_set_boot_time(now - since_boot);
|
2016-11-03 00:46:46 -04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#else
|
2016-11-03 22:55:25 -04:00
|
|
|
errno = ENOSYS;
|
2016-11-03 00:46:46 -04:00
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
2016-11-21 04:15:37 -05:00
|
|
|
|
2017-08-16 02:58:30 -04:00
|
|
|
int usleep(useconds_t us)
|
|
|
|
{
|
|
|
|
const int us_per_tick = portTICK_PERIOD_MS * 1000;
|
|
|
|
if (us < us_per_tick) {
|
2020-07-21 01:07:34 -04:00
|
|
|
esp_rom_delay_us((uint32_t) us);
|
2017-08-16 02:58:30 -04: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 19:18:14 -04:00
|
|
|
unsigned int sleep(unsigned int seconds)
|
|
|
|
{
|
|
|
|
usleep(seconds*1000000UL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-22 23:53:58 -04:00
|
|
|
int clock_settime(clockid_t clock_id, const struct timespec *tp)
|
2016-11-21 04:15:37 -05:00
|
|
|
{
|
2020-06-22 23:53:58 -04:00
|
|
|
#if IMPL_NEWLIB_TIME_FUNCS
|
2018-06-25 02:12:12 -04:00
|
|
|
if (tp == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
struct timeval tv;
|
|
|
|
switch (clock_id) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int clock_gettime (clockid_t clock_id, struct timespec *tp)
|
|
|
|
{
|
2020-06-22 23:53:58 -04:00
|
|
|
#if IMPL_NEWLIB_TIME_FUNCS
|
2018-06-25 02:12:12 -04:00
|
|
|
if (tp == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
struct timeval tv;
|
|
|
|
uint64_t monotonic_time_us = 0;
|
|
|
|
switch (clock_id) {
|
|
|
|
case CLOCK_REALTIME:
|
2019-07-16 11:52:04 -04:00
|
|
|
_gettimeofday_r(NULL, &tv, NULL);
|
2018-06-25 02:12:12 -04:00
|
|
|
tp->tv_sec = tv.tv_sec;
|
|
|
|
tp->tv_nsec = tv.tv_usec * 1000L;
|
|
|
|
break;
|
|
|
|
case CLOCK_MONOTONIC:
|
2020-06-22 23:53:58 -04:00
|
|
|
monotonic_time_us = esp_time_impl_get_time();
|
2018-06-25 02:12:12 -04:00
|
|
|
tp->tv_sec = monotonic_time_us / 1000000LL;
|
|
|
|
tp->tv_nsec = (monotonic_time_us % 1000000LL) * 1000L;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int clock_getres (clockid_t clock_id, struct timespec *res)
|
|
|
|
{
|
2020-06-22 23:53:58 -04:00
|
|
|
#if IMPL_NEWLIB_TIME_FUNCS
|
2018-06-25 02:12:12 -04:00
|
|
|
if (res == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2020-06-22 23:53:58 -04:00
|
|
|
|
2018-06-25 02:12:12 -04:00
|
|
|
res->tv_sec = 0;
|
2020-10-07 05:39:11 -04:00
|
|
|
res->tv_nsec = esp_system_get_time_resolution();
|
2020-06-22 23:53:58 -04:00
|
|
|
|
2018-06-25 02:12:12 -04:00
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
2020-06-22 23:53:58 -04:00
|
|
|
|
|
|
|
void esp_newlib_time_init(void)
|
|
|
|
{
|
|
|
|
esp_time_impl_init();
|
2020-11-10 02:40:01 -05:00
|
|
|
}
|