99#include "../registers/memory/dma.h"
1010#include "../registers/system/interrupt_manager.h"
1111#include "../registers/system/pin_manager.h"
12+ #include "../registers/system/watchdog.h"
1213#include "../registers/timers/tmr2.h"
1314#include "buffer.h"
1415
@@ -210,3 +211,111 @@ response_t INTERVAL_GetState(void) {
210211
211212 return SUCCESS ;
212213}
214+
215+ response_t INTERVAL_IntervalMeasure (void ) {
216+
217+ uint16_t timeout = UART1_ReadInt (); // t * 64e6 >> 16
218+ uint8_t pins = UART1_Read ();
219+ uint8_t modes = UART1_Read ();
220+
221+ IC_PARAMS_ConfigureIntervalCaptureWithIC1AndIC2 (pins & 0xF ,
222+ IC_PARAMS_CAPTURE_TIMER_PERIPHERAL ,
223+ IC_PARAMS_CAPTURE_INTERRUPT_EVERY_EVENT , modes & 0x7 );
224+ IC_PARAMS_ConfigureIntervalCaptureWithIC3AndIC4 ((pins >> 4 ) & 0xF ,
225+ IC_PARAMS_CAPTURE_TIMER_PERIPHERAL ,
226+ IC_PARAMS_CAPTURE_INTERRUPT_EVERY_EVENT , (modes >> 3 ) & 0x7 );
227+
228+ IC_PARAMS_ManualTriggerAll ();
229+
230+ while ((!_IC1IF ) && (IC2TMR < timeout )) WATCHDOG_TimerClear ();
231+ UART1_WriteInt (IC1BUF );
232+ UART1_WriteInt (IC2BUF );
233+
234+ while ((!_IC3IF ) && (IC2TMR < timeout )) WATCHDOG_TimerClear ();
235+ UART1_WriteInt (IC3BUF );
236+ UART1_WriteInt (IC4BUF );
237+ UART1_WriteInt (IC2TMR );
238+
239+ IC_PARAMS_DisableAllModules ();
240+
241+ return SUCCESS ;
242+ }
243+
244+ response_t INTERVAL_TimeMeasure (void ) {
245+
246+ uint16_t timeout = UART1_ReadInt (); // t * 64e6 >> 16
247+ uint8_t pins = UART1_Read ();
248+ uint8_t modes = UART1_Read ();
249+ uint8_t intrpts = UART1_Read ();
250+
251+ if ((pins & 0xF ) == 4 || ((pins >> 4 ) & 0xF ) == 4 ) {
252+ CMP4_SetupComparator ();
253+ CVR_SetupComparator ();
254+ }
255+
256+ IC_PARAMS_ConfigureIntervalCaptureWithIC1AndIC2 (pins & 0xF ,
257+ IC_PARAMS_CAPTURE_TIMER2 , (intrpts & 0xF ) - 1 , modes & 0x7 );
258+ IC_PARAMS_ConfigureIntervalCaptureWithIC3AndIC4 ((pins >> 4 ) & 0xF ,
259+ IC_PARAMS_CAPTURE_TIMER2 , ((intrpts >> 4 ) & 0xF ) - 1 , (modes >> 3 ) & 0x7 );
260+
261+ TMR2_Initialize ();
262+
263+ SetDefaultDIGITAL_STATES ();
264+
265+ IC_PARAMS_ManualTriggerAll ();
266+ TMR2_Start ();
267+
268+ if ((modes >> 6 ) & 0x1 ) {
269+ RPOR5bits .RP54R = RPN_DEFAULT_PORT ; // Disconnect SQR1 pin
270+ ((modes >> 7 ) & 0x1 ) ? SQR1_SetHigh () : SQR1_SetLow ();
271+ }
272+
273+ while ((!_IC1IF || !_IC3IF ) && (IC2TMR < timeout )) WATCHDOG_TimerClear ();
274+
275+ uint8_t i ;
276+ for (i = 0 ; i < (intrpts & 0xF ); i ++ ) {
277+ UART1_WriteInt (IC1BUF );
278+ UART1_WriteInt (IC2BUF );
279+ }
280+ for (i = 0 ; i < ((intrpts >> 4 ) & 0xF ); i ++ ) {
281+ UART1_WriteInt (IC3BUF );
282+ UART1_WriteInt (IC4BUF );
283+ }
284+
285+ IC1_InterruptFlagClear ();
286+ IC3_InterruptFlagClear ();
287+
288+ UART1_WriteInt (IC2TMR );
289+
290+ IC_PARAMS_DisableAllModules ();
291+ TMR2_Stop ();
292+
293+ return SUCCESS ;
294+ }
295+
296+ response_t INTERVAL_UntilEvent (void ) {
297+
298+ uint16_t timeout = UART1_ReadInt (); // t * 64e6 >> 16
299+ uint8_t mode = UART1_Read ();
300+ uint8_t pin = UART1_Read ();
301+
302+ IC_PARAMS_ConfigureIntervalCaptureWithIC1AndIC2 (pin & 0xF ,
303+ IC_PARAMS_CAPTURE_TIMER_PERIPHERAL , (mode & 0x3 ) - 1 , mode & 0x7 );
304+
305+ while (!_IC1IF && (IC2TMR < timeout )) WATCHDOG_TimerClear ();
306+
307+ IC1_InterruptFlagClear ();
308+
309+ UART1_WriteInt (IC2TMR );
310+
311+ uint8_t i ;
312+ for (i = 0 ; i < (mode & 0x3 ); i ++ ) {
313+ UART1_WriteInt (IC1BUF );
314+ UART1_WriteInt (IC2BUF );
315+ }
316+
317+ IC_PARAMS_DisableAllModules ();
318+ TMR2_Stop ();
319+
320+ return SUCCESS ;
321+ }
0 commit comments