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