Skip to content

Commit c008078

Browse files
committed
feat: frequency measurements
1 parent 2c42af5 commit c008078

File tree

18 files changed

+427
-10
lines changed

18 files changed

+427
-10
lines changed

pslab-core.X/commands.c

Lines changed: 5 additions & 5 deletions
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
},

pslab-core.X/helpers/interval.c

Lines changed: 110 additions & 1 deletion
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+
}

pslab-core.X/helpers/interval.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ extern "C" {
8989

9090
response_t INTERVAL_GetState(void);
9191

92+
response_t INTERVAL_IntervalMeasure(void);
93+
94+
response_t INTERVAL_TimeMeasure(void);
95+
96+
response_t INTERVAL_UntilEvent(void);
97+
9298
// Getters and setters
9399

94100
void SetDIGITAL_STATES(uint8_t);

pslab-core.X/instruments/logicanalyzer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ response_t LOGICANALYZER_FourChannel(void) {
140140
INTERVAL_CaptureFour(points, mode, prescaler);
141141

142142
if (trigger & 1) {
143-
LA_TRIGGER_STATE = trigger & 2 ? RISING_EDGE : FALLING_EDGE;
143+
LA_TRIGGER_STATE = trigger & 2 ? FALLING_EDGE : RISING_EDGE;
144144
LA_TRIGGER_CHANNEL = 0;
145145

146146
if ((trigger >> 2) & 1) {

pslab-core.X/instruments/multimeter.c

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
#include "../commands.h"
22
#include "../bus/uart/uart1.h"
33
#include "../helpers/delay.h"
4+
#include "../helpers/interval.h"
5+
#include "../registers/comparators/ic1.h"
6+
#include "../registers/comparators/ic2.h"
7+
#include "../registers/comparators/ic_params.h"
48
#include "../registers/converters/adc1.h"
59
#include "../registers/converters/ctmu.h"
10+
#include "../registers/comparators/cmp4.h"
11+
#include "../registers/comparators/cvr.h"
612
#include "../registers/memory/dma.h"
713
#include "../registers/system/pin_manager.h"
14+
#include "../registers/timers/timer_params.h"
15+
#include "../registers/timers/tmr2.h"
16+
#include "../registers/timers/tmr3.h"
817
#include "../registers/timers/tmr5.h"
918
#include "multimeter.h"
1019

@@ -47,7 +56,7 @@ void GetCapacitance_InitCTMU_TMR5(uint8_t current_range, uint8_t trim,
4756
CTMUICONbits.IRNG = current_range;
4857

4958
TMR5_Initialize();
50-
TMR5_PrescalerSet(TMR_PRESCALER_64);
59+
TMR5_SetPrescaler(TMR_PRESCALER_64);
5160
TMR5_Period16BitSet(charge_time);
5261
}
5362

@@ -193,6 +202,140 @@ response_t MULTIMETER_GetCapacitance(void) {
193202
return SUCCESS;
194203
}
195204

205+
response_t MULTIMETER_HighFrequency(void) {
206+
207+
uint8_t config = UART1_Read();
208+
209+
LED_SetLow();
210+
211+
if ((config & 0xF) == 4) {
212+
CVR_SetupComparator();
213+
CMP4_SetupComparator();
214+
}
215+
216+
RPINR3bits.T2CKR = PIN_MANAGER_DIGITAL_PINS[config & 0xF];
217+
218+
TMR2_Initialize();
219+
TMR3_Initialize();
220+
TMR5_Initialize();
221+
222+
TMR2_CombineWithTimer3();
223+
TMR2_SetExternalClockAsSource();
224+
TMR2_SetPrescaler((config >> 4) & 0x4);
225+
TMR5_SetPrescaler(TMR_PRESCALER_256);
226+
TMR5_Period16BitSet(25000); // 100 millisecond sampling
227+
228+
_T5IP = 0x01; // Set Timer5 Interrupt Priority Level
229+
TMR5_InterruptEnable();
230+
TMR2_Start();
231+
TMR5_Start();
232+
233+
TMR5_WaitForInterruptEvent();
234+
235+
LED_SetHigh();
236+
UART1_Write(1); // Scaling factor
237+
UART1_WriteInt(TMR2_Counter16BitGet());
238+
UART1_WriteInt(TMR3_Carry16BitGet());
239+
240+
return SUCCESS;
241+
}
242+
243+
response_t MULTIMETER_HighFrequencyAlt(void) {
244+
245+
uint8_t config = UART1_Read();
246+
247+
LED_SetLow();
248+
249+
if ((config & 0xF) == 4) {
250+
CVR_SetupComparator();
251+
CMP4_SetupComparator();
252+
}
253+
254+
TMR2_Initialize();
255+
TMR2_SetExternalClockAsSource();
256+
TMR2_SetPrescaler((config >> 4) & 0x4);
257+
TMR5_Initialize();
258+
IC1_Initialize();
259+
IC2_Initialize();
260+
261+
RPINR7bits.IC1R = RPN_DEFAULT_PORT;
262+
RPINR7bits.IC2R = RPN_DEFAULT_PORT;
263+
RPINR3bits.T2CKR = PIN_MANAGER_DIGITAL_PINS[config & 0xF];
264+
265+
IC1_SetCaptureTimer(IC_PARAMS_CAPTURE_TIMER2);
266+
IC1_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT_EVERY_SECOND);
267+
IC1_CombineOddEvenICModules();
268+
IC1_UseSourceTo(IC_PARAMS_SOURCE_TASK_TRIGGER);
269+
IC1_SetCaptureMode(IC_PARAMS_CAPTURE_MODE_EVERY_16TH_RISING_EDGE);
270+
271+
IC2_SetCaptureTimer(IC_PARAMS_CAPTURE_TIMER2);
272+
IC2_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT_EVERY_SECOND);
273+
IC2_CombineOddEvenICModules();
274+
IC2_UseSourceTo(IC_PARAMS_SOURCE_TASK_TRIGGER);
275+
IC2_SetCaptureMode(IC_PARAMS_CAPTURE_MODE_EVERY_16TH_RISING_EDGE);
276+
277+
TMR5_SetPrescaler(TMR_PRESCALER_256);
278+
TMR5_Period16BitSet(25000); // 100 millisecond sampling
279+
280+
_T5IP = 0x01;
281+
TMR2_Start();
282+
TMR5_Start();
283+
IC1_ManualTriggerSet();
284+
IC1_ManualTriggerSet();
285+
286+
TMR5_WaitForInterruptEvent();
287+
288+
LED_SetHigh();
289+
UART1_Write(1); // Scaling factor
290+
UART1_WriteInt(IC1_CaptureTimeRead());
291+
UART1_WriteInt(IC2_CaptureTimeRead());
292+
293+
return SUCCESS;
294+
}
295+
296+
response_t MULTIMETER_LowFrequency(void) {
297+
298+
uint16_t timeout = UART1_ReadInt();
299+
uint8_t config = UART1_Read();
300+
301+
RPINR7bits.IC1R = PIN_MANAGER_DIGITAL_PINS[config & 0xF];
302+
303+
IC1_Initialize();
304+
IC1_SetCaptureTimer(IC_PARAMS_CAPTURE_TIMER_PERIPHERAL);
305+
IC1_CombineOddEvenICModules();
306+
IC1_SetCaptureMode(IC_PARAMS_CAPTURE_MODE_EVERY_16TH_RISING_EDGE);
307+
IC1_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT_EVERY_SECOND);
308+
309+
IC2_Initialize();
310+
IC2_SetCaptureTimer(IC_PARAMS_CAPTURE_TIMER_PERIPHERAL);
311+
IC2_CombineOddEvenICModules();
312+
IC2_SetCaptureMode(IC_PARAMS_CAPTURE_MODE_EVERY_16TH_RISING_EDGE);
313+
IC2_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT_EVERY_SECOND);
314+
315+
IC1_ManualTriggerSet();
316+
IC2_ManualTriggerSet();
317+
SetDefaultDIGITAL_STATES();
318+
319+
IC1_InterruptFlagClear();
320+
while ((IC2_CaptureTimeRead() < timeout) && (!_IC1IF));
321+
IC1_InterruptFlagClear();
322+
323+
RPINR7bits.IC1R = RPN_DEFAULT_PORT;
324+
325+
if ((IC2_CaptureTimeRead() >= timeout) ||
326+
(IC2_HasCaptureBufferOverflowed())) {
327+
UART1_Write(1);
328+
} else {
329+
UART1_Write(0);
330+
}
331+
332+
UART1_WriteInt(IC1_CaptureDataRead());
333+
UART1_WriteInt(IC2_CaptureDataRead());
334+
IC_PARAMS_DisableAllModules();
335+
336+
return SUCCESS;
337+
}
338+
196339
response_t MULTIMETER_GetCTMUVolts(void) {
197340

198341
uint8_t config = UART1_Read();

pslab-core.X/instruments/multimeter.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ extern "C" {
2929
*/
3030
response_t MULTIMETER_GetCapacitance(void);
3131

32+
response_t MULTIMETER_HighFrequency(void);
33+
34+
response_t MULTIMETER_HighFrequencyAlt(void);
35+
36+
response_t MULTIMETER_LowFrequency(void);
3237
/**
3338
* @brief Measurements using Charge Time Measurement Unit
3439
*

pslab-core.X/registers/comparators/ic1.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ uint16_t IC1_CaptureDataRead(void) {
4242
return (IC1BUF);
4343
}
4444

45+
uint16_t IC1_CaptureTimeRead(void) {
46+
return (IC1TMR);
47+
}
48+
4549
void IC1_ManualTriggerSet(void) {
4650
IC1CON2bits.TRIGSTAT = true;
4751
}

pslab-core.X/registers/comparators/ic1.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,12 @@ extern "C" {
278278
IC1CON2bits.IC32 = 1;
279279
}
280280

281+
uint16_t IC1_CaptureTimeRead(void);
282+
283+
inline static void IC1_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT i) {
284+
IC1CON1bits.ICI = i;
285+
}
286+
281287
inline static void IC1_UseSourceTo(IC_PARAMS_SOURCE_TASK t) {
282288
IC1CON2bits.ICTRIG = t;
283289
}

0 commit comments

Comments
 (0)