Ewout van Bekkum | 660ebba | 2020-11-11 13:40:10 -0800 | [diff] [blame] | 1 | // Copyright 2020 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
| 15 | #include "pw_chrono/system_clock.h" |
| 16 | |
| 17 | #include <atomic> |
| 18 | #include <chrono> |
| 19 | #include <limits> |
| 20 | #include <mutex> |
| 21 | |
| 22 | #include "FreeRTOS.h" |
| 23 | #include "pw_interrupt/context.h" |
Ewout van Bekkum | da2a62d | 2021-03-12 11:34:47 -0800 | [diff] [blame] | 24 | #include "pw_sync/interrupt_spin_lock.h" |
Ewout van Bekkum | 660ebba | 2020-11-11 13:40:10 -0800 | [diff] [blame] | 25 | #include "task.h" |
| 26 | |
| 27 | namespace pw::chrono::backend { |
| 28 | namespace { |
| 29 | |
Ewout van Bekkum | da2a62d | 2021-03-12 11:34:47 -0800 | [diff] [blame] | 30 | sync::InterruptSpinLock system_clock_interrupt_spin_lock; |
Ewout van Bekkum | 660ebba | 2020-11-11 13:40:10 -0800 | [diff] [blame] | 31 | int64_t overflow_tick_count = 0; |
| 32 | TickType_t native_tick_count = 0; |
| 33 | static_assert(!SystemClock::is_nmi_safe, |
| 34 | "global state is not atomic nor double buferred"); |
| 35 | |
| 36 | // The tick count resets to 0, ergo the overflow count is the max count + 1. |
| 37 | constexpr int64_t kNativeOverflowTickCount = |
| 38 | static_cast<int64_t>(std::numeric_limits<TickType_t>::max()) + 1; |
| 39 | |
| 40 | } // namespace |
| 41 | |
| 42 | int64_t GetSystemClockTickCount() { |
Ewout van Bekkum | da2a62d | 2021-03-12 11:34:47 -0800 | [diff] [blame] | 43 | std::lock_guard lock(system_clock_interrupt_spin_lock); |
Ewout van Bekkum | 660ebba | 2020-11-11 13:40:10 -0800 | [diff] [blame] | 44 | const TickType_t new_native_tick_count = interrupt::InInterruptContext() |
| 45 | ? xTaskGetTickCountFromISR() |
| 46 | : xTaskGetTickCount(); |
| 47 | // WARNING: This must be called more than once per overflow period! |
| 48 | if (new_native_tick_count < native_tick_count) { |
| 49 | // Native tick count overflow detected! |
| 50 | overflow_tick_count += kNativeOverflowTickCount; |
| 51 | } |
| 52 | native_tick_count = new_native_tick_count; |
| 53 | return overflow_tick_count + native_tick_count; |
| 54 | } |
| 55 | |
| 56 | } // namespace pw::chrono::backend |