fix: parse CRSF battery as big-endian per protocol spec

CRSF battery frame is big-endian: voltage(u16 BE 0.1V),
current(u16 BE 0.1A), capacity(u24 BE mAh), remaining(u8 %).

Previous code read little-endian with wrong byte count (7 vs 8)
and wrong scaling (/1000 vs /10), producing 9.98V for a 1S battery.
This commit is contained in:
2026-06-14 23:34:12 +02:00
parent ef5012b9d4
commit d67d9b29d2
4 changed files with 39 additions and 23 deletions
+12 -8
View File
@@ -60,13 +60,15 @@ void test_parse_link_stats_short(void) {
void test_parse_battery(void) {
uint8_t buf[32];
uint8_t payload[7] = {
0x40, 0x03, /* voltage: 0x0340 = 832 -> 0.832V */
0x00, 0x00, /* current: 0 */
0x00, 0x00, /* capacity: 0 */
/* CRSF battery: voltage(u16 BE 0.1V), current(u16 BE 0.1A),
capacity(u24 BE mAh), remaining(u8 %) = 8 bytes */
uint8_t payload[8] = {
0x03, 0xE8, /* voltage: 0x03E8 = 1000 -> 100.0V (10S LiPo) */
0x00, 0x64, /* current: 0x0064 = 100 -> 10.0A */
0x00, 0x03, 0xE8, /* capacity: 0x0003E8 = 1000mAh */
0x64 /* remaining: 100% */
};
build_frame(buf, 0x08, CEL_CRSF_TYPE_BATTERY, payload, 7);
build_frame(buf, 0x08, CEL_CRSF_TYPE_BATTERY, payload, 8);
cel_crsf_frame frame;
TEST_ASSERT_EQUAL_INT(0, cel_crsf_frame_parse(&frame, buf, sizeof(buf)));
@@ -74,13 +76,15 @@ void test_parse_battery(void) {
cel_telemetry telem;
TEST_ASSERT_EQUAL_INT(0, cel_crsf_telemetry_parse(&frame, &telem));
TEST_ASSERT_EQUAL_UINT(CEL_TELEM_BATTERY, telem.type);
TEST_ASSERT_EQUAL_UINT16(0x0340, telem.data.battery.voltage_mv);
TEST_ASSERT_EQUAL_UINT16(0x03E8, telem.data.battery.voltage_x10);
TEST_ASSERT_EQUAL_UINT16(0x0064, telem.data.battery.current_x10);
TEST_ASSERT_EQUAL_UINT32(0x0003E8, telem.data.battery.capacity_mah);
TEST_ASSERT_EQUAL_UINT8(0x64, telem.data.battery.remaining_pct);
}
void test_parse_heartbeat(void) {
uint8_t buf[32];
uint8_t payload[2] = {0x10, 0x80}; /* origin_addr = 0x8010 */
uint8_t payload[2] = {0x10, 0x80}; /* origin_addr LE = 0x8010 */
build_frame(buf, 0x10, CEL_CRSF_TYPE_HEARTBEAT, payload, 2);
cel_crsf_frame frame;
@@ -94,7 +98,7 @@ void test_parse_heartbeat(void) {
void test_parse_airspeed(void) {
uint8_t buf[32];
uint8_t payload[2] = {0x00, 0x01}; /* speed = 0x0100 = 256 km/h */
uint8_t payload[2] = {0x00, 0x01}; /* speed LE = 0x0100 = 256 km/h */
build_frame(buf, 0x08, CEL_CRSF_TYPE_AIRSPEED, payload, 2);
cel_crsf_frame frame;