DIY Auto-Correlator 1.0
Auto-Correlator Card implementation using Teensy 4.x microcontrollers.
pit.hpp
Go to the documentation of this file.
1#pragma once
2#include "errors.hpp"
3
4
6template <unsigned int ChID>
8{
9
10public:
11
12 //Period values are in microseconds (us)
13 double Actual_period = 0;
14 double Req_period = 0;
15
16 uint32_t Clk_freq = uint32_t(24 * 1e6);
17 uint32_t LoadVal = 0;
18 const static constexpr uint32_t PIT_MAX_COUNTER = 4294967295;
19
21{ static_assert(ChID <=3, "No such PIT channel exists!"); }
22
24 void static enable_PITs() __attribute__((always_inline))
25 {
26 PIT_MCR = 0x00; //Write 0 is ON
27 }
28
30 void static disable_PITs() __attribute__((always_inline))
31 {
32 PIT_MCR = 0x01; //Write 1 is OFF
33 }
34
36 void static pause_resume_PITs() __attribute__((always_inline))
37 {
38 PIT_MCR ^= PIT_MCR_FRZ;
39 }
40
41
43 void start() __attribute__((always_inline))
44 {
45 IMXRT_PIT_CHANNELS[ChID].TCTRL |= PIT_TCTRL_TEN; //Set BIT
46 }
47
49 void stop() __attribute__((always_inline))
50 {
51 IMXRT_PIT_CHANNELS[ChID].TCTRL &= ~PIT_TCTRL_TEN; //Unset BIT
52 }
53
58 Error_t set_gate_time(double gt_microseconds) __attribute__((flatten))
59 {
60 double ldval_ = gt_microseconds * this->Clk_freq * 1e-6;
61 uint32_t ldval = ceil(ldval_);
62
63 this->Req_period = gt_microseconds;
64
65 if(ldval > PIT_MAX_COUNTER)
66 {
67 this->LoadVal = PIT_MAX_COUNTER;
68 IMXRT_PIT_CHANNELS[ChID].LDVAL = LoadVal;
69 this->Actual_period = ((this->LoadVal+1) * this->tick_period_us());
71
72 }
73
74 else if(ldval < 0)
75 {
76 this->LoadVal = PIT_MAX_COUNTER;
77 IMXRT_PIT_CHANNELS[ChID].LDVAL = LoadVal;
78 this->Actual_period = ((this->LoadVal+1) * this->tick_period_us());
80 }
81
82 else
83 {
84 this->LoadVal = uint32_t((ldval)) - 1;
85 IMXRT_PIT_CHANNELS[ChID].LDVAL = LoadVal;
86 this->Actual_period = ((this->LoadVal+1) * this->tick_period_us());
87 return Error_t::Success;
88 }
89 }
90
93 double period_error_us() const __attribute__((always_inline))
94 {
95 return (this->Req_period - this->Actual_period);
96 }
97
99 double tick_period_us() __attribute__((always_inline))
100 {
101 return 1e6 / double(this->Clk_freq);
102 }
103
104
108 unsigned int static get_xbar_in_pin() __attribute__((always_inline))
109 {
110 if(ChID == 0)
111 { return 56; }
112
113 else if(ChID == 1)
114 { return 57; }
115
116 else if (ChID == 2)
117 { return 58; }
118
119 else if (ChID == 3)
120 { return 59; }
121 }
122
124 void interrupt() __attribute__((always_inline))
125 {
126 IMXRT_PIT_CHANNELS[ChID].TCTRL |= PIT_TCTRL_TIE; //Set BIT
127 }
128
130 void no_interrupt() __attribute__((always_inline))
131 {
132 IMXRT_PIT_CHANNELS[ChID].TCTRL &= ~PIT_TCTRL_TIE; //Clear BIT
133
134 }
135
137 void static set_interrupt(void (*isr_fn)(), unsigned int priority ) __attribute__((always_inline))
138 {
139 //_assert_(priority <= 255, "Priority value should be below 255.");
140 NVIC_ENABLE_IRQ(IRQ_PIT);
141 NVIC_SET_PRIORITY(IRQ_PIT, priority);
142 attachInterruptVector(IRQ_PIT, isr_fn);
143 }
144
146 void clear_interrupt_flag() __attribute__((always_inline))
147 {
148 IMXRT_PIT_CHANNELS[ChID].TFLG = 1; //Clear by writing 1 (TIF is the only field in the register)
149 }
150
151
154 void sel_FBUS_clock() __attribute__((always_inline))
155 {
156 CCM_CSCMR1 &= ~CCM_CSCMR1_PERCLK_CLK_SEL; //Clear CCM-Peripheral clock gate bit -> gate down
157 Clk_freq = F_BUS_ACTUAL; //Set Equal to Peripheral Bus frequency
158 }
159
162 void sel_24MHz_clock() __attribute__((always_inline))
163 {
164 CCM_CSCMR1 |= CCM_CSCMR1_PERCLK_CLK_SEL; //Set CCM-Peripheral clock gate bit -> gate up
165 this->Clk_freq = uint32_t(24*1e6); //24MHz
166 }
167
168
169}; //End of class PIT Controller
Interface for PIT timers on Teensy 4.x microcontrollers.
Definition: pit.hpp:8
static void pause_resume_PITs() __attribute__((always_inline))
Pause/Resume - Toggle all PIT Channels.
Definition: pit.hpp:36
double tick_period_us() __attribute__((always_inline))
Returns the value of one Tick - Minimum resolution of the timer.
Definition: pit.hpp:99
double period_error_us() const __attribute__((always_inline))
Returns the error in the gate timing period due to the finite resolution of the timers....
Definition: pit.hpp:93
void start() __attribute__((always_inline))
Start counting on the timer.
Definition: pit.hpp:43
double Actual_period
Actual Time Period set by the device due to finite resolution.
Definition: pit.hpp:13
void stop() __attribute__((always_inline))
stop counting on the timer.
Definition: pit.hpp:49
static void disable_PITs() __attribute__((always_inline))
Disable all PIT Channels.
Definition: pit.hpp:30
void sel_FBUS_clock() __attribute__((always_inline))
Sets the clock source frequency of all PIT channels to the value F_BUS_ACTUAL (= F_CPU_ACTUAL / 4 ) w...
Definition: pit.hpp:154
void no_interrupt() __attribute__((always_inline))
Disable Interrupts that are triggered after one gate time completion.
Definition: pit.hpp:130
unsigned static int get_xbar_in_pin() __attribute__((always_inline))
Returns the correspoding XBAR1-A Input Pins for the corresponding channel TRIGGER signal.
Definition: pit.hpp:108
double Req_period
Time Period requested by the user.
Definition: pit.hpp:14
static constexpr const uint32_t PIT_MAX_COUNTER
Constant - Maximum possible counter value. 32 bit counter.
Definition: pit.hpp:18
static void enable_PITs() __attribute__((always_inline))
Enable all PIT channels.
Definition: pit.hpp:24
static void set_interrupt(void(*isr_fn)(), unsigned int priority) __attribute__((always_inline))
Sets a common ISR and its priority for all PIT Channels.
Definition: pit.hpp:137
Error_t set_gate_time(double gt_microseconds) __attribute__((flatten))
Sets the gate time after which the counter returns to zero and marks the end of onegate interval.
Definition: pit.hpp:58
uint32_t LoadVal
The value loaded in to the counter during after overflow.
Definition: pit.hpp:17
PITController()
Definition: pit.hpp:20
void sel_24MHz_clock() __attribute__((always_inline))
Sets the source clock frequency of all PIT channels to 24 MHz which is the default oscillator clock....
Definition: pit.hpp:162
uint32_t Clk_freq
Clock frequency used by the timer. Initalized to 24MHz → default oscillator clock.
Definition: pit.hpp:16
void clear_interrupt_flag() __attribute__((always_inline))
Clears the interrupt flag and hence prepares the timer for the next gate interval....
Definition: pit.hpp:146
void interrupt() __attribute__((always_inline))
Enable Interruptsthat are triggered after one gate time completion.
Definition: pit.hpp:124
Error_t
Enumarates the error codes thrown by the different modules.
Definition: errors.hpp:17
@ Counter_Underflow
Passed Value caused a counter underflow.
@ Success
No error.
@ Counter_Overflow
Passed Value caused a counter overflow.
void isr_fn()
ISR function used for processing counter values.
Definition: featureline1.hpp:31