DIY Auto-Correlator 1.0
Auto-Correlator Card implementation using Teensy 4.x microcontrollers.
lifetime_timer.hpp
Go to the documentation of this file.
1#pragma once
2#include <imxrt.h>
3
10{
11
12private:
13 //static constexpr bool Singleton_flag = false; //!< Enforces a singleton template
14public:
15
16 //0 - Constructor
18 {
19 //_assert_(!Singleton_flag, "PIT_LifetimeTimer : The resource is already constructed.");
20 //const_cast<bool*>(Singleton_flag) = true;
21 }
22
23
24 //1
28 void init() __attribute__((always_inline))
29 {
30 // Enable on PIT
31 PIT_MCR = 0x00;
32
33 //Timer 1
34 PIT_LDVAL1 = 0xFFFFFFFF; // setup timer 1 for maximum counting period
35 PIT_TCTRL1 = 0x0; // disable timer 1 interrupts
36 PIT_TCTRL1 |= PIT_TCTRL_CHN; // chain timer 1 to timer 0
37
38 // Timer 0
39 PIT_LDVAL0 = 0xFFFFFFFF; // setup timer 0 for maximum counting period
40 PIT_TCTRL0 = 0x0; // disable timer 0 interrupts // Attention: This might be incorrect
41
42 asm volatile ("dsb");
43
44 }
45
46 //2
48 void start() __attribute__((always_inline))
49 {
50 PIT_TCTRL1 |= PIT_TCTRL_TEN; // start timer 1
51 PIT_TCTRL0 |= PIT_TCTRL_TEN; // start timer 0
52 }
53
54 //3
56 void stop() __attribute__((always_inline))
57 {
58 PIT_TCTRL0 &= ~PIT_TCTRL_TEN; // stop timer 0
59 PIT_TCTRL1 &= ~PIT_TCTRL_TEN; // stop timer 1
60 }
61
62 //4
64 void reset() __attribute__((always_inline))
65 {
66 this->stop();
67 PIT_LDVAL1 = 0xFFFFFFFF;
68 PIT_LDVAL0 = 0xFFFFFFFF;
69 }
70
71 //5
73 uint64_t read_val() __attribute__((always_inline))
74 {
75 //To access the lifetime, read first LTMR64H and then LTMR64L.
76 uint64_t current_uptime = (PIT_LTMR64H << 32); //Bit shift Left 32 bits
77 current_uptime = current_uptime + PIT_LTMR64L;
78 return current_uptime;
79 }
80
81 //6
83 uint32_t read_low_val() __attribute__((always_inline))
84 {
85 return PIT_LTMR64L;
86 }
87
88
89 //7
91 uint32_t read_high_val() __attribute__((always_inline))
92 {
93 return PIT_LTMR64H;
94 }
95
96
97 //8
99 uint64_t elapsed64() __attribute__((always_inline))
100 {
101 return 0xFFFFFFFFFFFFFFFF - read_val();
102 }
103
104
105 //9
108 uint32_t elapsed32() __attribute__((always_inline))
109 {
110 return 0xFFFFFFFF - PIT_LTMR64L;
111 }
112
113}; //End of class PIT_LifetimeTimer
Interface for using the "Life Time TImer" functionality of the Periodic Interrut Timer on Teensy 4....
Definition: lifetime_timer.hpp:10
uint64_t elapsed64() __attribute__((always_inline))
Returns the elapsed duration from the start call of the timer. The return is the complete 64bit value...
Definition: lifetime_timer.hpp:99
uint32_t read_high_val() __attribute__((always_inline))
Returns the higher 32-bit half of the 64-bit timing value.
Definition: lifetime_timer.hpp:91
uint64_t read_val() __attribute__((always_inline))
Returns the complete 64-bit timing value.
Definition: lifetime_timer.hpp:73
uint32_t read_low_val() __attribute__((always_inline))
Returns the lower 32-bit half of the 64-bit timing value.
Definition: lifetime_timer.hpp:83
void reset() __attribute__((always_inline))
Stops the timers and resets the timer back to the init value (max) - 0xFFFFFFFFFFFFFF.
Definition: lifetime_timer.hpp:64
void start() __attribute__((always_inline))
Starts the timers for down counting.
Definition: lifetime_timer.hpp:48
PIT_LifetimeTimer()
Definition: lifetime_timer.hpp:17
void stop() __attribute__((always_inline))
Stops the timers from counting. The timers values will freeze.
Definition: lifetime_timer.hpp:56
void init() __attribute__((always_inline))
Sets up Timer 0 and Timer 1 channels for Lifetime timing - chained configuration. This function also ...
Definition: lifetime_timer.hpp:28
uint32_t elapsed32() __attribute__((always_inline))
Returns the elapsed duration from the start call of the timer. The return is the lower 32-bit half el...
Definition: lifetime_timer.hpp:108