John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/time/time.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 2 | |
Brian Silverman | d057569 | 2015-02-21 16:24:02 -0500 | [diff] [blame] | 3 | #include <inttypes.h> |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 4 | #include <string.h> |
| 5 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 6 | #include <chrono> |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 7 | #include <ctime> |
Austin Schuh | e92f7e6 | 2019-12-25 13:54:41 -0800 | [diff] [blame] | 8 | #include <iomanip> |
Brian Silverman | d057569 | 2015-02-21 16:24:02 -0500 | [diff] [blame] | 9 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 10 | #ifdef __linux__ |
| 11 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 12 | #include "glog/logging.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 13 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 14 | #else // __linux__ |
| 15 | |
| 16 | #include "motors/core/kinetis.h" |
| 17 | |
| 18 | // The systick interrupt increments this every 1ms. |
| 19 | extern "C" volatile uint32_t systick_millis_count; |
| 20 | |
| 21 | #endif // __linux__ |
| 22 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 23 | namespace chrono = ::std::chrono; |
| 24 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 25 | #ifdef __linux__ |
| 26 | |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 27 | namespace std { |
| 28 | namespace this_thread { |
| 29 | template <> |
| 30 | void 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 Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 44 | PCHECK(returnval == 0 || returnval == EINTR) |
| 45 | << ": clock_nanosleep(" << static_cast<uintmax_t>(CLOCK_MONOTONIC) |
| 46 | << ", TIMER_ABSTIME, " << &end_time_timespec << ", nullptr) failed"; |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 47 | } while (returnval != 0); |
| 48 | } |
| 49 | |
| 50 | } // namespace this_thread |
| 51 | } // namespace std |
| 52 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 53 | #endif // __linux__ |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 54 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 55 | namespace aos { |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 56 | |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 57 | std::ostream &operator<<(std::ostream &stream, |
| 58 | const aos::monotonic_clock::time_point &now) { |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 59 | if (now < monotonic_clock::epoch()) { |
| 60 | std::chrono::seconds seconds = |
| 61 | std::chrono::duration_cast<std::chrono::seconds>( |
| 62 | now.time_since_epoch()); |
| 63 | |
| 64 | stream << "-" << -seconds.count() << "." << std::setfill('0') |
| 65 | << std::setw(9) |
| 66 | << std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 67 | seconds - now.time_since_epoch()) |
| 68 | .count() |
| 69 | << "sec"; |
| 70 | } else { |
| 71 | std::chrono::seconds seconds = |
| 72 | std::chrono::duration_cast<std::chrono::seconds>( |
| 73 | now.time_since_epoch()); |
| 74 | stream << seconds.count() << "." << std::setfill('0') << std::setw(9) |
| 75 | << std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 76 | now.time_since_epoch() - seconds) |
| 77 | .count() |
| 78 | << "sec"; |
| 79 | } |
| 80 | return stream; |
| 81 | } |
| 82 | |
| 83 | std::ostream &operator<<(std::ostream &stream, |
| 84 | const aos::realtime_clock::time_point &now) { |
| 85 | std::tm tm; |
| 86 | std::chrono::seconds seconds = |
| 87 | now < realtime_clock::epoch() |
| 88 | ? std::chrono::duration_cast<std::chrono::seconds>( |
| 89 | now.time_since_epoch() - std::chrono::nanoseconds(999999999)) |
| 90 | : std::chrono::duration_cast<std::chrono::seconds>( |
Austin Schuh | e92f7e6 | 2019-12-25 13:54:41 -0800 | [diff] [blame] | 91 | now.time_since_epoch()); |
| 92 | |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 93 | std::time_t seconds_t = seconds.count(); |
| 94 | stream << std::put_time(localtime_r(&seconds_t, &tm), "%Y-%m-%d_%H-%M-%S.") |
| 95 | << std::setfill('0') << std::setw(9) |
| 96 | << std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 97 | now.time_since_epoch() - seconds) |
| 98 | .count(); |
| 99 | return stream; |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 100 | } |
| 101 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 102 | namespace time { |
| 103 | |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 104 | struct timespec to_timespec(const ::aos::monotonic_clock::duration duration) { |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 105 | struct timespec time_timespec; |
| 106 | ::std::chrono::seconds sec = |
| 107 | ::std::chrono::duration_cast<::std::chrono::seconds>(duration); |
| 108 | ::std::chrono::nanoseconds nsec = |
| 109 | ::std::chrono::duration_cast<::std::chrono::nanoseconds>(duration - sec); |
| 110 | time_timespec.tv_sec = sec.count(); |
| 111 | time_timespec.tv_nsec = nsec.count(); |
| 112 | return time_timespec; |
| 113 | } |
| 114 | |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 115 | struct timespec to_timespec(const ::aos::monotonic_clock::time_point time) { |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 116 | return to_timespec(time.time_since_epoch()); |
| 117 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 118 | |
| 119 | ::aos::monotonic_clock::time_point from_timeval(struct timeval t) { |
| 120 | return monotonic_clock::epoch() + std::chrono::seconds(t.tv_sec) + |
| 121 | std::chrono::microseconds(t.tv_usec); |
| 122 | } |
| 123 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 124 | } // namespace time |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 125 | |
| 126 | constexpr monotonic_clock::time_point monotonic_clock::min_time; |
Brian Silverman | 9435727 | 2019-02-23 21:00:54 -0800 | [diff] [blame] | 127 | constexpr monotonic_clock::time_point monotonic_clock::max_time; |
Brian Silverman | 2eb8976 | 2019-02-17 15:16:37 -0800 | [diff] [blame] | 128 | constexpr realtime_clock::time_point realtime_clock::min_time; |
Brian Silverman | 9435727 | 2019-02-23 21:00:54 -0800 | [diff] [blame] | 129 | constexpr realtime_clock::time_point realtime_clock::max_time; |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 130 | |
| 131 | monotonic_clock::time_point monotonic_clock::now() noexcept { |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 132 | #ifdef __linux__ |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 133 | struct timespec current_time; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 134 | PCHECK(clock_gettime(CLOCK_MONOTONIC, ¤t_time) == 0) |
| 135 | << ": clock_gettime(" << static_cast<uintmax_t>(CLOCK_MONOTONIC) << ", " |
| 136 | << ¤t_time << ") failed"; |
| 137 | |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 138 | return time_point(::std::chrono::seconds(current_time.tv_sec) + |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 139 | ::std::chrono::nanoseconds(current_time.tv_nsec)); |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 140 | |
| 141 | #else // __linux__ |
| 142 | |
| 143 | __disable_irq(); |
| 144 | const uint32_t current_counter = SYST_CVR; |
| 145 | uint32_t ms_count = systick_millis_count; |
| 146 | const uint32_t istatus = SCB_ICSR; |
| 147 | __enable_irq(); |
| 148 | // If the interrupt is pending and the timer has already wrapped from 0 back |
| 149 | // up to its max, then add another ms. |
| 150 | if ((istatus & SCB_ICSR_PENDSTSET) && current_counter > 50) { |
| 151 | ++ms_count; |
| 152 | } |
| 153 | |
| 154 | // It counts down, but everything we care about counts up. |
| 155 | const uint32_t counter_up = ((F_CPU / 1000) - 1) - current_counter; |
| 156 | |
| 157 | // "3.2.1.2 System Tick Timer" in the TRM says "The System Tick Timer's clock |
| 158 | // source is always the core clock, FCLK". |
| 159 | using systick_duration = |
| 160 | std::chrono::duration<uint32_t, std::ratio<1, F_CPU>>; |
| 161 | |
| 162 | return time_point(aos::time::round<std::chrono::nanoseconds>( |
| 163 | std::chrono::milliseconds(ms_count) + systick_duration(counter_up))); |
| 164 | |
| 165 | #endif // __linux__ |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 168 | #ifdef __linux__ |
| 169 | realtime_clock::time_point realtime_clock::now() noexcept { |
| 170 | struct timespec current_time; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 171 | PCHECK(clock_gettime(CLOCK_REALTIME, ¤t_time) == 0) |
| 172 | << "clock_gettime(" << static_cast<uintmax_t>(CLOCK_REALTIME) << ", " |
| 173 | << ¤t_time << ") failed"; |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 174 | |
| 175 | return time_point(::std::chrono::seconds(current_time.tv_sec) + |
| 176 | ::std::chrono::nanoseconds(current_time.tv_nsec)); |
| 177 | } |
| 178 | #endif // __linux__ |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 179 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 180 | } // namespace aos |