1 | /* |
---|
2 | * SPDX-License-Identifier: BSD-2-Clause |
---|
3 | * |
---|
4 | * Copyright (C) 2018 embedded brains GmbH |
---|
5 | * |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | */ |
---|
27 | |
---|
28 | #undef __STRICT_ANSI__ |
---|
29 | |
---|
30 | #include <t.h> |
---|
31 | |
---|
32 | #include <inttypes.h> |
---|
33 | #include <stdatomic.h> |
---|
34 | #include <stdio.h> |
---|
35 | #include <time.h> |
---|
36 | |
---|
37 | #ifdef __rtems__ |
---|
38 | #include <rtems/counter.h> |
---|
39 | #include <rtems/score/timecounter.h> |
---|
40 | #endif |
---|
41 | |
---|
42 | #ifdef __rtems__ |
---|
43 | static T_time |
---|
44 | round_sbt(T_time time) |
---|
45 | { |
---|
46 | /* |
---|
47 | * One 1ns consists of 4.30 fractions of 1/2**32. Round up close to |
---|
48 | * the middle. This turns the conversion mapping of struct timespec to |
---|
49 | * sbintime_t and back into the identity function. |
---|
50 | */ |
---|
51 | return time + 2; |
---|
52 | } |
---|
53 | #endif |
---|
54 | |
---|
55 | const char * |
---|
56 | T_time_to_string_ns(T_time time, T_time_string string) |
---|
57 | { |
---|
58 | uint32_t s; |
---|
59 | uint32_t f; |
---|
60 | |
---|
61 | #ifdef __rtems__ |
---|
62 | time = round_sbt(time); |
---|
63 | s = (uint32_t)(time >> 32); |
---|
64 | f = (uint32_t)(((uint64_t)1000000000 * (uint32_t)time) >> 32); |
---|
65 | #else |
---|
66 | s = (uint32_t)(time / 1000000000); |
---|
67 | f = (uint32_t)(time % 1000000000); |
---|
68 | #endif |
---|
69 | |
---|
70 | (void)T_snprintf(string, sizeof(T_time_string), |
---|
71 | "%" PRIu32 ".%09" PRIu32, s, f); |
---|
72 | return string; |
---|
73 | } |
---|
74 | |
---|
75 | const char * |
---|
76 | T_time_to_string_us(T_time time, T_time_string string) |
---|
77 | { |
---|
78 | uint32_t s; |
---|
79 | uint32_t f; |
---|
80 | |
---|
81 | #ifdef __rtems__ |
---|
82 | time = round_sbt(time); |
---|
83 | s = (uint32_t)(time >> 32); |
---|
84 | f = (uint32_t)(((uint64_t)1000000 * (uint32_t)time) >> 32); |
---|
85 | #else |
---|
86 | time /= 1000; |
---|
87 | s = (uint32_t)(time / 1000000); |
---|
88 | f = (uint32_t)(time % 1000000); |
---|
89 | #endif |
---|
90 | |
---|
91 | (void)T_snprintf(string, sizeof(T_time_string), |
---|
92 | "%" PRIu32 ".%06" PRIu32, s, f); |
---|
93 | return string; |
---|
94 | } |
---|
95 | |
---|
96 | const char * |
---|
97 | T_time_to_string_ms(T_time time, T_time_string string) |
---|
98 | { |
---|
99 | uint32_t s; |
---|
100 | uint32_t f; |
---|
101 | |
---|
102 | #ifdef __rtems__ |
---|
103 | time = round_sbt(time); |
---|
104 | s = (uint32_t)(time >> 32); |
---|
105 | f = (uint32_t)(((uint64_t)1000 * (uint32_t)time) >> 32); |
---|
106 | #else |
---|
107 | time /= 1000000; |
---|
108 | s = (uint32_t)(time / 1000); |
---|
109 | f = (uint32_t)(time % 1000); |
---|
110 | #endif |
---|
111 | |
---|
112 | (void)T_snprintf(string, sizeof(T_time_string), |
---|
113 | "%" PRIu32 ".%03" PRIu32, s, f); |
---|
114 | return string; |
---|
115 | } |
---|
116 | |
---|
117 | const char * |
---|
118 | T_time_to_string_s(T_time time, T_time_string string) |
---|
119 | { |
---|
120 | uint32_t s; |
---|
121 | |
---|
122 | #ifdef __rtems__ |
---|
123 | time = round_sbt(time); |
---|
124 | s = (uint32_t)(time >> 32); |
---|
125 | #else |
---|
126 | s = (uint32_t)(time / 1000000000); |
---|
127 | #endif |
---|
128 | |
---|
129 | (void)T_snprintf(string, sizeof(T_time_string), "%" PRIu32, s); |
---|
130 | return string; |
---|
131 | } |
---|
132 | |
---|
133 | const char * |
---|
134 | T_ticks_to_string_ns(T_ticks ticks, T_time_string string) |
---|
135 | { |
---|
136 | return T_time_to_string_ns(T_ticks_to_time(ticks), string); |
---|
137 | } |
---|
138 | |
---|
139 | const char * |
---|
140 | T_ticks_to_string_us(T_ticks ticks, T_time_string string) |
---|
141 | { |
---|
142 | return T_time_to_string_us(T_ticks_to_time(ticks), string); |
---|
143 | } |
---|
144 | |
---|
145 | const char * |
---|
146 | T_ticks_to_string_ms(T_ticks ticks, T_time_string string) |
---|
147 | { |
---|
148 | return T_time_to_string_ms(T_ticks_to_time(ticks), string); |
---|
149 | } |
---|
150 | |
---|
151 | const char * |
---|
152 | T_ticks_to_string_s(T_ticks ticks, T_time_string string) |
---|
153 | { |
---|
154 | return T_time_to_string_s(T_ticks_to_time(ticks), string); |
---|
155 | } |
---|
156 | |
---|
157 | uint64_t |
---|
158 | T_ticks_to_time(T_ticks ticks) |
---|
159 | { |
---|
160 | #ifdef __rtems__ |
---|
161 | return (uint64_t)rtems_counter_ticks_to_sbintime(ticks); |
---|
162 | #else |
---|
163 | return ticks; |
---|
164 | #endif |
---|
165 | } |
---|
166 | |
---|
167 | T_ticks |
---|
168 | T_time_to_ticks(T_time time) |
---|
169 | { |
---|
170 | #ifdef __rtems__ |
---|
171 | return rtems_counter_sbintime_to_ticks((sbintime_t)time); |
---|
172 | #else |
---|
173 | return time; |
---|
174 | #endif |
---|
175 | } |
---|
176 | |
---|
177 | T_time |
---|
178 | T_seconds_and_nanoseconds_to_time(uint32_t s, uint32_t ns) |
---|
179 | { |
---|
180 | #ifdef __rtems__ |
---|
181 | struct timespec ts; |
---|
182 | |
---|
183 | ts.tv_sec = s; |
---|
184 | ts.tv_nsec = (long)ns; |
---|
185 | return (T_time)tstosbt(ts); |
---|
186 | #else |
---|
187 | return (T_time)s * (T_time)1000000000 + (T_time)ns; |
---|
188 | #endif |
---|
189 | } |
---|
190 | |
---|
191 | void |
---|
192 | T_time_to_seconds_and_nanoseconds(T_time time, uint32_t *s, uint32_t *ns) |
---|
193 | { |
---|
194 | #ifdef __rtems__ |
---|
195 | time = round_sbt(time); |
---|
196 | *s = (uint32_t)(time >> 32); |
---|
197 | *ns = (uint32_t)(((uint64_t)1000000000 * (uint32_t)time) >> 32); |
---|
198 | #else |
---|
199 | *s = (uint32_t)(time / 1000000000); |
---|
200 | *ns = (uint32_t)(time % 1000000000); |
---|
201 | #endif |
---|
202 | } |
---|
203 | |
---|
204 | #ifndef __rtems__ |
---|
205 | T_time |
---|
206 | T_now(void) |
---|
207 | { |
---|
208 | struct timespec tp; |
---|
209 | |
---|
210 | (void)clock_gettime(CLOCK_MONOTONIC, &tp); |
---|
211 | return (T_time)tp.tv_sec * (T_time)1000000000 + (T_time)tp.tv_nsec; |
---|
212 | } |
---|
213 | |
---|
214 | T_ticks |
---|
215 | T_tick(void) |
---|
216 | { |
---|
217 | return T_now(); |
---|
218 | } |
---|
219 | #endif |
---|
220 | |
---|
221 | static atomic_uint dummy_time; |
---|
222 | |
---|
223 | T_time |
---|
224 | T_now_dummy(void) |
---|
225 | { |
---|
226 | return atomic_fetch_add_explicit(&dummy_time, 1, memory_order_relaxed); |
---|
227 | } |
---|
228 | |
---|
229 | T_time |
---|
230 | T_now_via_tick(void) |
---|
231 | { |
---|
232 | return T_ticks_to_time(T_tick()); |
---|
233 | } |
---|