Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional serial protocol: Graupner HoTT SUMD #2137

Merged
merged 8 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ <h2>Serial Protocol</h2>
<option value='1'>Inverted CRSF</option>
<option value='2'>SBUS</option>
<option value='3'>Inverted SBUS</option>
<option value='4'>SUMD</option>
</select>
<label>Serial Protocol</label>
</div>
Expand Down
4 changes: 4 additions & 0 deletions src/html/scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ function updateConfig(data) {
_('rcvr-uart-baud').disabled = false;
_('rcvr-uart-baud').value = '420000';
}
if (_('serial-protocol').value == 4) {
_('rcvr-uart-baud').disabled = true;
_('rcvr-uart-baud').value = '115200';
}
else {
_('rcvr-uart-baud').disabled = true;
_('rcvr-uart-baud').value = '100000';
Expand Down
3 changes: 2 additions & 1 deletion src/include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ enum eSerialProtocol : uint8_t
PROTOCOL_CRSF,
PROTOCOL_INVERTED_CRSF,
PROTOCOL_SBUS,
PROTOCOL_INVERTED_SBUS
PROTOCOL_INVERTED_SBUS,
PROTOCOL_SUMD
};

#ifndef UNIT_TEST
Expand Down
4 changes: 2 additions & 2 deletions src/lib/CONFIG/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ typedef struct {
forceTlmOff:1,
rateInitialIdx:4; // Rate to start rateCycling at on boot
uint8_t modelId;
uint8_t serialProtocol:2,
unused:6;
uint8_t serialProtocol:3,
unused:5;
rx_config_pwm_t pwmChannels[PWM_MAX_CHANNELS];
} rx_config_t;

Expand Down
2 changes: 1 addition & 1 deletion src/lib/LUA/rx_devLUA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static const char *rxModes = "50Hz;60Hz;100Hz;160Hz;333Hz;400Hz;10kHzDuty;On/Off
static struct luaItem_selection luaSerialProtocol = {
{"Protocol", CRSF_TEXT_SELECTION},
0, // value
"CRSF;Inverted CRSF;SBUS;Inverted SBUS",
"CRSF;Inverted CRSF;SBUS;Inverted SBUS;SUMD",
STR_EMPTYSPACE
};

