mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-23 16:25:31 +03:00
reorganization of uart-based receiver drivers
FEATURE_SPEKTRUM has been removed and replaced with FEATURE_SERIALRX. cli option serialrx_type now configures what type of receiver it is 0 = spektrum1024, 1 = spektrum2048, 2 = sbus sbus will need hardware inverter to use. also cleaned up receiver drivers to assign readrawRC callback instead of assigning in code in main() none of this has been tested. git-svn-id: https://afrodevices.googlecode.com/svn/trunk/baseflight@418 7c89a4a9-59b9-e629-4cfe-3a2d53b20e61
This commit is contained in:
parent
04ab548d2e
commit
2272e1a5a6
10 changed files with 191 additions and 31 deletions
108
src/sbus.c
Normal file
108
src/sbus.c
Normal file
|
@ -0,0 +1,108 @@
|
|||
#include "board.h"
|
||||
#include "mw.h"
|
||||
|
||||
// driver for SBUS receiver using UART2
|
||||
|
||||
#define SBUS_MAX_CHANNEL 8
|
||||
#define SBUS_FRAME_SIZE 25
|
||||
#define SBUS_SYNCBYTE 0x0F
|
||||
#define SBUS_ENDBYTE 0x00
|
||||
#define SBUS_OFFSET 988
|
||||
|
||||
static bool sbusFrameDone = false;
|
||||
static void sbusDataReceive(uint16_t c);
|
||||
static uint16_t sbusReadRawRC(uint8_t chan);
|
||||
|
||||
// external vars (ugh)
|
||||
extern int16_t failsafeCnt;
|
||||
|
||||
static uint32_t sbusChannelData[SBUS_MAX_CHANNEL];
|
||||
|
||||
void sbusInit(rcReadRawDataPtr *callback)
|
||||
{
|
||||
int b;
|
||||
for (b = 0; b < SBUS_MAX_CHANNEL; b ++)
|
||||
sbusChannelData[b] = 2 * (mcfg.midrc - SBUS_OFFSET);
|
||||
core.rcvrport = uartOpen(USART2, sbusDataReceive, 100000, MODE_RX);
|
||||
if (callback)
|
||||
*callback = sbusReadRawRC;
|
||||
}
|
||||
|
||||
struct sbus_dat
|
||||
{
|
||||
unsigned int chan0 : 11;
|
||||
unsigned int chan1 : 11;
|
||||
unsigned int chan2 : 11;
|
||||
unsigned int chan3 : 11;
|
||||
unsigned int chan4 : 11;
|
||||
unsigned int chan5 : 11;
|
||||
unsigned int chan6 : 11;
|
||||
unsigned int chan7 : 11;
|
||||
unsigned int chan8 : 11;
|
||||
unsigned int chan9 : 11;
|
||||
unsigned int chan10 : 11;
|
||||
unsigned int chan11 : 11;
|
||||
} __attribute__ ((__packed__));
|
||||
|
||||
typedef union
|
||||
{
|
||||
uint8_t in[SBUS_FRAME_SIZE];
|
||||
struct sbus_dat msg;
|
||||
} sbus_msg;
|
||||
|
||||
static sbus_msg sbus;
|
||||
|
||||
// Receive ISR callback
|
||||
static void sbusDataReceive(uint16_t c)
|
||||
{
|
||||
uint32_t sbusTime;
|
||||
static uint32_t sbusTimeLast;
|
||||
static uint8_t sbusFramePosition;
|
||||
|
||||
sbusTime = micros();
|
||||
if ((sbusTime - sbusTimeLast) > 4000)
|
||||
sbusFramePosition = 0;
|
||||
sbusTimeLast = sbusTime;
|
||||
|
||||
if (sbusFramePosition == 0 && c != SBUS_SYNCBYTE)
|
||||
return;
|
||||
|
||||
sbusFrameDone = false; // lazy main loop didnt fetch the stuff
|
||||
if (sbusFramePosition != 0)
|
||||
sbus.in[sbusFramePosition - 1] = (uint8_t)c;
|
||||
|
||||
if (sbusFramePosition == SBUS_FRAME_SIZE - 1) {
|
||||
if (sbus.in[sbusFramePosition - 1] == SBUS_ENDBYTE)
|
||||
sbusFrameDone = true;
|
||||
sbusFramePosition = 0;
|
||||
} else {
|
||||
sbusFramePosition++;
|
||||
}
|
||||
}
|
||||
|
||||
bool sbusFrameComplete(void)
|
||||
{
|
||||
if (sbusFrameDone) {
|
||||
if (!((sbus.in[22] >> 3) & 0x0001)) { // failsave flag
|
||||
failsafeCnt = 0; // clear FailSafe counter
|
||||
sbusChannelData[0] = sbus.msg.chan0;
|
||||
sbusChannelData[1] = sbus.msg.chan1;
|
||||
sbusChannelData[2] = sbus.msg.chan2;
|
||||
sbusChannelData[3] = sbus.msg.chan3;
|
||||
sbusChannelData[4] = sbus.msg.chan4;
|
||||
sbusChannelData[5] = sbus.msg.chan5;
|
||||
sbusChannelData[6] = sbus.msg.chan6;
|
||||
sbusChannelData[7] = sbus.msg.chan7;
|
||||
// need more channels? No problem. Add them.
|
||||
sbusFrameDone = false;
|
||||
return true;
|
||||
}
|
||||
sbusFrameDone = false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static uint16_t sbusReadRawRC(uint8_t chan)
|
||||
{
|
||||
return sbusChannelData[mcfg.rcmap[chan]] / 2 + SBUS_OFFSET;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue