Skip to content

Commit

Permalink
feat: frequency measurements
Browse files Browse the repository at this point in the history
  • Loading branch information
CloudyPadmal committed Feb 5, 2022
1 parent 2c42af5 commit c008078
Show file tree
Hide file tree
Showing 18 changed files with 427 additions and 10 deletions.
10 changes: 5 additions & 5 deletions pslab-core.X/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,13 @@ command_func_t* const cmd_table[NUM_PRIMARY_CMDS + 1][NUM_SECONDARY_CMDS_MAX + 1
},
{ // 10 TIMING
// 0 1 GET_TIMING 2 3
Undefined, Unimplemented, Undefined, Undefined,
Undefined, INTERVAL_UntilEvent, Undefined, Undefined,
// 4 START_ONE_CHAN_LA 5 START_TWO_CHAN_LA 6 START_FOUR_CHAN_LA 7 FETCH_DMA_DATA
LOGICANALYZER_OneChannel, LOGICANALYZER_TwoChannel, LOGICANALYZER_FourChannel, Removed,
// 8 FETCH_INT_DMA_DATA 9 FETCH_LONG_DMA_DATA 10 COMPARATOR_TO_LA 11 GET_INITIAL_STATES
BUFFER_FetchInt, BUFFER_FetchLong, Unimplemented, INTERVAL_GetState,
// 12 TIMING_MEASUREMENTS 13 INTERVAL_MEASUREMENTS 14 CONFIGURE_COMPARATOR 15 START_ALTERNATE_ONE_CHAN_LA
Unimplemented, Unimplemented, Removed, LOGICANALYZER_OneChannelAlt,
INTERVAL_TimeMeasure, INTERVAL_IntervalMeasure, Removed, LOGICANALYZER_OneChannelAlt,
// 16 START_THREE_CHAN_LA 17 STOP_LA 18 19
LOGICANALYZER_ThreeChannel, LOGICANALYZER_Stop, Undefined, Undefined,
// 20 21 22 23
Expand All @@ -244,17 +244,17 @@ command_func_t* const cmd_table[NUM_PRIMARY_CMDS + 1][NUM_SECONDARY_CMDS_MAX + 1
},
{ // 11 COMMON
// 0 1 GET_CTMU_VOLTAGE 2 GET_CAPACITANCE 3 GET_FREQUENCY
Undefined, MULTIMETER_GetCTMUVolts, MULTIMETER_GetCapacitance, Unimplemented,
Undefined, MULTIMETER_GetCTMUVolts, MULTIMETER_GetCapacitance, MULTIMETER_LowFrequency,
// 4 GET_INDUCTANCE 5 GET_VERSION 6 7
Unimplemented, DEVICE_GetVersion, Undefined, Undefined,
// 8 RETRIEVE_BUFFER 9 GET_HIGH_FREQUENCY 10 CLEAR_BUFFER 11 SET_RGB1
BUFFER_Retrieve, Unimplemented, BUFFER_Clear, Removed,
BUFFER_Retrieve, MULTIMETER_HighFrequency, BUFFER_Clear, Removed,
// 12 READ_PROGRAM_ADDRESS 13 WRITE_PROGRAM_ADDRESS 14 READ_DATA_ADDRESS 15 WRITE_DATA_ADDRESS
Removed, Removed, DEVICE_ReadRegisterData, DEVICE_WriteRegisterData,
// 16 GET_CAP_RANGE 17 SET_RGB2 18 READ_LOG 19 RESTORE_STANDALONE
Unimplemented, Removed, Removed, DEVICE_Reset,
// 20 GET_ALTERNATE_HIGH_FREQUENCY 21 SET_RGB_COMMON 22 SET_RGB3 23 START_CTMU
Unimplemented, LIGHT_RGBPin, Removed, CTMU_Start,
MULTIMETER_HighFrequencyAlt, LIGHT_RGBPin, Removed, CTMU_Start,
// 24 STOP_CTMU 25 START_COUNTING 26 FETCH_COUNT 27 FILL_BUFFER
CTMU_Stop, SENSORS_StartCounter, SENSORS_GetCounter, BUFFER_Fill,
},
Expand Down
111 changes: 110 additions & 1 deletion pslab-core.X/helpers/interval.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "../registers/memory/dma.h"
#include "../registers/system/interrupt_manager.h"
#include "../registers/system/pin_manager.h"
#include "../registers/system/watchdog.h"
#include "../registers/timers/tmr2.h"
#include "buffer.h"

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

TMR2_Initialize();
TMR2_PrescalerSet(prescaler & 0xF);
TMR2_SetPrescaler(prescaler & 0xF);
TMR2_Counter16BitSet(1);

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

return SUCCESS;
}

