Skip to content

Commit

Permalink
feat: frequency functions
Browse files Browse the repository at this point in the history
  • Loading branch information
CloudyPadmal committed Nov 21, 2021
1 parent 5b32a33 commit 1b37cf9
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 22 deletions.
28 changes: 14 additions & 14 deletions pslab-core.X/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,20 +240,20 @@ command_func_t* const cmd_table[NUM_PRIMARY_CMDS + 1][NUM_SECONDARY_CMDS_MAX + 1
Undefined, Undefined, Undefined, Undefined,
},
{ // 11 COMMON
// 0 1 GET_CTMU_VOLTAGE 2 GET_CAPACITANCE 3 GET_FREQUENCY
Undefined, Unimplemented, MULTIMETER_GetCapacitance, Unimplemented,
// 4 GET_INDUCTANCE 5 GET_VERSION 6 7
Unimplemented, DEVICE_GetVersion, Undefined, Undefined,
// 8 RETRIEVE_BUFFER 9 GET_HIGH_FREQUENCY 10 CLEAR_BUFFER 11 SETRGB
BUFFER_Retrieve, Unimplemented, Unimplemented, Unimplemented,
// 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 SETRGB2 18 READ_LOG 19 RESTORE_STANDALONE
Unimplemented, Unimplemented, Unimplemented, DEVICE_Reset,
// 20 GET_ALTERNATE_HIGH_FREQUENCY 21 22 SETRGB3 23 START_CTMU
Unimplemented, Undefined, Unimplemented, Unimplemented,
// 24 STOP_CTMU 25 START_COUNTING 26 FETCH_COUNT 27 FILL_BUFFER
Unimplemented, SENSORS_StartCounter, SENSORS_GetCounter, Unimplemented,
// 0 1 GET_CTMU_VOLTAGE 2 GET_CAPACITANCE 3 GET_FREQUENCY
Undefined, Unimplemented, 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 SETRGB
BUFFER_Retrieve, MULTIMETER_HighFrequency, Unimplemented, Unimplemented,
// 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 SETRGB2 18 READ_LOG 19 RESTORE_STANDALONE
Unimplemented, Unimplemented, Unimplemented, DEVICE_Reset,
// 20 GET_ALTERNATE_HIGH_FREQUENCY 21 22 SETRGB3 23 START_CTMU
MULTIMETER_HighFrequencyAlt, Undefined, Unimplemented, Unimplemented,
// 24 STOP_CTMU 25 START_COUNTING 26 FETCH_COUNT 27 FILL_BUFFER
Unimplemented, SENSORS_StartCounter, SENSORS_GetCounter, Unimplemented,
},
{ // 12 PASSTHROUGH
// 0 1 2 3
Expand Down
2 changes: 1 addition & 1 deletion pslab-core.X/helpers/interval.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,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 Down
146 changes: 145 additions & 1 deletion pslab-core.X/instruments/multimeter.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
#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"

#define CHARGE 0
#define DISCHARGE 1
Expand Down Expand Up @@ -46,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 @@ -185,3 +195,137 @@ 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;
}
6 changes: 6 additions & 0 deletions pslab-core.X/instruments/multimeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ extern "C" {
* @return SUCCESS
*/
response_t MULTIMETER_GetCapacitance(void);

response_t MULTIMETER_HighFrequency(void);

response_t MULTIMETER_HighFrequencyAlt(void);

response_t MULTIMETER_LowFrequency(void);

#ifdef __cplusplus
}
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
2 changes: 2 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,8 @@ extern "C" {
IC1CON2bits.IC32 = 1;
}

uint16_t IC1_CaptureTimeRead(void);

inline static void IC1_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT i) {
IC1CON1bits.ICI = i;
}
Expand Down
4 changes: 4 additions & 0 deletions pslab-core.X/registers/comparators/ic2.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ uint16_t IC2_CaptureDataRead(void) {
return (IC2BUF);
}

