GR712RC unit tests 1.0
Bare-metal C++ hardware tests for the PLATO DPU (IAA-CSIC, 2019)
Loading...
Searching...
No Matches
mcal_gpt.cpp
Go to the documentation of this file.
1
10
14
15//#include <avr/interrupt.h>
16#include <mcal_gpt.h>
17#include <mcal_reg_access.h>
18
19namespace
20{
21 volatile mcal::gpt::value_type system_tick;
22
23 bool& gpt_is_initialized()
24 {
25 static bool is_init = bool(false);
26
27 return is_init;
28 }
29}
30
32{
33 return system_tick;
34}
35
36//constexpr std::uint32_t system_tick_100ms_reload = 0x000F8FFFUL;
38{
39 if(gpt_is_initialized() == false)
40 {
41 // Set the timer reload value
42 //mcal::reg::access<std::uint32_t, std::uint32_t, mcal::reg::syst_rvr, 0x000F8FFF>::reg_set();
44
45 // Reset the current value
47
48 // Set the system timer clock source to Main Clk and f_osc/8 = 2MHz and begin counting.
50
51 // Set the is-initialized indication flag.
52 gpt_is_initialized() = true;
53 }
54}
55
56extern "C" void __sys_tick_handler() __attribute__((used, noinline));
57void __sys_tick_handler() //__attribute__((used, noinline))
58{
59 // Increment the 32-bit system tick with 0x80, representing 128 microseconds.
60 // Nuestra ISR esta saltando cada 100 ms.
61 system_tick += static_cast<std::uint8_t>(0xF0U);
62}
63
64//Provide the function get_time_elapsed used by chrono.
65mcal::gpt::value_type mcal::gpt::secure::get_time_elapsed()
66{
67 // Return the system tick using a multiple read to ensure
68 // data consistency of the high-byte of the system tick.
69
70 typedef std::uint32_t timer_address_type;
71 typedef std::uint32_t timer_register_type;
72
73 // Do the first read of the timer0 counter and the system tick.
75
76 // Read the system tick.
77 const mcal::gpt::value_type sys_tick_1 = system_tick;
78
79 // Do the second read of the timer0 counter and the system tick.
81
82 // Perform the consistency check and obtain the consistent microsecond tick.
83 const mcal::gpt::value_type consistent_microsecond_tick
84 = ((tim0_cnt_2 >= tim0_cnt_1) ? mcal::gpt::value_type(sys_tick_1 | std::uint8_t(tim0_cnt_1 >> 1U))
85 : mcal::gpt::value_type(system_tick | std::uint8_t(tim0_cnt_2 >> 1U)));
86
87 return (gpt_is_initialized() ? consistent_microsecond_tick : mcal::gpt::value_type(0U));
88
89}
90
91
void noinline
Definition mcal_gpt.cpp:56
void __sys_tick_handler() __attribute__((used
Definition mcal_gpt.cpp:57
General purpose timer abstraction layer.
void init(const config_type *)
Definition mcal_gpt.cpp:37
std::uint32_t value_type
Definition mcal_gpt.h:42
value_type get_system_time()
Definition mcal_gpt.cpp:31
void config_type
Definition mcal_gpt.h:41
static register_value_type reg_get()