response_t INTERVAL_IntervalMeasure(void) {

uint16_t timeout = UART1_ReadInt(); // t * 64e6 >> 16
uint8_t pins = UART1_Read();
uint8_t modes = UART1_Read();

IC_PARAMS_ConfigureIntervalCaptureWithIC1AndIC2(pins & 0xF,
IC_PARAMS_CAPTURE_TIMER_PERIPHERAL,
IC_PARAMS_CAPTURE_INTERRUPT_EVERY_EVENT, modes & 0x7);
IC_PARAMS_ConfigureIntervalCaptureWithIC3AndIC4((pins >> 4) & 0xF,
IC_PARAMS_CAPTURE_TIMER_PERIPHERAL,
IC_PARAMS_CAPTURE_INTERRUPT_EVERY_EVENT, (modes >> 3) & 0x7);

IC_PARAMS_ManualTriggerAll();

while ((!_IC1IF) && (IC2TMR < timeout)) WATCHDOG_TimerClear();
UART1_WriteInt(IC1BUF);
UART1_WriteInt(IC2BUF);

while ((!_IC3IF) && (IC2TMR < timeout)) WATCHDOG_TimerClear();
UART1_WriteInt(IC3BUF);
UART1_WriteInt(IC4BUF);
UART1_WriteInt(IC2TMR);

IC_PARAMS_DisableAllModules();

return SUCCESS;
}

response_t INTERVAL_TimeMeasure(void) {

uint16_t timeout = UART1_ReadInt(); // t * 64e6 >> 16
uint8_t pins = UART1_Read();
uint8_t modes = UART1_Read();
uint8_t intrpts = UART1_Read();

if ((pins & 0xF) == 4 || ((pins >> 4) & 0xF) == 4) {
CMP4_SetupComparator();
CVR_SetupComparator();
}

IC_PARAMS_ConfigureIntervalCaptureWithIC1AndIC2(pins & 0xF,
IC_PARAMS_CAPTURE_TIMER2, (intrpts & 0xF) - 1, modes & 0x7);
IC_PARAMS_ConfigureIntervalCaptureWithIC3AndIC4((pins >> 4) & 0xF,
IC_PARAMS_CAPTURE_TIMER2, ((intrpts >> 4) & 0xF) - 1, (modes >> 3) & 0x7);

TMR2_Initialize();

SetDefaultDIGITAL_STATES();

IC_PARAMS_ManualTriggerAll();
TMR2_Start();

if ((modes >> 6) & 0x1) {
RPOR5bits.RP54R = RPN_DEFAULT_PORT; // Disconnect SQR1 pin
((modes >> 7) & 0x1) ? SQR1_SetHigh() : SQR1_SetLow();
}

while ((!_IC1IF || !_IC3IF) && (IC2TMR < timeout)) WATCHDOG_TimerClear();

uint8_t i;
for (i = 0; i < (intrpts & 0xF); i++) {
UART1_WriteInt(IC1BUF);
UART1_WriteInt(IC2BUF);
}
for (i = 0; i < ((intrpts >> 4) & 0xF); i++) {
UART1_WriteInt(IC3BUF);
UART1_WriteInt(IC4BUF);
}

IC1_InterruptFlagClear();
IC3_InterruptFlagClear();

UART1_WriteInt(IC2TMR);

IC_PARAMS_DisableAllModules();
TMR2_Stop();

return SUCCESS;
}

