Skip to content

Commit 1b37cf9

Browse files
committed
feat: frequency functions
1 parent 5b32a33 commit 1b37cf9

File tree

12 files changed

+247
-22
lines changed

12 files changed

+247
-22
lines changed

pslab-core.X/commands.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -240,20 +240,20 @@ command_func_t* const cmd_table[NUM_PRIMARY_CMDS + 1][NUM_SECONDARY_CMDS_MAX + 1
240240
Undefined, Undefined, Undefined, Undefined,
241241
},
242242
{ // 11 COMMON
243-
// 0 1 GET_CTMU_VOLTAGE 2 GET_CAPACITANCE 3 GET_FREQUENCY
244-
Undefined, Unimplemented, MULTIMETER_GetCapacitance, Unimplemented,
245-
// 4 GET_INDUCTANCE 5 GET_VERSION 6 7
246-
Unimplemented, DEVICE_GetVersion, Undefined, Undefined,
247-
// 8 RETRIEVE_BUFFER 9 GET_HIGH_FREQUENCY 10 CLEAR_BUFFER 11 SETRGB
248-
BUFFER_Retrieve, Unimplemented, Unimplemented, Unimplemented,
249-
// 12 READ_PROGRAM_ADDRESS 13 WRITE_PROGRAM_ADDRESS 14 READ_DATA_ADDRESS 15 WRITE_DATA_ADDRESS
250-
Removed, Removed, DEVICE_ReadRegisterData, DEVICE_WriteRegisterData,
251-
// 16 GET_CAP_RANGE 17 SETRGB2 18 READ_LOG 19 RESTORE_STANDALONE
252-
Unimplemented, Unimplemented, Unimplemented, DEVICE_Reset,
253-
// 20 GET_ALTERNATE_HIGH_FREQUENCY 21 22 SETRGB3 23 START_CTMU
254-
Unimplemented, Undefined, Unimplemented, Unimplemented,
255-
// 24 STOP_CTMU 25 START_COUNTING 26 FETCH_COUNT 27 FILL_BUFFER
256-
Unimplemented, SENSORS_StartCounter, SENSORS_GetCounter, Unimplemented,
243+
// 0 1 GET_CTMU_VOLTAGE 2 GET_CAPACITANCE 3 GET_FREQUENCY
244+
Undefined, Unimplemented, MULTIMETER_GetCapacitance, MULTIMETER_LowFrequency,
245+
// 4 GET_INDUCTANCE 5 GET_VERSION 6 7
246+
Unimplemented, DEVICE_GetVersion, Undefined, Undefined,
247+
// 8 RETRIEVE_BUFFER 9 GET_HIGH_FREQUENCY 10 CLEAR_BUFFER 11 SETRGB
248+
BUFFER_Retrieve, MULTIMETER_HighFrequency, Unimplemented, Unimplemented,
249+
// 12 READ_PROGRAM_ADDRESS 13 WRITE_PROGRAM_ADDRESS 14 READ_DATA_ADDRESS 15 WRITE_DATA_ADDRESS
250+
Removed, Removed, DEVICE_ReadRegisterData, DEVICE_WriteRegisterData,
251+
// 16 GET_CAP_RANGE 17 SETRGB2 18 READ_LOG 19 RESTORE_STANDALONE
252+
Unimplemented, Unimplemented, Unimplemented, DEVICE_Reset,
253+
// 20 GET_ALTERNATE_HIGH_FREQUENCY 21 22 SETRGB3 23 START_CTMU
254+
MULTIMETER_HighFrequencyAlt, Undefined, Unimplemented, Unimplemented,
255+
// 24 STOP_CTMU 25 START_COUNTING 26 FETCH_COUNT 27 FILL_BUFFER
256+
Unimplemented, SENSORS_StartCounter, SENSORS_GetCounter, Unimplemented,
257257
},
258258
{ // 12 PASSTHROUGH
259259
// 0 1 2 3

pslab-core.X/helpers/interval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ void INTERVAL_CaptureFour(uint16_t count, uint16_t mode, uint8_t prescaler) {
185185
RPINR8bits.IC4R = PIN_MANAGER_DIGITAL_PINS_LA4;
186186

187187
TMR2_Initialize();
188-
TMR2_PrescalerSet(prescaler & 0xF);
188+
TMR2_SetPrescaler(prescaler & 0xF);
189189
TMR2_Counter16BitSet(1);
190190

191191
IC_PARAMS_SetCaptureTimer(IC_PARAMS_CAPTURE_TIMER2);

pslab-core.X/instruments/multimeter.c

Lines changed: 145 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
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"
18+
#include "multimeter.h"
919

1020
#define CHARGE 0
1121
#define DISCHARGE 1
@@ -46,7 +56,7 @@ void GetCapacitance_InitCTMU_TMR5(uint8_t current_range, uint8_t trim,
4656
CTMUICONbits.IRNG = current_range;
4757

4858
TMR5_Initialize();
49-
TMR5_PrescalerSet(TMR_PRESCALER_64);
59+
TMR5_SetPrescaler(TMR_PRESCALER_64);
5060
TMR5_Period16BitSet(charge_time);
5161
}
5262

@@ -185,3 +195,137 @@ response_t MULTIMETER_GetCapacitance(void) {
185195

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

pslab-core.X/instruments/multimeter.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ extern "C" {
2828
* @return SUCCESS
2929
*/
3030
response_t MULTIMETER_GetCapacitance(void);
31+
32+
response_t MULTIMETER_HighFrequency(void);
33+
34+
response_t MULTIMETER_HighFrequencyAlt(void);
35+
36+
response_t MULTIMETER_LowFrequency(void);
3137

3238
#ifdef __cplusplus
3339
}

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ extern "C" {
278278
IC1CON2bits.IC32 = 1;
279279
}
280280

281+
uint16_t IC1_CaptureTimeRead(void);
282+
281283
inline static void IC1_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT i) {
282284
IC1CON1bits.ICI = i;
283285
}

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

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

45+
uint16_t IC2_CaptureTimeRead(void) {
46+
return (IC2TMR);
47+
}
48+
4549
void IC2_ManualTriggerSet(void) {
4650
IC2CON2bits.TRIGSTAT = true;
4751
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ extern "C" {
284284
IC2CON2bits.IC32 = 1;
285285
}
286286

287+
uint16_t IC2_CaptureTimeRead(void);
288+
287289
inline static void IC2_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT i) {
288290
IC2CON1bits.ICI = i;
289291
}

pslab-core.X/registers/timers/tmr2.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,15 @@ extern "C" {
118118
*/
119119
void TMR2_Stop(void);
120120

121-
inline static void TMR2_PrescalerSet(TIMER_PARAMS_PRESCALER scale) {
121+
inline static void TMR2_CombineWithTimer3(void) {
122+
T2CONbits.T32 = 1;
123+
}
124+
125+
inline static void TMR2_SetExternalClockAsSource(void) {
126+
T2CONbits.TCS = 1;
127+
}
128+
129+
inline static void TMR2_SetPrescaler(TIMER_PARAMS_PRESCALER scale) {
122130
T2CONbits.TCKPS = scale;
123131
}
124132

pslab-core.X/registers/timers/tmr3.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
#include "tmr3.h"
33

44
void TMR3_Initialize(void) {
5-
// Clear timer 4 register
5+
// Clear timer 3 register
66
TMR3 = 0x00;
7+
TMR3HLD = 0x00;
78
//Period = 0 s; Frequency = 64000000 Hz; PR4 0;
89
PR3 = 0x00;
910
// Stops timer
@@ -16,6 +17,9 @@ void TMR3_Initialize(void) {
1617
T3CONbits.TCKPS = 0b00;
1718
// Internal clock (FP)
1819
T3CONbits.TCS = 0;
20+
// Disable interrupts
21+
_T3IE = 0;
22+
_T3IF = 0;
1923
}
2024

2125
void TMR3_Period16BitSet(uint16_t value) {
@@ -36,6 +40,14 @@ uint16_t TMR3_Counter16BitGet(void) {
3640
return (TMR3);
3741
}
3842

43+
void TMR3_Carry16BitSet(uint16_t value) {
44+
TMR3HLD = value;
45+
}
46+
47+
uint16_t TMR3_Carry16BitGet(void) {
48+
return TMR3HLD;
49+
}
50+
3951
void TMR3_Start(void) {
4052
/* Start the Timer */
4153
T3CONbits.TON = 1;

0 commit comments

Comments
 (0)