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 | |
| 6 | #include <atomic> |
| 7 | #include <chrono> |
Brian Silverman | d057569 | 2015-02-21 16:24:02 -0500 | [diff] [blame] | 8 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 9 | #ifdef __linux__ |
| 10 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 11 | #include "aos/mutex/mutex.h" |
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 { |
| 56 | namespace time { |
| 57 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 58 | struct 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 | |
| 70 | struct timespec to_timespec( |
| 71 | const ::aos::monotonic_clock::time_point time) { |
| 72 | return to_timespec(time.time_since_epoch()); |
| 73 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 74 | } // namespace time |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 75 | |
| 76 | constexpr monotonic_clock::time_point monotonic_clock::min_time; |
Brian Silverman | 9435727 | 2019-02-23 21:00:54 -0800 | [diff] [blame] | 77 | constexpr monotonic_clock::time_point monotonic_clock::max_time; |
Brian Silverman | 2eb8976 | 2019-02-17 15:16:37 -0800 | [diff] [blame] | 78 | constexpr realtime_clock::time_point realtime_clock::min_time; |
Brian Silverman | 9435727 | 2019-02-23 21:00:54 -0800 | [diff] [blame] | 79 | constexpr realtime_clock::time_point realtime_clock::max_time; |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 80 | |
| 81 | monotonic_clock::time_point monotonic_clock::now() noexcept { |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 82 | #ifdef __linux__ |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 83 | struct timespec current_time; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 84 | PCHECK(clock_gettime(CLOCK_MONOTONIC, ¤t_time) == 0) |
| 85 | << ": clock_gettime(" << static_cast<uintmax_t>(CLOCK_MONOTONIC) << ", " |
| 86 | << ¤t_time << ") failed"; |
| 87 | |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 88 | return time_point(::std::chrono::seconds(current_time.tv_sec) + |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 89 | ::std::chrono::nanoseconds(current_time.tv_nsec)); |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 90 | |
| 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 Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 118 | #ifdef __linux__ |
| 119 | realtime_clock::time_point realtime_clock::now() noexcept { |
| 120 | struct timespec current_time; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 121 | PCHECK(clock_gettime(CLOCK_REALTIME, ¤t_time) == 0) |
| 122 | << "clock_gettime(" << static_cast<uintmax_t>(CLOCK_REALTIME) << ", " |
| 123 | << ¤t_time << ") failed"; |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 124 | |
| 125 | return time_point(::std::chrono::seconds(current_time.tv_sec) + |
| 126 | ::std::chrono::nanoseconds(current_time.tv_nsec)); |
| 127 | } |
| 128 | #endif // __linux__ |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 129 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 130 | } // namespace aos |