From a3ae1e42d181014090935a870be128e4e00ffa5b Mon Sep 17 00:00:00 2001 From: Padmal Date: Sat, 24 Sep 2022 08:16:28 +0200 Subject: [PATCH 1/3] refactor: timer modules with comments and combined blocks --- src/registers/timers/tmr2.h | 54 +++++++++++++++++++++++++++++++++++-- src/registers/timers/tmr3.c | 10 ++++++- src/registers/timers/tmr3.h | 20 ++++++++++++++ src/registers/timers/tmr5.h | 17 ++++++++++++ 4 files changed, 98 insertions(+), 3 deletions(-) diff --git a/src/registers/timers/tmr2.h b/src/registers/timers/tmr2.h index 46b03a9f..aca7538a 100644 --- a/src/registers/timers/tmr2.h +++ b/src/registers/timers/tmr2.h @@ -117,8 +117,58 @@ extern "C" { None */ void TMR2_Stop(void); - - inline static void TMR2_PrescalerSet(TIMER_PARAMS_PRESCALER scale) { + + /** + @Summary + Combines Timer 2 and Timer 3 modules + + @Description + This routine combines Timer 2 and Timer 3 modules to form + a 32-bit timer module + + @Param + None + + @Returns + None. + */ + inline static void TMR2_CombineWithTimer3(void) { + T2CONbits.T32 = 1; + } + + /** + @Summary + Enable an external pin as timer clock source + + @Description + This routine enables one of the available external pins + to become the clock source for the Timer 2 module + + @Param + None + + @Returns + None. + */ + inline static void TMR2_SetExternalClockAsSource(void) { + T2CONbits.TCS = 1; + } + + /** + @Summary + Sets pre-scaler for Timer 2 module + + @Description + This routine defines the down sample rate for the operating frequency + of Timer 2 module + + @Param + scale: pre-scaler ratio (TIMER_PARAMS_PRESCALER) + + @Returns + None. + */ + inline static void TMR2_SetPrescaler(TIMER_PARAMS_PRESCALER scale) { T2CONbits.TCKPS = scale; } diff --git a/src/registers/timers/tmr3.c b/src/registers/timers/tmr3.c index 8869b123..3832e922 100644 --- a/src/registers/timers/tmr3.c +++ b/src/registers/timers/tmr3.c @@ -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 @@ -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) { @@ -36,6 +40,10 @@ uint16_t TMR3_Counter16BitGet(void) { return (TMR3); } +uint16_t TMR3_Carry16BitGet(void) { + return TMR3HLD; +} + void TMR3_Start(void) { /* Start the Timer */ T3CONbits.TON = 1; diff --git a/src/registers/timers/tmr3.h b/src/registers/timers/tmr3.h index 5e9286bc..eeb17a62 100644 --- a/src/registers/timers/tmr3.h +++ b/src/registers/timers/tmr3.h @@ -86,6 +86,26 @@ extern "C" { 16-bit current counter value */ uint16_t TMR3_Counter16BitGet(void); + + /** + @Summary + Provides 16-bit hold carry counter value + + @Description + When TMR2 and TMR3 are cascaded to form a single 32-bit timer, + TMR2 hold the Least Significant Word (LSW) and TMR3 holds the + Most Significant Word (MSW). When the LSW is read from TMR2, + the current value of TMR3 is moved to the TMR3HLD register. + The MSW can then be read from TMR3HLD without worrying about + the delay between reads. + + @Param + None. + + @Returns + 16-bit current hold carry value + */ + uint16_t TMR3_Carry16BitGet(void); /** @Summary diff --git a/src/registers/timers/tmr5.h b/src/registers/timers/tmr5.h index dcec9cdc..61fbea79 100644 --- a/src/registers/timers/tmr5.h +++ b/src/registers/timers/tmr5.h @@ -170,6 +170,23 @@ extern "C" { inline static void TMR5_InterruptDisable(void) { IEC1bits.T5IE = 0; } + + /** + @Summary + Enables the TMR interrupt. + + @Description + This routine enables the TMR interrupt. + + @Param + None. + + @Returns + None + */ + inline static void TMR5_InterruptEnable(void) { + IEC1bits.T5IE = 1; + } /** @Summary From 6d7193efaa21b00555d895504397d9141b77c769 Mon Sep 17 00:00:00 2001 From: Padmal Date: Sat, 24 Sep 2022 08:18:51 +0200 Subject: [PATCH 2/3] refactor: update method calls for timers --- src/helpers/interval.c | 2 +- src/instruments/multimeter.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/helpers/interval.c b/src/helpers/interval.c index 89bbc9cf..a77f949c 100644 --- a/src/helpers/interval.c +++ b/src/helpers/interval.c @@ -170,7 +170,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); diff --git a/src/instruments/multimeter.c b/src/instruments/multimeter.c index 54383511..ac090673 100644 --- a/src/instruments/multimeter.c +++ b/src/instruments/multimeter.c @@ -47,7 +47,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); } From 57509a6b004e37d578f8de3f54b06c378807d53a Mon Sep 17 00:00:00 2001 From: Padmal Date: Sat, 24 Sep 2022 08:20:45 +0200 Subject: [PATCH 3/3] refactor: ic modules with comments and combined blocks --- src/registers/comparators/ic1.h | 59 ++++++++++++++++++++- src/registers/comparators/ic2.h | 59 ++++++++++++++++++++- src/registers/comparators/ic3.h | 59 ++++++++++++++++++++- src/registers/comparators/ic4.h | 75 ++++++++++++++++++++++++++- src/registers/comparators/ic_params.c | 51 +++++++++++++++++- src/registers/comparators/ic_params.h | 50 ++++++++++++++++++ 6 files changed, 344 insertions(+), 9 deletions(-) diff --git a/src/registers/comparators/ic1.h b/src/registers/comparators/ic1.h index 84de5db2..2f419dbd 100644 --- a/src/registers/comparators/ic1.h +++ b/src/registers/comparators/ic1.h @@ -277,11 +277,66 @@ extern "C" { inline static void IC1_CombineOddEvenICModules(void) { IC1CON2bits.IC32 = 1; } - + + /** + @Summary + Sets the trigger event for capture complete interrupt + + @Description + This routine sets the trigger event for the input capture module to + notify the ISR that a capture event is complete + + @Preconditions + IC1_Initialize function should have been called + + @Param + IC_PARAMS_CAPTURE_INTERRUPT + + @Returns + None + */ + inline static void IC1_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT i) { + IC1CON1bits.ICI = i; + } + + /** + @Summary + Defines the purpose of input capture pin source + + @Description + This routine defines if the pin attached to IC module is used either to + trigger the IC1TMR or to synchronize the timer with another timer module. + + @Preconditions + IC1_Initialize function should have been called + + @Param + IC_PARAMS_SOURCE_TASK + + @Returns + None + */ inline static void IC1_UseSourceTo(IC_PARAMS_SOURCE_TASK t) { IC1CON2bits.ICTRIG = t; } - + + /** + @Summary + Clears the set interrupt flag of IC1 capture interrupt + + @Description + This routine will clear the interrupt flag of the IC1 capture + module. + + @Preconditions + IC1_Initialize function should have been called + + @Param + None + + @Returns + None + */ inline static void IC1_InterruptFlagClear(void) { IFS0bits.IC1IF = 0; } diff --git a/src/registers/comparators/ic2.h b/src/registers/comparators/ic2.h index 0ab6865e..d5bd13d7 100644 --- a/src/registers/comparators/ic2.h +++ b/src/registers/comparators/ic2.h @@ -283,11 +283,66 @@ extern "C" { inline static void IC2_CombineOddEvenICModules(void) { IC2CON2bits.IC32 = 1; } - + + /** + @Summary + Sets the trigger event for capture complete interrupt + + @Description + This routine sets the trigger event for the input capture module to + notify the ISR that a capture event is complete + + @Preconditions + IC2_Initialize function should have been called + + @Param + IC_PARAMS_CAPTURE_INTERRUPT + + @Returns + None + */ + inline static void IC2_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT i) { + IC2CON1bits.ICI = i; + } + + /** + @Summary + Defines the purpose of input capture pin source + + @Description + This routine defines if the pin attached to IC module is used either to + trigger the IC2TMR or to synchronize the timer with another timer module. + + @Preconditions + IC2_Initialize function should have been called + + @Param + IC_PARAMS_SOURCE_TASK + + @Returns + None + */ inline static void IC2_UseSourceTo(IC_PARAMS_SOURCE_TASK t) { IC2CON2bits.ICTRIG = t; } - + + /** + @Summary + Clears the set interrupt flag of IC2 capture interrupt + + @Description + This routine will clear the interrupt flag of the IC2 capture + module. + + @Preconditions + IC2_Initialize function should have been called + + @Param + None + + @Returns + None + */ inline static void IC2_InterruptFlagClear(void) { IFS0bits.IC2IF = 0; } diff --git a/src/registers/comparators/ic3.h b/src/registers/comparators/ic3.h index 2d96dd57..939addeb 100644 --- a/src/registers/comparators/ic3.h +++ b/src/registers/comparators/ic3.h @@ -283,11 +283,66 @@ extern "C" { inline static void IC3_CombineOddEvenICModules(void) { IC3CON2bits.IC32 = 1; } - + + /** + @Summary + Sets the trigger event for capture complete interrupt + + @Description + This routine sets the trigger event for the input capture module to + notify the ISR that a capture event is complete + + @Preconditions + IC3_Initialize function should have been called + + @Param + IC_PARAMS_CAPTURE_INTERRUPT + + @Returns + None + */ + inline static void IC3_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT i) { + IC3CON1bits.ICI = i; + } + + /** + @Summary + Defines the purpose of input capture pin source + + @Description + This routine defines if the pin attached to IC module is used either to + trigger the IC3TMR or to synchronize the timer with another timer module. + + @Preconditions + IC3_Initialize function should have been called + + @Param + IC_PARAMS_SOURCE_TASK + + @Returns + None + */ inline static void IC3_UseSourceTo(IC_PARAMS_SOURCE_TASK t) { IC3CON2bits.ICTRIG = t; } - + + /** + @Summary + Clears the set interrupt flag of IC3 capture interrupt + + @Description + This routine will clear the interrupt flag of the IC3 capture + module. + + @Preconditions + IC3_Initialize function should have been called + + @Param + None + + @Returns + None + */ inline static void IC3_InterruptFlagClear(void) { IFS2bits.IC3IF = 0; } diff --git a/src/registers/comparators/ic4.h b/src/registers/comparators/ic4.h index 19413c64..ccece2fe 100644 --- a/src/registers/comparators/ic4.h +++ b/src/registers/comparators/ic4.h @@ -283,18 +283,89 @@ extern "C" { inline static void IC4_CombineOddEvenICModules(void) { IC4CON2bits.IC32 = 1; } - + + /** + @Summary + Defines the purpose of input capture pin source + + @Description + This routine defines if the pin attached to IC module is used either to + trigger the IC4TMR or to synchronize the timer with another timer module. + + @Preconditions + IC4_Initialize function should have been called + + @Param + IC_PARAMS_SOURCE_TASK + + @Returns + None + */ inline static void IC4_UseSourceTo(IC_PARAMS_SOURCE_TASK t) { IC4CON2bits.ICTRIG = t; } + /** + @Summary + Clears the set interrupt flag of IC4 capture interrupt + + @Description + This routine will clear the interrupt flag of the IC4 capture + module. + + @Preconditions + IC4_Initialize function should have been called + + @Param + None + + @Returns + None + */ inline static void IC4_InterruptFlagClear(void) { IFS2bits.IC4IF = 0; } - + + /** + @Summary + Enable IC4 interrupt + + @Description + This routine will enable IC4 interrupt. + + @Preconditions + IC4_Initialize function should have been called + + @Param + None + + @Returns + None + */ inline static void IC4_InterruptEnable(void) { IEC2bits.IC4IE = 1; } + + /** + @Summary + Sets the trigger event for capture complete interrupt + + @Description + This routine sets the trigger event for the input capture module to + notify the ISR that a capture event is complete + + @Preconditions + IC4_Initialize function should have been called + + @Param + IC_PARAMS_CAPTURE_INTERRUPT + + @Returns + None + */ + inline static void IC4_InputCaptureInterruptOn(IC_PARAMS_CAPTURE_INTERRUPT i) { + IC4CON1bits.ICI = i; + } inline static void IC4_InterruptDisable(void) { IEC2bits.IC4IE = 0; diff --git a/src/registers/comparators/ic_params.c b/src/registers/comparators/ic_params.c index 0cd53fd9..fb8dceca 100644 --- a/src/registers/comparators/ic_params.c +++ b/src/registers/comparators/ic_params.c @@ -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(); @@ -63,4 +64,52 @@ void IC_PARAMS_SetCaptureTimer(IC_PARAMS_CAPTURE_TIMER t) { IC2_SetCaptureTimer(t); IC3_SetCaptureTimer(t); IC4_SetCaptureTimer(t); -} \ No newline at end of file +} + +void IC_PARAMS_ConfigureIntervalCaptureWithIC1AndIC2(uint8_t pin, + IC_PARAMS_CAPTURE_TIMER timer, + IC_PARAMS_CAPTURE_INTERRUPT interrupt, + IC_PARAMS_CAPTURE_MODE mode) { + + IC1_InterruptFlagClear(); + + RPINR7bits.IC1R = PIN_MANAGER_DIGITAL_PINS[pin]; + + IC1_Initialize(); + IC1_CombineOddEvenICModules(); + IC1_SetCaptureTimer(timer); + IC1_InputCaptureInterruptOn(interrupt); + IC1_UseSourceTo(IC_PARAMS_SOURCE_TASK_TRIGGER); + IC1_SetCaptureMode(mode); + + IC2_Initialize(); + IC2_CombineOddEvenICModules(); + IC2_SetCaptureTimer(timer); + IC2_InputCaptureInterruptOn(interrupt); + 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 interrupt, + IC_PARAMS_CAPTURE_MODE mode) { + + IC3_InterruptFlagClear(); + + RPINR8bits.IC3R = PIN_MANAGER_DIGITAL_PINS[pin]; + + IC3_Initialize(); + IC3_CombineOddEvenICModules(); + IC3_SetCaptureTimer(timer); + IC3_InputCaptureInterruptOn(interrupt); + IC3_UseSourceTo(IC_PARAMS_SOURCE_TASK_TRIGGER); + IC3_SetCaptureMode(mode); + + IC4_Initialize(); + IC4_CombineOddEvenICModules(); + IC4_SetCaptureTimer(timer); + IC4_InputCaptureInterruptOn(interrupt); + IC4_UseSourceTo(IC_PARAMS_SOURCE_TASK_TRIGGER); + IC4_SetCaptureMode(mode); +} diff --git a/src/registers/comparators/ic_params.h b/src/registers/comparators/ic_params.h index 90659d48..d38e83fa 100644 --- a/src/registers/comparators/ic_params.h +++ b/src/registers/comparators/ic_params.h @@ -88,6 +88,56 @@ extern "C" { void IC_PARAMS_CombineOddEvenModules(void); void IC_PARAMS_SetCaptureTimer(IC_PARAMS_CAPTURE_TIMER); + + /** + @Summary + Combines IC1 and IC2 modules to enable concatenated operation + + @Description + This routine combines both IC1 and IC2 capture modules to act + them as 32-bit counters + + @Preconditions + None + + @Param + pin: source pin for the input capture clock module (PIN_MANAGER_DIGITAL_PINS) + timer: timer to run the capture modules (IC_PARAMS_CAPTURE_TIMER) + interrupt: interrupt trigger event (IC_PARAMS_CAPTURE_INTERRUPT) + mode: capture event (IC_PARAMS_CAPTURE_MODE) + + @Returns + None. + */ + void IC_PARAMS_ConfigureIntervalCaptureWithIC1AndIC2(uint8_t pin, + IC_PARAMS_CAPTURE_TIMER timer, + IC_PARAMS_CAPTURE_INTERRUPT interrupt, + IC_PARAMS_CAPTURE_MODE mode); + + /** + @Summary + Combines IC3 and IC4 modules to enable concatenated operation + + @Description + This routine combines both IC3 and IC4 capture modules to act + them as 32-bit counters + + @Preconditions + None + + @Param + pin: source pin for the input capture clock module (PIN_MANAGER_DIGITAL_PINS) + timer: timer to run the capture modules (IC_PARAMS_CAPTURE_TIMER) + interrupt: interrupt trigger event (IC_PARAMS_CAPTURE_INTERRUPT) + mode: capture event (IC_PARAMS_CAPTURE_MODE) + + @Returns + None. + */ + void IC_PARAMS_ConfigureIntervalCaptureWithIC3AndIC4(uint8_t pin, + IC_PARAMS_CAPTURE_TIMER timer, + IC_PARAMS_CAPTURE_INTERRUPT interrupt, + IC_PARAMS_CAPTURE_MODE mode); #ifdef __cplusplus }