Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
cc2500_rx.c
Go to the documentation of this file.
1 #include "cc2500_compat.h"
2 
3 #include "cc2500_rx.h"
4 #include "cc2500_rx_spi.h"
5 #include "cc2500_settings.h"
6 
7 #define UNUSED(x) (void)(x)
8 
9 // CAUTION: LARGE PARTS OF THIS FILE ARE COMMENTED OUT!
10 // betaflight/src/main/rx/rx.c @ 4e8249e
11 /*
12  * This file is part of Cleanflight and Betaflight.
13  *
14  * Cleanflight and Betaflight are free software. You can redistribute
15  * this software and/or modify this software under the terms of the
16  * GNU General Public License as published by the Free Software
17  * Foundation, either version 3 of the License, or (at your option)
18  * any later version.
19  *
20  * Cleanflight and Betaflight are distributed in the hope that they
21  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
22  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23  * See the GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this software.
27  *
28  * If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #include <stdbool.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 
35 #include <string.h>
36 
37 //#include "platform.h"
38 //
39 //#include "build/build_config.h"
40 //#include "build/debug.h"
41 //
42 //#include "common/maths.h"
43 //#include "common/utils.h"
44 //#include "common/filter.h"
45 //
46 //#include "config/config_reset.h"
47 //#include "config/feature.h"
48 //
49 //#include "drivers/adc.h"
50 //#include "drivers/rx/rx_pwm.h"
51 //#include "drivers/rx/rx_spi.h"
52 //#include "drivers/time.h"
53 //
54 //#include "fc/config.h"
55 //#include "fc/rc_controls.h"
56 //#include "fc/rc_modes.h"
57 //
58 //#include "flight/failsafe.h"
59 //
60 //#include "io/serial.h"
61 //
62 //#include "pg/pg.h"
63 //#include "pg/pg_ids.h"
64 //#include "pg/rx.h"
65 //
66 //#include "rx/rx.h"
67 //#include "rx/pwm.h"
68 //#include "rx/fport.h"
69 //#include "rx/sbus.h"
70 //#include "rx/spektrum.h"
71 //#include "rx/srxl2.h"
72 //#include "rx/sumd.h"
73 //#include "rx/sumh.h"
74 //#include "rx/msp.h"
75 //#include "rx/xbus.h"
76 //#include "rx/ibus.h"
77 //#include "rx/jetiexbus.h"
78 //#include "rx/crsf.h"
79 //#include "rx/rx_spi.h"
80 //#include "rx/targetcustomserial.h"
81 //
82 //
83 //const char rcChannelLetters[] = "AERT12345678abcdefgh";
84 
85 static uint16_t rssi = 0; // range: [0;1023]
86 //static uint8_t rssi_dbm = 130; // range: [0;130] display 0 to -130
87 //static timeUs_t lastMspRssiUpdateUs = 0;
88 
90 
91 //#ifdef USE_RX_LINK_QUALITY_INFO
92 //static uint16_t linkQuality = 0;
93 //#endif
94 //
95 //#define MSP_RSSI_TIMEOUT_US 1500000 // 1.5 sec
96 //
97 //#define RSSI_ADC_DIVISOR (4096 / 1024)
98 //#define RSSI_OFFSET_SCALING (1024 / 100.0f)
99 
101 //linkQualitySource_e linkQualitySource;
102 //
103 //static bool rxDataProcessingRequired = false;
104 //static bool auxiliaryProcessingRequired = false;
105 //
106 //static bool rxSignalReceived = false;
107 //static bool rxFlightChannelsValid = false;
108 //static bool rxIsInFailsafeMode = true;
110 
111 //static timeUs_t rxNextUpdateAtUs = 0;
112 //static uint32_t needRxSignalBefore = 0;
114 //static uint32_t suspendRxSignalUntil = 0;
115 //static uint8_t skipRxSamples = 0;
116 //
117 //static int16_t rcRaw[MAX_SUPPORTED_RC_CHANNEL_COUNT]; // interval [1000;2000]
120 
121 #define MAX_INVALID_PULS_TIME 300
122 //#define PPM_AND_PWM_SAMPLE_COUNT 3
123 //
124 //#define DELAY_50_HZ (1000000 / 50)
125 //#define DELAY_33_HZ (1000000 / 33)
126 #define DELAY_10_HZ (1000000 / 10)
127 //#define DELAY_5_HZ (1000000 / 5)
128 //#define SKIP_RC_ON_SUSPEND_PERIOD 1500000 // 1.5 second period in usec (call frequency independent)
129 //#define SKIP_RC_SAMPLES_ON_RESUME 2 // flush 2 samples to drop wrong measurements (timing independent)
130 
133 
134 //PG_REGISTER_ARRAY_WITH_RESET_FN(rxChannelRangeConfig_t, NON_AUX_CHANNEL_COUNT, rxChannelRangeConfigs, PG_RX_CHANNEL_RANGE_CONFIG, 0);
135 //void pgResetFn_rxChannelRangeConfigs(rxChannelRangeConfig_t *rxChannelRangeConfigs)
136 //{
137 // // set default calibration to full range and 1:1 mapping
138 // for (int i = 0; i < NON_AUX_CHANNEL_COUNT; i++) {
139 // rxChannelRangeConfigs[i].min = PWM_RANGE_MIN;
140 // rxChannelRangeConfigs[i].max = PWM_RANGE_MAX;
141 // }
142 //}
143 //
144 //PG_REGISTER_ARRAY_WITH_RESET_FN(rxFailsafeChannelConfig_t, MAX_SUPPORTED_RC_CHANNEL_COUNT, rxFailsafeChannelConfigs, PG_RX_FAILSAFE_CHANNEL_CONFIG, 0);
145 //void pgResetFn_rxFailsafeChannelConfigs(rxFailsafeChannelConfig_t *rxFailsafeChannelConfigs)
146 //{
147 // for (int i = 0; i < MAX_SUPPORTED_RC_CHANNEL_COUNT; i++) {
148 // rxFailsafeChannelConfigs[i].mode = (i < NON_AUX_CHANNEL_COUNT) ? RX_FAILSAFE_MODE_AUTO : RX_FAILSAFE_MODE_HOLD;
149 // rxFailsafeChannelConfigs[i].step = (i == THROTTLE)
150 // ? CHANNEL_VALUE_TO_RXFAIL_STEP(RX_MIN_USEC)
151 // : CHANNEL_VALUE_TO_RXFAIL_STEP(RX_MID_USEC);
152 // }
153 //}
154 //
155 //void resetAllRxChannelRangeConfigurations(rxChannelRangeConfig_t *rxChannelRangeConfig) {
156 // // set default calibration to full range and 1:1 mapping
157 // for (int i = 0; i < NON_AUX_CHANNEL_COUNT; i++) {
158 // rxChannelRangeConfig->min = PWM_RANGE_MIN;
159 // rxChannelRangeConfig->max = PWM_RANGE_MAX;
160 // rxChannelRangeConfig++;
161 // }
162 //}
163 
165 {
167  UNUSED(channel);
168 
169  return PPM_RCVR_TIMEOUT;
170 }
171 
173 {
175 
176  return RX_FRAME_PENDING;
177 }
178 
180 {
182 
183  return true;
184 }
185 
186 //STATIC_UNIT_TESTED bool isPulseValid(uint16_t pulseDuration)
187 //{
188 // return pulseDuration >= rxConfig()->rx_min_usec &&
189 // pulseDuration <= rxConfig()->rx_max_usec;
190 //}
191 //
192 //#ifdef USE_SERIAL_RX
193 //static bool serialRxInit(const rxConfig_t *rxConfig, rxRuntimeConfig_t *rxRuntimeConfig)
194 //{
195 // bool enabled = false;
196 // switch (rxRuntimeConfig->serialrxProvider) {
197 //#ifdef USE_SERIALRX_SRXL2
198 // case SERIALRX_SRXL2:
199 // enabled = srxl2RxInit(rxConfig, rxRuntimeConfig);
200 // break;
201 //#endif
202 //#ifdef USE_SERIALRX_SPEKTRUM
203 // case SERIALRX_SRXL:
204 // case SERIALRX_SPEKTRUM1024:
205 // case SERIALRX_SPEKTRUM2048:
206 // enabled = spektrumInit(rxConfig, rxRuntimeConfig);
207 // break;
208 //#endif
209 //#ifdef USE_SERIALRX_SBUS
210 // case SERIALRX_SBUS:
211 // enabled = sbusInit(rxConfig, rxRuntimeConfig);
212 // break;
213 //#endif
214 //#ifdef USE_SERIALRX_SUMD
215 // case SERIALRX_SUMD:
216 // enabled = sumdInit(rxConfig, rxRuntimeConfig);
217 // break;
218 //#endif
219 //#ifdef USE_SERIALRX_SUMH
220 // case SERIALRX_SUMH:
221 // enabled = sumhInit(rxConfig, rxRuntimeConfig);
222 // break;
223 //#endif
224 //#ifdef USE_SERIALRX_XBUS
225 // case SERIALRX_XBUS_MODE_B:
226 // case SERIALRX_XBUS_MODE_B_RJ01:
227 // enabled = xBusInit(rxConfig, rxRuntimeConfig);
228 // break;
229 //#endif
230 //#ifdef USE_SERIALRX_IBUS
231 // case SERIALRX_IBUS:
232 // enabled = ibusInit(rxConfig, rxRuntimeConfig);
233 // break;
234 //#endif
235 //#ifdef USE_SERIALRX_JETIEXBUS
236 // case SERIALRX_JETIEXBUS:
237 // enabled = jetiExBusInit(rxConfig, rxRuntimeConfig);
238 // break;
239 //#endif
240 //#ifdef USE_SERIALRX_CRSF
241 // case SERIALRX_CRSF:
242 // enabled = crsfRxInit(rxConfig, rxRuntimeConfig);
243 // break;
244 //#endif
245 //#ifdef USE_SERIALRX_TARGET_CUSTOM
246 // case SERIALRX_TARGET_CUSTOM:
247 // enabled = targetCustomSerialRxInit(rxConfig, rxRuntimeConfig);
248 // break;
249 //#endif
250 //#ifdef USE_SERIALRX_FPORT
251 // case SERIALRX_FPORT:
252 // enabled = fportRxInit(rxConfig, rxRuntimeConfig);
253 // break;
254 //#endif
255 // default:
256 // enabled = false;
257 // break;
258 // }
259 // return enabled;
260 //}
261 //#endif
262 
263 void rxInit(void)
264 {
267  } else if (featureIsEnabled(FEATURE_RX_PPM)) {
269  } else if (featureIsEnabled(FEATURE_RX_SERIAL)) {
271  } else if (featureIsEnabled(FEATURE_RX_MSP)) {
273  } else if (featureIsEnabled(FEATURE_RX_SPI)) {
275  } else {
277  }
282  rcSampleIndex = 0;
284 
285  for (int i = 0; i < MAX_SUPPORTED_RC_CHANNEL_COUNT; i++) {
286  rcData[i] = rxConfig()->midrc;
288  }
289 
290 // rcData[THROTTLE] = (featureIsEnabled(FEATURE_3D)) ? rxConfig()->midrc : rxConfig()->rx_min_usec;
291 //
292 // // Initialize ARM switch to OFF position when arming via switch is defined
293 // // TODO - move to rc_mode.c
294 // for (int i = 0; i < MAX_MODE_ACTIVATION_CONDITION_COUNT; i++) {
295 // const modeActivationCondition_t *modeActivationCondition = modeActivationConditions(i);
296 // if (modeActivationCondition->modeId == BOXARM && IS_RANGE_USABLE(&modeActivationCondition->range)) {
297 // // ARM switch is defined, determine an OFF value
298 // uint16_t value;
299 // if (modeActivationCondition->range.startStep > 0) {
300 // value = MODE_STEP_TO_CHANNEL_VALUE((modeActivationCondition->range.startStep - 1));
301 // } else {
302 // value = MODE_STEP_TO_CHANNEL_VALUE((modeActivationCondition->range.endStep + 1));
303 // }
304 // // Initialize ARM AUX channel to OFF value
305 // rcData[modeActivationCondition->auxChannelIndex + NON_AUX_CHANNEL_COUNT] = value;
306 // }
307 // }
308 
309  switch (rxRuntimeConfig.rxProvider) {
310  default:
311 
312  break;
313 #ifdef USE_SERIAL_RX
314  case RX_PROVIDER_SERIAL:
315  {
316  const bool enabled = serialRxInit(rxConfig(), &rxRuntimeConfig);
317  if (!enabled) {
320  }
321  }
322 
323  break;
324 #endif
325 
326 #ifdef USE_RX_MSP
327  case RX_PROVIDER_MSP:
328  rxMspInit(rxConfig(), &rxRuntimeConfig);
329  needRxSignalMaxDelayUs = DELAY_5_HZ;
330 
331  break;
332 #endif
333 
334 #ifdef USE_RX_SPI
335  case RX_PROVIDER_SPI:
336  {
337  const bool enabled = rxSpiInit(rxSpiConfig(), &rxRuntimeConfig);
338  if (!enabled) {
341  }
342  }
343 
344  break;
345 #endif
346 
347 #if defined(USE_PWM) || defined(USE_PPM)
348  case RX_PROVIDER_PPM:
350  rxPwmInit(rxConfig(), &rxRuntimeConfig);
351 
352  break;
353 #endif
354  }
355 
356 #if defined(USE_ADC)
359  } else
360 #endif
361  if (rxConfig()->rssi_channel > 0) {
363  }
364 
365  // Setup source frame RSSI filtering to take averaged values every FRAME_ERR_RESAMPLE_US
367 
369 }
370 
371 //bool rxIsReceivingSignal(void)
372 //{
373 // return rxSignalReceived;
374 //}
375 //
376 //bool rxAreFlightChannelsValid(void)
377 //{
378 // return rxFlightChannelsValid;
379 //}
380 //
381 //void suspendRxPwmPpmSignal(void)
382 //{
383 //#if defined(USE_PWM) || defined(USE_PPM)
384 // if (rxRuntimeConfig.rxProvider == RX_PROVIDER_PARALLEL_PWM || rxRuntimeConfig.rxProvider == RX_PROVIDER_PPM) {
385 // suspendRxSignalUntil = micros() + SKIP_RC_ON_SUSPEND_PERIOD;
386 // skipRxSamples = SKIP_RC_SAMPLES_ON_RESUME;
387 // failsafeOnRxSuspend(SKIP_RC_ON_SUSPEND_PERIOD);
388 // }
389 //#endif
390 //}
391 //
392 //void resumeRxPwmPpmSignal(void)
393 //{
394 //#if defined(USE_PWM) || defined(USE_PPM)
395 // if (rxRuntimeConfig.rxProvider == RX_PROVIDER_PARALLEL_PWM || rxRuntimeConfig.rxProvider == RX_PROVIDER_PPM) {
396 // suspendRxSignalUntil = micros();
397 // skipRxSamples = SKIP_RC_SAMPLES_ON_RESUME;
398 // failsafeOnRxResume();
399 // }
400 //#endif
401 //}
402 //
403 //#ifdef USE_RX_LINK_QUALITY_INFO
404 //#define LINK_QUALITY_SAMPLE_COUNT 16
405 //
406 //STATIC_UNIT_TESTED uint16_t updateLinkQualitySamples(uint16_t value)
407 //{
408 // static uint16_t samples[LINK_QUALITY_SAMPLE_COUNT];
409 // static uint8_t sampleIndex = 0;
410 // static uint16_t sum = 0;
411 //
412 // sum += value - samples[sampleIndex];
413 // samples[sampleIndex] = value;
414 // sampleIndex = (sampleIndex + 1) % LINK_QUALITY_SAMPLE_COUNT;
415 // return sum / LINK_QUALITY_SAMPLE_COUNT;
416 //}
417 //#endif
418 //
419 //static void setLinkQuality(bool validFrame, timeDelta_t currentDeltaTime)
420 //{
421 //#ifdef USE_RX_LINK_QUALITY_INFO
422 // if (linkQualitySource != LQ_SOURCE_RX_PROTOCOL_CRSF) {
423 // // calculate new sample mean
424 // linkQuality = updateLinkQualitySamples(validFrame ? LINK_QUALITY_MAX_VALUE : 0);
425 // }
426 //#endif
427 //
428 // if (rssiSource == RSSI_SOURCE_FRAME_ERRORS) {
429 // static uint16_t tot_rssi = 0;
430 // static uint16_t cnt_rssi = 0;
431 // static timeDelta_t resample_time = 0;
432 //
433 // resample_time += currentDeltaTime;
434 // tot_rssi += validFrame ? RSSI_MAX_VALUE : 0;
435 // cnt_rssi++;
436 //
437 // if (resample_time >= FRAME_ERR_RESAMPLE_US) {
438 // setRssi(tot_rssi / cnt_rssi, rssiSource);
439 // tot_rssi = 0;
440 // cnt_rssi = 0;
441 // resample_time -= FRAME_ERR_RESAMPLE_US;
442 // }
443 // }
444 //}
445 //
446 //void setLinkQualityDirect(uint16_t linkqualityValue)
447 //{
448 //#ifdef USE_RX_LINK_QUALITY_INFO
449 // linkQuality = linkqualityValue;
450 //#else
451 // UNUSED(linkqualityValue);
452 //#endif
453 //}
454 //
455 //bool rxUpdateCheck(timeUs_t currentTimeUs, timeDelta_t currentDeltaTime)
456 //{
457 // bool signalReceived = false;
458 // bool useDataDrivenProcessing = true;
459 //
460 // switch (rxRuntimeConfig.rxProvider) {
461 // default:
462 //
463 // break;
464 //#if defined(USE_PWM) || defined(USE_PPM)
465 // case RX_PROVIDER_PPM:
466 // if (isPPMDataBeingReceived()) {
467 // signalReceived = true;
468 // rxIsInFailsafeMode = false;
469 // needRxSignalBefore = currentTimeUs + needRxSignalMaxDelayUs;
470 // resetPPMDataReceivedState();
471 // }
472 //
473 // break;
474 // case RX_PROVIDER_PARALLEL_PWM:
475 // if (isPWMDataBeingReceived()) {
476 // signalReceived = true;
477 // rxIsInFailsafeMode = false;
478 // needRxSignalBefore = currentTimeUs + needRxSignalMaxDelayUs;
479 // useDataDrivenProcessing = false;
480 // }
481 //
482 // break;
483 //#endif
484 // case RX_PROVIDER_SERIAL:
485 // case RX_PROVIDER_MSP:
486 // case RX_PROVIDER_SPI:
487 // {
488 // const uint8_t frameStatus = rxRuntimeConfig.rcFrameStatusFn(&rxRuntimeConfig);
489 // if (frameStatus & RX_FRAME_COMPLETE) {
490 // rxIsInFailsafeMode = (frameStatus & RX_FRAME_FAILSAFE) != 0;
491 // bool rxFrameDropped = (frameStatus & RX_FRAME_DROPPED) != 0;
492 // signalReceived = !(rxIsInFailsafeMode || rxFrameDropped);
493 // if (signalReceived) {
494 // needRxSignalBefore = currentTimeUs + needRxSignalMaxDelayUs;
495 // }
496 //
497 // setLinkQuality(signalReceived, currentDeltaTime);
498 // }
499 //
500 // if (frameStatus & RX_FRAME_PROCESSING_REQUIRED) {
501 // auxiliaryProcessingRequired = true;
502 // }
503 // }
504 //
505 // break;
506 // }
507 //
508 // if (signalReceived) {
509 // rxSignalReceived = true;
510 // } else if (currentTimeUs >= needRxSignalBefore) {
511 // rxSignalReceived = false;
512 // }
513 //
514 // if ((signalReceived && useDataDrivenProcessing) || cmpTimeUs(currentTimeUs, rxNextUpdateAtUs) > 0) {
515 // rxDataProcessingRequired = true;
516 // }
517 //
518 // return rxDataProcessingRequired || auxiliaryProcessingRequired; // data driven or 50Hz
519 //}
520 //
521 //#if defined(USE_PWM) || defined(USE_PPM)
522 //static uint16_t calculateChannelMovingAverage(uint8_t chan, uint16_t sample)
523 //{
524 // static int16_t rcSamples[MAX_SUPPORTED_RX_PARALLEL_PWM_OR_PPM_CHANNEL_COUNT][PPM_AND_PWM_SAMPLE_COUNT];
525 // static int16_t rcDataMean[MAX_SUPPORTED_RX_PARALLEL_PWM_OR_PPM_CHANNEL_COUNT];
526 // static bool rxSamplesCollected = false;
527 //
528 // const uint8_t currentSampleIndex = rcSampleIndex % PPM_AND_PWM_SAMPLE_COUNT;
529 //
530 // // update the recent samples and compute the average of them
531 // rcSamples[chan][currentSampleIndex] = sample;
532 //
533 // // avoid returning an incorrect average which would otherwise occur before enough samples
534 // if (!rxSamplesCollected) {
535 // if (rcSampleIndex < PPM_AND_PWM_SAMPLE_COUNT) {
536 // return sample;
537 // }
538 // rxSamplesCollected = true;
539 // }
540 //
541 // rcDataMean[chan] = 0;
542 // for (int sampleIndex = 0; sampleIndex < PPM_AND_PWM_SAMPLE_COUNT; sampleIndex++) {
543 // rcDataMean[chan] += rcSamples[chan][sampleIndex];
544 // }
545 // return rcDataMean[chan] / PPM_AND_PWM_SAMPLE_COUNT;
546 //}
547 //#endif
548 //
549 //static uint16_t getRxfailValue(uint8_t channel)
550 //{
551 // const rxFailsafeChannelConfig_t *channelFailsafeConfig = rxFailsafeChannelConfigs(channel);
552 //
553 // switch (channelFailsafeConfig->mode) {
554 // case RX_FAILSAFE_MODE_AUTO:
555 // switch (channel) {
556 // case ROLL:
557 // case PITCH:
558 // case YAW:
559 // return rxConfig()->midrc;
560 // case THROTTLE:
561 // if (featureIsEnabled(FEATURE_3D) && !IS_RC_MODE_ACTIVE(BOX3D) && !flight3DConfig()->switched_mode3d) {
562 // return rxConfig()->midrc;
563 // } else {
564 // return rxConfig()->rx_min_usec;
565 // }
566 // }
567 //
568 // FALLTHROUGH;
569 // default:
570 // case RX_FAILSAFE_MODE_INVALID:
571 // case RX_FAILSAFE_MODE_HOLD:
572 // return rcData[channel];
573 //
574 // case RX_FAILSAFE_MODE_SET:
575 // return RXFAIL_STEP_TO_CHANNEL_VALUE(channelFailsafeConfig->step);
576 // }
577 //}
578 //
579 //STATIC_UNIT_TESTED uint16_t applyRxChannelRangeConfiguraton(int sample, const rxChannelRangeConfig_t *range)
580 //{
581 // // Avoid corruption of channel with a value of PPM_RCVR_TIMEOUT
582 // if (sample == PPM_RCVR_TIMEOUT) {
583 // return PPM_RCVR_TIMEOUT;
584 // }
585 //
586 // sample = scaleRange(sample, range->min, range->max, PWM_RANGE_MIN, PWM_RANGE_MAX);
587 // sample = constrain(sample, PWM_PULSE_MIN, PWM_PULSE_MAX);
588 //
589 // return sample;
590 //}
591 //
592 //static void readRxChannelsApplyRanges(void)
593 //{
594 // for (int channel = 0; channel < rxChannelCount; channel++) {
595 //
596 // const uint8_t rawChannel = channel < RX_MAPPABLE_CHANNEL_COUNT ? rxConfig()->rcmap[channel] : channel;
597 //
598 // // sample the channel
599 // uint16_t sample = rxRuntimeConfig.rcReadRawFn(&rxRuntimeConfig, rawChannel);
600 //
601 // // apply the rx calibration
602 // if (channel < NON_AUX_CHANNEL_COUNT) {
603 // sample = applyRxChannelRangeConfiguraton(sample, rxChannelRangeConfigs(channel));
604 // }
605 //
606 // rcRaw[channel] = sample;
607 // }
608 //}
609 //
610 //static void detectAndApplySignalLossBehaviour(void)
611 //{
612 // const uint32_t currentTimeMs = millis();
613 //
614 // const bool useValueFromRx = rxSignalReceived && !rxIsInFailsafeMode;
615 //
616 // DEBUG_SET(DEBUG_RX_SIGNAL_LOSS, 0, rxSignalReceived);
617 // DEBUG_SET(DEBUG_RX_SIGNAL_LOSS, 1, rxIsInFailsafeMode);
618 //
619 // rxFlightChannelsValid = true;
620 // for (int channel = 0; channel < rxChannelCount; channel++) {
621 // uint16_t sample = rcRaw[channel];
622 //
623 // const bool validPulse = useValueFromRx && isPulseValid(sample);
624 //
625 // if (validPulse) {
626 // rcInvalidPulsPeriod[channel] = currentTimeMs + MAX_INVALID_PULS_TIME;
627 // } else {
628 // if (cmp32(currentTimeMs, rcInvalidPulsPeriod[channel]) < 0) {
629 // continue; // skip to next channel to hold channel value MAX_INVALID_PULS_TIME
630 // } else {
631 // sample = getRxfailValue(channel); // after that apply rxfail value
632 // if (channel < NON_AUX_CHANNEL_COUNT) {
633 // rxFlightChannelsValid = false;
634 // }
635 // }
636 // }
637 //#if defined(USE_PWM) || defined(USE_PPM)
638 // if (rxRuntimeConfig.rxProvider == RX_PROVIDER_PARALLEL_PWM || rxRuntimeConfig.rxProvider == RX_PROVIDER_PPM) {
639 // // smooth output for PWM and PPM
640 // rcData[channel] = calculateChannelMovingAverage(channel, sample);
641 // } else
642 //#endif
643 // {
644 // rcData[channel] = sample;
645 // }
646 // }
647 //
648 // if (rxFlightChannelsValid && !IS_RC_MODE_ACTIVE(BOXFAILSAFE)) {
649 // failsafeOnValidDataReceived();
650 // } else {
651 // rxIsInFailsafeMode = true;
652 // failsafeOnValidDataFailed();
653 // for (int channel = 0; channel < rxChannelCount; channel++) {
654 // rcData[channel] = getRxfailValue(channel);
655 // }
656 // }
657 // DEBUG_SET(DEBUG_RX_SIGNAL_LOSS, 3, rcData[THROTTLE]);
658 //}
659 //
660 //bool calculateRxChannelsAndUpdateFailsafe(timeUs_t currentTimeUs)
661 //{
662 // if (auxiliaryProcessingRequired) {
663 // auxiliaryProcessingRequired = !rxRuntimeConfig.rcProcessFrameFn(&rxRuntimeConfig);
664 // }
665 //
666 // if (!rxDataProcessingRequired) {
667 // return false;
668 // }
669 //
670 // rxDataProcessingRequired = false;
671 // rxNextUpdateAtUs = currentTimeUs + DELAY_33_HZ;
672 //
673 // // only proceed when no more samples to skip and suspend period is over
674 // if (skipRxSamples || currentTimeUs <= suspendRxSignalUntil) {
675 // if (currentTimeUs > suspendRxSignalUntil) {
676 // skipRxSamples--;
677 // }
678 //
679 // return true;
680 // }
681 //
682 // readRxChannelsApplyRanges();
683 // detectAndApplySignalLossBehaviour();
684 //
685 // rcSampleIndex++;
686 //
687 // return true;
688 //}
689 //
690 //void parseRcChannels(const char *input, rxConfig_t *rxConfig)
691 //{
692 // for (const char *c = input; *c; c++) {
693 // const char *s = strchr(rcChannelLetters, *c);
694 // if (s && (s < rcChannelLetters + RX_MAPPABLE_CHANNEL_COUNT)) {
695 // rxConfig->rcmap[s - rcChannelLetters] = c - input;
696 // }
697 // }
698 //}
699 
700 void setRssiDirect(uint16_t newRssi, rssiSource_e source)
701 {
702  if (source != rssiSource) {
703  return;
704  }
705 
706  rssi = newRssi;
707 }
708 
709 #define RSSI_SAMPLE_COUNT 16
710 
712 {
713  static uint16_t samples[RSSI_SAMPLE_COUNT];
714  static uint8_t sampleIndex = 0;
715  static unsigned sum = 0;
716 
717  sum += value - samples[sampleIndex];
718  samples[sampleIndex] = value;
719  sampleIndex = (sampleIndex + 1) % RSSI_SAMPLE_COUNT;
720  return sum / RSSI_SAMPLE_COUNT;
721 }
722 
723 void setRssi(uint16_t rssiValue, rssiSource_e source)
724 {
725  if (source != rssiSource) {
726  return;
727  }
728 
729  // Filter RSSI value
730  if (source == RSSI_SOURCE_FRAME_ERRORS) {
731  rssi = pt1FilterApply(&frameErrFilter, rssiValue);
732  } else {
733  // calculate new sample mean
734  rssi = updateRssiSamples(rssiValue);
735  }
736 }
737 
738 //void setRssiMsp(uint8_t newMspRssi)
739 //{
740 // if (rssiSource == RSSI_SOURCE_NONE) {
741 // rssiSource = RSSI_SOURCE_MSP;
742 // }
743 //
744 // if (rssiSource == RSSI_SOURCE_MSP) {
745 // rssi = ((uint16_t)newMspRssi) << 2;
746 // lastMspRssiUpdateUs = micros();
747 // }
748 //}
749 //
750 //static void updateRSSIPWM(void)
751 //{
752 // // Read value of AUX channel as rssi
753 // int16_t pwmRssi = rcData[rxConfig()->rssi_channel - 1];
754 //
755 // // RSSI_Invert option
756 // if (rxConfig()->rssi_invert) {
757 // pwmRssi = ((2000 - pwmRssi) + 1000);
758 // }
759 //
760 // // Range of rawPwmRssi is [1000;2000]. rssi should be in [0;1023];
761 // setRssiDirect(constrain(((pwmRssi - 1000) / 1000.0f) * RSSI_MAX_VALUE, 0, RSSI_MAX_VALUE), RSSI_SOURCE_RX_CHANNEL);
762 //}
763 //
764 //static void updateRSSIADC(timeUs_t currentTimeUs)
765 //{
766 //#ifndef USE_ADC
767 // UNUSED(currentTimeUs);
768 //#else
769 // static uint32_t rssiUpdateAt = 0;
770 //
771 // if ((int32_t)(currentTimeUs - rssiUpdateAt) < 0) {
772 // return;
773 // }
774 // rssiUpdateAt = currentTimeUs + DELAY_50_HZ;
775 //
776 // const uint16_t adcRssiSample = adcGetChannel(ADC_RSSI);
777 // uint16_t rssiValue = adcRssiSample / RSSI_ADC_DIVISOR;
778 //
779 // // RSSI_Invert option
780 // if (rxConfig()->rssi_invert) {
781 // rssiValue = RSSI_MAX_VALUE - rssiValue;
782 // }
783 //
784 // setRssi(rssiValue, RSSI_SOURCE_ADC);
785 //#endif
786 //}
787 //
788 //void updateRSSI(timeUs_t currentTimeUs)
789 //{
790 // switch (rssiSource) {
791 // case RSSI_SOURCE_RX_CHANNEL:
792 // updateRSSIPWM();
793 // break;
794 // case RSSI_SOURCE_ADC:
795 // updateRSSIADC(currentTimeUs);
796 // break;
797 // case RSSI_SOURCE_MSP:
798 // if (cmpTimeUs(micros(), lastMspRssiUpdateUs) > MSP_RSSI_TIMEOUT_US) {
799 // rssi = 0;
800 // }
801 // break;
802 // default:
803 // break;
804 // }
805 //}
806 //
807 //uint16_t getRssi(void)
808 //{
809 // return rxConfig()->rssi_scale / 100.0f * rssi + rxConfig()->rssi_offset * RSSI_OFFSET_SCALING;
810 //}
811 //
812 //uint8_t getRssiPercent(void)
813 //{
814 // return scaleRange(getRssi(), 0, RSSI_MAX_VALUE, 0, 100);
815 //}
816 //
817 //uint8_t getRssiDbm(void)
818 //{
819 // return rssi_dbm;
820 //}
821 //
822 //#define RSSI_SAMPLE_COUNT_DBM 16
823 //
824 //static uint8_t updateRssiDbmSamples(uint8_t value)
825 //{
826 // static uint16_t samplesdbm[RSSI_SAMPLE_COUNT_DBM];
827 // static uint8_t sampledbmIndex = 0;
828 // static unsigned sumdbm = 0;
829 //
830 // sumdbm += value - samplesdbm[sampledbmIndex];
831 // samplesdbm[sampledbmIndex] = value;
832 // sampledbmIndex = (sampledbmIndex + 1) % RSSI_SAMPLE_COUNT_DBM;
833 // return sumdbm / RSSI_SAMPLE_COUNT_DBM;
834 //}
835 //
836 //void setRssiDbm(uint8_t rssiDbmValue, rssiSource_e source)
837 //{
838 // if (source != rssiSource) {
839 // return;
840 // }
841 //
842 // rssi_dbm = updateRssiDbmSamples(rssiDbmValue);
843 //}
844 //
845 //void setRssiDbmDirect(uint8_t newRssiDbm, rssiSource_e source)
846 //{
847 // if (source != rssiSource) {
848 // return;
849 // }
850 //
851 // rssi_dbm = newRssiDbm;
852 //}
853 //
854 //#ifdef USE_RX_LINK_QUALITY_INFO
855 //uint16_t rxGetLinkQuality(void)
856 //{
857 // return linkQuality;
858 //}
859 //
860 //uint16_t rxGetLinkQualityPercent(void)
861 //{
862 // return (linkQualitySource == LQ_SOURCE_RX_PROTOCOL_CRSF) ? (linkQuality / 3.41) : scaleRange(linkQuality, 0, LINK_QUALITY_MAX_VALUE, 0, 100);
863 //}
864 //#endif
865 //
866 //uint16_t rxGetRefreshRate(void)
867 //{
868 // return rxRuntimeConfig.rxRefreshRate;
869 //}
870 //
871 //bool isRssiConfigured(void)
872 //{
873 // return rssiSource != RSSI_SOURCE_NONE;
874 //}
uint16_t
unsigned short uint16_t
Definition: types.h:16
RX_PROVIDER_NONE
@ RX_PROVIDER_NONE
Definition: cc2500_rx.h:134
FEATURE_RX_PPM
@ FEATURE_RX_PPM
Definition: cc2500_compat.h:136
FEATURE_RX_SPI
@ FEATURE_RX_SPI
Definition: cc2500_compat.h:155
rxRuntimeConfig_s::rxProvider
rxProvider_t rxProvider
Definition: cc2500_rx.h:143
rxRuntimeConfig_s::rcProcessFrameFn
rcProcessFrameFnPtr rcProcessFrameFn
Definition: cc2500_rx.h:149
rssiSource_e
rssiSource_e
Definition: cc2500_rx.h:154
channel
static uint8_t channel
Definition: ADS8344.c:80
rxConfig_s::midrc
uint16_t midrc
Definition: cc2500_settings.h:65
millis
#define millis()
Definition: cc2500_compat.h:176
pt1FilterInit
void pt1FilterInit(pt1Filter_t *filter, float k)
Definition: cc2500_compat.c:61
frameErrFilter
static pt1Filter_t frameErrFilter
Definition: cc2500_rx.c:89
cc2500_rx_spi.h
NON_AUX_CHANNEL_COUNT
#define NON_AUX_CHANNEL_COUNT
Definition: cc2500_rx.h:80
pt1FilterGain
float pt1FilterGain(float f_cut, float dT)
Definition: cc2500_compat.c:55
rxChannelCount
static uint8_t rxChannelCount
Definition: cc2500_rx.c:109
rcData
int16_t rcData[MAX_SUPPORTED_RC_CHANNEL_COUNT]
Definition: cc2500_rx.c:118
needRxSignalMaxDelayUs
static uint32_t needRxSignalMaxDelayUs
Definition: cc2500_rx.c:113
uint32_t
unsigned long uint32_t
Definition: types.h:18
GET_FRAME_ERR_LPF_FREQUENCY
#define GET_FRAME_ERR_LPF_FREQUENCY(period)
Definition: cc2500_settings.h:51
setRssiDirect
void setRssiDirect(uint16_t newRssi, rssiSource_e source)
Definition: cc2500_rx.c:700
rcInvalidPulsPeriod
uint32_t rcInvalidPulsPeriod[MAX_SUPPORTED_RC_CHANNEL_COUNT]
Definition: cc2500_rx.c:119
nullProcessFrame
static bool nullProcessFrame(const rxRuntimeConfig_t *rxRuntimeConfig)
Definition: cc2500_rx.c:179
MIN
#define MIN(x, y)
Definition: sdLog.c:42
rssiSource
rssiSource_e rssiSource
Definition: cc2500_rx.c:100
FEATURE_RX_MSP
@ FEATURE_RX_MSP
Definition: cc2500_compat.h:147
MAX_INVALID_PULS_TIME
#define MAX_INVALID_PULS_TIME
Definition: cc2500_rx.c:121
RSSI_SAMPLE_COUNT
#define RSSI_SAMPLE_COUNT
Definition: cc2500_rx.c:709
MAX_SUPPORTED_RC_CHANNEL_COUNT
#define MAX_SUPPORTED_RC_CHANNEL_COUNT
Definition: cc2500_rx.h:78
rxRuntimeConfig
rxRuntimeConfig_t rxRuntimeConfig
Definition: cc2500_rx.c:131
rcSampleIndex
static uint8_t rcSampleIndex
Definition: cc2500_rx.c:132
nullReadRawRC
static uint16_t nullReadRawRC(const rxRuntimeConfig_t *rxRuntimeConfig, uint8_t channel)
Definition: cc2500_rx.c:164
RX_PROVIDER_SERIAL
@ RX_PROVIDER_SERIAL
Definition: cc2500_rx.h:137
FRAME_ERR_RESAMPLE_US
#define FRAME_ERR_RESAMPLE_US
Definition: cc2500_settings.h:49
updateRssiSamples
static uint16_t updateRssiSamples(uint16_t value)
Definition: cc2500_rx.c:711
cc2500_settings.h
int16_t
signed short int16_t
Definition: types.h:17
uint8_t
unsigned char uint8_t
Definition: types.h:14
rxConfig
const rxConfig_t * rxConfig(void)
Definition: cc2500_settings.c:77
DELAY_10_HZ
#define DELAY_10_HZ
Definition: cc2500_rx.c:126
FEATURE_RX_SERIAL
@ FEATURE_RX_SERIAL
Definition: cc2500_compat.h:138
rxRuntimeConfig_s::rcFrameStatusFn
rcFrameStatusFnPtr rcFrameStatusFn
Definition: cc2500_rx.h:148
rxSpiConfig
const rxSpiConfig_t * rxSpiConfig(void)
Definition: cc2500_settings.c:87
rxInit
void rxInit(void)
!TODO remove this extern, only needed once for channelCount
Definition: cc2500_rx.c:263
cc2500_rx.h
pt1Filter_s
Definition: cc2500_compat.h:107
rxRuntimeConfig_s::serialrxProvider
SerialRXType serialrxProvider
Definition: cc2500_rx.h:144
pt1FilterApply
float pt1FilterApply(pt1Filter_t *filter, float input)
Definition: cc2500_compat.c:70
RSSI_SOURCE_ADC
@ RSSI_SOURCE_ADC
Definition: cc2500_rx.h:156
featureIsEnabled
#define featureIsEnabled(mask)
Definition: cc2500_compat.h:163
RX_PROVIDER_SPI
@ RX_PROVIDER_SPI
Definition: cc2500_rx.h:139
rxRuntimeConfig_s::rcReadRawFn
rcReadRawDataFnPtr rcReadRawFn
Definition: cc2500_rx.h:147
rxSpiInit
bool rxSpiInit(const rxSpiConfig_t *rxSpiConfig, rxRuntimeConfig_t *rxRuntimeConfig)
Definition: cc2500_rx_spi.c:228
FEATURE_RSSI_ADC
@ FEATURE_RSSI_ADC
Definition: cc2500_compat.h:148
UNUSED
#define UNUSED(x)
Definition: cc2500_rx.c:7
RSSI_SOURCE_FRAME_ERRORS
@ RSSI_SOURCE_FRAME_ERRORS
Definition: cc2500_rx.h:160
rxRuntimeConfig_s
Definition: cc2500_rx.h:142
PPM_RCVR_TIMEOUT
#define PPM_RCVR_TIMEOUT
Definition: cc2500_compat.h:193
rxConfig_s::serialrx_provider
uint8_t serialrx_provider
Definition: cc2500_settings.h:55
RX_PROVIDER_PPM
@ RX_PROVIDER_PPM
Definition: cc2500_rx.h:136
rxRuntimeConfig_s::channelCount
uint8_t channelCount
Definition: cc2500_rx.h:145
RX_FRAME_PENDING
@ RX_FRAME_PENDING
Definition: cc2500_rx.h:52
RX_PROVIDER_PARALLEL_PWM
@ RX_PROVIDER_PARALLEL_PWM
Definition: cc2500_rx.h:135
setRssi
void setRssi(uint16_t rssiValue, rssiSource_e source)
Definition: cc2500_rx.c:723
nullFrameStatus
static uint8_t nullFrameStatus(rxRuntimeConfig_t *rxRuntimeConfig)
Definition: cc2500_rx.c:172
FEATURE_RX_PARALLEL_PWM
@ FEATURE_RX_PARALLEL_PWM
Definition: cc2500_compat.h:146
rssi
static uint16_t rssi
Definition: cc2500_rx.c:85
cc2500_compat.h
RSSI_SOURCE_RX_CHANNEL
@ RSSI_SOURCE_RX_CHANNEL
Definition: cc2500_rx.h:157
RX_PROVIDER_MSP
@ RX_PROVIDER_MSP
Definition: cc2500_rx.h:138