NMEA2000 Library  0.1
Library to handle NMEA 2000 Communication written in C++
N2kTimer.cpp
Go to the documentation of this file.
1/*
2 * N2kTimer.cpp
3 *
4 * Copyright (c) 2022-2024 Timo Lappalainen, Kave Oy, www.kave.fi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22*/
23
24#include "N2kTimer.h"
25
27
28// *****************************************************************************
29#if defined(ARDUINO_ARCH_ESP32) || defined(ESP_PLATFORM)
30 // N2kMillis64() and N2kMillis() as inline on header
31#elif defined(__linux__) || defined(__linux) || defined(linux)
32 #include <time.h>
33 uint64_t N2kMillis64() {
34 struct timespec ticker;
35
36 clock_gettime(CLOCK_MONOTONIC, &ticker);
37 return (((uint64_t)ticker.tv_sec * 1000) + (ticker.tv_nsec / 1000000));
38 }
39uint32_t N2kMillis() { return N2kMillis64(); }
40#else
41 #if defined(ARDUINO)
42 // N2kMillis() as inline on header
43 #else
44 extern "C" {
45 // Current uptime in milliseconds. Must be implemented by application.
46 extern uint32_t millis();
47 }
48 uint32_t N2kMillis() { return millis(); }
49 #endif
50
51 uint64_t N2kMillis64() {
52 static uint32_t RollCount=0;
53 static uint32_t LastRead=0;
54 uint32_t Now=millis();
55 if ( LastRead>Now ) RollCount++;
56 LastRead=Now;
57 return ((uint64_t)RollCount)<<32 | Now;
58 }
59#endif
60
61#if !defined(N2kUse64bitSchedulerTime)
62// *****************************************************************************
64 return N2kMillis();
65}
66#endif
uint64_t N2kMillis64()
Definition: N2kTimer.cpp:51
uint32_t N2kMillis()
Definition: N2kTimer.cpp:48
uint32_t millis()
The file contains function and classes for best timing performance.
uint32_t tN2kSchedulerTime
Definition: N2kTimer.h:272
tN2kSchedulerTime Now()
Definition: N2kTimer.cpp:63
static uint64_t SyncOffset
???
Definition: N2kTimer.h:127