* Re #2999: More strict SBUS frame check (only allow 25 byte frames, check end byte) * SBUS fixes
This commit is contained in:
committed by
Bertrand Songis
parent
7eba760797
commit
29a139559d
+16
-21
@@ -24,40 +24,31 @@
|
|||||||
#define SBUS_FRAME_GAP_DELAY 1000 // 500uS
|
#define SBUS_FRAME_GAP_DELAY 1000 // 500uS
|
||||||
|
|
||||||
#define SBUS_START_BYTE 0x0F
|
#define SBUS_START_BYTE 0x0F
|
||||||
|
#define SBUS_END_BYTE 0x00
|
||||||
#define SBUS_FLAGS_IDX 23
|
#define SBUS_FLAGS_IDX 23
|
||||||
#define SBUS_FRAMELOST_BIT 2
|
#define SBUS_FRAMELOST_BIT 2
|
||||||
#define SBUS_FAILSAFE_BIT 3
|
#define SBUS_FAILSAFE_BIT 3
|
||||||
|
|
||||||
#define SBUS_CH_BITS 11
|
#define SBUS_CH_BITS 11
|
||||||
#define SBUS_CH_MASK ((1<<SBUS_CH_BITS)-1)
|
#define SBUS_CH_MASK ((1<<SBUS_CH_BITS)-1)
|
||||||
|
|
||||||
#define SBUS_CH_CENTER 0x3E0
|
#define SBUS_CH_CENTER 0x3E0
|
||||||
|
|
||||||
uint8_t SbusFrame[SBUS_MAX_FRAME_SIZE];
|
|
||||||
uint16_t SbusTimer ;
|
|
||||||
uint8_t SbusIndex = 0 ;
|
|
||||||
|
|
||||||
// Range for pulses (ppm input) is [-512:+512]
|
// Range for pulses (ppm input) is [-512:+512]
|
||||||
void processSbusFrame(uint8_t * sbus, int16_t * pulses, uint32_t size)
|
void processSbusFrame(uint8_t * sbus, int16_t * pulses, uint32_t size)
|
||||||
{
|
{
|
||||||
uint32_t inputbitsavailable = 0;
|
if (size != SBUS_FRAME_SIZE || sbus[0] != SBUS_START_BYTE || sbus[SBUS_FRAME_SIZE-1] != SBUS_END_BYTE) {
|
||||||
uint32_t inputbits = 0;
|
|
||||||
|
|
||||||
if (sbus[0] != SBUS_START_BYTE) {
|
|
||||||
return; // not a valid SBUS frame
|
return; // not a valid SBUS frame
|
||||||
}
|
}
|
||||||
|
if ((sbus[SBUS_FLAGS_IDX] & (1<<SBUS_FAILSAFE_BIT)) || (sbus[SBUS_FLAGS_IDX] & (1<<SBUS_FRAMELOST_BIT))) {
|
||||||
if (size < SBUS_MIN_FRAME_SIZE) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (size > SBUS_FLAGS_IDX && ((sbus[SBUS_FLAGS_IDX] & (1<<SBUS_FAILSAFE_BIT)) || (sbus[SBUS_FLAGS_IDX] & (1<<SBUS_FRAMELOST_BIT)))) {
|
|
||||||
return; // SBUS invalid frame or failsafe mode
|
return; // SBUS invalid frame or failsafe mode
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip start byte
|
sbus++; // skip start byte
|
||||||
sbus++;
|
|
||||||
|
|
||||||
|
uint32_t inputbitsavailable = 0;
|
||||||
|
uint32_t inputbits = 0;
|
||||||
for (uint32_t i=0; i<NUM_TRAINER; i++) {
|
for (uint32_t i=0; i<NUM_TRAINER; i++) {
|
||||||
while (inputbitsavailable < SBUS_CH_BITS) {
|
while (inputbitsavailable < SBUS_CH_BITS) {
|
||||||
inputbits |= *sbus++ << inputbitsavailable;
|
inputbits |= *sbus++ << inputbitsavailable;
|
||||||
@@ -76,12 +67,16 @@ void processSbusInput()
|
|||||||
#if !defined(SIMU)
|
#if !defined(SIMU)
|
||||||
uint8_t rxchar;
|
uint8_t rxchar;
|
||||||
uint32_t active = 0;
|
uint32_t active = 0;
|
||||||
|
static uint8_t SbusIndex = 0;
|
||||||
|
static uint16_t SbusTimer;
|
||||||
|
static uint8_t SbusFrame[SBUS_FRAME_SIZE];
|
||||||
|
|
||||||
while (sbusGetByte(&rxchar)) {
|
while (sbusGetByte(&rxchar)) {
|
||||||
active = 1;
|
active = 1;
|
||||||
SbusFrame[SbusIndex++] = rxchar;
|
if (SbusIndex > SBUS_FRAME_SIZE-1) {
|
||||||
if (SbusIndex > SBUS_MAX_FRAME_SIZE-1) {
|
SbusIndex = SBUS_FRAME_SIZE-1;
|
||||||
SbusIndex = SBUS_MAX_FRAME_SIZE-1;
|
|
||||||
}
|
}
|
||||||
|
SbusFrame[SbusIndex++] = rxchar;
|
||||||
}
|
}
|
||||||
if (active) {
|
if (active) {
|
||||||
SbusTimer = getTmr2MHz();
|
SbusTimer = getTmr2MHz();
|
||||||
|
|||||||
+1
-2
@@ -22,8 +22,7 @@
|
|||||||
#define _SBUS_H_
|
#define _SBUS_H_
|
||||||
|
|
||||||
#define SBUS_BAUDRATE 100000
|
#define SBUS_BAUDRATE 100000
|
||||||
#define SBUS_MIN_FRAME_SIZE 23
|
#define SBUS_FRAME_SIZE 25
|
||||||
#define SBUS_MAX_FRAME_SIZE 28
|
|
||||||
|
|
||||||
void processSbusInput();
|
void processSbusInput();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user