blob: 549dae780b64a1044783bbc4c61796db3fb56cf1 [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 {
56namespace time {
57
Neil Balch229001a2018-01-07 18:22:52 -080058struct timespec to_timespec(
59 const ::aos::monotonic_clock::duration duration) {
60 struct timespec time_timespec;
61 ::std::chrono::seconds sec =
62 ::std::chrono::duration_cast<::std::chrono::seconds>(duration);
63 ::std::chrono::nanoseconds nsec =
64 ::std::chrono::duration_cast<::std::chrono::nanoseconds>(duration - sec);
65 time_timespec.tv_sec = sec.count();
66 time_timespec.tv_nsec = nsec.count();
67 return time_timespec;
68}
69
70struct timespec to_timespec(
71 const ::aos::monotonic_clock::time_point time) {
72 return to_timespec(time.time_since_epoch());
73}
brians343bc112013-02-10 01:53:46 +000074} // namespace time
Austin Schuh858c0212016-11-25 17:23:30 -080075
76constexpr monotonic_clock::time_point monotonic_clock::min_time;
Brian Silverman94357272019-02-23 21:00:54 -080077constexpr monotonic_clock::time_point monotonic_clock::max_time;
Brian Silverman2eb89762019-02-17 15:16:37 -080078constexpr realtime_clock::time_point realtime_clock::min_time;
Brian Silverman94357272019-02-23 21:00:54 -080079constexpr realtime_clock::time_point realtime_clock::max_time;
Austin Schuh858c0212016-11-25 17:23:30 -080080
81monotonic_clock::time_point monotonic_clock::now() noexcept {
Brian Silvermanc03a30c2019-02-16 18:21:56 -080082#ifdef __linux__
Austin Schuh858c0212016-11-25 17:23:30 -080083 struct timespec current_time;
Austin Schuhf257f3c2019-10-27 21:00:43 -070084 PCHECK(clock_gettime(CLOCK_MONOTONIC, &current_time) == 0)
85 << ": clock_gettime(" << static_cast<uintmax_t>(CLOCK_MONOTONIC) << ", "
86 << &current_time << ") failed";
87
Austin Schuh858c0212016-11-25 17:23:30 -080088 return time_point(::std::chrono::seconds(current_time.tv_sec) +
Alex Perrycb7da4b2019-08-28 19:35:56 -070089 ::std::chrono::nanoseconds(current_time.tv_nsec));
Brian Silvermanc03a30c2019-02-16 18:21:56 -080090
91#else // __linux__
92
93 __disable_irq();
94 const uint32_t current_counter = SYST_CVR;
95 uint32_t ms_count = systick_millis_count;
96 const uint32_t istatus = SCB_ICSR;
97 __enable_irq();
98 // If the interrupt is pending and the timer has already wrapped from 0 back
99 // up to its max, then add another ms.
100 if ((istatus & SCB_ICSR_PENDSTSET) && current_counter > 50) {
101 ++ms_count;
102 }
103
104 // It counts down, but everything we care about counts up.
105 const uint32_t counter_up = ((F_CPU / 1000) - 1) - current_counter;
106
107 // "3.2.1.2 System Tick Timer" in the TRM says "The System Tick Timer's clock
108 // source is always the core clock, FCLK".
109 using systick_duration =
110 std::chrono::duration<uint32_t, std::ratio<1, F_CPU>>;
111
112 return time_point(aos::time::round<std::chrono::nanoseconds>(
113 std::chrono::milliseconds(ms_count) + systick_duration(counter_up)));
114
115#endif // __linux__
Austin Schuh858c0212016-11-25 17:23:30 -0800116}
117
Brian Silvermana3688802019-02-16 19:31:26 -0800118#ifdef __linux__
119realtime_clock::time_point realtime_clock::now() noexcept {
120 struct timespec current_time;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700121 PCHECK(clock_gettime(CLOCK_REALTIME, &current_time) == 0)
122 << "clock_gettime(" << static_cast<uintmax_t>(CLOCK_REALTIME) << ", "
123 << &current_time << ") failed";
Brian Silvermana3688802019-02-16 19:31:26 -0800124
125 return time_point(::std::chrono::seconds(current_time.tv_sec) +
126 ::std::chrono::nanoseconds(current_time.tv_nsec));
127}
128#endif // __linux__
Austin Schuh858c0212016-11-25 17:23:30 -0800129
brians343bc112013-02-10 01:53:46 +0000130} // namespace aos