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 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 12 | #include "aos/mutex/mutex.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 | e92f7e6 | 2019-12-25 13:54:41 -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; |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 82 | } |
| 83 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 84 | namespace time { |
| 85 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 86 | struct 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 | |
| 98 | struct timespec to_timespec( |
| 99 | const ::aos::monotonic_clock::time_point time) { |
| 100 | return to_timespec(time.time_since_epoch()); |
| 101 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 102 | } // namespace time |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 103 | |
| 104 | constexpr monotonic_clock::time_point monotonic_clock::min_time; |
Brian Silverman | 9435727 | 2019-02-23 21:00:54 -0800 | [diff] [blame] | 105 | constexpr monotonic_clock::time_point monotonic_clock::max_time; |
Brian Silverman | 2eb8976 | 2019-02-17 15:16:37 -0800 | [diff] [blame] | 106 | constexpr realtime_clock::time_point realtime_clock::min_time; |
Brian Silverman | 9435727 | 2019-02-23 21:00:54 -0800 | [diff] [blame] | 107 | constexpr realtime_clock::time_point realtime_clock::max_time; |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 108 | |
| 109 | monotonic_clock::time_point monotonic_clock::now() noexcept { |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 110 | #ifdef __linux__ |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 111 | struct timespec current_time; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 112 | PCHECK(clock_gettime(CLOCK_MONOTONIC, ¤t_time) == 0) |
| 113 | << ": clock_gettime(" << static_cast<uintmax_t>(CLOCK_MONOTONIC) << ", " |
| 114 | << ¤t_time << ") failed"; |
| 115 | |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 116 | return time_point(::std::chrono::seconds(current_time.tv_sec) + |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 117 | ::std::chrono::nanoseconds(current_time.tv_nsec)); |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 118 | |
| 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 Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 146 | #ifdef __linux__ |
| 147 | realtime_clock::time_point realtime_clock::now() noexcept { |
| 148 | struct timespec current_time; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 149 | PCHECK(clock_gettime(CLOCK_REALTIME, ¤t_time) == 0) |
| 150 | << "clock_gettime(" << static_cast<uintmax_t>(CLOCK_REALTIME) << ", " |
| 151 | << ¤t_time << ") failed"; |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 152 | |
| 153 | return time_point(::std::chrono::seconds(current_time.tv_sec) + |
| 154 | ::std::chrono::nanoseconds(current_time.tv_nsec)); |
| 155 | } |
| 156 | #endif // __linux__ |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 157 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 158 | } // namespace aos |