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 crsfFrameStatus(void);
49 uint16_t crsfReadRawRC(const rxRuntimeState_t
*rxRuntimeState
, uint8_t chan
);
51 extern bool crsfFrameDone
;
52 extern crsfFrame_t crsfFrame
;
53 extern crsfFrame_t crsfChannelDataFrame
;
54 extern uint32_t crsfChannelData
[CRSF_MAX_CHANNEL
];
58 PG_REGISTER(rxConfig_t
, rxConfig
, PG_RX_CONFIG
, 0);
61 #include "unittest_macros.h"
62 #include "gtest/gtest.h"
64 // CRC8 implementation with polynom = x^8+x^7+x^6+x^4+x^2+1 (0xD5)
65 const unsigned char crc8tab
[256] = {
66 0x00, 0xD5, 0x7F, 0xAA, 0xFE, 0x2B, 0x81, 0x54, 0x29, 0xFC, 0x56, 0x83, 0xD7, 0x02, 0xA8, 0x7D,
67 0x52, 0x87, 0x2D, 0xF8, 0xAC, 0x79, 0xD3, 0x06, 0x7B, 0xAE, 0x04, 0xD1, 0x85, 0x50, 0xFA, 0x2F,
68 0xA4, 0x71, 0xDB, 0x0E, 0x5A, 0x8F, 0x25, 0xF0, 0x8D, 0x58, 0xF2, 0x27, 0x73, 0xA6, 0x0C, 0xD9,
69 0xF6, 0x23, 0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, 0xFD, 0x57, 0x82, 0xFF, 0x2A, 0x80, 0x55, 0x01, 0xD4, 0x7E, 0xAB,
81 0x84, 0x51, 0xFB, 0x2E, 0x7A, 0xAF, 0x05, 0xD0, 0xAD, 0x78, 0xD2, 0x07, 0x53, 0x86, 0x2C, 0xF9
84 uint8_t crc8_buf(const uint8_t * ptr
, uint8_t len
)
87 for (uint8_t i
=0; i
<len
; i
++) {
88 crc
= crc8tab
[crc
^ *ptr
++];
93 uint8_t crc8_dvb_s2_buf(const uint8_t * ptr
, uint8_t len
)
96 for (uint8_t i
=0; i
<len
; i
++) {
97 crc
= crc8_dvb_s2(crc
, *ptr
++);
102 TEST(CrossFireTest
, CRC
)
104 static const uint8_t buf1
[] ="abcdefghijklmnopqrstuvwxyz";
110 crc2
= crc8_dvb_s2(0, 1);
111 EXPECT_EQ(crc1
, crc2
);
114 crc2
= crc8_dvb_s2(0, 2);
115 EXPECT_EQ(crc1
, crc2
);
117 crc1
= crc8_buf(buf1
, 26);
118 crc2
= crc8_dvb_s2_buf(buf1
, 26);
119 EXPECT_EQ(crc1
, crc2
);
122 TEST(CrossFireTest
, TestCrsfFrameStatus
)
124 crsfFrameDone
= true;
125 crsfFrame
.frame
.deviceAddress
= CRSF_ADDRESS_CRSF_RECEIVER
;
126 crsfFrame
.frame
.frameLength
= 0;
127 crsfFrame
.frame
.type
= CRSF_FRAMETYPE_RC_CHANNELS_PACKED
;
128 memset(crsfFrame
.frame
.payload
, 0, CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
);
129 const uint8_t crc
= crsfFrameCRC();
130 crsfFrame
.frame
.payload
[CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
] = crc
;
132 const uint8_t status
= crsfFrameStatus();
133 EXPECT_EQ(RX_FRAME_COMPLETE
, status
);
134 EXPECT_EQ(false, crsfFrameDone
);
136 EXPECT_EQ(CRSF_ADDRESS_CRSF_RECEIVER
, crsfFrame
.frame
.deviceAddress
);
137 EXPECT_EQ(CRSF_FRAMETYPE_RC_CHANNELS_PACKED
, crsfFrame
.frame
.type
);
138 for (int ii
= 0; ii
< CRSF_MAX_CHANNEL
; ++ii
) {
139 EXPECT_EQ(0, crsfChannelData
[ii
]);
145 * <Device address> <Frame length> < Type> <Payload> < CRC>
146 * So RC channels frame is:
147 * <0x00> <0x18> <0x16> <22-bytes payload> < CRC>
148 * 26 bytes altogther.
150 TEST(CrossFireTest
, TestCrsfFrameStatusUnpacking
)
152 crsfFrameDone
= true;
153 crsfFrame
.frame
.deviceAddress
= CRSF_ADDRESS_CRSF_RECEIVER
;
154 crsfFrame
.frame
.frameLength
= CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
+ CRSF_FRAME_LENGTH_TYPE_CRC
;;
155 crsfFrame
.frame
.type
= CRSF_FRAMETYPE_RC_CHANNELS_PACKED
;
156 // 16 11-bit channels packed into 22 bytes of data
157 crsfFrame
.frame
.payload
[0] = 0xFF; // bits 0-7
158 crsfFrame
.frame
.payload
[1] = 0xFF; // bits 8-15
159 crsfFrame
.frame
.payload
[2] = 0x00; // bits 16-23
160 crsfFrame
.frame
.payload
[3] = 0x00; // bits 24-31
161 crsfFrame
.frame
.payload
[4] = 0x58; // bits 32-39 0101100.
162 crsfFrame
.frame
.payload
[5] = 0x01; // bits 40-47 ....0001
163 crsfFrame
.frame
.payload
[6] = 0x00; // bits 48-55 0.......
164 crsfFrame
.frame
.payload
[7] = 0xf0; // bits 56-64 11110000
165 crsfFrame
.frame
.payload
[8] = 0x01; // bits 65-71 ......01
166 crsfFrame
.frame
.payload
[9] = 0x60; // bits 72-79 011.....
167 crsfFrame
.frame
.payload
[10] = 0xe2; // bits 80-87 11100010
168 crsfFrame
.frame
.payload
[11] = 0;
169 crsfFrame
.frame
.payload
[12] = 0;
170 crsfFrame
.frame
.payload
[13] = 0;
171 crsfFrame
.frame
.payload
[14] = 0;
172 crsfFrame
.frame
.payload
[15] = 0;
173 crsfFrame
.frame
.payload
[16] = 0;
174 crsfFrame
.frame
.payload
[17] = 0;
175 crsfFrame
.frame
.payload
[18] = 0;
176 crsfFrame
.frame
.payload
[19] = 0;
177 crsfFrame
.frame
.payload
[20] = 0;
178 crsfFrame
.frame
.payload
[21] = 0;
179 const uint8_t crc
= crsfFrameCRC();
180 crsfFrame
.frame
.payload
[CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
] = crc
;
182 memcpy(&crsfChannelDataFrame
, &crsfFrame
, sizeof(crsfFrame
));
183 const uint8_t status
= crsfFrameStatus();
184 EXPECT_EQ(RX_FRAME_COMPLETE
, status
);
185 EXPECT_EQ(false, crsfFrameDone
);
187 EXPECT_EQ(CRSF_ADDRESS_CRSF_RECEIVER
, crsfFrame
.frame
.deviceAddress
);
188 EXPECT_EQ(CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
+ CRSF_FRAME_LENGTH_TYPE_CRC
, crsfFrame
.frame
.frameLength
);
189 EXPECT_EQ(CRSF_FRAMETYPE_RC_CHANNELS_PACKED
, crsfFrame
.frame
.type
);
190 EXPECT_EQ(0x7ff, crsfChannelData
[0]);
191 EXPECT_EQ(0x1f, crsfChannelData
[1]);
192 EXPECT_EQ(0, crsfChannelData
[2]);
193 EXPECT_EQ(172, crsfChannelData
[3]); // 172 = 0x0ac, 0001 0101100, bits 33-43
194 EXPECT_EQ(0, crsfChannelData
[4]);
195 EXPECT_EQ(992, crsfChannelData
[5]); // 992 = 0x3e0, 01 1110000 0, bits 55-65
196 EXPECT_EQ(0, crsfChannelData
[6]);
197 EXPECT_EQ(1811, crsfChannelData
[7]); // 1811 = 0x713, 1110 0010 011, bits 77-87
198 EXPECT_EQ(0, crsfChannelData
[8]);
199 EXPECT_EQ(0, crsfChannelData
[9]);
200 EXPECT_EQ(0, crsfChannelData
[10]);
201 EXPECT_EQ(0, crsfChannelData
[11]);
202 EXPECT_EQ(0, crsfChannelData
[12]);
203 EXPECT_EQ(0, crsfChannelData
[13]);
204 EXPECT_EQ(0, crsfChannelData
[14]);
205 EXPECT_EQ(0, crsfChannelData
[15]);
208 const uint8_t capturedData
[] = {
209 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,
210 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,
213 typedef struct crsfRcChannelsFrame_s
{
214 uint8_t deviceAddress
;
217 uint8_t payload
[CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
+ CRSF_FRAME_LENGTH_CRC
];
218 } crsfRcChannelsFrame_t
;
221 TEST(CrossFireTest
, TestCapturedData
)
223 //const int frameCount = sizeof(capturedData) / sizeof(crsfRcChannelsFrame_t);
224 const crsfRcChannelsFrame_t
*framePtr
= (const crsfRcChannelsFrame_t
*)capturedData
;
225 crsfFrame
= *(const crsfFrame_t
*)framePtr
;
226 crsfFrameDone
= true;
227 memcpy(&crsfChannelDataFrame
, &crsfFrame
, sizeof(crsfFrame
));
228 uint8_t status
= crsfFrameStatus();
229 EXPECT_EQ(RX_FRAME_COMPLETE
, status
);
230 EXPECT_EQ(false, crsfFrameDone
);
231 EXPECT_EQ(RX_FRAME_COMPLETE
, status
);
232 EXPECT_EQ(false, crsfFrameDone
);
233 EXPECT_EQ(CRSF_ADDRESS_BROADCAST
, crsfFrame
.frame
.deviceAddress
);
234 EXPECT_EQ(CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
+ CRSF_FRAME_LENGTH_TYPE_CRC
, crsfFrame
.frame
.frameLength
);
235 EXPECT_EQ(CRSF_FRAMETYPE_RC_CHANNELS_PACKED
, crsfFrame
.frame
.type
);
236 EXPECT_EQ(189, crsfChannelData
[0]);
237 EXPECT_EQ(993, crsfChannelData
[1]);
238 EXPECT_EQ(978, crsfChannelData
[2]);
239 EXPECT_EQ(983, crsfChannelData
[3]);
240 uint8_t crc
= crsfFrameCRC();
241 EXPECT_EQ(crc
, crsfFrame
.frame
.payload
[CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
]);
242 EXPECT_EQ(999, crsfReadRawRC(NULL
, 0));
243 EXPECT_EQ(1501, crsfReadRawRC(NULL
, 1));
244 EXPECT_EQ(1492, crsfReadRawRC(NULL
, 2));
245 EXPECT_EQ(1495, crsfReadRawRC(NULL
, 3));
248 crsfFrame
= *(const crsfFrame_t
*)framePtr
;
249 crsfFrameDone
= true;
250 memcpy(&crsfChannelDataFrame
, &crsfFrame
, sizeof(crsfFrame
));
251 status
= crsfFrameStatus();
252 EXPECT_EQ(RX_FRAME_COMPLETE
, status
);
253 EXPECT_EQ(false, crsfFrameDone
);
254 EXPECT_EQ(RX_FRAME_COMPLETE
, status
);
255 EXPECT_EQ(false, crsfFrameDone
);
256 EXPECT_EQ(CRSF_ADDRESS_BROADCAST
, crsfFrame
.frame
.deviceAddress
);
257 EXPECT_EQ(CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
+ CRSF_FRAME_LENGTH_TYPE_CRC
, crsfFrame
.frame
.frameLength
);
258 EXPECT_EQ(CRSF_FRAMETYPE_RC_CHANNELS_PACKED
, crsfFrame
.frame
.type
);
259 EXPECT_EQ(189, crsfChannelData
[0]);
260 EXPECT_EQ(993, crsfChannelData
[1]);
261 EXPECT_EQ(978, crsfChannelData
[2]);
262 EXPECT_EQ(981, crsfChannelData
[3]);
263 crc
= crsfFrameCRC();
264 EXPECT_EQ(crc
, crsfFrame
.frame
.payload
[CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
]);
268 TEST(CrossFireTest
, TestCrsfDataReceive
)
270 crsfFrameDone
= false;
271 const uint8_t *pData
= capturedData
;
272 for (unsigned int ii
= 0; ii
< sizeof(crsfRcChannelsFrame_t
); ++ii
) {
273 crsfDataReceive(*pData
++);
275 EXPECT_EQ(false, crsfFrameDone
); // data is not a valid rc channels frame so don't expect crsfFrameDone to be true
276 EXPECT_EQ(CRSF_ADDRESS_BROADCAST
, crsfFrame
.frame
.deviceAddress
);
277 EXPECT_EQ(CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
+ CRSF_FRAME_LENGTH_TYPE_CRC
, crsfFrame
.frame
.frameLength
);
278 EXPECT_EQ(CRSF_FRAMETYPE_RC_CHANNELS_PACKED
, crsfFrame
.frame
.type
);
279 uint8_t crc
= crsfFrameCRC();
280 for (int ii
= 0; ii
< CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
; ++ii
) {
281 EXPECT_EQ(capturedData
[ii
+ 3], crsfFrame
.frame
.payload
[ii
]);
283 EXPECT_EQ(crc
, crsfFrame
.frame
.payload
[CRSF_FRAME_RC_CHANNELS_PAYLOAD_SIZE
]);
290 int16_t debug
[DEBUG16_VALUE_COUNT
];
291 uint32_t micros(void) {return dummyTimeUs
;}
292 serialPort_t
*openSerialPort(serialPortIdentifier_e
, serialPortFunction_e
, serialReceiveCallbackPtr
, void *, uint32_t, portMode_e
, portOptions_e
) {return NULL
;}
293 serialPortConfig_t
*findSerialPortConfig(serialPortFunction_e
) {return NULL
;}
294 bool telemetryCheckRxPortShared(const serialPortConfig_t
*) {return false;}
295 serialPort_t
*telemetrySharedPort
= NULL
;
296 void crsfScheduleDeviceInfoResponse(void) {};
297 void crsfScheduleMspResponse(void) {};
298 bool bufferMspFrame(uint8_t *, int) {return true;}
299 bool isBatteryVoltageAvailable(void) { return true; }
300 bool isAmperageAvailable(void) { return true; }