response_t INTERVAL_UntilEvent(void) {

uint16_t timeout = UART1_ReadInt(); // t * 64e6 >> 16
uint8_t mode = UART1_Read();
uint8_t pin = UART1_Read();

IC_PARAMS_ConfigureIntervalCaptureWithIC1AndIC2(pin & 0xF,
IC_PARAMS_CAPTURE_TIMER_PERIPHERAL, (mode & 0x3) - 1, mode & 0x7);

while (!_IC1IF && (IC2TMR < timeout)) WATCHDOG_TimerClear();

IC1_InterruptFlagClear();

UART1_WriteInt(IC2TMR);

uint8_t i;
for (i = 0; i < (mode & 0x3); i++) {
UART1_WriteInt(IC1BUF);
UART1_WriteInt(IC2BUF);
}

IC_PARAMS_DisableAllModules();
TMR2_Stop();

return SUCCESS;
}
6 changes: 6 additions & 0 deletions pslab-core.X/helpers/interval.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ extern "C" {

response_t INTERVAL_GetState(void);

response_t INTERVAL_IntervalMeasure(void);

response_t INTERVAL_TimeMeasure(void);

response_t INTERVAL_UntilEvent(void);

// Getters and setters

void SetDIGITAL_STATES(uint8_t);
Expand Down
2 changes: 1 addition & 1 deletion pslab-core.X/instruments/logicanalyzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ response_t LOGICANALYZER_FourChannel(void) {
INTERVAL_CaptureFour(points, mode, prescaler);

if (trigger & 1) {
LA_TRIGGER_STATE = trigger & 2 ? RISING_EDGE : FALLING_EDGE;
LA_TRIGGER_STATE = trigger & 2 ? FALLING_EDGE : RISING_EDGE;
LA_TRIGGER_CHANNEL = 0;

if ((trigger >> 2) & 1) {
Expand Down
145 changes: 144 additions & 1 deletion pslab-core.X/instruments/multimeter.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
#include "../commands.h"
#include "../bus/uart/uart1.h"
#include "../helpers/delay.h"
#include "../helpers/interval.h"
#include "../registers/comparators/ic1.h"
#include "../registers/comparators/ic2.h"
#include "../registers/comparators/ic_params.h"
#include "../registers/converters/adc1.h"
#include "../registers/converters/ctmu.h"
#include "../registers/comparators/cmp4.h"
#include "../registers/comparators/cvr.h"
#include "../registers/memory/dma.h"
#include "../registers/system/pin_manager.h"
#include "../registers/timers/timer_params.h"
#include "../registers/timers/tmr2.h"
#include "../registers/timers/tmr3.h"
#include "../registers/timers/tmr5.h"
#include "multimeter.h"

Expand Down Expand Up @@ -47,7 +56,7 @@ void GetCapacitance_InitCTMU_TMR5(uint8_t current_range, uint8_t trim,
CTMUICONbits.IRNG = current_range;

TMR5_Initialize();
TMR5_PrescalerSet(TMR_PRESCALER_64);
TMR5_SetPrescaler(TMR_PRESCALER_64);
TMR5_Period16BitSet(charge_time);
}

Expand Down Expand Up @@ -193,6 +202,140 @@ response_t MULTIMETER_GetCapacitance(void) {
return SUCCESS;
}

response_t MULTIMETER_HighFrequency(void) {

uint8_t config = UART1_Read();

LED_SetLow();

if ((config & 0xF) == 4) {
CVR_SetupComparator();
CMP4_SetupComparator();
}

RPINR3bits.T2CKR = PIN_MANAGER_DIGITAL_PINS[config & 0xF];

TMR2_Initialize();
TMR3_Initialize();
TMR5_Initialize();

TMR2_CombineWithTimer3();
TMR2_SetExternalClockAsSource();
TMR2_SetPrescaler((config >> 4) & 0x4);
TMR5_SetPrescaler(TMR_PRESCALER_256);
TMR5_Period16BitSet(25000); // 100 millisecond sampling

_T5IP = 0x01; // Set Timer5 Interrupt Priority Level
TMR5_InterruptEnable();
TMR2_Start();
TMR5_Start();

TMR5_WaitForInterruptEvent();

LED_SetHigh();
UART1_Write(1); // Scaling factor
UART1_WriteInt(TMR2_Counter16BitGet());
UART1_WriteInt(TMR3_Carry16BitGet());

return SUCCESS;
}

response_t MULTIMETER_HighFrequencyAlt(void) {

uint8_t config = UART1_Read();

LED_SetLow();

if ((config & 0xF) == 4) {
CVR_SetupComparator();
CMP4_SetupComparator();
}

TMR2_Initialize();
TMR2_SetExternalClockAsSource();
TMR2_SetPrescaler((config >> 4) & 0x4);
TMR5_Initialize();
IC1_Initialize();
IC2_Initialize();

RPINR7bits.IC1R = RPN_DEFAULT_PORT;
RPINR7bits.IC2R = RPN_DEFAULT_PORT;
RPINR3bits.T2CKR = PIN_MANAGER_DIGITAL_PINS[config & 0xF];

IC1_SetCaptureTimer(IC_PARAMS_CAPTURE_TIMER2);
IC1_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT_EVERY_SECOND);
IC1_CombineOddEvenICModules();
IC1_UseSourceTo(IC_PARAMS_SOURCE_TASK_TRIGGER);
IC1_SetCaptureMode(IC_PARAMS_CAPTURE_MODE_EVERY_16TH_RISING_EDGE);

IC2_SetCaptureTimer(IC_PARAMS_CAPTURE_TIMER2);
IC2_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT_EVERY_SECOND);
IC2_CombineOddEvenICModules();
IC2_UseSourceTo(IC_PARAMS_SOURCE_TASK_TRIGGER);
IC2_SetCaptureMode(IC_PARAMS_CAPTURE_MODE_EVERY_16TH_RISING_EDGE);

TMR5_SetPrescaler(TMR_PRESCALER_256);
TMR5_Period16BitSet(25000); // 100 millisecond sampling

_T5IP = 0x01;
TMR2_Start();
TMR5_Start();
IC1_ManualTriggerSet();
IC1_ManualTriggerSet();

TMR5_WaitForInterruptEvent();

LED_SetHigh();
UART1_Write(1); // Scaling factor
UART1_WriteInt(IC1_CaptureTimeRead());
UART1_WriteInt(IC2_CaptureTimeRead());

return SUCCESS;
}

response_t MULTIMETER_LowFrequency(void) {

uint16_t timeout = UART1_ReadInt();
uint8_t config = UART1_Read();

RPINR7bits.IC1R = PIN_MANAGER_DIGITAL_PINS[config & 0xF];

IC1_Initialize();
IC1_SetCaptureTimer(IC_PARAMS_CAPTURE_TIMER_PERIPHERAL);
IC1_CombineOddEvenICModules();
IC1_SetCaptureMode(IC_PARAMS_CAPTURE_MODE_EVERY_16TH_RISING_EDGE);
IC1_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT_EVERY_SECOND);

IC2_Initialize();
IC2_SetCaptureTimer(IC_PARAMS_CAPTURE_TIMER_PERIPHERAL);
IC2_CombineOddEvenICModules();
IC2_SetCaptureMode(IC_PARAMS_CAPTURE_MODE_EVERY_16TH_RISING_EDGE);
IC2_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT_EVERY_SECOND);

IC1_ManualTriggerSet();
IC2_ManualTriggerSet();
SetDefaultDIGITAL_STATES();

IC1_InterruptFlagClear();
while ((IC2_CaptureTimeRead() < timeout) && (!_IC1IF));
IC1_InterruptFlagClear();

RPINR7bits.IC1R = RPN_DEFAULT_PORT;

if ((IC2_CaptureTimeRead() >= timeout) ||
(IC2_HasCaptureBufferOverflowed())) {
UART1_Write(1);
} else {
UART1_Write(0);
}

UART1_WriteInt(IC1_CaptureDataRead());
UART1_WriteInt(IC2_CaptureDataRead());
IC_PARAMS_DisableAllModules();

return SUCCESS;
}

