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 | |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 3 | #include <algorithm> |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 4 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 5 | #include <cinttypes> |
| 6 | #include <cstring> |
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 | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 12 | #include "absl/strings/numbers.h" |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 13 | #include "glog/logging.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 14 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 15 | #else // __linux__ |
| 16 | |
| 17 | #include "motors/core/kinetis.h" |
| 18 | |
| 19 | // The systick interrupt increments this every 1ms. |
| 20 | extern "C" volatile uint32_t systick_millis_count; |
| 21 | |
| 22 | #endif // __linux__ |
| 23 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 24 | namespace chrono = ::std::chrono; |
| 25 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 26 | #ifdef __linux__ |
| 27 | |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 28 | namespace std { |
| 29 | namespace this_thread { |
| 30 | template <> |
| 31 | void 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 Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 45 | PCHECK(returnval == 0 || returnval == EINTR) |
| 46 | << ": clock_nanosleep(" << static_cast<uintmax_t>(CLOCK_MONOTONIC) |
| 47 | << ", TIMER_ABSTIME, " << &end_time_timespec << ", nullptr) failed"; |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 48 | } while (returnval != 0); |
| 49 | } |
| 50 | |
| 51 | } // namespace this_thread |
| 52 | } // namespace std |
| 53 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 54 | #endif // __linux__ |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 55 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 56 | namespace aos { |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 57 | |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 58 | std::ostream &operator<<(std::ostream &stream, |
| 59 | const aos::monotonic_clock::time_point &now) { |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 60 | 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; |
| 82 | } |
| 83 | |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 84 | #ifdef __linux__ |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 85 | std::optional<monotonic_clock::time_point> monotonic_clock::FromString( |
| 86 | const std::string_view now) { |
| 87 | // This should undo the operator << above. |
| 88 | if (now.size() < 14) { |
| 89 | return std::nullopt; |
| 90 | } |
| 91 | |
| 92 | if (now.substr(now.size() - 3, now.size()) != "sec") { |
| 93 | return std::nullopt; |
| 94 | } |
| 95 | |
Brian J Griglak | 259048b | 2022-01-27 12:30:55 -0700 | [diff] [blame] | 96 | if (now[now.size() - 13] != '.') { |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 97 | return std::nullopt; |
| 98 | } |
| 99 | |
Brian J Griglak | 259048b | 2022-01-27 12:30:55 -0700 | [diff] [blame] | 100 | bool negative = now[0] == '-'; |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 101 | |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 102 | std::string_view sec( |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 103 | now.substr(negative ? 1 : 0, now.size() - (negative ? 14 : 13))); |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 104 | std::string_view nsec(now.substr(now.size() - 12, 9)); |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 105 | |
| 106 | if (!std::all_of(sec.begin(), sec.end(), ::isdigit) || |
| 107 | !std::all_of(nsec.begin(), nsec.end(), ::isdigit)) { |
| 108 | return std::nullopt; |
| 109 | } |
| 110 | |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 111 | std::chrono::seconds::rep seconds_data; |
| 112 | if (!absl::SimpleAtoi(sec, &seconds_data)) { |
| 113 | return std::nullopt; |
| 114 | } |
| 115 | |
| 116 | std::chrono::nanoseconds::rep nanoseconds_data; |
| 117 | if (!absl::SimpleAtoi(nsec, &nanoseconds_data)) { |
| 118 | return std::nullopt; |
| 119 | } |
| 120 | |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 121 | return monotonic_clock::time_point( |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 122 | std::chrono::seconds((negative ? -1 : 1) * seconds_data) + |
| 123 | std::chrono::nanoseconds((negative ? -1 : 1) * nanoseconds_data)); |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | std::optional<realtime_clock::time_point> realtime_clock::FromString( |
| 127 | const std::string_view now) { |
| 128 | // This should undo the operator << above. |
| 129 | |
| 130 | if (now.size() < 25) { |
| 131 | return std::nullopt; |
| 132 | } |
| 133 | |
Brian J Griglak | 259048b | 2022-01-27 12:30:55 -0700 | [diff] [blame] | 134 | if (now[now.size() - 10] != '.') { |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 135 | return std::nullopt; |
| 136 | } |
| 137 | |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 138 | std::string_view nsec(now.substr(now.size() - 9, 9)); |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 139 | |
| 140 | if (!std::all_of(nsec.begin(), nsec.end(), ::isdigit)) { |
| 141 | return std::nullopt; |
| 142 | } |
| 143 | |
| 144 | struct tm tm; |
| 145 | std::istringstream ss(std::string(now.substr(0, now.size() - 10))); |
| 146 | ss >> std::get_time(&tm, "%Y-%m-%d_%H-%M-%S"); |
Austin Schuh | 76a313c | 2021-11-30 19:16:35 -0800 | [diff] [blame] | 147 | if (ss.fail()) { |
| 148 | return std::nullopt; |
| 149 | } |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 150 | tm.tm_isdst = -1; |
| 151 | |
| 152 | time_t seconds = mktime(&tm); |
| 153 | |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 154 | std::chrono::nanoseconds::rep nanoseconds_data; |
| 155 | if (!absl::SimpleAtoi(nsec, &nanoseconds_data)) { |
| 156 | return std::nullopt; |
| 157 | } |
| 158 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 159 | return realtime_clock::time_point(std::chrono::seconds(seconds) + |
| 160 | std::chrono::nanoseconds(nanoseconds_data)); |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 161 | } |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 162 | #endif |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 163 | |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 164 | std::ostream &operator<<(std::ostream &stream, |
| 165 | const aos::realtime_clock::time_point &now) { |
| 166 | std::tm tm; |
| 167 | std::chrono::seconds seconds = |
| 168 | now < realtime_clock::epoch() |
Austin Schuh | b2ac056 | 2021-09-13 23:23:33 -0700 | [diff] [blame] | 169 | ? (std::chrono::duration_cast<std::chrono::seconds>( |
| 170 | now.time_since_epoch() + std::chrono::nanoseconds(1)) - |
| 171 | std::chrono::seconds(1)) |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 172 | : std::chrono::duration_cast<std::chrono::seconds>( |
Austin Schuh | e92f7e6 | 2019-12-25 13:54:41 -0800 | [diff] [blame] | 173 | now.time_since_epoch()); |
| 174 | |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 175 | std::time_t seconds_t = seconds.count(); |
| 176 | stream << std::put_time(localtime_r(&seconds_t, &tm), "%Y-%m-%d_%H-%M-%S.") |
| 177 | << std::setfill('0') << std::setw(9) |
| 178 | << std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 179 | now.time_since_epoch() - seconds) |
| 180 | .count(); |
| 181 | return stream; |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 182 | } |
| 183 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 184 | namespace time { |
| 185 | |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 186 | struct timespec to_timespec(const ::aos::monotonic_clock::duration duration) { |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 187 | struct timespec time_timespec; |
| 188 | ::std::chrono::seconds sec = |
| 189 | ::std::chrono::duration_cast<::std::chrono::seconds>(duration); |
| 190 | ::std::chrono::nanoseconds nsec = |
| 191 | ::std::chrono::duration_cast<::std::chrono::nanoseconds>(duration - sec); |
| 192 | time_timespec.tv_sec = sec.count(); |
| 193 | time_timespec.tv_nsec = nsec.count(); |
| 194 | return time_timespec; |
| 195 | } |
| 196 | |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 197 | struct timespec to_timespec(const ::aos::monotonic_clock::time_point time) { |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 198 | return to_timespec(time.time_since_epoch()); |
| 199 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 200 | |
| 201 | ::aos::monotonic_clock::time_point from_timeval(struct timeval t) { |
| 202 | return monotonic_clock::epoch() + std::chrono::seconds(t.tv_sec) + |
| 203 | std::chrono::microseconds(t.tv_usec); |
| 204 | } |
| 205 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 206 | } // namespace time |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 207 | |
| 208 | constexpr monotonic_clock::time_point monotonic_clock::min_time; |
Brian Silverman | 9435727 | 2019-02-23 21:00:54 -0800 | [diff] [blame] | 209 | constexpr monotonic_clock::time_point monotonic_clock::max_time; |
Brian Silverman | 2eb8976 | 2019-02-17 15:16:37 -0800 | [diff] [blame] | 210 | constexpr realtime_clock::time_point realtime_clock::min_time; |
Brian Silverman | 9435727 | 2019-02-23 21:00:54 -0800 | [diff] [blame] | 211 | constexpr realtime_clock::time_point realtime_clock::max_time; |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 212 | |
| 213 | monotonic_clock::time_point monotonic_clock::now() noexcept { |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 214 | #ifdef __linux__ |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 215 | struct timespec current_time; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 216 | PCHECK(clock_gettime(CLOCK_MONOTONIC, ¤t_time) == 0) |
| 217 | << ": clock_gettime(" << static_cast<uintmax_t>(CLOCK_MONOTONIC) << ", " |
| 218 | << ¤t_time << ") failed"; |
| 219 | |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 220 | return time_point(::std::chrono::seconds(current_time.tv_sec) + |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 221 | ::std::chrono::nanoseconds(current_time.tv_nsec)); |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 222 | |
| 223 | #else // __linux__ |
| 224 | |
| 225 | __disable_irq(); |
| 226 | const uint32_t current_counter = SYST_CVR; |
| 227 | uint32_t ms_count = systick_millis_count; |
| 228 | const uint32_t istatus = SCB_ICSR; |
| 229 | __enable_irq(); |
| 230 | // If the interrupt is pending and the timer has already wrapped from 0 back |
| 231 | // up to its max, then add another ms. |
| 232 | if ((istatus & SCB_ICSR_PENDSTSET) && current_counter > 50) { |
| 233 | ++ms_count; |
| 234 | } |
| 235 | |
| 236 | // It counts down, but everything we care about counts up. |
| 237 | const uint32_t counter_up = ((F_CPU / 1000) - 1) - current_counter; |
| 238 | |
| 239 | // "3.2.1.2 System Tick Timer" in the TRM says "The System Tick Timer's clock |
| 240 | // source is always the core clock, FCLK". |
| 241 | using systick_duration = |
| 242 | std::chrono::duration<uint32_t, std::ratio<1, F_CPU>>; |
| 243 | |
Milo Lin | fd40acb | 2021-11-06 14:51:36 -0700 | [diff] [blame] | 244 | return time_point(std::chrono::round<std::chrono::nanoseconds>( |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 245 | std::chrono::milliseconds(ms_count) + systick_duration(counter_up))); |
| 246 | |
| 247 | #endif // __linux__ |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 250 | #ifdef __linux__ |
| 251 | realtime_clock::time_point realtime_clock::now() noexcept { |
| 252 | struct timespec current_time; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 253 | PCHECK(clock_gettime(CLOCK_REALTIME, ¤t_time) == 0) |
| 254 | << "clock_gettime(" << static_cast<uintmax_t>(CLOCK_REALTIME) << ", " |
| 255 | << ¤t_time << ") failed"; |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 256 | |
| 257 | return time_point(::std::chrono::seconds(current_time.tv_sec) + |
| 258 | ::std::chrono::nanoseconds(current_time.tv_nsec)); |
| 259 | } |
| 260 | #endif // __linux__ |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 261 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 262 | } // namespace aos |