Skip to content

feat: interval and timing measurement functions #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 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 Down
109 changes: 109 additions & 0 deletions 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 @@ -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
4 changes: 4 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,10 @@ extern "C" {
IC1CON2bits.IC32 = 1;
}

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
4 changes: 4 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,10 @@ extern "C" {
IC2CON2bits.IC32 = 1;
}

inline static void IC2_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT i) {
IC2CON1bits.ICI = i;
}

inline static void IC2_UseSourceTo(IC_PARAMS_SOURCE_TASK t) {
IC2CON2bits.ICTRIG = t;
}
Expand Down
4 changes: 4 additions & 0 deletions pslab-core.X/registers/comparators/ic3.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ extern "C" {
IC3CON2bits.IC32 = 1;
}

inline static void IC3_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT i) {
IC3CON1bits.ICI = i;
}

inline static void IC3_UseSourceTo(IC_PARAMS_SOURCE_TASK t) {
IC3CON2bits.ICTRIG = t;
}
Expand Down
4 changes: 4 additions & 0 deletions pslab-core.X/registers/comparators/ic4.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ extern "C" {
IEC2bits.IC4IE = 1;
}

inline static void IC4_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT i) {
IC4CON1bits.ICI = i;
}

inline static void IC4_InterruptDisable(void) {
IEC2bits.IC4IE = 0;
}
Expand Down
49 changes: 49 additions & 0 deletions pslab-core.X/registers/comparators/ic_params.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ic2.h"
#include "ic3.h"
#include "ic4.h"
#include "../system/pin_manager.h"

void IC_PARAMS_InitiateAll(void) {
IC1_Initialize();
Expand Down Expand Up @@ -63,4 +64,52 @@ void IC_PARAMS_SetCaptureTimer(IC_PARAMS_CAPTURE_TIMER t) {
IC2_SetCaptureTimer(t);
IC3_SetCaptureTimer(t);
IC4_SetCaptureTimer(t);
}

void IC_PARAMS_ConfigureIntervalCaptureWithIC1AndIC2(uint8_t pin,
IC_PARAMS_CAPTURE_TIMER timer,
IC_PARAMS_CAPTURE_INTERRUPT intrpt,
IC_PARAMS_CAPTURE_MODE mode) {

IC1_InterruptFlagClear();

RPINR7bits.IC1R = PIN_MANAGER_DIGITAL_PINS[pin];

IC1_Initialize();
IC1_CombineOddEvenICModules();
IC1_SetCaptureTimer(timer);
IC1_InputCaptureInterruptOn(intrpt);
IC1_UseSourceTo(IC_PARAMS_SOURCE_TASK_TRIGGER);
IC1_SetCaptureMode(mode);

IC2_Initialize();
IC2_CombineOddEvenICModules();
IC2_SetCaptureTimer(timer);
IC2_InputCaptureInterruptOn(intrpt);
IC2_UseSourceTo(IC_PARAMS_SOURCE_TASK_TRIGGER);
IC2_SetCaptureMode(mode);
}

void IC_PARAMS_ConfigureIntervalCaptureWithIC3AndIC4(uint8_t pin,
IC_PARAMS_CAPTURE_TIMER timer,
IC_PARAMS_CAPTURE_INTERRUPT intrpt,
IC_PARAMS_CAPTURE_MODE mode) {

IC3_InterruptFlagClear();

RPINR8bits.IC3R = PIN_MANAGER_DIGITAL_PINS[pin];

IC3_Initialize();
IC3_CombineOddEvenICModules();
IC3_SetCaptureTimer(timer);
IC3_InputCaptureInterruptOn(intrpt);
IC3_UseSourceTo(IC_PARAMS_SOURCE_TASK_TRIGGER);
IC3_SetCaptureMode(mode);

IC4_Initialize();
IC4_CombineOddEvenICModules();
IC4_SetCaptureTimer(timer);
IC4_InputCaptureInterruptOn(intrpt);
IC4_UseSourceTo(IC_PARAMS_SOURCE_TASK_TRIGGER);
IC4_SetCaptureMode(mode);
}
10 changes: 10 additions & 0 deletions pslab-core.X/registers/comparators/ic_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ extern "C" {
void IC_PARAMS_CombineOddEvenModules(void);

void IC_PARAMS_SetCaptureTimer(IC_PARAMS_CAPTURE_TIMER);

void IC_PARAMS_ConfigureIntervalCaptureWithIC1AndIC2(uint8_t pin,
IC_PARAMS_CAPTURE_TIMER timer,
IC_PARAMS_CAPTURE_INTERRUPT intrpt,
IC_PARAMS_CAPTURE_MODE mode);

void IC_PARAMS_ConfigureIntervalCaptureWithIC3AndIC4(uint8_t pin,
IC_PARAMS_CAPTURE_TIMER timer,
IC_PARAMS_CAPTURE_INTERRUPT intrpt,
IC_PARAMS_CAPTURE_MODE mode);

#ifdef __cplusplus
}
Expand Down