diff --git a/hal/include/HAL/cpp/fpga_clock.h b/hal/include/HAL/cpp/fpga_clock.h new file mode 100644 index 0000000000..d89638c457 --- /dev/null +++ b/hal/include/HAL/cpp/fpga_clock.h @@ -0,0 +1,32 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2017. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#pragma once + +#include +#include + +namespace hal { + +class fpga_clock { + public: + typedef std::chrono::microseconds::rep rep; + typedef std::chrono::microseconds::period period; + typedef std::chrono::microseconds duration; + typedef std::chrono::time_point time_point; + + static fpga_clock::time_point now() noexcept; + static constexpr bool is_steady = true; + + static constexpr fpga_clock::time_point epoch() { return time_point(zero()); } + + static constexpr fpga_clock::duration zero() { return duration(0); } + + static constexpr time_point min_time{ + time_point(duration(std::numeric_limits::min()))}; +}; +} // namespace hal diff --git a/hal/lib/shared/cpp/fpga_clock.cpp b/hal/lib/shared/cpp/fpga_clock.cpp new file mode 100644 index 0000000000..35d891ddda --- /dev/null +++ b/hal/lib/shared/cpp/fpga_clock.cpp @@ -0,0 +1,21 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2017. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include "HAL/cpp/fpga_clock.h" + +#include "HAL/HAL.h" + +namespace hal { +constexpr fpga_clock::time_point fpga_clock::min_time; + +fpga_clock::time_point fpga_clock::now() noexcept { + int32_t status = 0; + uint64_t currentTime = HAL_GetFPGATime(&status); + if (status != 0) return epoch(); + return time_point(std::chrono::microseconds(currentTime)); +} +} // namespace hal