blob: b05a46c83be4b87cbf611c093a19ae43918ab5b1 [file] [log] [blame]
Ewout van Bekkum660ebba2020-11-11 13:40:10 -08001// 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 Bekkumda2a62d2021-03-12 11:34:47 -080024#include "pw_sync/interrupt_spin_lock.h"
Ewout van Bekkum660ebba2020-11-11 13:40:10 -080025#include "task.h"
26
27namespace pw::chrono::backend {
28namespace {
29
Ewout van Bekkumda2a62d2021-03-12 11:34:47 -080030sync::InterruptSpinLock system_clock_interrupt_spin_lock;
Ewout van Bekkum660ebba2020-11-11 13:40:10 -080031int64_t overflow_tick_count = 0;
32TickType_t native_tick_count = 0;
33static_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.
37constexpr int64_t kNativeOverflowTickCount =
38 static_cast<int64_t>(std::numeric_limits<TickType_t>::max()) + 1;
39
40} // namespace
41
42int64_t GetSystemClockTickCount() {
Ewout van Bekkumda2a62d2021-03-12 11:34:47 -080043 std::lock_guard lock(system_clock_interrupt_spin_lock);
Ewout van Bekkum660ebba2020-11-11 13:40:10 -080044 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