static void ppmEdgeCallback(timerCCHandlerRec_t* cbRec, captureCompare_t capture) { UNUSED(cbRec); ppmISREvent(SOURCE_EDGE, capture); int32_t i; uint32_t previousTime = ppmDev.currentTime; uint32_t previousCapture = ppmDev.currentCapture; /* Grab the new count */ uint32_t currentTime = capture; /* Convert to 32-bit timer result */ currentTime += ppmDev.largeCounter; if (capture < previousCapture) { if (ppmDev.overflowed) { currentTime += PPM_TIMER_PERIOD; } } // Divide by 8 if Oneshot125 is active and this is a CC3D board currentTime = currentTime >> ppmCountShift; /* Capture computation */ if (currentTime > previousTime) { ppmDev.deltaTime = currentTime - (previousTime + (ppmDev.overflowed ? (PPM_TIMER_PERIOD >> ppmCountShift) : 0)); } else {
static void ppmOverflowCallback(timerOvrHandlerRec_t* cbRec, captureCompare_t capture) { UNUSED(cbRec); ppmISREvent(SOURCE_OVERFLOW, capture); ppmDev.largeCounter += capture + 1; if (capture == PPM_TIMER_PERIOD - 1) { ppmDev.overflowed = true; } }
static void ppmEdgeCallback(timerCCHandlerRec_t* cbRec, captureCompare_t capture) { UNUSED(cbRec); ppmISREvent(SOURCE_EDGE, capture); int32_t i; uint32_t previousTime = ppmDev.currentTime; uint32_t previousCapture = ppmDev.currentCapture; /* Grab the new count */ uint32_t currentTime = capture; /* Convert to 32-bit timer result */ currentTime += ppmDev.largeCounter; if (capture < previousCapture) { if (ppmDev.overflowed) { currentTime += PPM_TIMER_PERIOD; } } // Divide value if Oneshot, Multishot or brushed motors are active and the timer is shared currentTime = currentTime / ppmCountDivisor; /* Capture computation */ if (currentTime > previousTime) { ppmDev.deltaTime = currentTime - (previousTime + (ppmDev.overflowed ? (PPM_TIMER_PERIOD / ppmCountDivisor) : 0)); } else { ppmDev.deltaTime = (PPM_TIMER_PERIOD / ppmCountDivisor) + currentTime - previousTime; } ppmDev.overflowed = false; /* Store the current measurement */ ppmDev.currentTime = currentTime; ppmDev.currentCapture = capture; /* Sync pulse detection */ if (ppmDev.deltaTime > PPM_IN_MIN_SYNC_PULSE_US) { if (ppmDev.pulseIndex == ppmDev.numChannelsPrevFrame && ppmDev.pulseIndex >= PPM_IN_MIN_NUM_CHANNELS && ppmDev.pulseIndex <= PPM_IN_MAX_NUM_CHANNELS) { /* If we see n simultaneous frames of the same number of channels we save it as our frame size */ if (ppmDev.stableFramesSeenCount < PPM_STABLE_FRAMES_REQUIRED_COUNT) { ppmDev.stableFramesSeenCount++; } else { ppmDev.numChannels = ppmDev.pulseIndex; } } else { ppmDev.stableFramesSeenCount = 0; } /* Check if the last frame was well formed */ if (ppmDev.pulseIndex == ppmDev.numChannels && ppmDev.tracking) { /* The last frame was well formed */ for (i = 0; i < ppmDev.numChannels; i++) { captures[i] = ppmDev.captures[i]; } for (i = ppmDev.numChannels; i < PPM_IN_MAX_NUM_CHANNELS; i++) { captures[i] = PPM_RCVR_TIMEOUT; } ppmFrameCount++; } ppmDev.tracking = true; ppmDev.numChannelsPrevFrame = ppmDev.pulseIndex; ppmDev.pulseIndex = 0; /* We rely on the supervisor to set captureValue to invalid if no valid frame is found otherwise we ride over it */ } else if (ppmDev.tracking) { /* Valid pulse duration 0.75 to 2.5 ms*/ if (ppmDev.deltaTime > PPM_IN_MIN_CHANNEL_PULSE_US && ppmDev.deltaTime < PPM_IN_MAX_CHANNEL_PULSE_US && ppmDev.pulseIndex < PPM_IN_MAX_NUM_CHANNELS) { ppmDev.captures[ppmDev.pulseIndex] = ppmDev.deltaTime; ppmDev.pulseIndex++; } else { /* Not a valid pulse duration */ ppmDev.tracking = false; for (i = 0; i < PWM_PORTS_OR_PPM_CAPTURE_COUNT; i++) { ppmDev.captures[i] = PPM_RCVR_TIMEOUT; } } } }