diff --git a/src/drv_pwm.c b/src/drv_pwm.c index 068ad2adb..1773e5de1 100755 --- a/src/drv_pwm.c +++ b/src/drv_pwm.c @@ -242,13 +242,27 @@ static pwmPortData_t *pwmInConfig(uint8_t port, timerCCCallbackPtr callback, uin return p; } +static void failsafeCheck(uint8_t channel, uint16_t pulse) +{ + static uint8_t goodPulses; + + if (channel < 4 && pulse > failsafeThreshold) + goodPulses |= (1 << channel); // if signal is valid - mark channel as OK + if (goodPulses == 0x0F) { // If first four chanells have good pulses, clear FailSafe counter + goodPulses = 0; + if (failsafeCnt > 20) + failsafeCnt -= 20; + else + failsafeCnt = 0; + } +} + static void ppmCallback(uint8_t port, uint16_t capture) { uint16_t diff; static uint16_t now; static uint16_t last = 0; static uint8_t chan = 0; - static uint8_t GoodPulses; last = now; now = capture; @@ -257,20 +271,11 @@ static void ppmCallback(uint8_t port, uint16_t capture) if (diff > 2700) { // Per http://www.rcgroups.com/forums/showpost.php?p=21996147&postcount=3960 "So, if you use 2.5ms or higher as being the reset for the PPM stream start, you will be fine. I use 2.7ms just to be safe." chan = 0; } else { - if (diff > 750 && diff < 2250 && chan < MAX_INPUTS) { // 750 to 2250 ms is our 'valid' channel range + if (diff > PULSE_MIN && diff < PULSE_MAX && chan < MAX_INPUTS) { // 750 to 2250 ms is our 'valid' channel range captures[chan] = diff; - if (chan < 4 && diff > failsafeThreshold) - GoodPulses |= (1 << chan); // if signal is valid - mark channel as OK - if (GoodPulses == 0x0F) { // If first four chanells have good pulses, clear FailSafe counter - GoodPulses = 0; - if (failsafeCnt > 20) - failsafeCnt -= 20; - else - failsafeCnt = 0; - } + failsafeCheck(chan, diff); } chan++; - failsafeCnt = 0; } } @@ -284,12 +289,13 @@ static void pwmCallback(uint8_t port, uint16_t capture) pwmPorts[port].fall = capture; // compute capture pwmPorts[port].capture = pwmPorts[port].fall - pwmPorts[port].rise; - captures[pwmPorts[port].channel] = pwmPorts[port].capture; + if (pwmPorts[port].capture > PULSE_MIN && pwmPorts[port].capture < PULSE_MAX) { // valid pulse width + captures[pwmPorts[port].channel] = pwmPorts[port].capture; + failsafeCheck(pwmPorts[port].channel, pwmPorts[port].capture); + } // switch state pwmPorts[port].state = 0; pwmICConfig(timerHardware[port].tim, timerHardware[port].channel, TIM_ICPolarity_Rising); - // reset failsafe - failsafeCnt = 0; } } diff --git a/src/drv_pwm.h b/src/drv_pwm.h index 8b597ca5b..918d88f40 100755 --- a/src/drv_pwm.h +++ b/src/drv_pwm.h @@ -3,7 +3,9 @@ #define MAX_MOTORS 12 #define MAX_SERVOS 8 #define MAX_INPUTS 8 -#define PULSE_1MS (1000) // 1ms pulse width +#define PULSE_1MS (1000) // 1ms pulse width +#define PULSE_MIN (750) // minimum PWM pulse width which is considered valid +#define PULSE_MAX (2250) // maximum PWM pulse width which is considered valid typedef struct drv_pwm_config_t { bool enableInput; diff --git a/src/drv_pwm_fy90q.c b/src/drv_pwm_fy90q.c index cb33e6b8e..a4448ff18 100644 --- a/src/drv_pwm_fy90q.c +++ b/src/drv_pwm_fy90q.c @@ -85,7 +85,7 @@ static void ppmIRQHandler(TIM_TypeDef *tim) if (diff > 4000) { chan = 0; } else { - if (diff > 750 && diff < 2250 && chan < 8) { // 750 to 2250 ms is our 'valid' channel range + if (diff > PULSE_MIN && diff < PULSE_MAX && chan < 8) { // 750 to 2250 ms is our 'valid' channel range Inputs[chan].capture = diff; if (chan < 4 && diff > FAILSAFE_DETECT_TRESHOLD) GoodPulses |= (1 << chan); // if signal is valid - mark channel as OK diff --git a/src/mw.c b/src/mw.c index 6655dbbc4..d92fe6eef 100755 --- a/src/mw.c +++ b/src/mw.c @@ -226,13 +226,7 @@ void annexCode(void) uint16_t pwmReadRawRC(uint8_t chan) { - uint16_t data; - - data = pwmRead(mcfg.rcmap[chan]); - if (data < 750 || data > 2250) - data = mcfg.midrc; - - return data; + return pwmRead(mcfg.rcmap[chan]); } void computeRC(void)