diff --git a/conf/airframes/ENAC/fixed-wing/apogee.xml b/conf/airframes/ENAC/fixed-wing/apogee.xml index 5a928beb9e..5c62d51f53 100644 --- a/conf/airframes/ENAC/fixed-wing/apogee.xml +++ b/conf/airframes/ENAC/fixed-wing/apogee.xml @@ -23,10 +23,10 @@ - - + + - + diff --git a/conf/firmwares/subsystems/shared/radio_control_sbus.makefile b/conf/firmwares/subsystems/shared/radio_control_sbus.makefile new file mode 100644 index 0000000000..2e8e9ecbe1 --- /dev/null +++ b/conf/firmwares/subsystems/shared/radio_control_sbus.makefile @@ -0,0 +1,20 @@ +# +# 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 + +# convert SBUS_PORT to upper and lower case strings: +SBUS_PORT_UPPER=$(shell echo $(SBUS_PORT) | tr a-z A-Z) +SBUS_PORT_LOWER=$(shell echo $(SBUS_PORT) | tr A-Z a-z) + +$(TARGET).CFLAGS += -DUSE_$(SBUS_PORT_UPPER) -D$(SBUS_PORT_UPPER)_BAUD=B100000 +$(TARGET).CFLAGS += -DSBUS_UART_DEV=$(SBUS_PORT_LOWER) +$(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 + diff --git a/conf/radios/T10CG_SBUS.xml b/conf/radios/T10CG_SBUS.xml new file mode 100644 index 0000000000..137caa7b20 --- /dev/null +++ b/conf/radios/T10CG_SBUS.xml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + + diff --git a/sw/airborne/arch/lpc21/mcu_periph/uart_arch.c b/sw/airborne/arch/lpc21/mcu_periph/uart_arch.c index 125dac1c6f..bab01322a7 100644 --- a/sw/airborne/arch/lpc21/mcu_periph/uart_arch.c +++ b/sw/airborne/arch/lpc21/mcu_periph/uart_arch.c @@ -68,6 +68,10 @@ void uart_periph_set_baudrate(struct uart_periph* p, uint32_t baud) { uart_enable_interrupts(p); } +void uart_periph_set_bits_stop_parity(struct uart_periph* p, uint8_t __attribute__((unused)) bits, uint8_t __attribute__((unused)) stop, uint8_t __attribute__((unused)) parity) { + // TBD +} + void uart_transmit(struct uart_periph* p, uint8_t data ) { uint16_t temp; unsigned cpsr; diff --git a/sw/airborne/arch/stm32/mcu_periph/uart_arch.c b/sw/airborne/arch/stm32/mcu_periph/uart_arch.c index fb73f70983..335a90ed50 100644 --- a/sw/airborne/arch/stm32/mcu_periph/uart_arch.c +++ b/sw/airborne/arch/stm32/mcu_periph/uart_arch.c @@ -41,11 +41,8 @@ void uart_periph_set_baudrate(struct uart_periph* p, uint32_t baud) { - /* Configure USART */ + /* Configure USART baudrate */ usart_set_baudrate((uint32_t)p->reg_addr, baud); - usart_set_databits((uint32_t)p->reg_addr, 8); - usart_set_stopbits((uint32_t)p->reg_addr, USART_STOPBITS_1); - usart_set_parity((uint32_t)p->reg_addr, USART_PARITY_NONE); /* Disable Idle Line interrupt */ USART_CR1((uint32_t)p->reg_addr) &= ~USART_CR1_IDLEIE; @@ -61,6 +58,33 @@ void uart_periph_set_baudrate(struct uart_periph* p, uint32_t baud) { } +void uart_periph_set_bits_stop_parity(struct uart_periph* p, uint8_t bits, uint8_t stop, uint8_t parity) { + /* Configure USART parity and data bits */ + if (parity == UPARITY_EVEN) { + usart_set_parity((uint32_t)p->reg_addr, USART_PARITY_EVEN); + if (bits == UBITS_7) + usart_set_databits((uint32_t)p->reg_addr, 8); + else // 8 data bits by default + usart_set_databits((uint32_t)p->reg_addr, 9); + } + else if (parity == USART_PARITY_ODD) { + usart_set_parity((uint32_t)p->reg_addr, USART_PARITY_ODD); + if (bits == UBITS_7) + usart_set_databits((uint32_t)p->reg_addr, 8); + else // 8 data bits by default + usart_set_databits((uint32_t)p->reg_addr, 9); + } + else { // 8 data bist, NO_PARITY by default + usart_set_parity((uint32_t)p->reg_addr, USART_PARITY_NONE); + usart_set_databits((uint32_t)p->reg_addr, 8); // is 7bits without parity possible ? + } + /* Configure USART stop bits */ + if (stop == USTOP_2) + usart_set_stopbits((uint32_t)p->reg_addr, USART_STOPBITS_2); + else // 1 stop bit by default + usart_set_stopbits((uint32_t)p->reg_addr, USART_STOPBITS_1); +} + void uart_periph_set_mode(struct uart_periph* p, bool_t tx_enabled, bool_t rx_enabled, bool_t hw_flow_control) { uint32_t mode = 0; if (tx_enabled) @@ -173,6 +197,18 @@ static inline void usart_enable_irq(uint8_t IRQn) { #define UART1_HW_FLOW_CONTROL FALSE #endif +#ifndef UART1_BITS +#define UART1_BITS UBITS_8 +#endif + +#ifndef UART1_STOP +#define UART1_STOP USTOP_1 +#endif + +#ifndef UART1_PARITY +#define UART1_PARITY UPARITY_NO +#endif + void uart1_init( void ) { uart_periph_init(&uart1); @@ -201,7 +237,8 @@ void uart1_init( void ) { /* Configure USART1, enable hardware flow control*/ uart_periph_set_mode(&uart1, USE_UART1_TX, USE_UART1_RX, UART1_HW_FLOW_CONTROL); - /* Set USART1 baudrate and enable interrupt */ + /* Set USART1 parameters and enable interrupt */ + uart_periph_set_bits_stop_parity(&uart1, UART1_BITS, UART1_STOP, UART1_PARITY); uart_periph_set_baudrate(&uart1, UART1_BAUD); } @@ -224,6 +261,18 @@ void usart1_isr(void) { usart_isr(&uart1); } #define UART2_HW_FLOW_CONTROL FALSE #endif +#ifndef UART2_BITS +#define UART2_BITS UBITS_8 +#endif + +#ifndef UART2_STOP +#define UART2_STOP USTOP_1 +#endif + +#ifndef UART2_PARITY +#define UART2_PARITY UPARITY_NO +#endif + void uart2_init( void ) { uart_periph_init(&uart2); @@ -253,6 +302,7 @@ void uart2_init( void ) { uart_periph_set_mode(&uart2, USE_UART2_TX, USE_UART2_RX, UART2_HW_FLOW_CONTROL); /* Configure USART */ + uart_periph_set_bits_stop_parity(&uart2, UART2_BITS, UART2_STOP, UART2_PARITY); uart_periph_set_baudrate(&uart2, UART2_BAUD); } @@ -275,6 +325,18 @@ void usart2_isr(void) { usart_isr(&uart2); } #define UART3_HW_FLOW_CONTROL FALSE #endif +#ifndef UART3_BITS +#define UART3_BITS UBITS_8 +#endif + +#ifndef UART3_STOP +#define UART3_STOP USTOP_1 +#endif + +#ifndef UART3_PARITY +#define UART3_PARITY UPARITY_NO +#endif + void uart3_init( void ) { uart_periph_init(&uart3); @@ -304,6 +366,7 @@ void uart3_init( void ) { uart_periph_set_mode(&uart3, USE_UART3_TX, USE_UART3_RX, UART3_HW_FLOW_CONTROL); /* Configure USART */ + uart_periph_set_bits_stop_parity(&uart3, UART3_BITS, UART3_STOP, UART3_PARITY); uart_periph_set_baudrate(&uart3, UART3_BAUD); } @@ -322,6 +385,18 @@ void usart3_isr(void) { usart_isr(&uart3); } #define USE_UART4_RX TRUE #endif +#ifndef UART4_BITS +#define UART4_BITS UBITS_8 +#endif + +#ifndef UART4_STOP +#define UART4_STOP USTOP_1 +#endif + +#ifndef UART4_PARITY +#define UART4_PARITY UPARITY_NO +#endif + void uart4_init( void ) { uart_periph_init(&uart4); @@ -342,6 +417,7 @@ void uart4_init( void ) { /* Configure USART */ uart_periph_set_mode(&uart4, USE_UART4_TX, USE_UART4_RX, FALSE); + uart_periph_set_bits_stop_parity(&uart4, UART4_BITS, UART4_STOP, UART4_PARITY); uart_periph_set_baudrate(&uart4, UART4_BAUD); } @@ -360,6 +436,18 @@ void uart4_isr(void) { usart_isr(&uart4); } #define USE_UART5_RX TRUE #endif +#ifndef UART5_BITS +#define UART5_BITS UBITS_8 +#endif + +#ifndef UART5_STOP +#define UART5_STOP USTOP_1 +#endif + +#ifndef UART5_PARITY +#define UART5_PARITY UPARITY_NO +#endif + void uart5_init( void ) { uart_periph_init(&uart5); @@ -380,6 +468,7 @@ void uart5_init( void ) { /* Configure USART */ uart_periph_set_mode(&uart5, USE_UART5_TX, USE_UART5_RX, FALSE); + uart_periph_set_bits_stop_parity(&uart5, UART5_BITS, UART5_STOP, UART5_PARITY); uart_periph_set_baudrate(&uart5, UART5_BAUD); } @@ -402,6 +491,18 @@ void uart5_isr(void) { usart_isr(&uart5); } #define UART6_HW_FLOW_CONTROL FALSE #endif +#ifndef UART6_BITS +#define UART6_BITS UBITS_8 +#endif + +#ifndef UART6_STOP +#define UART6_STOP USTOP_1 +#endif + +#ifndef UART6_PARITY +#define UART6_PARITY UPARITY_NO +#endif + void uart6_init( void ) { uart_periph_init(&uart6); @@ -430,7 +531,7 @@ void uart6_init( void ) { /* Configure USART Tx,Rx and hardware flow control*/ uart_periph_set_mode(&uart6, USE_UART6_TX, USE_UART6_RX, UART6_HW_FLOW_CONTROL); - + uart_periph_set_bits_stop_parity(&uart6, UART6_BITS, UART6_STOP, UART6_PARITY); uart_periph_set_baudrate(&uart6, UART6_BAUD); } diff --git a/sw/airborne/boards/apogee_1.0.h b/sw/airborne/boards/apogee_1.0.h index 705dca0eda..78ff267397 100644 --- a/sw/airborne/boards/apogee_1.0.h +++ b/sw/airborne/boards/apogee_1.0.h @@ -112,18 +112,11 @@ #define POWER_SWITCH_LED 9 -/* Uart2 RX polarity, on PB13, 1 on LED_ON, 0 on LED_OFF */ -#ifndef USE_LED_10 -#define USE_LED_10 1 -#endif -#define LED_10_GPIO GPIOB -#define LED_10_GPIO_CLK RCC_AHB1ENR_IOPBEN -#define LED_10_GPIO_PIN GPIO13 -#define LED_10_GPIO_ON gpio_set -#define LED_10_GPIO_OFF gpio_clear -#define LED_10_AFIO_REMAP ((void)0) -#define RC_POLARITY_LED 10 +/* Pint to set Uart2 RX polarity, on PB13, output high inverts, low doesn't */ +#define RC_POLARITY_GPIO_PORT GPIOB +#define RC_POLARITY_GPIO_PIN GPIO13 + /* Default actuators driver */ #define DEFAULT_ACTUATORS "subsystems/actuators/actuators_pwm.h" diff --git a/sw/airborne/firmwares/fixedwing/fbw_downlink.h b/sw/airborne/firmwares/fixedwing/fbw_downlink.h index 061a7334ed..7fde1f3140 100644 --- a/sw/airborne/firmwares/fixedwing/fbw_downlink.h +++ b/sw/airborne/firmwares/fixedwing/fbw_downlink.h @@ -66,6 +66,14 @@ PPM_NB_CHANNEL, \ ppm_pulses_usec); \ } +#elif defined RADIO_CONTROL_TYPE_SBUS +#include "subsystems/radio_control/sbus.h" +#define PERIODIC_SEND_PPM(_trans, _dev) { \ + DOWNLINK_SEND_PPM(_trans, _dev, \ + &radio_control.frame_rate, \ + SBUS_NB_CHANNEL, \ + sbus.pulses); \ +} #else #define PERIODIC_SEND_PPM(_trans, _dev) {} #endif diff --git a/sw/airborne/led.h b/sw/airborne/led.h index 46e8dc662f..f9aa9e2c55 100644 --- a/sw/airborne/led.h +++ b/sw/airborne/led.h @@ -74,6 +74,16 @@ static inline void led_init ( void ) { LED_OFF(8); #endif /* LED_8 */ +#if USE_LED_9 + LED_INIT(9); + LED_OFF(9); +#endif /* LED_9 */ + +#if USE_LED_10 + LED_INIT(10); + LED_OFF(10); +#endif /* LED_10 */ + #ifdef USE_LED_BODY LED_INIT(BODY); LED_OFF(BODY); diff --git a/sw/airborne/mcu_periph/uart.h b/sw/airborne/mcu_periph/uart.h index 1776ae2ca5..84fc1626a6 100644 --- a/sw/airborne/mcu_periph/uart.h +++ b/sw/airborne/mcu_periph/uart.h @@ -46,10 +46,21 @@ #define B19200 19200 #define B38400 38400 #define B57600 57600 +#define B100000 100000 #define B115200 115200 #define B230400 230400 #define B921600 921600 +#define UBITS_7 7 +#define UBITS_8 8 + +#define USTOP_1 1 +#define USTOP_2 2 + +#define UPARITY_NO 0 +#define UPARITY_ODD 1 +#define UPARITY_EVEN 2 + /** * UART peripheral */ @@ -75,6 +86,7 @@ struct uart_periph { extern void uart_periph_init(struct uart_periph* p); extern void uart_periph_set_baudrate(struct uart_periph* p, uint32_t baud); +extern void uart_periph_set_bits_stop_parity(struct uart_periph* p, uint8_t bits, uint8_t stop, uint8_t parity); extern void uart_periph_set_mode(struct uart_periph* p, bool_t tx_enabled, bool_t rx_enabled, bool_t hw_flow_control); extern void uart_transmit(struct uart_periph* p, uint8_t data); extern bool_t uart_check_free_space(struct uart_periph* p, uint8_t len); @@ -97,6 +109,7 @@ extern void uart0_init(void); #define UART0Getch() uart_getch(&uart0) #define UART0TxRunning uart0.tx_running #define UART0SetBaudrate(_b) uart_periph_set_baudrate(&uart0, _b) +#define UART0SetBitsStopParity(_b, _s, _p) uart_periph_set_bits_stop_parity(&uart0, _b, _s, _p) #endif // USE_UART0 @@ -112,6 +125,7 @@ extern void uart1_init(void); #define UART1Getch() uart_getch(&uart1) #define UART1TxRunning uart1.tx_running #define UART1SetBaudrate(_b) uart_periph_set_baudrate(&uart1, _b) +#define UART1SetBitsStopParity(_b, _s, _p) uart_periph_set_bits_stop_parity(&uart1, _b, _s, _p) #endif // USE_UART1 @@ -127,6 +141,7 @@ extern void uart2_init(void); #define UART2Getch() uart_getch(&uart2) #define UART2TxRunning uart2.tx_running #define UART2SetBaudrate(_b) uart_periph_set_baudrate(&uart2, _b) +#define UART2SetBitsStopParity(_b, _s, _p) uart_periph_set_bits_stop_parity(&uart2, _b, _s, _p) #endif // USE_UART2 @@ -142,6 +157,7 @@ extern void uart3_init(void); #define UART3Getch() uart_getch(&uart3) #define UART3TxRunning uart3.tx_running #define UART3SetBaudrate(_b) uart_periph_set_baudrate(&uart3, _b) +#define UART3SetBitsStopParity(_b, _s, _p) uart_periph_set_bits_stop_parity(&uart3, _b, _s, _p) #endif // USE_UART3 @@ -157,6 +173,7 @@ extern void uart4_init(void); #define UART4Getch() uart_getch(&uart4) #define UART4TxRunning uart4.tx_running #define UART4SetBaudrate(_b) uart_periph_set_baudrate(&uart4, _b) +#define UART4SetBitsStopParity(_b, _s, _p) uart_periph_set_bits_stop_parity(&uart4, _b, _s, _p) #endif // USE_UART4 @@ -172,6 +189,7 @@ extern void uart5_init(void); #define UART5Getch() uart_getch(&uart5) #define UART5TxRunning uart5.tx_running #define UART5SetBaudrate(_b) uart_periph_set_baudrate(&uart5, _b) +#define UART5SetBitsStopParity(_b, _s, _p) uart_periph_set_bits_stop_parity(&uart5, _b, _s, _p) #endif // USE_UART5 @@ -187,6 +205,7 @@ extern void uart6_init(void); #define UART6Getch() uart_getch(&uart6) #define UART6TxRunning uart6.tx_running #define UART6SetBaudrate(_b) uart_periph_set_baudrate(&uart6, _b) +#define UART6SetBitsStopParity(_b, _s, _p) uart_periph_set_bits_stop_parity(&uart6, _b, _s, _p) #endif // USE_UART6 diff --git a/sw/airborne/subsystems/radio_control/sbus.c b/sw/airborne/subsystems/radio_control/sbus.c new file mode 100644 index 0000000000..726e0781c5 --- /dev/null +++ b/sw/airborne/subsystems/radio_control/sbus.c @@ -0,0 +1,145 @@ +/* + * 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 "mcu_periph/gpio.h" +#include + +/* + * 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_FLAGS_BYTE 22 +#define SBUS_FRAME_LOST_BIT 2 + +#define SBUS_STATUS_UNINIT 0 +#define SBUS_STATUS_GOT_START 1 + +/** Set polarity using RC_POLARITY_GPIO. + * SBUS signal has a reversed polarity compared to normal UART + * this allows to using hardware UART peripheral by changing + * the input signal polarity. + * Setting this gpio ouput high inverts the signal, + * output low sets it to normal polarity. + */ +#ifndef RC_SET_POLARITY +#define RC_SET_POLARITY gpio_output_high +#endif + + +/** 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) + uart_periph_set_bits_stop_parity(&SBUS_UART_DEV, UBITS_8, USTOP_2, UPARITY_EVEN); + uart_periph_set_baudrate(&SBUS_UART_DEV, B100000); + + // Set polarity +#ifdef RC_POLARITY_GPIO_PORT + gpio_setup_output(RC_POLARITY_GPIO_PORT, RC_POLARITY_GPIO_PIN); + RC_SET_POLARITY(RC_POLARITY_GPIO_PORT, RC_POLARITY_GPIO_PIN); +#endif +} + + +/** Decode the raw buffer */ +static void decode_sbus_buffer (const uint8_t *src, uint16_t *dst, bool_t *available) +{ + // 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< 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 */