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" 24 #include "pw_sync/interrupt_spin_lock.h" 25 #include "task.h" 26 27 namespace pw::chrono::backend { 28 namespace { 29 30 sync::InterruptSpinLock system_clock_interrupt_spin_lock; 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 GetSystemClockTickCount()42int64_t GetSystemClockTickCount() { 43 std::lock_guard lock(system_clock_interrupt_spin_lock); 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 57