blob: 95044a43b3d79b30543d5f6707bf1532c61cf1e6 [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
6#include <atomic>
7#include <chrono>
Brian Silvermand0575692015-02-21 16:24:02 -05008
Brian Silvermanc03a30c2019-02-16 18:21:56 -08009#ifdef __linux__
10
John Park33858a32018-09-28 23:05:48 -070011#include "aos/mutex/mutex.h"
Austin Schuhf257f3c2019-10-27 21:00:43 -070012#include "glog/logging.h"
brians343bc112013-02-10 01:53:46 +000013
Brian Silvermanc03a30c2019-02-16 18:21:56 -080014#else // __linux__
15
16#include "motors/core/kinetis.h"
17
18// The systick interrupt increments this every 1ms.
19extern "C" volatile uint32_t systick_millis_count;
20
21#endif // __linux__
22
Austin Schuhf2a50ba2016-12-24 16:16:26 -080023namespace chrono = ::std::chrono;
24
Brian Silvermanc03a30c2019-02-16 18:21:56 -080025#ifdef __linux__
26
Austin Schuh793d6b92016-05-01 13:28:14 -070027namespace std {
28namespace this_thread {
29template <>
30void sleep_until(const ::aos::monotonic_clock::time_point &end_time) {
31 struct timespec end_time_timespec;
32 ::std::chrono::seconds sec =
33 ::std::chrono::duration_cast<::std::chrono::seconds>(
34 end_time.time_since_epoch());
35 ::std::chrono::nanoseconds nsec =
36 ::std::chrono::duration_cast<::std::chrono::nanoseconds>(
37 end_time.time_since_epoch() - sec);
38 end_time_timespec.tv_sec = sec.count();
39 end_time_timespec.tv_nsec = nsec.count();
40 int returnval;
41 do {
42 returnval = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
43 &end_time_timespec, nullptr);
Austin Schuhf257f3c2019-10-27 21:00:43 -070044 PCHECK(returnval == 0 || returnval == EINTR)
45 << ": clock_nanosleep(" << static_cast<uintmax_t>(CLOCK_MONOTONIC)
46 << ", TIMER_ABSTIME, " << &end_time_timespec << ", nullptr) failed";
Austin Schuh793d6b92016-05-01 13:28:14 -070047 } while (returnval != 0);
48}
49
50} // namespace this_thread
51} // namespace std
52
Brian Silvermanc03a30c2019-02-16 18:21:56 -080053#endif // __linux__
Austin Schuh793d6b92016-05-01 13:28:14 -070054
brians343bc112013-02-10 01:53:46 +000055namespace aos {
Austin Schuhde8a8ff2019-11-30 15:25:36 -080056
57void PrintTo(const monotonic_clock::time_point t, std::ostream *os) {
58 auto s = std::chrono::duration_cast<std::chrono::seconds>(t.time_since_epoch());
59 *os << s.count() << "."
60 << std::chrono::duration_cast<std::chrono::nanoseconds>(
61 t.time_since_epoch() - s)
62 .count()
63 << "sec";
64}
65
brians343bc112013-02-10 01:53:46 +000066namespace time {
67
Neil Balch229001a2018-01-07 18:22:52 -080068struct timespec to_timespec(
69 const ::aos::monotonic_clock::duration duration) {
70 struct timespec time_timespec;
71 ::std::chrono::seconds sec =
72 ::std::chrono::duration_cast<::std::chrono::seconds>(duration);
73 ::std::chrono::nanoseconds nsec =
74 ::std::chrono::duration_cast<::std::chrono::nanoseconds>(duration - sec);
75 time_timespec.tv_sec = sec.count();
76 time_timespec.tv_nsec = nsec.count();
77 return time_timespec;
78}
79
80struct timespec to_timespec(
81 const ::aos::monotonic_clock::time_point time) {
82 return to_timespec(time.time_since_epoch());
83}
brians343bc112013-02-10 01:53:46 +000084} // namespace time
Austin Schuh858c0212016-11-25 17:23:30 -080085
86constexpr monotonic_clock::time_point monotonic_clock::min_time;
Brian Silverman94357272019-02-23 21:00:54 -080087constexpr monotonic_clock::time_point monotonic_clock::max_time;
Brian Silverman2eb89762019-02-17 15:16:37 -080088constexpr realtime_clock::time_point realtime_clock::min_time;
Brian Silverman94357272019-02-23 21:00:54 -080089constexpr realtime_clock::time_point realtime_clock::max_time;
Austin Schuh858c0212016-11-25 17:23:30 -080090
91monotonic_clock::time_point monotonic_clock::now() noexcept {
Brian Silvermanc03a30c2019-02-16 18:21:56 -080092#ifdef __linux__
Austin Schuh858c0212016-11-25 17:23:30 -080093 struct timespec current_time;
Austin Schuhf257f3c2019-10-27 21:00:43 -070094 PCHECK(clock_gettime(CLOCK_MONOTONIC, &current_time) == 0)
95 << ": clock_gettime(" << static_cast<uintmax_t>(CLOCK_MONOTONIC) << ", "
96 << &current_time << ") failed";
97
Austin Schuh858c0212016-11-25 17:23:30 -080098 return time_point(::std::chrono::seconds(current_time.tv_sec) +
Alex Perrycb7da4b2019-08-28 19:35:56 -070099 ::std::chrono::nanoseconds(current_time.tv_nsec));
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800100
101#else // __linux__
102
103 __disable_irq();
104 const uint32_t current_counter = SYST_CVR;
105 uint32_t ms_count = systick_millis_count;
106 const uint32_t istatus = SCB_ICSR;
107 __enable_irq();
108 // If the interrupt is pending and the timer has already wrapped from 0 back
109 // up to its max, then add another ms.
110 if ((istatus & SCB_ICSR_PENDSTSET) && current_counter > 50) {
111 ++ms_count;
112 }
113
114 // It counts down, but everything we care about counts up.
115 const uint32_t counter_up = ((F_CPU / 1000) - 1) - current_counter;
116
117 // "3.2.1.2 System Tick Timer" in the TRM says "The System Tick Timer's clock
118 // source is always the core clock, FCLK".
119 using systick_duration =
120 std::chrono::duration<uint32_t, std::ratio<1, F_CPU>>;
121
122 return time_point(aos::time::round<std::chrono::nanoseconds>(
123 std::chrono::milliseconds(ms_count) + systick_duration(counter_up)));
124
125#endif // __linux__
Austin Schuh858c0212016-11-25 17:23:30 -0800126}
127
Brian Silvermana3688802019-02-16 19:31:26 -0800128#ifdef __linux__
129realtime_clock::time_point realtime_clock::now() noexcept {
130 struct timespec current_time;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700131 PCHECK(clock_gettime(CLOCK_REALTIME, &current_time) == 0)
132 << "clock_gettime(" << static_cast<uintmax_t>(CLOCK_REALTIME) << ", "
133 << &current_time << ") failed";
Brian Silvermana3688802019-02-16 19:31:26 -0800134
135 return time_point(::std::chrono::seconds(current_time.tv_sec) +
136 ::std::chrono::nanoseconds(current_time.tv_nsec));
137}
138#endif // __linux__
Austin Schuh858c0212016-11-25 17:23:30 -0800139
brians343bc112013-02-10 01:53:46 +0000140} // namespace aos