mirror of
https://github.com/paparazzi/paparazzi.git
synced 2026-06-05 06:54:49 +08:00
[radio] add initial support for Futaba SBUS protocol
This commit is contained in:
committed by
Felix Ruess
parent
2eda944ac8
commit
785ad78633
@@ -23,10 +23,10 @@
|
||||
<define name="USE_GYRO_PITCH_RATE"/>
|
||||
|
||||
<target name="sim" board="pc"/>
|
||||
<target name="ap" board="apogee_0.99"/>
|
||||
<configure name="FLASH_MODE" value="SWD"/>
|
||||
<target name="ap" board="apogee_1.0"/>
|
||||
<!--configure name="FLASH_MODE" value="SWD"/-->
|
||||
|
||||
<subsystem name="radio_control" type="ppm"/>
|
||||
<subsystem name="radio_control" type="sbus"/>
|
||||
|
||||
<!-- Communication -->
|
||||
<subsystem name="telemetry" type="transparent"/>
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
#
|
||||
# Makefile for shared radio_control SBUS subsystem
|
||||
#
|
||||
|
||||
$(TARGET).CFLAGS += -DRADIO_CONTROL
|
||||
ifneq ($(RADIO_CONTROL_LED),none)
|
||||
ap.CFLAGS += -DRADIO_CONTROL_LED=$(RADIO_CONTROL_LED)
|
||||
endif
|
||||
$(TARGET).CFLAGS += -DUSE_$(SBUS_PORT) -DSBUS_LINK=$(SBUS_PORT) -D$(SBUS_PORT)_BAUD=B100000
|
||||
$(TARGET).CFLAGS += -DRADIO_CONTROL_TYPE_H=\"subsystems/radio_control/sbus.h\"
|
||||
$(TARGET).CFLAGS += -DRADIO_CONTROL_TYPE_SBUS
|
||||
$(TARGET).srcs += $(SRC_SUBSYSTEMS)/radio_control.c
|
||||
$(TARGET).srcs += $(SRC_SUBSYSTEMS)/radio_control/sbus.c
|
||||
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Alexandre Bustico, Gautier Hattenberger
|
||||
*
|
||||
* This file is part of paparazzi.
|
||||
*
|
||||
* paparazzi is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* paparazzi 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with paparazzi; see the file COPYING. If not, write to
|
||||
* the Free Software Foundation, 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
/** @file subsystems/radio_control/sbus.c
|
||||
*
|
||||
* Futaba SBUS decoder
|
||||
*/
|
||||
|
||||
#include "subsystems/radio_control.h"
|
||||
#include "subsystems/radio_control/sbus.h"
|
||||
#include BOARD_CONFIG
|
||||
#include "mcu_periph/uart.h"
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
* SBUS protocol and state machine status
|
||||
*/
|
||||
#define SBUS_START_BYTE 0x0f
|
||||
#define SBUS_END_BYTE 0x00
|
||||
#define SBUS_BIT_PER_CHANNEL 11
|
||||
#define SBUS_BIT_PER_BYTE 8
|
||||
|
||||
#define SBUS_STATUS_UNINIT 0
|
||||
#define SBUS_STATUS_GOT_START 1
|
||||
|
||||
/** Set polarity using RC_POLARITY_LED.
|
||||
* SBUS signal has a reversed polarity compared to normal UART
|
||||
* this allow to using hardware UART peripheral by changing
|
||||
* the input signal polarity
|
||||
*/
|
||||
#ifndef RC_SET_POLARITY
|
||||
#define RC_SET_POLARITY LED_ON
|
||||
#endif
|
||||
|
||||
/** UART selecting
|
||||
*/
|
||||
#define __SBusLink(dev, _x) dev##_x
|
||||
#define _SBusLink(dev, _x) __SBusLink(dev, _x)
|
||||
#define SBusLink(_x) _SBusLink(SBUS_LINK, _x)
|
||||
|
||||
/* SBUS struct */
|
||||
struct _sbus sbus;
|
||||
|
||||
// Init function
|
||||
void radio_control_impl_init(void) {
|
||||
sbus.frame_available = FALSE;
|
||||
sbus.status = SBUS_STATUS_UNINIT;
|
||||
|
||||
// Set UART parameters (100K, 8 bits, 2 stops, even parity)
|
||||
SBusLink(SetBitsStopParity(UBITS_8, USTOP_2, UPARITY_EVEN));
|
||||
SBusLink(SetBaudrate(B100000));
|
||||
|
||||
// Set polarity
|
||||
#ifdef RC_POLARITY_LED
|
||||
RC_SET_POLARITY(RC_POLARITY_LED);
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* This part is used by the autopilot to read data from a uart
|
||||
*/
|
||||
#define SBusBuffer() SBusLink(ChAvailable())
|
||||
#define SBusGet() SBusLink(Getch())
|
||||
|
||||
// Decode the raw buffer
|
||||
static void decode_sbus_buffer (const uint8_t *src, uint16_t *dst)
|
||||
{
|
||||
// reset counters
|
||||
uint8_t byteInRawBuf = 0;
|
||||
uint8_t bitInRawBuf = 0;
|
||||
uint8_t channel = 0;
|
||||
uint8_t bitInChannel = 0;
|
||||
|
||||
// clear bits
|
||||
memset (dst, 0, SBUS_NB_CHANNEL*sizeof(uint16_t));
|
||||
|
||||
// decode sbus data
|
||||
for (uint8_t i=0; i< (SBUS_NB_CHANNEL*SBUS_BIT_PER_CHANNEL); i++) {
|
||||
if (src[byteInRawBuf] & (1<<bitInRawBuf))
|
||||
dst[channel] |= (1<<bitInChannel);
|
||||
|
||||
bitInRawBuf++;
|
||||
bitInChannel++;
|
||||
|
||||
if (bitInRawBuf == SBUS_BIT_PER_BYTE) {
|
||||
bitInRawBuf = 0;
|
||||
byteInRawBuf++;
|
||||
}
|
||||
if (bitInChannel == SBUS_BIT_PER_CHANNEL) {
|
||||
bitInChannel = 0;
|
||||
channel++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Decoding event function
|
||||
// Reading from UART
|
||||
void sbus_decode_event(void) {
|
||||
uint8_t rbyte;
|
||||
if (SBusBuffer()) {
|
||||
do {
|
||||
rbyte = SBusGet();
|
||||
switch (sbus.status) {
|
||||
case SBUS_STATUS_UNINIT:
|
||||
// Wait for the start byte
|
||||
if (rbyte == SBUS_START_BYTE) {
|
||||
sbus.status++;
|
||||
sbus.idx = 0;
|
||||
}
|
||||
break;
|
||||
case SBUS_STATUS_GOT_START:
|
||||
// Store buffer
|
||||
sbus.buffer[sbus.idx] = rbyte;
|
||||
sbus.idx++;
|
||||
if (sbus.idx == SBUS_BUF_LENGTH) {
|
||||
// Decode if last byte is the correct end byte
|
||||
if (rbyte == SBUS_END_BYTE) decode_sbus_buffer(sbus.buffer, sbus.pulses);
|
||||
sbus.status = SBUS_STATUS_UNINIT;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} while (SBusBuffer());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Alexandre Bustico, Gautier Hattenberger
|
||||
*
|
||||
* This file is part of paparazzi.
|
||||
*
|
||||
* paparazzi is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* paparazzi 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with paparazzi; see the file COPYING. If not, write to
|
||||
* the Free Software Foundation, 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#ifndef RC_SBUS_H
|
||||
#define RC_SBUS_H
|
||||
|
||||
/** @file subsystems/radio_control/sbus.h
|
||||
*
|
||||
* Futaba SBUS decoder
|
||||
*/
|
||||
|
||||
#include "std.h"
|
||||
|
||||
///**
|
||||
// * Architecture dependant code
|
||||
// */
|
||||
//#include "subsystems/radio_control/ppm_arch.h"
|
||||
///* must be implemented by arch dependant code */
|
||||
//extern void ppm_arch_init(void);
|
||||
|
||||
/**
|
||||
* Dummy macro to use radio.h file
|
||||
*/
|
||||
#define RC_PPM_TICKS_OF_USEC(_v) (_v)
|
||||
#define RC_PPM_SIGNED_TICKS_OF_USEC(_v) (_v)
|
||||
#define USEC_OF_RC_PPM_TICKS(_v) (_v)
|
||||
|
||||
/**
|
||||
* Generated code holding the description of a given
|
||||
* transmitter
|
||||
*/
|
||||
#include "generated/radio.h"
|
||||
|
||||
/**
|
||||
* Define number of channels
|
||||
*/
|
||||
#define SBUS_BUF_LENGTH 24
|
||||
#define SBUS_NB_CHANNEL 16
|
||||
#define RADIO_CONTROL_NB_CHANNEL SBUS_NB_CHANNEL
|
||||
|
||||
/**
|
||||
* SBUS structure
|
||||
*/
|
||||
struct _sbus {
|
||||
uint16_t pulses[SBUS_NB_CHANNEL]; ///< decoded values
|
||||
bool_t frame_available; ///< new frame available
|
||||
uint8_t buffer[SBUS_BUF_LENGTH]; ///< input buffer
|
||||
uint8_t idx; ///< input index
|
||||
uint8_t status; ///< decoder state machine status
|
||||
};
|
||||
|
||||
extern struct _sbus sbus;
|
||||
|
||||
/**
|
||||
* Decoding event function
|
||||
*/
|
||||
extern void sbus_decode_event(void);
|
||||
|
||||
/**
|
||||
* Event macro with handler callback
|
||||
*/
|
||||
#define RadioControlEvent(_received_frame_handler) { \
|
||||
sbus_decode_event(); \
|
||||
if (sbus.frame_available) { \
|
||||
radio_control.frame_cpt++; \
|
||||
radio_control.time_since_last_frame = 0; \
|
||||
if (radio_control.radio_ok_cpt > 0) { \
|
||||
radio_control.radio_ok_cpt--; \
|
||||
} else { \
|
||||
radio_control.status = RC_OK; \
|
||||
NormalizePpmIIR(sbus.pulses,radio_control); \
|
||||
_received_frame_handler(); \
|
||||
} \
|
||||
sbus.frame_available = FALSE; \
|
||||
} \
|
||||
}
|
||||
|
||||
#endif /* RC_SBUS_H */
|
||||
Reference in New Issue
Block a user