Skip to content

Commit

Permalink
[radio] rewrite the Spektrum driver to be generic (#2250)
Browse files Browse the repository at this point in the history
This is based on #1799 and replace it.
Now, the normal UART driver is used instead of the specific code. It
makes it possible to use on only arch, including ChibiOS.
Binding is also possible, but for boards using ChibiOS and SD logger, it
is recommended to disable it for the binding process as the LSE clock
required by the RTC is longer to start (bind pulses should be issued
before 200ms after power up).
The configuration is backward compatible and some defines in the boards'
header could be even removed. Only Apogee, Chimera and Elle0 have been
updated as it is the only board tested for now. There is no harm to keep
the old defines.
  • Loading branch information
gautierhattenberger committed Apr 28, 2018
1 parent 578ffe2 commit 424cd78
Show file tree
Hide file tree
Showing 20 changed files with 556 additions and 1,301 deletions.
2 changes: 1 addition & 1 deletion conf/airframes/ENAC/fixed-wing/chimera.xml
Expand Up @@ -18,7 +18,7 @@ UBX GPS / HMC58XX mag (drotek)
<configure name="PERIODIC_FREQUENCY" value="100"/>
</target>
<target name="sim" board="pc">
<module name="radio_control" type="ppm"/>
<module name="radio_control" type="spektrum"/>
</target>

<!-- Communication -->
Expand Down
76 changes: 48 additions & 28 deletions conf/modules/radio_control_spektrum.xml
Expand Up @@ -8,50 +8,70 @@
Define USE_DSMX on STM32 microcontrollers to bind in DSMX instead of DSM2
</description>
<configure name="RADIO_CONTROL_LED" value="none|num" description="LED number or 'none' to disable"/>
<configure name="RADIO_CONTROL_SPEKTRUM_PRIMARY_PORT" value="num" description="UART number for primary spektrum receiver"/>
<configure name="RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT" value="num" description="UART number for optional secondary spektrum receiver"/>
<configure name="RADIO_CONTROL_SPEKTRUM_PRIMARY_PORT" value="uartX" description="UART for primary spektrum receiver"/>
<configure name="RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT" value="uartX" description="UART for optional secondary spektrum receiver"/>
<configure name="USE_SECONDARY_SPEKTRUM_RECEIVER" value="0|1" description="enable secondary receiver (default: disabled)"/>
<define name="USE_DSMX" value="FALSE|TRUE" description="Set to true to use DSMX instead of DSM2"/>
</doc>
<header>
<file name="spektrum.h" dir="subsystems/radio_control"/>
</header>
<makefile target="ap|fbw|sim|nps|hitl|test_radio_control">
<makefile target="ap|fbw|sim|nps|test_radio_control">
<configure name="RADIO_CONTROL_LED" default="none"/>
<define name="RADIO_CONTROL_LED" value="$(RADIO_CONTROL_LED)" cond="ifneq ($(RADIO_CONTROL_LED),none)"/>
<define name="RADIO_CONTROL_TYPE_H" value="subsystems/radio_control/spektrum.h" type="string"/>
<raw>
ifeq ($(ARCH), lpc21)
ifndef RADIO_CONTROL_SPEKTRUM_MODEL
RADIO_CONTROL_SPEKTRUM_MODEL=\"subsystems/radio_control/spektrum_dx7se.h\"
endif
RC_CFLAGS += -DRADIO_CONTROL_SPEKTRUM_MODEL_H=$(RADIO_CONTROL_SPEKTRUM_MODEL)
endif
</raw>
</makefile>
<makefile target="fbw|sim|nps|hitl|test_radio_control">
<configure name="USE_SECONDARY_SPEKTRUM_RECEIVER" default="0"/>
<makefile target="fbw|test_radio_control">
<define name="RADIO_CONTROL"/>
<define name="RADIO_CONTROL_BIND_IMPL_FUNC" value="radio_control_spektrum_try_bind"/>
<define name="RADIO_CONTROL_SPEKTRUM_PRIMARY_PORT" value="SPEKTRUM_$(RADIO_CONTROL_SPEKTRUM_PRIMARY_PORT)"/>
<define name="OVERRIDE_$(RADIO_CONTROL_SPEKTRUM_PRIMARY_PORT)_IRQ_HANDLER"/>
<define name="RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT" value="SPEKTRUM_$(RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT)" cond="ifneq ($(USE_SECONDARY_SPEKTRUM_RECEIVER),0)"/>
<define name="OVERRIDE_$(RADIO_CONTROL_SPEKTRUM_PRIMARY_PORT)_IRQ_HANDLER" cond="ifneq ($(USE_SECONDARY_SPEKTRUM_RECEIVER),0)"/>
<define name="RADIO_CONTROL_TYPE_H" value="subsystems/radio_control/spektrum.h" type="string"/>
<define name="RADIO_CONTROL_BIND_IMPL_FUNC" value="spektrum_try_bind"/>

<configure name="SPEKTRUM_PRIMARY_UART" value="$(RADIO_CONTROL_SPEKTRUM_PRIMARY_PORT)" case="upper|lower"/>
<define name="USE_$(SPEKTRUM_PRIMARY_UART_UPPER)"/>
<define name="SPEKTRUM_PRIMARY_UART" value="$(SPEKTRUM_PRIMARY_UART_LOWER)"/>
<define name="SPEKTRUM_PRIMARY_UART_UPPER" value="$(SPEKTRUM_PRIMARY_UART_UPPER)_GPIO"/>
<define name="$(SPEKTRUM_PRIMARY_UART_UPPER)_BAUD" value="B115200"/>
<define name="USE_$(SPEKTRUM_PRIMARY_UART_UPPER)_TX" value="FALSE"/>

<configure name="SPEKTRUM_SECONDARY_UART" value="$(RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT)" case="upper|lower"/>
<configure name="USE_SECONDARY_SPEKTRUM_RECEIVER" default="0"/>
<define name="USE_$(SPEKTRUM_SECONDARY_UART_UPPER)" cond="ifneq ($(USE_SECONDARY_SPEKTRUM_RECEIVER),0)"/>
<define name="SPEKTRUM_SECONDARY_UART" value="$(SPEKTRUM_SECONDARY_UART_LOWER)" cond="ifneq ($(USE_SECONDARY_SPEKTRUM_RECEIVER),0)"/>
<define name="SPEKTRUM_SECONDARY_UART_UPPER" value="$(SPEKTRUM_SECONDARY_UART_UPPER)_GPIO" cond="ifneq ($(USE_SECONDARY_SPEKTRUM_RECEIVER),0)"/>
<define name="$(SPEKTRUM_SECONDARY_UART_UPPER)_BAUD" value="B115200" cond="ifneq ($(USE_SECONDARY_SPEKTRUM_RECEIVER),0)"/>
<define name="USE_$(SPEKTRUM_SECONDARY_UART_UPPER)_TX" value="FALSE" cond="ifneq ($(USE_SECONDARY_SPEKTRUM_RECEIVER),0)"/>

<file name="radio_control.c" dir="subsystems"/>
<file name="spektrum.c" dir="subsystems/radio_control"/>
<file name="spektrum_arch.c" dir="$(SRC_ARCH)/subsystems/radio_control"/>
</makefile>
<makefile target="ap">
<define name="RADIO_CONTROL_TYPE_H" value="subsystems/radio_control/spektrum.h" type="string"/>
</makefile>
<makefile target="ap" cond="ifeq (,$(findstring $(SEPARATE_FBW),1 TRUE))">
<configure name="USE_SECONDARY_SPEKTRUM_RECEIVER" default="0"/>
<define name="RADIO_CONTROL"/>
<define name="RADIO_CONTROL_BIND_IMPL_FUNC" value="radio_control_spektrum_try_bind"/>
<define name="RADIO_CONTROL_SPEKTRUM_PRIMARY_PORT" value="SPEKTRUM_$(RADIO_CONTROL_SPEKTRUM_PRIMARY_PORT)"/>
<define name="OVERRIDE_$(RADIO_CONTROL_SPEKTRUM_PRIMARY_PORT)_IRQ_HANDLER"/>
<define name="RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT" value="SPEKTRUM_$(RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT)" cond="ifneq ($(USE_SECONDARY_SPEKTRUM_RECEIVER),0)"/>
<define name="OVERRIDE_$(RADIO_CONTROL_SPEKTRUM_PRIMARY_PORT)_IRQ_HANDLER" cond="ifneq ($(USE_SECONDARY_SPEKTRUM_RECEIVER),0)"/>
<define name="RADIO_CONTROL_BIND_IMPL_FUNC" value="spektrum_try_bind"/>

<configure name="SPEKTRUM_PRIMARY_UART" value="$(RADIO_CONTROL_SPEKTRUM_PRIMARY_PORT)" case="upper|lower"/>
<define name="USE_$(SPEKTRUM_PRIMARY_UART_UPPER)"/>
<define name="SPEKTRUM_PRIMARY_UART" value="$(SPEKTRUM_PRIMARY_UART_LOWER)"/>
<define name="SPEKTRUM_PRIMARY_UART_UPPER" value="$(SPEKTRUM_PRIMARY_UART_UPPER)_GPIO"/>
<define name="$(SPEKTRUM_PRIMARY_UART_UPPER)_BAUD" value="B115200"/>
<define name="USE_$(SPEKTRUM_PRIMARY_UART_UPPER)_TX" value="FALSE"/>

<configure name="SPEKTRUM_SECONDARY_UART" value="$(RADIO_CONTROL_SPEKTRUM_SECONDARY_PORT)" case="upper|lower"/>
<configure name="USE_SECONDARY_SPEKTRUM_RECEIVER" default="0"/>
<define name="USE_$(SPEKTRUM_SECONDARY_UART_UPPER)" cond="ifneq ($(USE_SECONDARY_SPEKTRUM_RECEIVER),0)"/>
<define name="SPEKTRUM_SECONDARY_UART" value="$(SPEKTRUM_SECONDARY_UART_LOWER)" cond="ifneq ($(USE_SECONDARY_SPEKTRUM_RECEIVER),0)"/>
<define name="SPEKTRUM_SECONDARY_UART_UPPER" value="$(SPEKTRUM_SECONDARY_UART_UPPER)_GPIO" cond="ifneq ($(USE_SECONDARY_SPEKTRUM_RECEIVER),0)"/>
<define name="$(SPEKTRUM_SECONDARY_UART_UPPER)_BAUD" value="B115200" cond="ifneq ($(USE_SECONDARY_SPEKTRUM_RECEIVER),0)"/>
<define name="USE_$(SPEKTRUM_SECONDARY_UART_UPPER)_TX" value="FALSE" cond="ifneq ($(USE_SECONDARY_SPEKTRUM_RECEIVER),0)"/>

<file name="radio_control.c" dir="subsystems"/>
<file name="spektrum.c" dir="subsystems/radio_control"/>
<file name="spektrum_arch.c" dir="$(SRC_ARCH)/subsystems/radio_control"/>
</makefile>
<makefile target="sim|nps|hitl">
<define name="RADIO_CONTROL"/>
<define name="RADIO_CONTROL_TYPE_H" value="subsystems/radio_control/spektrum_arch.h" type="string"/>
<file name="radio_control.c" dir="subsystems"/>
<file_arch name="spektrum_arch.c" dir="subsystems/radio_control"/>
</makefile>
</module>

11 changes: 9 additions & 2 deletions sw/airborne/arch/chibios/mcu_periph/sys_time_arch.c
Expand Up @@ -94,8 +94,15 @@ uint32_t get_sys_time_msec(void)
*/
void sys_time_usleep(uint32_t us)
{
uint64_t wait_st = ((uint64_t)us * CH_CFG_ST_FREQUENCY) / 1000000UL;
chThdSleep((systime_t)wait_st);
if (us < 1000) {
// for small time, use the polled version instead of thread sleep
chSysDisable();
chSysPolledDelayX(US2RTC(STM32_HCLK, us));
chSysEnable();
} else {
uint64_t wait_st = ((uint64_t)us * CH_CFG_ST_FREQUENCY) / 1000000UL;
chThdSleep((systime_t)wait_st);
}
}

void sys_time_msleep(uint16_t ms)
Expand Down
34 changes: 0 additions & 34 deletions sw/airborne/arch/lpc21/subsystems/radio_control/spektrum_arch.c

This file was deleted.

100 changes: 0 additions & 100 deletions sw/airborne/arch/lpc21/subsystems/radio_control/spektrum_arch.h

This file was deleted.

8 changes: 4 additions & 4 deletions sw/airborne/arch/sim/subsystems/radio_control/spektrum_arch.c
Expand Up @@ -27,7 +27,7 @@
*/

#include "subsystems/radio_control.h"
#include "subsystems/radio_control/spektrum.h"
#include "subsystems/radio_control/spektrum_arch.h"
#include "std.h"
#include <inttypes.h>

Expand All @@ -39,13 +39,11 @@

static bool spektrum_available;

void radio_control_spektrum_try_bind(void) {}

void radio_control_impl_init(void)
{
spektrum_available = false;
}
void RadioControlEventImp(void (*frame_handler)(void))
void spektrum_event(void (*frame_handler)(void))
{
if (spektrum_available) {
radio_control.frame_cpt++;
Expand All @@ -56,6 +54,8 @@ void RadioControlEventImp(void (*frame_handler)(void))
spektrum_available = false;
}

void spektrum_try_bind(void) {}

#if USE_NPS
#ifdef RADIO_CONTROL
void radio_control_feed(void)
Expand Down
40 changes: 4 additions & 36 deletions sw/airborne/arch/sim/subsystems/radio_control/spektrum_arch.h
Expand Up @@ -23,43 +23,11 @@
#ifndef RADIO_CONTROL_SPEKTRUM_ARCH_H
#define RADIO_CONTROL_SPEKTRUM_ARCH_H

#include "subsystems/radio_control/spektrum_radio.h"

/*
* All Spektrum and JR 2.4 GHz transmitters
* have the same channel assignments.
*/


#ifndef RADIO_CONTROL_NB_CHANNEL
#define RADIO_CONTROL_NB_CHANNEL 12
#endif

#if RADIO_CONTROL_NB_CHANNEL > 12
#error "RADIO_CONTROL_NB_CHANNEL mustn't be higher than 12."
#endif

/* channel assignments */
#define RADIO_THROTTLE 0
#define RADIO_ROLL 1
#define RADIO_PITCH 2
#define RADIO_YAW 3
#define RADIO_GEAR 4
#define RADIO_FLAP 5
#define RADIO_AUX1 5
#define RADIO_AUX2 6
#define RADIO_AUX3 7
#define RADIO_AUX4 8
#define RADIO_AUX5 9
#define RADIO_AUX6 10
#define RADIO_AUX7 11

/* really for a 9 channel transmitter
we would swap the order of these */
#ifndef RADIO_MODE
#define RADIO_MODE RADIO_GEAR
#endif

extern void RadioControlEventImp(void (*_received_frame_handler)(void));
extern void spektrum_event(void (*_received_frame_handler)(void));
#define RadioControlEventImp spektrum_event
extern void spektrum_try_bind(void);

#if USE_NPS
extern void radio_control_feed(void);
Expand Down

0 comments on commit 424cd78

Please sign in to comment.