|
| 1 | +/////////////////////////////////////////////////////////////////////////////// |
| 2 | +// Copyright Christopher Kormanyos 2007 - 2025. |
| 3 | +// Distributed under the Boost Software License, |
| 4 | +// Version 1.0. (See accompanying file LICENSE_1_0.txt |
| 5 | +// or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +// |
| 7 | + |
| 8 | +#include <mcal_gpt.h> |
| 9 | +#include <mcal_reg.h> |
| 10 | + |
| 11 | +#include <util/utility/util_attribute.h> |
| 12 | + |
| 13 | +namespace |
| 14 | +{ |
| 15 | + // The one (and only one) system tick. |
| 16 | + volatile auto mcal_gpt_system_tick = mcal::gpt::value_type { }; |
| 17 | + |
| 18 | + auto gpt_is_initialized() -> bool& ATTRIBUTE(used, noinline); |
| 19 | + |
| 20 | + auto gpt_is_initialized() -> bool& |
| 21 | + { |
| 22 | + static auto is_init = bool { }; |
| 23 | + |
| 24 | + return is_init; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +extern "C" |
| 29 | +auto __vector_16() -> void ATTRIBUTE(signal, used, externally_visible); |
| 30 | + |
| 31 | +auto __vector_16() -> void |
| 32 | +{ |
| 33 | + // Increment the 64-bit system tick with 0x100, representing 256 [(1/2) us]. |
| 34 | + // This is basically the roll-over of the 8-bit timer0 at 2MHz each 128us. |
| 35 | + |
| 36 | + const auto new_tick = |
| 37 | + static_cast<mcal::gpt::value_type> |
| 38 | + ( |
| 39 | + mcal_gpt_system_tick + static_cast<std::uint16_t>(UINT16_C(0x100)) |
| 40 | + ); |
| 41 | + |
| 42 | + mcal_gpt_system_tick = new_tick; |
| 43 | +} |
| 44 | + |
| 45 | +auto mcal::gpt::init(const config_type*) -> void |
| 46 | +{ |
| 47 | + if(!gpt_is_initialized()) |
| 48 | + { |
| 49 | + // Clear the timer0 overflow flag. |
| 50 | + mcal::reg::reg_access_static<std::uint8_t, std::uint8_t, mcal::reg::tifr0, static_cast<std::uint8_t>(UINT8_C(0x01))>::reg_set(); |
| 51 | + |
| 52 | + // Enable the timer0 overflow interrupt. |
| 53 | + mcal::reg::reg_access_static<std::uint8_t, std::uint8_t, mcal::reg::timsk0, static_cast<std::uint8_t>(UINT8_C(0x01))>::reg_set(); |
| 54 | + |
| 55 | + // Set the timer0 clock source to f_osc/8 = 2MHz and begin counting. |
| 56 | + mcal::reg::reg_access_static<std::uint8_t, std::uint8_t, mcal::reg::tccr0b, static_cast<std::uint8_t>(UINT8_C(0x02))>::reg_set(); |
| 57 | + |
| 58 | + // Set the is-initialized indication flag. |
| 59 | + gpt_is_initialized() = true; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +auto mcal::gpt::secure::get_time_elapsed() -> mcal::gpt::value_type |
| 64 | +{ |
| 65 | + if(gpt_is_initialized()) |
| 66 | + { |
| 67 | + // Return the system tick using a multiple read to ensure data consistency. |
| 68 | + |
| 69 | + using timer_address_type = std::uint8_t; |
| 70 | + using timer_register_type = std::uint8_t; |
| 71 | + |
| 72 | + // Do the first read of the timer0 counter and the system tick. |
| 73 | + const auto t0_cnt_1 = mcal::reg::reg_access_static<timer_address_type, timer_register_type, mcal::reg::tcnt0>::reg_get(); |
| 74 | + const auto sys_tick_1 = mcal_gpt_system_tick; |
| 75 | + |
| 76 | + // Do the second read of the timer0 counter. |
| 77 | + const auto t0_cnt_2 = mcal::reg::reg_access_static<timer_address_type, timer_register_type, mcal::reg::tcnt0>::reg_get(); |
| 78 | + |
| 79 | + const auto t0_tick_is_consistent = (t0_cnt_2 >= t0_cnt_1); |
| 80 | + |
| 81 | + // Perform the consistency check. |
| 82 | + const auto consistent_half_microsecond_tick = |
| 83 | + static_cast<value_type> |
| 84 | + ( |
| 85 | + t0_tick_is_consistent ? static_cast<value_type>(sys_tick_1 | t0_cnt_1) |
| 86 | + : static_cast<value_type>(mcal_gpt_system_tick | t0_cnt_2) |
| 87 | + ); |
| 88 | + |
| 89 | + // Scale the timer0 tick to 1MHz and perform a rounding correction. |
| 90 | + return |
| 91 | + static_cast<value_type> |
| 92 | + ( |
| 93 | + static_cast<std::uint64_t> |
| 94 | + ( |
| 95 | + static_cast<std::uint64_t> |
| 96 | + ( |
| 97 | + static_cast<std::uint64_t>(consistent_half_microsecond_tick) |
| 98 | + + static_cast<std::uint8_t>(UINT8_C(1)) |
| 99 | + ) |
| 100 | + / static_cast<std::uint8_t>(UINT8_C(2)) |
| 101 | + ) |
| 102 | + ); |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + return static_cast<value_type>(UINT8_C(0)); |
| 107 | + } |
| 108 | +} |
0 commit comments