Skip to content

Commit a248ca4

Browse files
committed
feat: frequency measurements
1 parent 94c3f1e commit a248ca4

File tree

17 files changed

+2708
-1839
lines changed

17 files changed

+2708
-1839
lines changed

src/commands.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,13 @@ command_func_t* const cmd_table[NUM_PRIMARY_CMDS + 1][NUM_SECONDARY_CMDS_MAX + 1
228228
},
229229
{ // 10 TIMING
230230
// 0 1 GET_TIMING 2 3
231-
Undefined, Unimplemented, Undefined, Undefined,
231+
Undefined, INTERVAL_UntilEvent, Undefined, Undefined,
232232
// 4 START_ONE_CHAN_LA 5 START_TWO_CHAN_LA 6 START_FOUR_CHAN_LA 7 FETCH_DMA_DATA
233233
LOGICANALYZER_OneChannel, LOGICANALYZER_TwoChannel, LOGICANALYZER_FourChannel, Removed,
234234
// 8 FETCH_INT_DMA_DATA 9 FETCH_LONG_DMA_DATA 10 COMPARATOR_TO_LA 11 GET_INITIAL_STATES
235235
BUFFER_FetchInt, BUFFER_FetchLong, Unimplemented, INTERVAL_GetState,
236236
// 12 TIMING_MEASUREMENTS 13 INTERVAL_MEASUREMENTS 14 CONFIGURE_COMPARATOR 15 START_ALTERNATE_ONE_CHAN_LA
237-
Unimplemented, Unimplemented, Removed, LOGICANALYZER_OneChannelAlt,
237+
INTERVAL_TimeMeasure, INTERVAL_IntervalMeasure, Removed, LOGICANALYZER_OneChannelAlt,
238238
// 16 START_THREE_CHAN_LA 17 STOP_LA 18 19
239239
LOGICANALYZER_ThreeChannel, LOGICANALYZER_Stop, Undefined, Undefined,
240240
// 20 21 22 23
@@ -244,17 +244,17 @@ command_func_t* const cmd_table[NUM_PRIMARY_CMDS + 1][NUM_SECONDARY_CMDS_MAX + 1
244244
},
245245
{ // 11 COMMON
246246
// 0 1 GET_CTMU_VOLTAGE 2 GET_CAPACITANCE 3 GET_FREQUENCY
247-
Undefined, MULTIMETER_GetCTMUVolts, MULTIMETER_GetCapacitance, Unimplemented,
247+
Undefined, MULTIMETER_GetCTMUVolts, MULTIMETER_GetCapacitance, MULTIMETER_LowFrequency,
248248
// 4 GET_INDUCTANCE 5 GET_VERSION 6 7
249249
Unimplemented, DEVICE_GetVersion, Undefined, Undefined,
250250
// 8 RETRIEVE_BUFFER 9 GET_HIGH_FREQUENCY 10 CLEAR_BUFFER 11 SET_RGB1
251-
BUFFER_Retrieve, Unimplemented, BUFFER_Clear, Removed,
251+
BUFFER_Retrieve, MULTIMETER_HighFrequency, BUFFER_Clear, Removed,
252252
// 12 READ_PROGRAM_ADDRESS 13 WRITE_PROGRAM_ADDRESS 14 READ_DATA_ADDRESS 15 WRITE_DATA_ADDRESS
253253
Removed, Removed, DEVICE_ReadRegisterData, DEVICE_WriteRegisterData,
254254
// 16 GET_CAP_RANGE 17 SET_RGB2 18 READ_LOG 19 RESTORE_STANDALONE
255255
Unimplemented, Removed, Removed, DEVICE_Reset,
256256
// 20 GET_ALTERNATE_HIGH_FREQUENCY 21 SET_RGB_COMMON 22 SET_RGB3 23 START_CTMU
257-
Unimplemented, LIGHT_RGBPin, Removed, CTMU_Start,
257+
MULTIMETER_HighFrequencyAlt, LIGHT_RGBPin, Removed, CTMU_Start,
258258
// 24 STOP_CTMU 25 START_COUNTING 26 FETCH_COUNT 27 FILL_BUFFER
259259
CTMU_Stop, SENSORS_StartCounter, SENSORS_GetCounter, BUFFER_Fill,
260260
},

src/helpers/interval.c

+110-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "../registers/memory/dma.h"
1010
#include "../registers/system/interrupt_manager.h"
1111
#include "../registers/system/pin_manager.h"
12+
#include "../registers/system/watchdog.h"
1213
#include "../registers/timers/tmr2.h"
1314
#include "buffer.h"
1415

@@ -170,7 +171,7 @@ void INTERVAL_CaptureFour(uint16_t count, uint16_t mode, uint8_t prescaler) {
170171
RPINR8bits.IC4R = PIN_MANAGER_DIGITAL_PINS_LA4;
171172

172173
TMR2_Initialize();
173-
TMR2_PrescalerSet(prescaler & 0xF);
174+
TMR2_SetPrescaler(prescaler & 0xF);
174175
TMR2_Counter16BitSet(1);
175176

176177
IC_PARAMS_SetCaptureTimer(IC_PARAMS_CAPTURE_TIMER2);
@@ -196,3 +197,111 @@ response_t INTERVAL_GetState(void) {
196197

197198
return SUCCESS;
198199
}
200+
201+
response_t INTERVAL_IntervalMeasure(void) {
202+
203+
uint16_t timeout = UART1_ReadInt(); // t * 64e6 >> 16
204+
uint8_t pins = UART1_Read();
205+
uint8_t modes = UART1_Read();
206+
207+
IC_PARAMS_ConfigureIntervalCaptureWithIC1AndIC2(pins & 0xF,
208+
IC_PARAMS_CAPTURE_TIMER_PERIPHERAL,
209+
IC_PARAMS_CAPTURE_INTERRUPT_EVERY_EVENT, modes & 0x7);
210+
IC_PARAMS_ConfigureIntervalCaptureWithIC3AndIC4((pins >> 4) & 0xF,
211+
IC_PARAMS_CAPTURE_TIMER_PERIPHERAL,
212+
IC_PARAMS_CAPTURE_INTERRUPT_EVERY_EVENT, (modes >> 3) & 0x7);
213+
214+
IC_PARAMS_ManualTriggerAll();
215+
216+
while ((!_IC1IF) && (IC2TMR < timeout)) WATCHDOG_TimerClear();
217+
UART1_WriteInt(IC1BUF);
218+
UART1_WriteInt(IC2BUF);
219+
220+
while ((!_IC3IF) && (IC2TMR < timeout)) WATCHDOG_TimerClear();
221+
UART1_WriteInt(IC3BUF);
222+
UART1_WriteInt(IC4BUF);
223+
UART1_WriteInt(IC2TMR);
224+
225+
IC_PARAMS_DisableAllModules();
226+
227+
return SUCCESS;
228+
}
229+
230+
response_t INTERVAL_TimeMeasure(void) {
231+
232+
uint16_t timeout = UART1_ReadInt(); // t * 64e6 >> 16
233+
uint8_t pins = UART1_Read();
234+
uint8_t modes = UART1_Read();
235+
uint8_t intrpts = UART1_Read();
236+
237+
if ((pins & 0xF) == 4 || ((pins >> 4) & 0xF) == 4) {
238+
CMP4_SetupComparator();
239+
CVR_SetupComparator();
240+
}
241+
242+
IC_PARAMS_ConfigureIntervalCaptureWithIC1AndIC2(pins & 0xF,
243+
IC_PARAMS_CAPTURE_TIMER2, (intrpts & 0xF) - 1, modes & 0x7);
244+
IC_PARAMS_ConfigureIntervalCaptureWithIC3AndIC4((pins >> 4) & 0xF,
245+
IC_PARAMS_CAPTURE_TIMER2, ((intrpts >> 4) & 0xF) - 1, (modes >> 3) & 0x7);
246+
247+
TMR2_Initialize();
248+
249+
SetDefaultDIGITAL_STATES();
250+
251+
IC_PARAMS_ManualTriggerAll();
252+
TMR2_Start();
253+
254+
if ((modes >> 6) & 0x1) {
255+
RPOR5bits.RP54R = RPN_DEFAULT_PORT; // Disconnect SQR1 pin
256+
((modes >> 7) & 0x1) ? SQR1_SetHigh() : SQR1_SetLow();
257+
}
258+
259+
while ((!_IC1IF || !_IC3IF) && (IC2TMR < timeout)) WATCHDOG_TimerClear();
260+
261+
uint8_t i;
262+
for (i = 0; i < (intrpts & 0xF); i++) {
263+
UART1_WriteInt(IC1BUF);
264+
UART1_WriteInt(IC2BUF);
265+
}
266+
for (i = 0; i < ((intrpts >> 4) & 0xF); i++) {
267+
UART1_WriteInt(IC3BUF);
268+
UART1_WriteInt(IC4BUF);
269+
}
270+
271+
IC1_InterruptFlagClear();
272+
IC3_InterruptFlagClear();
273+
274+
UART1_WriteInt(IC2TMR);
275+
276+
IC_PARAMS_DisableAllModules();
277+
TMR2_Stop();
278+
279+
return SUCCESS;
280+
}
281+
282+
response_t INTERVAL_UntilEvent(void) {
283+
284+
uint16_t timeout = UART1_ReadInt(); // t * 64e6 >> 16
285+
uint8_t mode = UART1_Read();
286+
uint8_t pin = UART1_Read();
287+
288+
IC_PARAMS_ConfigureIntervalCaptureWithIC1AndIC2(pin & 0xF,
289+
IC_PARAMS_CAPTURE_TIMER_PERIPHERAL, (mode & 0x3) - 1, mode & 0x7);
290+
291+
while (!_IC1IF && (IC2TMR < timeout)) WATCHDOG_TimerClear();
292+
293+
IC1_InterruptFlagClear();
294+
295+
UART1_WriteInt(IC2TMR);
296+
297+
uint8_t i;
298+
for (i = 0; i < (mode & 0x3); i++) {
299+
UART1_WriteInt(IC1BUF);
300+
UART1_WriteInt(IC2BUF);
301+
}
302+
303+
IC_PARAMS_DisableAllModules();
304+
TMR2_Stop();
305+
306+
return SUCCESS;
307+
}

src/helpers/interval.h

+119-1
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,126 @@ extern "C" {
8686
* @return None
8787
*/
8888
void INTERVAL_CaptureFour(uint16_t count, uint16_t mode, uint8_t prescaler);
89-
89+
90+
/**
91+
* @brief Reads DMA status registry data
92+
*
93+
* @description
94+
* This method will sequentially read register addresses at BUFFER pointer
95+
* and all four DMA channel pointers. It will also read digital state parameters.
96+
*
97+
* There are no input parameters to this method. The output of this method should
98+
* be read over serial in the following order.
99+
* 1. (int) BUFFER pointer
100+
* 2. (int) DMA Channel 0 pointer
101+
* 3. (int) DMA Channel 1 pointer
102+
* 4. (int) DMA Channel 2 pointer
103+
* 5. (int) DMA Channel 3 pointer
104+
* 6. (char) Digital states
105+
* 7. (char) Digital states error
106+
*
107+
* @return SUCCESS
108+
*/
90109
response_t INTERVAL_GetState(void);
110+
111+
/**
112+
* @brief Measures the time interval between two pin state change events
113+
*
114+
* @description
115+
* This method will count the time difference between two pin change events
116+
* attached to two pins.
117+
* The events can be any event defined at the list of events in `IC_PARAMS_CAPTURE_MODE`.
118+
* The pins should be any pin in the list of `PIN_MANAGER_DIGITAL_PINS`.
119+
*
120+
* @param timeout : period of wait until the operation is aborted
121+
* @param pins : input pins defined at `PIN_MANAGER_DIGITAL_PINS`
122+
* | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
123+
* | PIN EVENT 2 | PIN EVENT 1 |
124+
* @param modes : pin change event defined at `IC_PARAMS_CAPTURE_MODE`
125+
* | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
126+
* | X | X | EVENT 2 | EVENT 1 |
127+
*
128+
* The output of this method should be read over serial in the following order.
129+
* 1. (int) IC1BUF - LSW
130+
* 2. (int) IC2BUF - MSW
131+
* Combine 1. and 2. to get the trigger time of the event 1
132+
* 3. (int) IC3BUF - LSW
133+
* 4. (int) IC4BUF - MSW
134+
* Combine 3. and 4. to get the trigger time of the event 2
135+
* 5. (int) IC2TMR
136+
*
137+
* @return SUCCESS
138+
*/
139+
response_t INTERVAL_IntervalMeasure(void);
140+
141+
/**
142+
* @brief Measures the time between multiple pin state change events
143+
*
144+
* @description
145+
* This method will log time units for multiple changes occurred on
146+
* defined digital pins. Unlike `INTERVAL_IntervalMeasure` where it
147+
* measure only a single change of states, this method will measure
148+
* upto 4 change of pin states.
149+
* The events can be any event defined at the list of events in `IC_PARAMS_CAPTURE_MODE`.
150+
* The pins should be any pin in the list of `PIN_MANAGER_DIGITAL_PINS`.
151+
*
152+
* @param timeout : period of wait until the operation is aborted
153+
* @param pins : input pins defined at `PIN_MANAGER_DIGITAL_PINS`
154+
* | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
155+
* | PIN EVENT 2 | PIN EVENT 1 |
156+
* @param modes : pin change event defined at `IC_PARAMS_CAPTURE_MODE`
157+
* | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
158+
* | X | X | EVENT 2 | EVENT 1 |
159+
* @param intrpts : input pins defined at `PIN_MANAGER_DIGITAL_PINS`
160+
* | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
161+
* | INTERRUPT 2 | INTERRUPT 1 |
162+
*
163+
* The output of this method should be read over serial in the following order.
164+
* 1. (int) IC1BUF - LSW
165+
* 2. (int) IC2BUF - MSW
166+
* Combine 1. and 2. to get the trigger time of the change event. Depending on the
167+
* intrpts, the two registers (1. and 2.) may need to be read repeatedly to capture
168+
* timing data for each event occurrence.
169+
* 3. (int) IC3BUF - LSW
170+
* 4. (int) IC4BUF - MSW
171+
* Combine 3. and 4. to get the trigger time of the change event. Depending on the
172+
* intrpts, the two registers (3. and 4.) may need to be read repeatedly to capture
173+
* timing data for each event occurrence.
174+
* Note: ICxBUF is a 4-level buffer that can store time log for four change events.
175+
* 5. (int) IC2TMR
176+
*
177+
* @return SUCCESS
178+
*/
179+
response_t INTERVAL_TimeMeasure(void);
180+
181+
/**
182+
* @brief Measures the time until a pin state change event occurs
183+
*
184+
* @description
185+
* This method will stop counting time when the defined pin change event occurred.
186+
* The event can be any event defined at the list of events in `IC_PARAMS_CAPTURE_MODE`.
187+
* The pin should be any pin in the list of `PIN_MANAGER_DIGITAL_PINS`.
188+
*
189+
* @param timeout : period of wait until the operation is aborted
190+
* @param mode : pin change event defined at `IC_PARAMS_CAPTURE_MODE`
191+
* | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
192+
* | X | X | X | X | X | EVENT |
193+
* @param pin : input pin defined at `PIN_MANAGER_DIGITAL_PINS`
194+
* | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
195+
* | X | X | X | X | PIN EVENT |
196+
*
197+
* The output of this method should be read over serial in the following order.
198+
* 1. (int) IC2TMR
199+
* 2. (int) IC1BUF - LSW
200+
* 3. (int) IC2BUF - MSW
201+
* Combine 2. and 3. to get the trigger time of the change event. Depending on the mode,
202+
* the last two registers (2. and 3.) may need to be read repeatedly to capture timing
203+
* data for each event occurrence.
204+
* Note: ICxBUF is a 4-level buffer that can store time log for four change events.
205+
*
206+
* @return SUCCESS
207+
*/
208+
response_t INTERVAL_UntilEvent(void);
91209

92210
// Getters and setters
93211

0 commit comments

Comments
 (0)