Expand Down
2 changes: 2 additions & 0 deletions src/lib/OPTIONS/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ __attribute__ ((used)) const firmware_options_t firmwareOptions = {
.uart_baud = USE_AIRPORT_AT_BAUD,
#elif defined(USE_SBUS_PROTOCOL)
.uart_baud = 100000,
#elif defined(USE_SUMD_PROTOCOL)
.uart_baud = 115200,
#elif defined(RCVR_UART_BAUD)
.uart_baud = RCVR_UART_BAUD,
#else
Expand Down
120 changes: 120 additions & 0 deletions src/src/rx-serial/SerialSUMD.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include "SerialSUMD.h"
#include "CRSF.h"
#include "device.h"

#define SUMD_HEADER_SIZE 3 // 3 Bytes header
#define SUMD_DATA_SIZE_16CH (16*2) // 2 Bytes per channel
#define SUMD_CRC_SIZE 2 // 16 bit CRC
#define SUMD_FRAME_16CH_LEN (SUMD_HEADER_SIZE+SUMD_DATA_SIZE_16CH+SUMD_CRC_SIZE)

#define CRC_POLYNOME 0x1021

void SerialSUMD::setLinkQualityStats(uint16_t lq, uint16_t rssi)
{
linkQuality = lq;
rssiDBM = rssi;
}

uint32_t SerialSUMD::sendRCFrameToFC(bool frameAvailable, uint32_t *channelData)
{
if (!frameAvailable) {
return DURATION_IMMEDIATELY;
}

uint8_t outBuffer[SUMD_FRAME_16CH_LEN];

outBuffer[0] = 0xA8; //Graupner
outBuffer[1] = 0x01; //SUMD
outBuffer[2] = 0x10; //16CH

uint16_t us = (CRSF_to_US(ChannelData[0]) << 3);
outBuffer[3] = us >> 8;
outBuffer[4] = us & 0x00ff;
us = (CRSF_to_US(ChannelData[1]) << 3);
outBuffer[5] = us >> 8;
outBuffer[6] = us & 0x00ff;
us = (CRSF_to_US(ChannelData[2]) << 3);
outBuffer[7] = us >> 8;
outBuffer[8] = us & 0x00ff;
us = (CRSF_to_US(ChannelData[3]) << 3);
outBuffer[9] = us >> 8;
outBuffer[10] = us & 0x00ff;
us = (CRSF_to_US(ChannelData[7]) << 3); //channel 8 mapped to 5 to move arm channel away from the aileron function
outBuffer[11] = us >> 8;
outBuffer[12] = us & 0x00ff;
us = (CRSF_to_US(ChannelData[5]) << 3);
outBuffer[13] = us >> 8;
outBuffer[14] = us & 0x00ff;
us = (CRSF_to_US(ChannelData[6]) << 3);
outBuffer[15] = us >> 8;
outBuffer[16] = us & 0x00ff;
us = (CRSF_to_US(ChannelData[4]) << 3); //channel 5 mapped to 8
outBuffer[17] = us >> 8;
outBuffer[18] = us & 0x00ff;
us = (CRSF_to_US(ChannelData[8]) << 3);
outBuffer[19] = us >> 8;
outBuffer[20] = us & 0x00ff;
us = (CRSF_to_US(ChannelData[9]) << 3);
outBuffer[21] = us >> 8;
outBuffer[22] = us & 0x00ff;
us = (CRSF_to_US(ChannelData[10]) << 3);
outBuffer[23] = us >> 8;
outBuffer[24] = us & 0x00ff;
us = (CRSF_to_US(ChannelData[11]) << 3);
outBuffer[25] = us >> 8;
outBuffer[26] = us & 0x00ff;
us = (CRSF_to_US(ChannelData[12]) << 3);
outBuffer[27] = us >> 8;
outBuffer[28] = us & 0x00ff;
us = (CRSF_to_US(ChannelData[13]) << 3);
outBuffer[29] = us >> 8;
outBuffer[30] = us & 0x00ff;
us = (CRSF_to_US(ChannelData[14]) << 3);
outBuffer[31] = us >> 8;
outBuffer[32] = us & 0x00ff;
us = (CRSF_to_US(ChannelData[15]) << 3);
outBuffer[33] = us >> 8;
outBuffer[34] = us & 0x00ff;

uint16_t crc = crc16(outBuffer, (SUMD_HEADER_SIZE + SUMD_DATA_SIZE_16CH));
outBuffer[35] = (uint8_t)(crc >> 8);
outBuffer[36] = (uint8_t)(crc & 0x00ff);

_outputPort->write(outBuffer, sizeof(outBuffer));

return DURATION_IMMEDIATELY;
}

uint16_t SerialSUMD::crc16(uint8_t *data, uint8_t len)
mha1 marked this conversation as resolved.
Show resolved Hide resolved
{
uint16_t crc = 0;

for(uint8_t i = 0; i < len; i++)
{
crc = crc ^ ((int16_t)data[i] << 8);

for(uint8_t i = 0; i < 8; i++)
{
if (crc & 0x8000)
crc = (crc << 1) ^ CRC_POLYNOME;
else
crc = (crc << 1);
}
}

return crc;
}

void SerialSUMD::sendLinkStatisticsToFC()
{
// unsupported
}

void SerialSUMD::sendMSPFrameToFC(uint8_t* data)
{
// unsupported
}

void processByte(uint8_t byte) {
// unsupported
}
20 changes: 20 additions & 0 deletions src/src/rx-serial/SerialSUMD.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "SerialIO.h"

class SerialSUMD : public SerialIO {
public:
explicit SerialSUMD(Stream &out, Stream &in) : SerialIO(&out, &in) {}

virtual ~SerialSUMD() {}

void setLinkQualityStats(uint16_t lq, uint16_t rssi) override;
uint32_t sendRCFrameToFC(bool frameAvailable, uint32_t *channelData) override;
void sendMSPFrameToFC(uint8_t* data) override;
void sendLinkStatisticsToFC() override;

private:
uint16_t linkQuality = 0;
uint16_t rssiDBM = 0;

uint16_t crc16(uint8_t *data, uint8_t len);
void processByte(uint8_t byte) override {};
};
13 changes: 12 additions & 1 deletion src/src/rx_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "rx-serial/SerialNOOP.h"
#include "rx-serial/SerialCRSF.h"
#include "rx-serial/SerialSBUS.h"
#include "rx-serial/SerialSUMD.h"
#include "rx-serial/SerialAirPort.h"

#include "rx-serial/devSerialIO.h"
Expand Down Expand Up @@ -1130,6 +1131,7 @@ void MspReceiveComplete()
static void setupSerial()
{
bool sbusSerialOutput = false;
bool sumdSerialOutput = false;

if (OPT_CRSF_RCVR_NO_SERIAL)
{
Expand All @@ -1149,6 +1151,11 @@ static void setupSerial()
sbusSerialOutput = true;
serialBaud = 100000;
}
else if (config.GetSerialProtocol() == PROTOCOL_SUMD)
{
sumdSerialOutput = true;
serialBaud = 115200;
}
bool invert = config.GetSerialProtocol() == PROTOCOL_SBUS || config.GetSerialProtocol() == PROTOCOL_INVERTED_CRSF;

#ifdef PLATFORM_STM32
Expand Down Expand Up @@ -1204,7 +1211,7 @@ static void setupSerial()

#if defined(PLATFORM_ESP8266)
SerialConfig config = sbusSerialOutput ? SERIAL_8E2 : SERIAL_8N1;
SerialMode mode = sbusSerialOutput ? SERIAL_TX_ONLY : SERIAL_FULL;
SerialMode mode = (sbusSerialOutput || sumdSerialOutput) ? SERIAL_TX_ONLY : SERIAL_FULL;
Serial.begin(serialBaud, config, mode, -1, invert);
#elif defined(PLATFORM_ESP32)
uint32_t config = sbusSerialOutput ? SERIAL_8E2 : SERIAL_8N1;
Expand All @@ -1219,6 +1226,10 @@ static void setupSerial()
{
serialIO = new SerialSBUS(SERIAL_PROTOCOL_TX, SERIAL_PROTOCOL_RX);
}
else if (sumdSerialOutput)
{
serialIO = new SerialSUMD(SERIAL_PROTOCOL_TX, SERIAL_PROTOCOL_RX);
}
else
{
serialIO = new SerialCRSF(SERIAL_PROTOCOL_TX, SERIAL_PROTOCOL_RX);
Expand Down