blob: 2aa743edf41acf618c05c29722db6111da8b9e0e [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/time/time.h"
brians343bc112013-02-10 01:53:46 +00002
Brian Silvermand0575692015-02-21 16:24:02 -05003#include <inttypes.h>
Austin Schuhf2a50ba2016-12-24 16:16:26 -08004#include <string.h>
5
Austin Schuhf2a50ba2016-12-24 16:16:26 -08006#include <chrono>
James Kuszmaul38735e82019-12-07 16:42:06 -08007#include <ctime>
Austin Schuhe92f7e62019-12-25 13:54:41 -08008#include <iomanip>
Brian Silvermand0575692015-02-21 16:24:02 -05009
Brian Silvermanc03a30c2019-02-16 18:21:56 -080010#ifdef __linux__
11
John Park33858a32018-09-28 23:05:48 -070012#include "aos/mutex/mutex.h"
Austin Schuhf257f3c2019-10-27 21:00:43 -070013#include "glog/logging.h"
brians343bc112013-02-10 01:53:46 +000014
Brian Silvermanc03a30c2019-02-16 18:21:56 -080015#else // __linux__
16
17#include "motors/core/kinetis.h"
18
19// The systick interrupt increments this every 1ms.
20extern "C" volatile uint32_t systick_millis_count;
21
22#endif // __linux__
23
Austin Schuhf2a50ba2016-12-24 16:16:26 -080024namespace chrono = ::std::chrono;
25
Brian Silvermanc03a30c2019-02-16 18:21:56 -080026#ifdef __linux__
27
Austin Schuh793d6b92016-05-01 13:28:14 -070028namespace std {
29namespace this_thread {
30template <>
31void sleep_until(const ::aos::monotonic_clock::time_point &end_time) {
32 struct timespec end_time_timespec;
33 ::std::chrono::seconds sec =
34 ::std::chrono::duration_cast<::std::chrono::seconds>(
35 end_time.time_since_epoch());
36 ::std::chrono::nanoseconds nsec =
37 ::std::chrono::duration_cast<::std::chrono::nanoseconds>(
38 end_time.time_since_epoch() - sec);
39 end_time_timespec.tv_sec = sec.count();
40 end_time_timespec.tv_nsec = nsec.count();
41 int returnval;
42 do {
43 returnval = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
44 &end_time_timespec, nullptr);
Austin Schuhf257f3c2019-10-27 21:00:43 -070045 PCHECK(returnval == 0 || returnval == EINTR)
46 << ": clock_nanosleep(" << static_cast<uintmax_t>(CLOCK_MONOTONIC)
47 << ", TIMER_ABSTIME, " << &end_time_timespec << ", nullptr) failed";
Austin Schuh793d6b92016-05-01 13:28:14 -070048 } while (returnval != 0);
49}
50
51} // namespace this_thread
52} // namespace std
53
Brian Silvermanc03a30c2019-02-16 18:21:56 -080054#endif // __linux__
Austin Schuh793d6b92016-05-01 13:28:14 -070055
brians343bc112013-02-10 01:53:46 +000056namespace aos {
Austin Schuhde8a8ff2019-11-30 15:25:36 -080057
James Kuszmaul38735e82019-12-07 16:42:06 -080058std::ostream &operator<<(std::ostream &stream,
59 const aos::monotonic_clock::time_point &now) {
Austin Schuhe92f7e62019-12-25 13:54:41 -080060 if (now < monotonic_clock::epoch()) {
61 std::chrono::seconds seconds =
62 std::chrono::duration_cast<std::chrono::seconds>(
63 now.time_since_epoch());
64
65 stream << "-" << -seconds.count() << "." << std::setfill('0')
66 << std::setw(9)
67 << std::chrono::duration_cast<std::chrono::nanoseconds>(
68 seconds - now.time_since_epoch())
69 .count()
70 << "sec";
71 } else {
72 std::chrono::seconds seconds =
73 std::chrono::duration_cast<std::chrono::seconds>(
74 now.time_since_epoch());
75 stream << seconds.count() << "." << std::setfill('0') << std::setw(9)
76 << std::chrono::duration_cast<std::chrono::nanoseconds>(
77 now.time_since_epoch() - seconds)
78 .count()
79 << "sec";
80 }
81 return stream;
Austin Schuhde8a8ff2019-11-30 15:25:36 -080082}
83
brians343bc112013-02-10 01:53:46 +000084namespace time {
85
Neil Balch229001a2018-01-07 18:22:52 -080086struct timespec to_timespec(
87 const ::aos::monotonic_clock::duration duration) {
88 struct timespec time_timespec;
89 ::std::chrono::seconds sec =
90 ::std::chrono::duration_cast<::std::chrono::seconds>(duration);
91 ::std::chrono::nanoseconds nsec =
92 ::std::chrono::duration_cast<::std::chrono::nanoseconds>(duration - sec);
93 time_timespec.tv_sec = sec.count();
94 time_timespec.tv_nsec = nsec.count();
95 return time_timespec;
96}
97
98struct timespec to_timespec(
99 const ::aos::monotonic_clock::time_point time) {
100 return to_timespec(time.time_since_epoch());
101}
brians343bc112013-02-10 01:53:46 +0000102} // namespace time
Austin Schuh858c0212016-11-25 17:23:30 -0800103
104constexpr monotonic_clock::time_point monotonic_clock::min_time;
Brian Silverman94357272019-02-23 21:00:54 -0800105constexpr monotonic_clock::time_point monotonic_clock::max_time;
Brian Silverman2eb89762019-02-17 15:16:37 -0800106constexpr realtime_clock::time_point realtime_clock::min_time;
Brian Silverman94357272019-02-23 21:00:54 -0800107constexpr realtime_clock::time_point realtime_clock::max_time;
Austin Schuh858c0212016-11-25 17:23:30 -0800108
109monotonic_clock::time_point monotonic_clock::now() noexcept {
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800110#ifdef __linux__
Austin Schuh858c0212016-11-25 17:23:30 -0800111 struct timespec current_time;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700112 PCHECK(clock_gettime(CLOCK_MONOTONIC, &current_time) == 0)
113 << ": clock_gettime(" << static_cast<uintmax_t>(CLOCK_MONOTONIC) << ", "
114 << &current_time << ") failed";
115
Austin Schuh858c0212016-11-25 17:23:30 -0800116 return time_point(::std::chrono::seconds(current_time.tv_sec) +
Alex Perrycb7da4b2019-08-28 19:35:56 -0700117 ::std::chrono::nanoseconds(current_time.tv_nsec));
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800118
119#else // __linux__
120
121 __disable_irq();
122 const uint32_t current_counter = SYST_CVR;
123 uint32_t ms_count = systick_millis_count;
124 const uint32_t istatus = SCB_ICSR;
125 __enable_irq();
126 // If the interrupt is pending and the timer has already wrapped from 0 back
127 // up to its max, then add another ms.
128 if ((istatus & SCB_ICSR_PENDSTSET) && current_counter > 50) {
129 ++ms_count;
130 }
131
132 // It counts down, but everything we care about counts up.
133 const uint32_t counter_up = ((F_CPU / 1000) - 1) - current_counter;
134
135 // "3.2.1.2 System Tick Timer" in the TRM says "The System Tick Timer's clock
136 // source is always the core clock, FCLK".
137 using systick_duration =
138 std::chrono::duration<uint32_t, std::ratio<1, F_CPU>>;
139
140 return time_point(aos::time::round<std::chrono::nanoseconds>(
141 std::chrono::milliseconds(ms_count) + systick_duration(counter_up)));
142
143#endif // __linux__
Austin Schuh858c0212016-11-25 17:23:30 -0800144}
145
Brian Silvermana3688802019-02-16 19:31:26 -0800146#ifdef __linux__
147realtime_clock::time_point realtime_clock::now() noexcept {
148 struct timespec current_time;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700149 PCHECK(clock_gettime(CLOCK_REALTIME, &current_time) == 0)
150 << "clock_gettime(" << static_cast<uintmax_t>(CLOCK_REALTIME) << ", "
151 << &current_time << ") failed";
Brian Silvermana3688802019-02-16 19:31:26 -0800152
153 return time_point(::std::chrono::seconds(current_time.tv_sec) +
154 ::std::chrono::nanoseconds(current_time.tv_nsec));
155}
156#endif // __linux__
Austin Schuh858c0212016-11-25 17:23:30 -0800157
brians343bc112013-02-10 01:53:46 +0000158} // namespace aos