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/>.
27 #include "build/debug.h"
30 #include "pg/pg_ids.h"
33 #include "common/crc.h"
34 #include "common/utils.h"
36 #include "drivers/serial.h"
37 #include "io/serial.h"
42 #include "telemetry/msp_shared.h"
44 rssiSource_e rssiSource
;
46 void crsfDataReceive(uint16_t c
);
47 uint8_t crsfFrameCRC(void);
48 uint8_t crsfFrameCmdCRC(void);
49 uint8_t crsfFrameStatus(void);
50 float crsfReadRawRC(const rxRuntimeState_t
*rxRuntimeState
, uint8_t chan
);
52 extern bool crsfFrameDone
;
53 extern crsfFrame_t crsfFrame
;
54 extern crsfFrame_t crsfChannelDataFrame
;
55 extern uint32_t crsfChannelData
[CRSF_MAX_CHANNEL
];
59 PG_REGISTER(rxConfig_t
, rxConfig
, PG_RX_CONFIG
, 0);
62 #include "unittest_macros.h"
63 #include "gtest/gtest.h"
65 // CRC8 implementation with polynom = x^8+x^7+x^6+x^4+x^2+1 (0xD5)
66 const unsigned char crc8tab
[256] = {
67 0x00, 0xD5, 0x7F, 0xAA, 0xFE, 0x2B, 0x81, 0x54, 0x29, 0xFC, 0x56, 0x83, 0xD7, 0x02, 0xA8, 0x7D,
68 0x52, 0x87, 0x2D, 0xF8, 0xAC, 0x79, 0xD3, 0x06, 0x7B, 0xAE, 0x04, 0xD1, 0x85, 0x50, 0xFA, 0x2F,
69 0xA4, 0x71, 0xDB, 0x0E, 0x5A, 0x8F, 0x25, 0xF0, 0x8D, 0x58, 0xF2, 0x27, 0x73, 0xA6, 0x0C, 0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x03, 0xA9, 0x7C, 0x28, 0xFD, 0x57, 0x82, 0xFF, 0x2A, 0x80, 0x55, 0x01, 0xD4, 0x7E, 0xAB,
82 0x84, 0x51, 0xFB, 0x2E, 0x7A, 0xAF, 0x05, 0xD0, 0xAD, 0x78, 0xD2, 0x07, 0x53, 0x86, 0x2C, 0xF9
85 uint8_t crc8_buf(const uint8_t * ptr
, uint8_t len
)
88 for (uint8_t i
=0; i
<len
; i
++) {
89 crc
= crc8tab
[crc
^ *ptr
++];
94 uint8_t crc8_dvb_s2_buf(const uint8_t * ptr
, uint8_t len
)
97 for (uint8_t i
=0; i
<len
; i
++) {
98 crc
= crc8_dvb_s2(crc
, *ptr
++);
103 TEST(CrossFireTest
, CRC
)
105 static const uint8_t buf1
[] ="abcdefghijklmnopqrstuvwxyz";
111 crc2
= crc8_dvb_s2(0, 1);
112 EXPECT_EQ(crc1
, crc2
);
115 crc2
= crc8_dvb_s2(0, 2);
116 EXPECT_EQ(crc1
, crc2
);
118 crc1
= crc8_buf(buf1
, 26);
119 crc2
= crc8_dvb_s2_buf(buf1
, 26);
120 EXPECT_EQ(crc1
, crc2
);
123 TEST(CrossFireTest
, TestCrsfFrameStatus
)
125 crsfFrameDone
= true;
126 crsfFrame
.frame
.deviceAddress
= CRSF_ADDRESS_CRSF_RECEIVER
;
127 crsfFrame
.frame
.frameLength
= 0;
128 crsfFrame
.frame
.type
= CRSF_FRAMETYPE_RC_CHANNELS_PACKED
;
129 memset(crsfFrame
.frame
.payload
, 0, CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
);
130 const uint8_t crc
= crsfFrameCRC();
131 crsfFrame
.frame
.payload
[CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
] = crc
;
132 memcpy(&crsfChannelDataFrame
, &crsfFrame
, sizeof(crsfFrame
));
133 const uint8_t status
= crsfFrameStatus();
134 EXPECT_EQ(RX_FRAME_COMPLETE
, status
);
135 EXPECT_FALSE(crsfFrameDone
);
137 EXPECT_EQ(CRSF_ADDRESS_CRSF_RECEIVER
, crsfFrame
.frame
.deviceAddress
);
138 EXPECT_EQ(CRSF_FRAMETYPE_RC_CHANNELS_PACKED
, crsfFrame
.frame
.type
);
139 for (int ii
= 0; ii
< CRSF_MAX_CHANNEL
; ++ii
) {
140 EXPECT_EQ(0, crsfChannelData
[ii
]);
144 const uint8_t buadrateNegotiationFrame
[] = {
145 0xC8,0x0C,0x32,0xC8,0xEC,0x0A,0x70,0x01,0x00,0x1E,0x84,0x80,0x22,0x72
148 TEST(CrossFireTest
, TestCrsfCmdFrameCrc
)
150 crsfFrame
= *(const crsfFrame_t
*)buadrateNegotiationFrame
;
151 crsfFrameDone
= true;
152 const uint8_t crsfCmdFrameCrc
= crsfFrameCmdCRC();
153 const uint8_t crsfFrameCrc
= crsfFrameCRC();
154 EXPECT_EQ(crsfCmdFrameCrc
, crsfFrame
.frame
.payload
[crsfFrame
.frame
.frameLength
- CRSF_FRAME_LENGTH_ADDRESS
- CRSF_FRAME_LENGTH_FRAMELENGTH
- 1]);
155 EXPECT_EQ(crsfFrameCrc
, crsfFrame
.frame
.payload
[crsfFrame
.frame
.frameLength
- CRSF_FRAME_LENGTH_ADDRESS
- CRSF_FRAME_LENGTH_FRAMELENGTH
]);
160 * <Device address> <Frame length> < Type> <Payload> < CRC>
161 * So RC channels frame is:
162 * <0x00> <0x18> <0x16> <22-bytes payload> < CRC>
163 * 26 bytes altogther.
165 TEST(CrossFireTest
, TestCrsfFrameStatusUnpacking
)
167 crsfFrameDone
= true;
168 crsfFrame
.frame
.deviceAddress
= CRSF_ADDRESS_CRSF_RECEIVER
;
169 crsfFrame
.frame
.frameLength
= CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
+ CRSF_FRAME_LENGTH_TYPE_CRC
;;
170 crsfFrame
.frame
.type
= CRSF_FRAMETYPE_RC_CHANNELS_PACKED
;
171 // 16 11-bit channels packed into 22 bytes of data
172 crsfFrame
.frame
.payload
[0] = 0xFF; // bits 0-7
173 crsfFrame
.frame
.payload
[1] = 0xFF; // bits 8-15
174 crsfFrame
.frame
.payload
[2] = 0x00; // bits 16-23
175 crsfFrame
.frame
.payload
[3] = 0x00; // bits 24-31
176 crsfFrame
.frame
.payload
[4] = 0x58; // bits 32-39 0101100.
177 crsfFrame
.frame
.payload
[5] = 0x01; // bits 40-47 ....0001
178 crsfFrame
.frame
.payload
[6] = 0x00; // bits 48-55 0.......
179 crsfFrame
.frame
.payload
[7] = 0xf0; // bits 56-64 11110000
180 crsfFrame
.frame
.payload
[8] = 0x01; // bits 65-71 ......01
181 crsfFrame
.frame
.payload
[9] = 0x60; // bits 72-79 011.....
182 crsfFrame
.frame
.payload
[10] = 0xe2; // bits 80-87 11100010
183 crsfFrame
.frame
.payload
[11] = 0;
184 crsfFrame
.frame
.payload
[12] = 0;
185 crsfFrame
.frame
.payload
[13] = 0;
186 crsfFrame
.frame
.payload
[14] = 0;
187 crsfFrame
.frame
.payload
[15] = 0;
188 crsfFrame
.frame
.payload
[16] = 0;
189 crsfFrame
.frame
.payload
[17] = 0;
190 crsfFrame
.frame
.payload
[18] = 0;
191 crsfFrame
.frame
.payload
[19] = 0;
192 crsfFrame
.frame
.payload
[20] = 0;
193 crsfFrame
.frame
.payload
[21] = 0;
194 const uint8_t crc
= crsfFrameCRC();
195 crsfFrame
.frame
.payload
[CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
] = crc
;
197 memcpy(&crsfChannelDataFrame
, &crsfFrame
, sizeof(crsfFrame
));
198 const uint8_t status
= crsfFrameStatus();
199 EXPECT_EQ(RX_FRAME_COMPLETE
, status
);
200 EXPECT_FALSE(crsfFrameDone
);
202 EXPECT_EQ(CRSF_ADDRESS_CRSF_RECEIVER
, crsfFrame
.frame
.deviceAddress
);
203 EXPECT_EQ(CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
+ CRSF_FRAME_LENGTH_TYPE_CRC
, crsfFrame
.frame
.frameLength
);
204 EXPECT_EQ(CRSF_FRAMETYPE_RC_CHANNELS_PACKED
, crsfFrame
.frame
.type
);
205 EXPECT_EQ(0x7ff, crsfChannelData
[0]);
206 EXPECT_EQ(0x1f, crsfChannelData
[1]);
207 EXPECT_EQ(0, crsfChannelData
[2]);
208 EXPECT_EQ(172, crsfChannelData
[3]); // 172 = 0x0ac, 0001 0101100, bits 33-43
209 EXPECT_EQ(0, crsfChannelData
[4]);
210 EXPECT_EQ(992, crsfChannelData
[5]); // 992 = 0x3e0, 01 1110000 0, bits 55-65
211 EXPECT_EQ(0, crsfChannelData
[6]);
212 EXPECT_EQ(1811, crsfChannelData
[7]); // 1811 = 0x713, 1110 0010 011, bits 77-87
213 EXPECT_EQ(0, crsfChannelData
[8]);
214 EXPECT_EQ(0, crsfChannelData
[9]);
215 EXPECT_EQ(0, crsfChannelData
[10]);
216 EXPECT_EQ(0, crsfChannelData
[11]);
217 EXPECT_EQ(0, crsfChannelData
[12]);
218 EXPECT_EQ(0, crsfChannelData
[13]);
219 EXPECT_EQ(0, crsfChannelData
[14]);
220 EXPECT_EQ(0, crsfChannelData
[15]);
223 // example of 0x16 RC frame
224 const uint8_t capturedData
[] = {
225 0x00,0x18,0x16,0xBD,0x08,0x9F,0xF4,0xAE,0xF7,0xBD,0xEF,0x7D,0xEF,0xFB,0xAD,0xFD,0x45,0x2B,0x5A,0x01,0x00,0x00,0x00,0x00,0x00,0x6C,
226 0x00,0x18,0x16,0xBD,0x08,0x9F,0xF4,0xAA,0xF7,0xBD,0xEF,0x7D,0xEF,0xFB,0xAD,0xFD,0x45,0x2B,0x5A,0x01,0x00,0x00,0x00,0x00,0x00,0x94,
229 typedef struct crsfRcChannelsFrame_s
{
230 uint8_t deviceAddress
;
233 uint8_t payload
[CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
+ CRSF_FRAME_LENGTH_CRC
];
234 } crsfRcChannelsFrame_t
;
237 TEST(CrossFireTest
, TestCapturedData
)
239 //const int frameCount = sizeof(capturedData) / sizeof(crsfRcChannelsFrame_t);
240 const crsfRcChannelsFrame_t
*framePtr
= (const crsfRcChannelsFrame_t
*)capturedData
;
241 crsfFrame
= *(const crsfFrame_t
*)framePtr
;
242 crsfFrameDone
= true;
243 memcpy(&crsfChannelDataFrame
, &crsfFrame
, sizeof(crsfFrame
));
244 uint8_t status
= crsfFrameStatus();
245 EXPECT_EQ(RX_FRAME_COMPLETE
, status
);
246 EXPECT_FALSE(crsfFrameDone
);
247 EXPECT_EQ(RX_FRAME_COMPLETE
, status
);
248 EXPECT_FALSE(crsfFrameDone
);
249 EXPECT_EQ(CRSF_ADDRESS_BROADCAST
, crsfFrame
.frame
.deviceAddress
);
250 EXPECT_EQ(CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
+ CRSF_FRAME_LENGTH_TYPE_CRC
, crsfFrame
.frame
.frameLength
);
251 EXPECT_EQ(CRSF_FRAMETYPE_RC_CHANNELS_PACKED
, crsfFrame
.frame
.type
);
252 EXPECT_EQ(189, crsfChannelData
[0]);
253 EXPECT_EQ(993, crsfChannelData
[1]);
254 EXPECT_EQ(978, crsfChannelData
[2]);
255 EXPECT_EQ(983, crsfChannelData
[3]);
256 uint8_t crc
= crsfFrameCRC();
257 EXPECT_EQ(crc
, crsfFrame
.frame
.payload
[CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
]);
258 EXPECT_EQ(999, (uint16_t)crsfReadRawRC(NULL
, 0));
259 EXPECT_EQ(1501, (uint16_t)crsfReadRawRC(NULL
, 1));
260 EXPECT_EQ(1492, (uint16_t)crsfReadRawRC(NULL
, 2));
261 EXPECT_EQ(1495, (uint16_t)crsfReadRawRC(NULL
, 3));
264 crsfFrame
= *(const crsfFrame_t
*)framePtr
;
265 crsfFrameDone
= true;
266 memcpy(&crsfChannelDataFrame
, &crsfFrame
, sizeof(crsfFrame
));
267 status
= crsfFrameStatus();
268 EXPECT_EQ(RX_FRAME_COMPLETE
, status
);
269 EXPECT_FALSE(crsfFrameDone
);
270 EXPECT_EQ(RX_FRAME_COMPLETE
, status
);
271 EXPECT_FALSE(crsfFrameDone
);
272 EXPECT_EQ(CRSF_ADDRESS_BROADCAST
, crsfFrame
.frame
.deviceAddress
);
273 EXPECT_EQ(CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
+ CRSF_FRAME_LENGTH_TYPE_CRC
, crsfFrame
.frame
.frameLength
);
274 EXPECT_EQ(CRSF_FRAMETYPE_RC_CHANNELS_PACKED
, crsfFrame
.frame
.type
);
275 EXPECT_EQ(189, crsfChannelData
[0]);
276 EXPECT_EQ(993, crsfChannelData
[1]);
277 EXPECT_EQ(978, crsfChannelData
[2]);
278 EXPECT_EQ(981, crsfChannelData
[3]);
279 crc
= crsfFrameCRC();
280 EXPECT_EQ(crc
, crsfFrame
.frame
.payload
[CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
]);
283 // example of 0x17 Subset RC frame
284 /* Notes of Frame Contents
285 * frame type = 0x17 Subset RC Frame
286 * first channel packed = 4, bits 0-4
287 * channel resolution = 0x01 = 11-bit, bits 5-6
288 * reserved configuration = 0, bit 7
289 * first channel packed (Ch4) = 0 = 0x000, 000 0000 0000, bits 8 - 18
290 * second channel packed (Ch5) = 820 = 0x334, 011 0011 0100, bits 19 - 29
291 * third channel packed (Ch6) = 1959 = 0x7A7, 111 1010 0111, bits 30 - 40
292 * fourth channel packed (Ch7) = 2047 = 0x7FF, 111 1111 1111, bits 41 - 51
294 const uint8_t capturedSubsetData
[] = {
295 0xC8,0x09,0x17,0x24,0x00,0xA0,0xD9,0xE9,0xFF,0x0F,0xD1
298 TEST(CrossFireTest
, TestCapturedSubsetData
)
300 crsfFrame
= *(const crsfFrame_t
*)capturedSubsetData
;
301 crsfFrameDone
= true;
302 memcpy(&crsfChannelDataFrame
, &crsfFrame
, sizeof(crsfFrame
));
304 uint8_t status
= crsfFrameStatus();
305 EXPECT_EQ(RX_FRAME_COMPLETE
, status
);
306 EXPECT_FALSE(crsfFrameDone
);
307 EXPECT_EQ(CRSF_SYNC_BYTE
, crsfFrame
.frame
.deviceAddress
);
308 EXPECT_EQ(CRSF_FRAMETYPE_SUBSET_RC_CHANNELS_PACKED
, crsfFrame
.frame
.type
);
310 uint8_t crc
= crsfFrameCRC();
311 uint8_t startChannel
= crsfFrame
.frame
.payload
[0] & CRSF_SUBSET_RC_STARTING_CHANNEL_MASK
;
312 uint8_t channelRes
= (crsfFrame
.frame
.payload
[0] >> CRSF_SUBSET_RC_STARTING_CHANNEL_BITS
) & CRSF_SUBSET_RC_RES_CONFIGURATION_MASK
;
313 uint8_t reservedBit
= (crsfFrame
.frame
.payload
[0] >> (CRSF_SUBSET_RC_STARTING_CHANNEL_BITS
+ CRSF_SUBSET_RC_RES_CONFIGURATION_MASK
)) & CRSF_SUBSET_RC_RESERVED_CONFIGURATION_BITS
;
314 EXPECT_EQ(crc
, crsfFrame
.frame
.payload
[crsfFrame
.frame
.frameLength
- 2]);
315 EXPECT_EQ(4, startChannel
);
316 EXPECT_EQ(1, channelRes
);
317 EXPECT_EQ(0, reservedBit
);
319 EXPECT_EQ(0, crsfChannelData
[4]);
320 EXPECT_EQ(820, crsfChannelData
[5]);
321 EXPECT_EQ(1959, crsfChannelData
[6]);
322 EXPECT_EQ(2047, crsfChannelData
[7]);
324 EXPECT_EQ(988, (uint16_t)crsfReadRawRC(NULL
, 4));
325 EXPECT_EQ(1398, (uint16_t)crsfReadRawRC(NULL
, 5));
326 EXPECT_EQ(1967, (uint16_t)crsfReadRawRC(NULL
, 6));
327 EXPECT_EQ(2011, (uint16_t)crsfReadRawRC(NULL
, 7));
330 TEST(CrossFireTest
, TestCrsfDataReceive
)
332 crsfFrameDone
= false;
333 const uint8_t *pData
= capturedData
;
334 for (unsigned int ii
= 0; ii
< sizeof(crsfRcChannelsFrame_t
); ++ii
) {
335 crsfDataReceive(*pData
++);
337 EXPECT_FALSE(crsfFrameDone
); // data is not a valid rc channels frame so don't expect crsfFrameDone to be true
338 EXPECT_EQ(CRSF_ADDRESS_BROADCAST
, crsfFrame
.frame
.deviceAddress
);
339 EXPECT_EQ(CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
+ CRSF_FRAME_LENGTH_TYPE_CRC
, crsfFrame
.frame
.frameLength
);
340 EXPECT_EQ(CRSF_FRAMETYPE_RC_CHANNELS_PACKED
, crsfFrame
.frame
.type
);
341 uint8_t crc
= crsfFrameCRC();
342 for (int ii
= 0; ii
< CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
; ++ii
) {
343 EXPECT_EQ(capturedData
[ii
+ 3], crsfFrame
.frame
.payload
[ii
]);
345 EXPECT_EQ(crc
, crsfFrame
.frame
.payload
[CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
]);
352 int16_t debug
[DEBUG16_VALUE_COUNT
];
353 uint32_t micros(void) {return dummyTimeUs
;}
354 uint32_t microsISR(void) {return micros();}
355 serialPort_t
*openSerialPort(serialPortIdentifier_e
, serialPortFunction_e
, serialReceiveCallbackPtr
, void *, uint32_t, portMode_e
, portOptions_e
) {return NULL
;}
356 const serialPortConfig_t
*findSerialPortConfig(serialPortFunction_e
) {return NULL
;}
357 bool telemetryCheckRxPortShared(const serialPortConfig_t
*) {return false;}
358 serialPort_t
*telemetrySharedPort
= NULL
;
359 void crsfScheduleDeviceInfoResponse(void) {};
360 void crsfScheduleMspResponse(uint8_t ) {};
361 bool bufferMspFrame(uint8_t *, int) {return true;}
362 bool isBatteryVoltageAvailable(void) { return true; }
363 bool isAmperageAvailable(void) { return true; }
364 timeUs_t
rxFrameTimeUs(void) { return 0; }