Select one of the symbols to view example projects that use it.
 
Outline
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#include <reent.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/reent.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/lock.h>
#include "esp_system.h"
#include "esp_attr.h"
#include "esp_rom_sys.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_private/system_internal.h"
#include "soc/rtc.h"
#include "esp_time_impl.h"
#include "sdkconfig.h"
#define IMPL_NEWLIB_TIME_FUNCS
#define WEAK_UNLESS_TIMEFUNC_IMPL
s_adjtime_start_us
s_adjtime_total_correction_us
s_time_lock
adjust_boot_time()
get_adjusted_boot_time()
adjtime_corr_stop()
#define WEAK_UNLESS_TIMEFUNC_IMPL
adjtime(const struct timeval *, struct timeval *)
_times_r(struct _reent *, struct tms *)
_gettimeofday_r(struct _reent *, struct timeval *, void *)
settimeofday(const struct timeval *, const struct timezone *)
usleep(useconds_t)
sleep(unsigned int)
clock_settime(clockid_t, const struct timespec *)
clock_gettime(clockid_t, struct timespec *)
clock_getres(clockid_t, struct timespec *)
esp_newlib_time_init()
Files
loading...
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/newlib/time.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #include <errno.h> #include <stdlib.h> #include <time.h> #include <limits.h> #include <reent.h> #include <unistd.h> #include <sys/types.h> #include <sys/reent.h> #include <sys/time.h> #include <sys/times.h> #include <sys/lock.h> #include "esp_system.h" #include "esp_attr.h" #include "esp_rom_sys.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_private/system_internal.h" #include "soc/rtc.h" #include "esp_time_impl.h" #include "sdkconfig.h"20 includes #if !CONFIG_ESP_TIME_FUNCS_USE_NONE #define IMPL_NEWLIB_TIME_FUNCS 1 #endif #if IMPL_NEWLIB_TIME_FUNCS // time functions are implemented -- they should not be weak #define WEAK_UNLESS_TIMEFUNC_IMPL // stores the start time of the slew static uint64_t s_adjtime_start_us; // is how many microseconds total to slew static int64_t s_adjtime_total_correction_us; static _lock_t s_time_lock; // This function gradually changes boot_time to the correction value and immediately updates it. static uint64_t adjust_boot_time(void) { #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; }{...} 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), // 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. int64_t correction = (since_boot >> ADJTIME_CORRECTION_FACTOR) - (s_adjtime_start_us >> ADJTIME_CORRECTION_FACTOR); if (correction > 0) { 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; }{...} else { s_adjtime_total_correction_us += correction; boot_time -= correction; }{...} }{...} else { if ((s_adjtime_total_correction_us - correction) <= 0) { boot_time = boot_time + s_adjtime_total_correction_us; s_adjtime_start_us = 0; }{...} else { s_adjtime_total_correction_us -= correction; boot_time += correction; }{...} }{...} esp_time_impl_set_boot_time(boot_time); }{...} }{...} return boot_time; }{ ... } // Get the adjusted boot time. static uint64_t get_adjusted_boot_time(void) { _lock_acquire(&s_time_lock); uint64_t adjust_time = adjust_boot_time(); _lock_release(&s_time_lock); return adjust_time; }{ ... } // Applying the accumulated correction to base_time and stopping the smooth time adjustment. static void adjtime_corr_stop(void) { _lock_acquire(&s_time_lock); if (s_adjtime_start_us != 0) { adjust_boot_time(); s_adjtime_start_us = 0; }{...} _lock_release(&s_time_lock); }{ ... } #else/* ... */ // no time functions are actually implemented -- allow users to override them #define WEAK_UNLESS_TIMEFUNC_IMPL __attribute__((weak))/* ... */ #endif WEAK_UNLESS_TIMEFUNC_IMPL int adjtime(const struct timeval *delta, struct timeval *outdelta) { #if IMPL_NEWLIB_TIME_FUNCS if (outdelta != NULL) { _lock_acquire(&s_time_lock); adjust_boot_time(); if (s_adjtime_start_us != 0) { outdelta->tv_sec = s_adjtime_total_correction_us / 1000000L; outdelta->tv_usec = s_adjtime_total_correction_us % 1000000L; }{...} else { outdelta->tv_sec = 0; outdelta->tv_usec = 0; }{...} _lock_release(&s_time_lock); }{...} if (delta != NULL) { int64_t sec = delta->tv_sec; int64_t usec = delta->tv_usec; if (llabs(sec) > ((INT_MAX / 1000000L) - 1L)) { errno = EINVAL; 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. *//* ... */ _lock_acquire(&s_time_lock); // If correction is already in progress (s_adjtime_start_time_us != 0), then apply accumulated corrections. adjust_boot_time(); s_adjtime_start_us = esp_time_impl_get_time_since_boot(); s_adjtime_total_correction_us = sec * 1000000L + usec; _lock_release(&s_time_lock); }{...} return 0;/* ... */ #else errno = ENOSYS; return -1;/* ... */ #endif }{ ... } clock_t IRAM_ATTR _times_r(struct _reent *r, struct tms *ptms) { clock_t t = xTaskGetTickCount() * (portTICK_PERIOD_MS * CLK_TCK / 1000); ptms->tms_cstime = 0; 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; }{ ... } WEAK_UNLESS_TIMEFUNC_IMPL int IRAM_ATTR _gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz) { (void) tz; #if IMPL_NEWLIB_TIME_FUNCS if (tv) { uint64_t microseconds = get_adjusted_boot_time() + esp_time_impl_get_time_since_boot(); tv->tv_sec = microseconds / 1000000; tv->tv_usec = microseconds % 1000000; }{...} return 0;/* ... */ #else __errno_r(r) = ENOSYS; return -1;/* ... */ #endif }{ ... } WEAK_UNLESS_TIMEFUNC_IMPL int settimeofday(const struct timeval *tv, const struct timezone *tz) { (void) tz; #if IMPL_NEWLIB_TIME_FUNCS if (tv) { adjtime_corr_stop(); uint64_t now = ((uint64_t) tv->tv_sec) * 1000000LL + tv->tv_usec; uint64_t since_boot = esp_time_impl_get_time_since_boot(); esp_time_impl_set_boot_time(now - since_boot); }{...} return 0;/* ... */ #else errno = ENOSYS; return -1;/* ... */ #endif }{ ... } int usleep(useconds_t us) { /* Even at max tick rate, vTaskDelay can still delay for the max of the us argument, we just need to make sure the tick calculation does not overflow *//* ... */ const int64_t us_per_tick = portTICK_PERIOD_MS * 1000; if (us < us_per_tick) { esp_rom_delay_us((uint32_t) us); }{...} 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; }{ ... } unsigned int sleep(unsigned int seconds) { usleep(seconds * 1000000UL); return 0; }{ ... } WEAK_UNLESS_TIMEFUNC_IMPL int clock_settime(clockid_t clock_id, const struct timespec *tp) { #if IMPL_NEWLIB_TIME_FUNCS 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 }{ ... } WEAK_UNLESS_TIMEFUNC_IMPL int clock_gettime(clockid_t clock_id, struct timespec *tp) { #if IMPL_NEWLIB_TIME_FUNCS if (tp == NULL) { errno = EINVAL; return -1; }{...} struct timeval tv; uint64_t monotonic_time_us = 0; switch (clock_id) { 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;... }{...} return 0;/* ... */ #else errno = ENOSYS; return -1;/* ... */ #endif }{ ... } WEAK_UNLESS_TIMEFUNC_IMPL int clock_getres(clockid_t clock_id, struct timespec *res) { #if IMPL_NEWLIB_TIME_FUNCS if (res == NULL) { errno = EINVAL; return -1; }{...} res->tv_sec = 0; res->tv_nsec = esp_system_get_time_resolution(); return 0;/* ... */ #else errno = ENOSYS; return -1;/* ... */ #endif }{ ... } void esp_newlib_time_init(void) { esp_time_impl_init(); }{ ... }
Details