2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 * Driver for IBUS (Flysky) receiver
19 * - initial implementation for MultiWii by Cesco/Plüschi
20 * - implementation for BaseFlight by Andreas (fiendie) Tacke
21 * - ported to CleanFlight by Konstantin (digitalentity) Sharlaimov
30 #ifdef USE_SERIALRX_IBUS
32 #include "common/utils.h"
33 #include "common/time.h"
35 #include "drivers/system.h"
36 #include "drivers/time.h"
38 #include "drivers/serial.h"
39 #include "drivers/serial_uart.h"
40 #include "io/serial.h"
42 #include "telemetry/telemetry.h"
46 #include "telemetry/ibus.h"
47 #include "telemetry/ibus_shared.h"
49 #define IBUS_MAX_CHANNEL 18
50 //In AFHDS there is 18 channels encoded in 14 slots (each slot is 2 byte long)
51 #define IBUS_MAX_SLOTS 14
52 #define IBUS_BUFFSIZE 32
53 #define IBUS_MODEL_IA6B 0
54 #define IBUS_MODEL_IA6 1
55 #define IBUS_FRAME_GAP 500
57 #define IBUS_TELEMETRY_PACKET_LENGTH (4)
58 #define IBUS_SERIAL_RX_PACKET_LENGTH (32)
60 static uint8_t ibusModel
;
61 static uint8_t ibusSyncByte
;
62 static uint8_t ibusFrameSize
;
63 static uint8_t ibusChannelOffset
;
64 static uint8_t rxBytesToIgnore
;
65 static uint16_t ibusChecksum
;
67 static bool ibusFrameDone
= false;
68 static uint32_t ibusChannelData
[IBUS_MAX_CHANNEL
];
70 static uint8_t ibus
[IBUS_BUFFSIZE
] = { 0, };
73 static bool isValidIa6bIbusPacketLength(uint8_t length
)
75 return (length
== IBUS_TELEMETRY_PACKET_LENGTH
) || (length
== IBUS_SERIAL_RX_PACKET_LENGTH
);
79 // Receive ISR callback
80 static void ibusDataReceive(uint16_t c
, void *rxCallbackData
)
82 UNUSED(rxCallbackData
);
85 static timeUs_t ibusTimeLast
;
86 static uint8_t ibusFramePosition
;
90 if ((ibusTime
- ibusTimeLast
) > IBUS_FRAME_GAP
) {
91 ibusFramePosition
= 0;
93 } else if (rxBytesToIgnore
) {
98 ibusTimeLast
= ibusTime
;
100 if (ibusFramePosition
== 0) {
101 if (isValidIa6bIbusPacketLength(c
)) {
102 ibusModel
= IBUS_MODEL_IA6B
;
105 ibusChannelOffset
= 2;
106 ibusChecksum
= 0xFFFF;
107 } else if ((ibusSyncByte
== 0) && (c
== 0x55)) {
108 ibusModel
= IBUS_MODEL_IA6
;
111 ibusChecksum
= 0x0000;
112 ibusChannelOffset
= 1;
113 } else if (ibusSyncByte
!= c
) {
118 ibus
[ibusFramePosition
] = (uint8_t)c
;
120 if (ibusFramePosition
== ibusFrameSize
- 1) {
121 ibusFrameDone
= true;
127 uint16_t ibusCalculateChecksum(const uint8_t *ibusPacket
, size_t packetLength
)
129 uint16_t checksum
= 0xFFFF;
130 for (size_t i
= 0; i
< packetLength
- IBUS_CHECKSUM_SIZE
; i
++) {
131 checksum
-= ibusPacket
[i
];
137 static bool isChecksumOkIa6(void)
141 uint16_t chksum
, rxsum
;
142 chksum
= ibusChecksum
;
143 rxsum
= ibus
[ibusFrameSize
- 2] + (ibus
[ibusFrameSize
- 1] << 8);
144 for (i
= 0, offset
= ibusChannelOffset
; i
< IBUS_MAX_SLOTS
; i
++, offset
+= 2) {
145 chksum
+= ibus
[offset
] + (ibus
[offset
+ 1] << 8);
147 return chksum
== rxsum
;
150 bool ibusIsChecksumOkIa6b(const uint8_t *ibusPacket
, const uint8_t length
)
152 uint16_t calculatedChecksum
= ibusCalculateChecksum(ibusPacket
, length
);
154 // Note that there's a byte order swap to little endian here
155 return (calculatedChecksum
>> 8) == ibusPacket
[length
- 1]
156 && (calculatedChecksum
& 0xFF) == ibusPacket
[length
- 2];
159 static bool checksumIsOk(void) {
160 if (ibusModel
== IBUS_MODEL_IA6
) {
161 return isChecksumOkIa6();
163 return ibusIsChecksumOkIa6b(ibus
, ibusFrameSize
);
167 static void updateChannelData(void) {
171 for (i
= 0, offset
= ibusChannelOffset
; i
< IBUS_MAX_SLOTS
; i
++, offset
+= 2) {
172 ibusChannelData
[i
] = ibus
[offset
] + ((ibus
[offset
+ 1] & 0x0F) << 8);
174 //latest IBUS recievers are using prviously not used 4 bits on every channel to incresse total channel count
175 for (i
= IBUS_MAX_SLOTS
, offset
= ibusChannelOffset
+ 1; i
< IBUS_MAX_CHANNEL
; i
++, offset
+= 6) {
176 ibusChannelData
[i
] = ((ibus
[offset
] & 0xF0) >> 4) | (ibus
[offset
+ 2] & 0xF0) | ((ibus
[offset
+ 4] & 0xF0) << 4);
180 static uint8_t ibusFrameStatus(rxRuntimeConfig_t
*rxRuntimeConfig
)
182 UNUSED(rxRuntimeConfig
);
184 uint8_t frameStatus
= RX_FRAME_PENDING
;
186 if (!ibusFrameDone
) {
190 ibusFrameDone
= false;
192 if (checksumIsOk()) {
193 if (ibusModel
== IBUS_MODEL_IA6
|| ibusSyncByte
== 0x20) {
195 frameStatus
= RX_FRAME_COMPLETE
;
199 #if defined(USE_TELEMETRY) && defined(USE_TELEMETRY_IBUS)
200 rxBytesToIgnore
= respondToIbusRequest(ibus
);
208 static uint16_t ibusReadRawRC(const rxRuntimeConfig_t
*rxRuntimeConfig
, uint8_t chan
)
210 UNUSED(rxRuntimeConfig
);
211 return ibusChannelData
[chan
];
215 bool ibusInit(const rxConfig_t
*rxConfig
, rxRuntimeConfig_t
*rxRuntimeConfig
)
220 rxRuntimeConfig
->channelCount
= IBUS_MAX_CHANNEL
;
221 rxRuntimeConfig
->rcReadRawFn
= ibusReadRawRC
;
222 rxRuntimeConfig
->rcFrameStatusFn
= ibusFrameStatus
;
224 const serialPortConfig_t
*portConfig
= findSerialPortConfig(FUNCTION_RX_SERIAL
);
230 bool portShared
= isSerialPortShared(portConfig
, FUNCTION_RX_SERIAL
, FUNCTION_TELEMETRY_IBUS
);
232 bool portShared
= false;
236 serialPort_t
*ibusPort
= openSerialPort(portConfig
->identifier
,
241 portShared
? MODE_RXTX
: MODE_RX
,
242 SERIAL_NOT_INVERTED
| ((tristateWithDefaultOffIsActive(rxConfig
->halfDuplex
) || portShared
) ? SERIAL_BIDIR
: 0)
245 #if defined(USE_TELEMETRY) && defined(USE_TELEMETRY_IBUS)
247 initSharedIbusTelemetry(ibusPort
);
251 return ibusPort
!= NULL
;
254 #endif // USE_SERIALRX_IBUS