uint16_t IC2_CaptureTimeRead(void) {
return (IC2TMR);
}

void IC2_ManualTriggerSet(void) {
IC2CON2bits.TRIGSTAT = true;
}
Expand Down
2 changes: 2 additions & 0 deletions pslab-core.X/registers/comparators/ic2.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ extern "C" {
IC2CON2bits.IC32 = 1;
}

uint16_t IC2_CaptureTimeRead(void);

inline static void IC2_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT i) {
IC2CON1bits.ICI = i;
}
Expand Down
10 changes: 9 additions & 1 deletion pslab-core.X/registers/timers/tmr2.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,15 @@ extern "C" {
*/
void TMR2_Stop(void);

inline static void TMR2_PrescalerSet(TIMER_PARAMS_PRESCALER scale) {
inline static void TMR2_CombineWithTimer3(void) {
T2CONbits.T32 = 1;
}

inline static void TMR2_SetExternalClockAsSource(void) {
T2CONbits.TCS = 1;
}

inline static void TMR2_SetPrescaler(TIMER_PARAMS_PRESCALER scale) {
T2CONbits.TCKPS = scale;
}

Expand Down
14 changes: 13 additions & 1 deletion pslab-core.X/registers/timers/tmr3.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#include "tmr3.h"

void TMR3_Initialize(void) {
// Clear timer 4 register
// Clear timer 3 register
TMR3 = 0x00;
TMR3HLD = 0x00;
//Period = 0 s; Frequency = 64000000 Hz; PR4 0;
PR3 = 0x00;
// Stops timer
Expand All @@ -16,6 +17,9 @@ void TMR3_Initialize(void) {
T3CONbits.TCKPS = 0b00;
// Internal clock (FP)
T3CONbits.TCS = 0;
// Disable interrupts
_T3IE = 0;
_T3IF = 0;
}

void TMR3_Period16BitSet(uint16_t value) {
Expand All @@ -36,6 +40,14 @@ uint16_t TMR3_Counter16BitGet(void) {
return (TMR3);
}

void TMR3_Carry16BitSet(uint16_t value) {
TMR3HLD = value;
}

uint16_t TMR3_Carry16BitGet(void) {
return TMR3HLD;
}

void TMR3_Start(void) {
/* Start the Timer */
T3CONbits.TON = 1;
Expand Down
30 changes: 30 additions & 0 deletions pslab-core.X/registers/timers/tmr3.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,36 @@ extern "C" {
16-bit current counter value
*/
uint16_t TMR3_Counter16BitGet(void);

/**
@Summary
Updates 16-bit hold carry counter value
@Description
This routine sets 16-bit carry forward value for 32-bit counters
@Param
Hold value
@Returns
None.
*/
void TMR3_Carry16BitSet(uint16_t value);

/**
@Summary
Provides 16-bit hold carry counter value
@Description
This routine provides 16-bit carry forward value from 32-bit counters
@Param
None.
@Returns
16-bit current hold carry value
*/
uint16_t TMR3_Carry16BitGet(void);

/**
@Summary
Expand Down
21 changes: 17 additions & 4 deletions pslab-core.X/registers/timers/tmr5.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,23 @@ extern "C" {
inline static void TMR5_InterruptDisable(void) {
IEC1bits.T5IE = 0;
}

/**
@Summary
Enables the TMR interrupt.
@Description
This routine disables the TMR interrupt.
@Param
None.
@Returns
None
*/
inline static void TMR5_InterruptEnable(void) {
IEC1bits.T5IE = 1;
}

/**
@Summary
Expand All @@ -188,10 +205,6 @@ extern "C" {
IFS1bits.T5IF = 0;
}

inline static void TMR5_PrescalerSet(TIMER_PARAMS_PRESCALER scale) {
T5CONbits.TCKPS = scale;
}

void TMR5_WaitForInterruptEvent(void);

#ifdef __cplusplus // Provide C++ Compatibility
Expand Down

0 comments on commit 1b37cf9

Please sign in to comment.