|
5 | 5 | // or copy at http://www.boost.org/LICENSE_1_0.txt) |
6 | 6 | // |
7 | 7 |
|
8 | | -#include "BL602.h" |
9 | | - |
10 | 8 | #include <mcal_gpt.h> |
11 | 9 |
|
12 | | -//----------------------------------------------------------------------------------------- |
13 | | -// Defines |
14 | | -//----------------------------------------------------------------------------------------- |
15 | | -namespace |
| 10 | +#include "BL602.h" |
| 11 | + |
| 12 | +namespace local |
16 | 13 | { |
17 | 14 | constexpr std::uint64_t TIMEOUT_1S { UINT64_C(10000000) }; |
18 | 15 |
|
19 | | - // The one (and only one) system tick. |
20 | | - volatile auto mcal_gpt_system_tick = mcal::gpt::value_type { }; |
21 | | - |
22 | 16 | auto gpt_is_initialized() -> bool& __attribute__((used, noinline)); |
23 | 17 |
|
| 18 | + auto get_consistent_tick |
| 19 | + { |
| 20 | + []() |
| 21 | + { |
| 22 | + // Return the system tick using a multiple read to ensure data consistency. |
| 23 | + |
| 24 | + const volatile std::uint64_t time_first { CLIC_MTIME }; |
| 25 | + const volatile std::uint64_t time_second { CLIC_MTIME }; |
| 26 | + |
| 27 | + const bool |
| 28 | + is_steady |
| 29 | + { |
| 30 | + (static_cast<std::uint32_t>(time_second) >= static_cast<std::uint32_t>(time_first)) |
| 31 | + }; |
| 32 | + |
| 33 | + return |
| 34 | + static_cast<mcal::gpt::value_type> |
| 35 | + ( |
| 36 | + is_steady ? time_first : CLIC_MTIME |
| 37 | + ); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
24 | 41 | auto gpt_is_initialized() -> bool& |
25 | 42 | { |
26 | | - static auto is_init = bool { }; |
| 43 | + static bool is_init { }; |
27 | 44 |
|
28 | 45 | return is_init; |
29 | 46 | } |
30 | | -} |
31 | | - |
32 | | -extern "C" void Isr_TIMER_Interrupt (void) __attribute__((interrupt,used,noinline)); |
33 | | - |
34 | | -extern "C" |
35 | | -void Isr_TIMER_Interrupt (void) |
36 | | -{ |
37 | | - // Set the next timer timeout timeout. |
38 | | - |
39 | | - CLIC_MTIMECMP = (uint64_t)(CLIC_MTIME + TIMEOUT_1S); |
40 | | -} |
| 47 | +} // namespace local |
41 | 48 |
|
42 | 49 | extern "C" auto mcal_gpt_init() -> void; |
43 | 50 |
|
44 | 51 | extern "C" auto mcal_gpt_init() -> void |
45 | 52 | { |
46 | | - /* enabled selective hardware vectoring */ |
47 | | - CLIC_CFG |= 1ul; |
48 | | - |
49 | | - /* enable timer interrupt in CLIC vectored mode */ |
50 | | - CLIC_INTIE[7] = 1u; |
51 | 53 | } |
52 | 54 |
|
53 | 55 | auto mcal::gpt::init(const config_type*) -> void { } |
54 | 56 |
|
55 | 57 | auto mcal::gpt::secure::get_time_elapsed() -> mcal::gpt::value_type |
56 | 58 | { |
57 | | - if(!gpt_is_initialized()) |
| 59 | + if(!local::gpt_is_initialized()) |
58 | 60 | { |
59 | | - // Set the next timer interrupt timeout. |
60 | | - CLIC_MTIMECMP = (uint64_t)(CLIC_MTIME + TIMEOUT_1S); |
61 | | - |
62 | 61 | // Set the is-initialized indication flag. |
63 | | - gpt_is_initialized() = true; |
| 62 | + local::gpt_is_initialized() = true; |
64 | 63 | } |
65 | 64 |
|
66 | | - // Return the system tick using a multiple read to ensure data consistency. |
67 | | - |
68 | | - const volatile uint64_t time_first = CLIC_MTIME; |
69 | | - const volatile uint64_t time_second = CLIC_MTIME; |
70 | | - |
71 | | - const volatile bool is_steady = (((uint32_t) time_second > (uint32_t) time_first) ? true : false); |
72 | | - |
73 | | - value_type consistent_microsecond_tick = (is_steady ? time_first : CLIC_MTIME); |
74 | | - |
75 | | - consistent_microsecond_tick /= 10U; |
76 | | - |
77 | | - return consistent_microsecond_tick; |
| 65 | + // Convert the consistent tick to microseconds. |
| 66 | + return |
| 67 | + static_cast<mcal::gpt::value_type> |
| 68 | + ( |
| 69 | + static_cast<mcal::gpt::value_type> |
| 70 | + ( |
| 71 | + static_cast<mcal::gpt::value_type>(local::get_consistent_tick() + UINT32_C(5)) |
| 72 | + / UINT32_C(10) |
| 73 | + ) |
| 74 | + ); |
78 | 75 | } |
0 commit comments