DIY Auto-Correlator 1.0
Auto-Correlator Card implementation using Teensy 4.x microcontrollers.
featureline3.hpp
Go to the documentation of this file.
1#pragma once
2#include <Arduino.h>
3#include <imxrt.h>
4
5#include "global.hpp"
6
7
8#include "./../code/hardware/pins.hpp"
9#include "./../code/hardware/ledpanel.hpp"
10#include "./../code/hardware/errors.hpp"
11#include "./../code/hardware/pit.hpp"
12#include "./../code/hardware/qtmr1.hpp"
13
14#include "./../code/software/monitor_channel.hpp"
15
16
22//Scoped Resource
23#if DEVICE_FEATURELINE == 3
24 MonitorChannel<true> CounterMean;
25#endif
26
27void isr_fn3()
28{
29 //1. PIT Starts Counting
30 IMXRT_PIT_CHANNELS[PIT_CHANNEL_IN_USE].TFLG = 1;
31
32 //2. Starts counting again after reset
33 IMXRT_TMR1.CH[0].SCTRL &= ~TMR_SCTRL_IEF; //Clear Edge Flag
34
35 //3. Read Counter Value
36 Counter_val = IMXRT_TMR1.CH[0].CNTR;
37
38 //4. Reset Couter
39 IMXRT_TMR1.CH[0].CNTR = 0; //Reset Counter to 0.
40
41 //5. Assert Update Flag
42 Update_flag = true;
43
44 asm volatile ("dsb");
45 //-6. Toggle Pin for debugging
46 #if ISR_PIN_TOGGLE == 1
47 digitalToggleFast(ISR_TEST_TOOGLE_PIN);
48 #endif
49}
50
51
52
53
55{
56 //1. LEDPanel Indicator Setup
57 LEDPanel.init(); //Set Pinmode for all LEDs.
59
60 //2. Pin Modes
61 #if ISR_PIN_TOGGLE == 1
62 pinMode(ISR_TEST_TOOGLE_PIN, OUTPUT);
63 #endif
64 pinMode(TTL_C_PULSE_INPUT_PIN, INPUT);
65
66 // 3.1. PIT_t Setup
67 PI_t.enable_PITs(); //Enable all PITs
68 PI_t.sel_FBUS_clock(); //Select 150MHz clock
69 PI_t.interrupt(); //Enable Interrupts
70 PI_t.set_interrupt(isr_fn3, 255); //Set interrupt
71 // 3.2. Colllect Errors
72 Error_t er_invalid_gt = PI_t.set_gate_time(Gate_time_us);
73 Error_t er_period_precision = Errors::Precison_Threshold(PI_t.period_error_us(), Allowed_period_error_us);
74
75 //4. TTL_C Setup
76 TTL_c.init();
78
79
80 //5. Set Serial Sampling Time
81 extern uint32_t serial_max_count(double Gate_time_u, double Serial_out_time_ms);
82 SerialOut_After = serial_max_count(GATE_TIME_US, SAMPLING_TIME_MS);
83
84 //6. Error Validation Validation
85 Errors::Validate(er_invalid_gt);
86 Errors::Validate(er_period_precision);
87 LEDPanel.assert_errors();
88
89 //7. Serial Setup
90 Serial.begin(115200);
91 while(!Serial){}
92
93
94 //8.
95 asm volatile ("dsb");
96} //End of sampler_setup()
97
99{
100 while(1)
101 {
102
103 if(!LEDPanel.Error_State)
104 {
105 LEDPanel.set(LOOP_LED);
106 }
107
108 //Start Timers
109 TTL_c.start();
110 PI_t.start();
111
112 if(Update_flag)
113 {
114 noInterrupts(); //NO INTERRUPT BLOCK
115
116
117 Update_count++;
118 #if DEVICE_FEATURELINE == 3
119 CounterMean.push_back(Counter_val);
120 #endif
121
122
124 {
125 Serial.print("cv: ");
126 Serial.print(Counter_val);
127
128 #if DEVICE_FEATURELINE == 3
129 float mean = CounterMean.mean();
130 Serial.print(" m: ");
131 Serial.print(mean);
132
133 Serial.print(" c: ");
134 Serial.print(CounterMean.Count);
135
136 Serial.print(" a: ");
137 Serial.println(CounterMean.Accumulate);
138 //CounterMean.reset();
139 #endif
140
141 Update_count = 0;
142
143 }
144
145 interrupts(); //NO INTERRUPT BLOCK
146
147 } //Update
148
149 } //infinite loop
150
151} //End of sampler_loop()
static Error_t Precison_Threshold(double error_val, double error_limit)
Receives error value and error threshold and returns an appropriate error state.
Definition: errors.hpp:44
static void Validate(const Error_t error)
Receives an error state and sets up the corresponding LED indicator.
Definition: errors.hpp:31
Template specialization.
Definition: monitor_channel.hpp:14
void push_back(DataType data)
Adds datum to the channel.
Definition: monitor_channel.hpp:23
float_t mean() const __attribute__((always_inline))
Returns the estimated mean.
Definition: monitor_channel.hpp:31
volatile uint32_t Count
Count of the received values (Initialized to prevent DivideByZero error)
Definition: monitor_channel.hpp:19
volatile AccumulateType Accumulate
Sum of the received values.
Definition: monitor_channel.hpp:18
void init_pins() __attribute__((always_inline))
Sets the Primary and Secondary inputs for Capture Mode, routed through XBAR. Reference XBAR and IOMUX...
Definition: qtmr1.hpp:185
void start() __attribute__((always_inline))
Starts up-counting from the set counter value.
Definition: qtmr1.hpp:24
void init() __attribute__((always_inline))
Definition: qtmr1.hpp:107
Error_t
Enumarates the error codes thrown by the different modules.
Definition: errors.hpp:17
#define PIT_CHANNEL_IN_USE
Definition: types.hpp:12
void sampler_setup()
Definition: featureline3.hpp:54
void isr_fn3()
This featureline (see featurelines.md) samples Counter values at periodic intervals and prints it to ...
Definition: featureline3.hpp:27
void sampler_loop()
Definition: featureline3.hpp:98
const double Gate_time_us
Serial output is done after these many updates (default → overriden in the setup function).
Definition: global.cpp:29
TMR1Controller TTL_c
TTL_c Resource.
Definition: global.cpp:19
uint32_t SerialOut_After
Definition: global.cpp:28
const double Allowed_period_error_us
The gate time of TTL_C in microseconds (us)
Definition: global.cpp:30
volatile unsigned int Update_count
Stores the number of updates made on the correlator channels since the last serialout.
Definition: global.cpp:25
PITController< PIT_CHANNEL_IN_USE > PI_t
PI_t Resource.
Definition: global.cpp:18
volatile bool Update_flag
Indicates if a new value has arrived from the counting module.
Definition: global.cpp:24
volatile counter_t Counter_val
Stores the value read from the counter.
Definition: global.cpp:23
LEDSet< 5 > LEDPanel({LED_BUILTIN, LED_RED, LED_GREEN, LED_WHITE, LED_BLUE})
const int LED_WHITE
Definition: pins.hpp:9
const int LOOP_LED
Definition: pins.hpp:30
const int TTL_C_PULSE_INPUT_PIN
Definition: pins.hpp:19
uint32_t serial_max_count(double Gate_time_us, double Serial_out_time_ms)
Definition: utilities.cpp:7