From 07dd0df10212ab484a859ad0f08c64ee29cafd68 Mon Sep 17 00:00:00 2001 From: Jan NIJS Date: Fri, 6 Jan 2017 21:02:42 +0100 Subject: [PATCH 1/2] LP-477 Add "iBus RX on receiver port" support for revo(clone) boards. --- .../targets/boards/revolution/board_hw_defs.c | 31 +++++++++++++++++++ .../boards/revolution/firmware/pios_board.c | 5 +++ shared/uavobjectdefinition/hwsettings.xml | 4 +-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/flight/targets/boards/revolution/board_hw_defs.c b/flight/targets/boards/revolution/board_hw_defs.c index 74770d980..9e33beeb2 100644 --- a/flight/targets/boards/revolution/board_hw_defs.c +++ b/flight/targets/boards/revolution/board_hw_defs.c @@ -1218,6 +1218,37 @@ static const struct pios_usart_cfg pios_usart_ibus_flexi_cfg = { }, }; +static const struct pios_usart_cfg pios_usart_ibus_rcvr_cfg = { + .regs = USART6, + .remap = GPIO_AF_USART6, + .init = { + .USART_BaudRate = 115200, + .USART_WordLength = USART_WordLength_8b, + .USART_Parity = USART_Parity_No, + .USART_StopBits = USART_StopBits_1, + .USART_HardwareFlowControl = USART_HardwareFlowControl_None, + .USART_Mode = USART_Mode_Rx, + }, + .irq = { + .init = { + .NVIC_IRQChannel = USART6_IRQn, + .NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_HIGH, + .NVIC_IRQChannelSubPriority = 0, + .NVIC_IRQChannelCmd = ENABLE, + }, + }, + .rx = { + .gpio = GPIOC, + .init = { + .GPIO_Pin = GPIO_Pin_7, + .GPIO_Speed = GPIO_Speed_2MHz, + .GPIO_Mode = GPIO_Mode_AF, + .GPIO_OType = GPIO_OType_PP, + .GPIO_PuPd = GPIO_PuPd_UP + }, + }, +}; + #endif /* PIOS_INCLUDE_IBUS */ #if defined(PIOS_INCLUDE_EXBUS) diff --git a/flight/targets/boards/revolution/firmware/pios_board.c b/flight/targets/boards/revolution/firmware/pios_board.c index d324ef619..a7fc96298 100644 --- a/flight/targets/boards/revolution/firmware/pios_board.c +++ b/flight/targets/boards/revolution/firmware/pios_board.c @@ -1109,6 +1109,11 @@ void PIOS_Board_Init(void) case HWSETTINGS_RM_RCVRPORT_PPMGPS: PIOS_Board_configure_com(&pios_usart_rcvrport_cfg, PIOS_COM_GPS_RX_BUF_LEN, PIOS_COM_GPS_TX_BUF_LEN, &pios_usart_com_driver, &pios_com_gps_id); break; + case HWSETTINGS_RM_RCVRPORT_IBUS: +#if defined(PIOS_INCLUDE_IBUS) + PIOS_Board_configure_ibus(&pios_usart_ibus_rcvr_cfg); +#endif /* PIOS_INCLUDE_IBUS */ + break; } #if defined(PIOS_INCLUDE_GCSRCVR) diff --git a/shared/uavobjectdefinition/hwsettings.xml b/shared/uavobjectdefinition/hwsettings.xml index ae30f9ec5..f0dfe1950 100644 --- a/shared/uavobjectdefinition/hwsettings.xml +++ b/shared/uavobjectdefinition/hwsettings.xml @@ -11,9 +11,9 @@ - + limits="%0905NE:PPM+PWM:PPM+Telemetry:PPM+DebugConsole:PPM+ComBridge:PPM+MSP:PPM+MAVLink:PPM+GPS:Telemetry:DebugConsole:ComBridge:MSP:MAVLink:GPS:IBus;"/> From 9ed4f644aa5733bf20bab9af904a1bd95ad179e8 Mon Sep 17 00:00:00 2001 From: Jan NIJS Date: Sat, 7 Jan 2017 00:38:21 +0100 Subject: [PATCH 2/2] LP-477 Describe iBus packet format and add packet type validation check Also add a compile time protection to make sure we don't compile with bad buffer lengths. --- flight/pios/common/pios_ibus.c | 45 +++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/flight/pios/common/pios_ibus.c b/flight/pios/common/pios_ibus.c index 4ba74fcf2..e37b43014 100644 --- a/flight/pios/common/pios_ibus.c +++ b/flight/pios/common/pios_ibus.c @@ -1,7 +1,7 @@ /** ****************************************************************************** * @file pios_ibus.c - * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016. + * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016-2017. * dRonin, http://dRonin.org/, Copyright (C) 2016 * @addtogroup PIOS PIOS Core hardware abstraction layer * @{ @@ -33,11 +33,39 @@ #ifdef PIOS_INCLUDE_IBUS -// 1 sync byte, 1 unknown byte, 10x channels (uint16_t), 8 unknown bytes, 2 crc bytes -#define PIOS_IBUS_BUFLEN (1 + 1 + PIOS_IBUS_NUM_INPUTS * 2 + 8 + 2) -#define PIOS_IBUS_SYNCBYTE 0x20 +/* + iBus packet format learned from: https://bitbucket.org/daveborthwick/ibus_pic12f1572 + + An iBus packet consists of: + - the total packet length (1 byte) + - command / type field (1 byte) + - sequence of 16-bit (little endian) variables (2 bytes each) + - checksum (2 bytes) + + The most common packet length is 0x20 = 32 bytes + A packet of this type has enough room to transport data for 14 channels. + + The command / type field tells you the meaning of the next variables. + This field is set to 0x40 for RC command channels. + We must verify this field to be 0x40 so we can be sure that the next fields + can be interpreted as RC command channels. + + The checksum equals 0xFFFF minus the sum of all previous bytes in the packet. + + */ + +// 1 length byte, 1 type byte, 10x channels (uint16_t), 4 extra channels (unint16_t), 2 crc bytes +// We only decode the first 10 channels at this time. +#define PIOS_IBUS_BUFLEN (1 + 1 + PIOS_IBUS_NUM_INPUTS * 2 + (14 - PIOS_IBUS_NUM_INPUTS) * 2 + 2) +#define PIOS_IBUS_SYNCBYTE 0x20 // sync on the packet length byte +#define PIOS_IBUS_TYPEBYTE 0x40 #define PIOS_IBUS_MAGIC 0x84fd9a39 +// make sure to always allocate sufficient bufferspace: +#if (PIOS_IBUS_SYNCBYTE != PIOS_IBUS_BUFLEN) +#error PIOS_IBUS_BUFLEN must be updated to match the expected packet length +#endif + /** * @brief IBus receiver driver internal state data */ @@ -182,9 +210,16 @@ static uint16_t PIOS_IBUS_Receive(uint32_t context, uint8_t *buf, uint16_t buf_l } for (int i = 0; i < buf_len; i++) { + // byte 0: sync byte (packet length) should match: if (ibus_dev->buf_pos == 0 && buf[i] != PIOS_IBUS_SYNCBYTE) { continue; } + // byte 1: type field should match: + if (ibus_dev->buf_pos == 1 && buf[i] != PIOS_IBUS_TYPEBYTE) { + // not the correct type of data, restart from byte 0 + ibus_dev->buf_pos = 0; + continue; + } ibus_dev->rx_buf[ibus_dev->buf_pos++] = buf[i]; if (ibus_dev->buf_pos <= PIOS_IBUS_BUFLEN - 2) { @@ -238,10 +273,12 @@ static void PIOS_IBUS_Supervisor(uint32_t context) PIOS_Assert(PIOS_IBUS_Validate(ibus_dev)); + // clear rx buffer after 4.8 ms without input if (++ibus_dev->rx_timer > 3) { PIOS_IBUS_ResetBuffer(ibus_dev); } + // mark all channels invalid after 51.2 ms without valid data if (++ibus_dev->failsafe_timer > 32) { PIOS_IBUS_SetAllChannels(ibus_dev, PIOS_RCVR_TIMEOUT); }