2 * Authors (alphabetical order)
3 * - Andre Bernet <bernet.andre@gmail.com>
5 * - Bertrand Songis <bsongis@gmail.com>
6 * - Bryan J. Rentoul (Gruvin) <gruvin@gmail.com>
7 * - Cameron Weeks <th9xer@gmail.com>
10 * - Jean-Pierre Parisy
17 * - Romolo Manfredini <romolo.manfredini@gmail.com>
20 * opentx is based on code named
21 * gruvin9x by Bryan J. Rentoul: http://code.google.com/p/gruvin9x/,
22 * er9x by Erez Raviv: http://code.google.com/p/er9x/,
23 * and the original (and ongoing) project by
24 * Thomas Husterer, th9x: http://code.google.com/p/th9x/
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License version 2 as
28 * published by the Free Software Foundation.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
39 #define SBUS_MIN_FRAME_SIZE 23
40 #define SBUS_MAX_FRAME_SIZE 28
42 #define SBUS_FRAME_GAP_DELAY 1000 // 500uS
44 #define SBUS_START_BYTE 0x0F
45 #define SBUS_FLAGS_IDX 23
46 #define SBUS_FRAMELOST_BIT 2
47 #define SBUS_FAILSAFE_BIT 3
49 #define SBUS_CH_BITS 11
50 #define SBUS_CH_MASK ((1<<SBUS_CH_BITS)-1)
52 #define SBUS_CH_CENTER 0x3E0
55 uint8_t SbusFrame
[SBUS_MAX_FRAME_SIZE
];
57 uint8_t SbusIndex
= 0 ;
59 void processSbusFrame(uint8_t *sbus
, int16_t *pulses
, uint32_t size
)
61 uint32_t inputbitsavailable
= 0;
62 uint32_t inputbits
= 0;
64 if (sbus
[0] != SBUS_START_BYTE
) {
65 return; // not a valid SBUS frame
68 if (size
< SBUS_MIN_FRAME_SIZE
) {
72 if (size
> SBUS_FLAGS_IDX
&&
73 (sbus
[SBUS_FLAGS_IDX
-1] & (1<<SBUS_FAILSAFE_BIT
))) {
74 return; // SBUS failsafe mode
80 for (uint32_t i
=0; i
<NUM_TRAINER
; i
++) {
81 while (inputbitsavailable
< SBUS_CH_BITS
) {
82 inputbits
|= *sbus
++ << inputbitsavailable
;
83 inputbitsavailable
+= 8;
85 *pulses
++ = ((int32_t) (inputbits
& SBUS_CH_MASK
) - SBUS_CH_CENTER
) * 5 / 8;
86 inputbitsavailable
-= SBUS_CH_BITS
;
87 inputbits
>>= SBUS_CH_BITS
;
90 ppmInputValidityTimer
= PPM_IN_VALID_TIMEOUT
;
93 void processSbusInput()
97 while (sbusFifo
.pop(rxchar
)) {
99 SbusFrame
[SbusIndex
++] = rxchar
;
100 if (SbusIndex
> SBUS_MAX_FRAME_SIZE
-1) {
101 SbusIndex
= SBUS_MAX_FRAME_SIZE
-1;
105 SbusTimer
= getTmr2MHz();
110 if ((uint16_t) (getTmr2MHz() - SbusTimer
) > SBUS_FRAME_GAP_DELAY
) {
111 processSbusFrame(SbusFrame
, ppmInput
, SbusIndex
);