Implement SBUS output for external module bay. (#5225)
* Implement SBUS output for external module bay. * Bugfixes and Companion support
This commit is contained in:
committed by
Bertrand Songis
parent
710e6ddc4c
commit
fd782538d8
@@ -26,77 +26,11 @@
|
||||
|
||||
|
||||
|
||||
/* The protocol is heavily inspired by the DSM2 protocol, so reuse some the definitions where they are identical */
|
||||
|
||||
#define MULTI_SEND_BIND (1 << 7)
|
||||
#define MULTI_SEND_RANGECHECK (1 << 5)
|
||||
#define MULTI_SEND_AUTOBIND (1 << 6)
|
||||
|
||||
|
||||
#define BITLEN_MULTI (10*2) //100000 Baud => 10uS per bit
|
||||
|
||||
#if defined(PPM_PIN_SERIAL)
|
||||
static void sendByteMulti(uint8_t b)
|
||||
{
|
||||
uint8_t parity = 1;
|
||||
|
||||
putDsm2SerialBit(0); // Start bit
|
||||
for (uint8_t i=0; i<8; i++) { // 8 data Bits
|
||||
putDsm2SerialBit(b & 1);
|
||||
parity = parity ^ (b & 1);
|
||||
b >>= 1;
|
||||
}
|
||||
putDsm2SerialBit(!parity); // Even Parity bit
|
||||
|
||||
putDsm2SerialBit(1); // Stop bit
|
||||
putDsm2SerialBit(1); // Stop bit
|
||||
}
|
||||
#else
|
||||
static void _send_level(uint8_t v)
|
||||
{
|
||||
/* Copied over from DSM, this looks doubious and in my logic analyzer
|
||||
output the low->high is about 2 ns late */
|
||||
if (modulePulsesData[EXTERNAL_MODULE].dsm2.index & 1)
|
||||
v += 2;
|
||||
else
|
||||
v -= 2;
|
||||
|
||||
*modulePulsesData[EXTERNAL_MODULE].dsm2.ptr++ = v - 1;
|
||||
modulePulsesData[EXTERNAL_MODULE].dsm2.index+=1;
|
||||
modulePulsesData[EXTERNAL_MODULE].dsm2.rest -=v;
|
||||
}
|
||||
|
||||
static void sendByteMulti(uint8_t b) //max 11 changes 0 10 10 10 10 P 1
|
||||
{
|
||||
bool lev = 0;
|
||||
uint8_t parity = 1;
|
||||
|
||||
uint8_t len = BITLEN_MULTI; //max val: 10*20 < 256
|
||||
for (uint8_t i=0; i<=9; i++) { //8Bits + 1Parity + Stop=1
|
||||
bool nlev = b & 1; //lsb first
|
||||
parity = parity ^ (uint8_t)nlev;
|
||||
if (lev == nlev) {
|
||||
len += BITLEN_MULTI;
|
||||
}
|
||||
else {
|
||||
_send_level(len);
|
||||
len = BITLEN_MULTI;
|
||||
lev = nlev;
|
||||
}
|
||||
b = (b>>1) | 0x80; //shift in ones for stop bit and parity
|
||||
if (i==7)
|
||||
b = b ^ parity; // lowest bit is one from previous line
|
||||
}
|
||||
_send_level(len+ BITLEN_MULTI); // enlarge the last bit to be two stop bits long
|
||||
}
|
||||
#endif
|
||||
|
||||
// This is the data stream to send, prepare after 19.5 mS
|
||||
// Send after 22.5 mS
|
||||
|
||||
//static uint8_t *Dsm2_pulsePtr = pulses2MHz.pbyte ;
|
||||
|
||||
|
||||
#define MULTI_CHANS 16
|
||||
#define MULTI_CHAN_BITS 11
|
||||
void setupPulsesMultimodule(uint8_t port)
|
||||
@@ -180,9 +114,9 @@ void setupPulsesMultimodule(uint8_t port)
|
||||
|
||||
// header, byte 0, 0x55 for proto 0-31 0x54 for 32-63
|
||||
if (type <= 31)
|
||||
sendByteMulti(0x55);
|
||||
sendByteSbus(0x55);
|
||||
else
|
||||
sendByteMulti(0x54);
|
||||
sendByteSbus(0x54);
|
||||
|
||||
|
||||
// protocol byte
|
||||
@@ -190,16 +124,16 @@ void setupPulsesMultimodule(uint8_t port)
|
||||
if(g_model.moduleData[port].getMultiProtocol(true) != MM_RF_PROTO_DSM2)
|
||||
protoByte |= (g_model.moduleData[port].multi.autoBindMode << 6);
|
||||
|
||||
sendByteMulti(protoByte);
|
||||
sendByteSbus(protoByte);
|
||||
|
||||
// byte 2, subtype, powermode, model id
|
||||
sendByteMulti((uint8_t) ((g_model.header.modelId[port] & 0x0f)
|
||||
sendByteSbus((uint8_t) ((g_model.header.modelId[port] & 0x0f)
|
||||
| ((subtype & 0x7) << 4)
|
||||
| (g_model.moduleData[port].multi.lowPowerMode << 7))
|
||||
);
|
||||
|
||||
// byte 3
|
||||
sendByteMulti((uint8_t) optionValue);
|
||||
sendByteSbus((uint8_t) optionValue);
|
||||
|
||||
uint32_t bits = 0;
|
||||
uint8_t bitsavailable = 0;
|
||||
@@ -216,7 +150,7 @@ void setupPulsesMultimodule(uint8_t port)
|
||||
bits |= limit(0, value, 2047) << bitsavailable;
|
||||
bitsavailable += MULTI_CHAN_BITS;
|
||||
while (bitsavailable >= 8) {
|
||||
sendByteMulti((uint8_t) (bits & 0xff));
|
||||
sendByteSbus((uint8_t) (bits & 0xff));
|
||||
bits >>= 8;
|
||||
bitsavailable -= 8;
|
||||
}
|
||||
|
||||
@@ -77,6 +77,12 @@ enum ModuleFlag
|
||||
#define IS_MULTIMODULE_PROTOCOL(protocol) (0)
|
||||
#endif
|
||||
|
||||
#if defined(CPUARM)
|
||||
#define IS_SBUS_PROTOCOL(protocol) (protocol == PROTO_SBUS)
|
||||
#else
|
||||
#define IS_SBUS_PROTOCOL(protocol) (0)
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(CPUARM)
|
||||
#include "pulses_arm.h"
|
||||
|
||||
@@ -65,6 +65,9 @@ uint8_t getRequiredProtocol(uint8_t port)
|
||||
case MODULE_TYPE_R9M:
|
||||
required_protocol = PROTO_PXX;
|
||||
break;
|
||||
case MODULE_TYPE_SBUS:
|
||||
required_protocol = PROTO_SBUS;
|
||||
break;
|
||||
#if defined(MULTIMODULE)
|
||||
case MODULE_TYPE_MULTIMODULE:
|
||||
required_protocol = PROTO_MULTIMODULE;
|
||||
@@ -145,9 +148,10 @@ void setupPulses(uint8_t port)
|
||||
|
||||
#if defined(MULTIMODULE)
|
||||
case PROTO_MULTIMODULE:
|
||||
disable_multimodule(port);
|
||||
break;
|
||||
#endif
|
||||
case PROTO_SBUS:
|
||||
disable_sbusOut(port);
|
||||
break;
|
||||
|
||||
case PROTO_PPM:
|
||||
disable_ppm(port);
|
||||
@@ -166,6 +170,10 @@ void setupPulses(uint8_t port)
|
||||
setupPulsesPXX(port);
|
||||
scheduleNextMixerCalculation(port, 9);
|
||||
break;
|
||||
case PROTO_SBUS:
|
||||
setupPulsesSbus(port);
|
||||
scheduleNextMixerCalculation(port, (45+g_model.moduleData[port].sbus.refreshRate)/2);
|
||||
break;
|
||||
|
||||
#if defined(DSM2)
|
||||
case PROTO_DSM2_LP45:
|
||||
@@ -240,9 +248,11 @@ void setupPulses(uint8_t port)
|
||||
|
||||
#if defined(MULTIMODULE)
|
||||
case PROTO_MULTIMODULE:
|
||||
init_multimodule(port);
|
||||
break;
|
||||
#endif
|
||||
case PROTO_SBUS:
|
||||
init_sbusOut(port);
|
||||
break;
|
||||
|
||||
|
||||
case PROTO_PPM:
|
||||
init_ppm(port);
|
||||
|
||||
@@ -79,6 +79,7 @@ PACK(struct PxxUartPulsesData {
|
||||
/* DSM2 uses 2 header + 12 channel bytes, with max 10 changes (8n2) per byte + 16 bits trailer ~= 156 max pulses */
|
||||
/* Multimodule uses 3 bytes header + 22 channel bytes with max 11 changes per byte (8e2) + 16 bits trailer ~= 291 max pulses */
|
||||
/* Multimodule reuses some of the DSM2 function and structs since the protocols are similar enough */
|
||||
/* sbus is 1 byte header, 22 channel bytes (11bit * 16ch) + 1 byte flags */
|
||||
PACK(struct PxxTimerPulsesData {
|
||||
pulse_duration_t pulses[200];
|
||||
pulse_duration_t * ptr;
|
||||
@@ -87,11 +88,7 @@ PACK(struct PxxTimerPulsesData {
|
||||
uint32_t pcmOnesCount;
|
||||
});
|
||||
|
||||
#if defined(MULTIMODULE)
|
||||
#define MAX_PULSES_TRANSITIONS 300
|
||||
#else
|
||||
#define MAX_PULSES_TRANSITIONS 200
|
||||
#endif
|
||||
|
||||
PACK(struct Dsm2TimerPulsesData {
|
||||
pulse_duration_t pulses[MAX_PULSES_TRANSITIONS];
|
||||
@@ -128,6 +125,7 @@ union ModulePulsesData {
|
||||
* sizeof(ModulePulsesData). __ALIGNED is required for sizeof(ModulePulsesData) to be a multiple of the alignment.
|
||||
*/
|
||||
|
||||
/* TODO: internal pulsedata only needs 200 bytes vs 300 bytes for external, both use 300 byte since we have a common struct */
|
||||
extern ModulePulsesData modulePulsesData[NUM_MODULES];
|
||||
|
||||
union TrainerPulsesData {
|
||||
@@ -140,12 +138,14 @@ extern const uint16_t CRCTable[];
|
||||
void setupPulses(uint8_t port);
|
||||
void setupPulsesDSM2(uint8_t port);
|
||||
void setupPulsesMultimodule(uint8_t port);
|
||||
void setupPulsesSbus(uint8_t port);
|
||||
void setupPulsesPXX(uint8_t port);
|
||||
void setupPulsesPPMModule(uint8_t port);
|
||||
void setupPulsesPPMTrainer();
|
||||
void sendByteDsm2(uint8_t b);
|
||||
void putDsm2Flush();
|
||||
void putDsm2SerialBit(uint8_t bit);
|
||||
void sendByteSbus(uint8_t byte);
|
||||
|
||||
#if defined(HUBSAN)
|
||||
void Hubsan_Init();
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
* Copyright (C) OpenTX
|
||||
*
|
||||
* Based on code named
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "opentx.h"
|
||||
|
||||
|
||||
#define BITLEN_SBUS (10*2) //100000 Baud => 10uS per bit
|
||||
|
||||
|
||||
/* The protocol reuse some the DSM2 definitions where they are identical */
|
||||
|
||||
|
||||
#if defined(PPM_PIN_SERIAL)
|
||||
void sendByteSbus(uint8_t b)
|
||||
{
|
||||
uint8_t parity = 1;
|
||||
|
||||
putDsm2SerialBit(0); // Start bit
|
||||
for (uint8_t i=0; i<8; i++) { // 8 data Bits
|
||||
putDsm2SerialBit(b & 1);
|
||||
parity = parity ^ (b & 1);
|
||||
b >>= 1;
|
||||
}
|
||||
putDsm2SerialBit(!parity); // Even Parity bit
|
||||
|
||||
putDsm2SerialBit(1); // Stop bit
|
||||
putDsm2SerialBit(1); // Stop bit
|
||||
}
|
||||
#else
|
||||
static void _send_level(uint8_t v)
|
||||
{
|
||||
/* Copied over from DSM, this looks doubious and in my logic analyzer
|
||||
output the low->high is about 2 ns late */
|
||||
if (modulePulsesData[EXTERNAL_MODULE].dsm2.index & 1)
|
||||
v += 2;
|
||||
else
|
||||
v -= 2;
|
||||
|
||||
*modulePulsesData[EXTERNAL_MODULE].dsm2.ptr++ = v - 1;
|
||||
modulePulsesData[EXTERNAL_MODULE].dsm2.index+=1;
|
||||
modulePulsesData[EXTERNAL_MODULE].dsm2.rest -=v;
|
||||
}
|
||||
|
||||
void sendByteSbus(uint8_t b) //max 11 changes 0 10 10 10 10 P 1
|
||||
{
|
||||
bool lev = 0;
|
||||
uint8_t parity = 1;
|
||||
|
||||
uint8_t len = BITLEN_SBUS; //max val: 10*20 < 256
|
||||
for (uint8_t i=0; i<=9; i++) { //8Bits + 1Parity + Stop=1
|
||||
bool nlev = b & 1; //lsb first
|
||||
parity = parity ^ (uint8_t)nlev;
|
||||
if (lev == nlev) {
|
||||
len += BITLEN_SBUS;
|
||||
}
|
||||
else {
|
||||
_send_level(len);
|
||||
len = BITLEN_SBUS;
|
||||
lev = nlev;
|
||||
}
|
||||
b = (b>>1) | 0x80; //shift in ones for stop bit and parity
|
||||
if (i==7)
|
||||
b = b ^ parity; // lowest bit is one from previous line
|
||||
}
|
||||
_send_level(len+ BITLEN_SBUS); // enlarge the last bit to be two stop bits long
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
#define SBUS_NORMAL_CHANS 16
|
||||
#define SBUS_CHAN_BITS 11
|
||||
|
||||
|
||||
/* Definitions from CleanFlight/BetaFlight */
|
||||
|
||||
#define SBUS_FLAG_CHANNEL_17 (1 << 0)
|
||||
#define SBUS_FLAG_CHANNEL_18 (1 << 1)
|
||||
#define SBUS_FLAG_SIGNAL_LOSS (1 << 2)
|
||||
#define SBUS_FLAG_FAILSAFE_ACTIVE (1 << 3)
|
||||
#define SBUS_FRAME_BEGIN_BYTE 0x0F
|
||||
|
||||
#define SBUS_CHAN_CENTER 992
|
||||
|
||||
inline int getChannelValue(uint8_t port, int channel) {
|
||||
int ch = g_model.moduleData[port].channelsStart+channel;
|
||||
// We will ignore 17 and 18th if that brings us over the limit
|
||||
if (ch > 31)
|
||||
return 0;
|
||||
return channelOutputs[ch] + 2*PPM_CH_CENTER(ch) - 2*PPM_CENTER;
|
||||
}
|
||||
|
||||
void setupPulsesSbus(uint8_t port)
|
||||
{
|
||||
#if defined(PPM_PIN_SERIAL)
|
||||
modulePulsesData[EXTERNAL_MODULE].dsm2.serialByte = 0 ;
|
||||
modulePulsesData[EXTERNAL_MODULE].dsm2.serialBitCount = 0 ;
|
||||
#else
|
||||
modulePulsesData[EXTERNAL_MODULE].dsm2.rest = (g_model.moduleData[EXTERNAL_MODULE].sbus.refreshRate * 5 + 225)*200 ;
|
||||
modulePulsesData[EXTERNAL_MODULE].dsm2.index = 0;
|
||||
#endif
|
||||
|
||||
modulePulsesData[EXTERNAL_MODULE].dsm2.ptr = modulePulsesData[EXTERNAL_MODULE].dsm2.pulses;
|
||||
|
||||
|
||||
// Sync Byte
|
||||
sendByteSbus(SBUS_FRAME_BEGIN_BYTE);
|
||||
|
||||
uint32_t bits = 0;
|
||||
uint8_t bitsavailable = 0;
|
||||
|
||||
// byte 1-22, channels 0..2047, limits not really clear (B
|
||||
for (int i=0; i<SBUS_NORMAL_CHANS; i++) {
|
||||
int value = getChannelValue(port, i);
|
||||
|
||||
value = value*8/10 + SBUS_CHAN_CENTER;
|
||||
bits |= limit(0, value, 2047) << bitsavailable;
|
||||
bitsavailable += SBUS_CHAN_BITS;
|
||||
while (bitsavailable >= 8) {
|
||||
sendByteSbus((uint8_t) (bits & 0xff));
|
||||
bits >>= 8;
|
||||
bitsavailable -= 8;
|
||||
}
|
||||
}
|
||||
// Flags
|
||||
uint8_t flags=0;
|
||||
if (getChannelValue(port, 16) > 0)
|
||||
flags |=SBUS_FLAG_CHANNEL_17;
|
||||
if (getChannelValue(port, 17) > 0)
|
||||
flags |=SBUS_FLAG_CHANNEL_18;
|
||||
|
||||
sendByteSbus(flags);
|
||||
|
||||
// Last byte, always 0x0
|
||||
sendByteSbus(0x0);
|
||||
|
||||
putDsm2Flush();
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user