response_t MULTIMETER_GetCTMUVolts(void) {

uint8_t config = UART1_Read();
Expand Down
5 changes: 5 additions & 0 deletions pslab-core.X/instruments/multimeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ extern "C" {
*/
response_t MULTIMETER_GetCapacitance(void);

response_t MULTIMETER_HighFrequency(void);

response_t MULTIMETER_HighFrequencyAlt(void);

response_t MULTIMETER_LowFrequency(void);
/**
* @brief Measurements using Charge Time Measurement Unit
*
Expand Down
4 changes: 4 additions & 0 deletions pslab-core.X/registers/comparators/ic1.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ uint16_t IC1_CaptureDataRead(void) {
return (IC1BUF);
}

uint16_t IC1_CaptureTimeRead(void) {
return (IC1TMR);
}

void IC1_ManualTriggerSet(void) {
IC1CON2bits.TRIGSTAT = true;
}
Expand Down
6 changes: 6 additions & 0 deletions pslab-core.X/registers/comparators/ic1.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ extern "C" {
IC1CON2bits.IC32 = 1;
}

uint16_t IC1_CaptureTimeRead(void);

inline static void IC1_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT i) {
IC1CON1bits.ICI = i;
}

inline static void IC1_UseSourceTo(IC_PARAMS_SOURCE_TASK t) {
IC1CON2bits.ICTRIG = t;
}
Expand Down
Loading

0 comments on commit c008078

Please sign in to comment.