From 21367d3a22b3b603ba851204d7dd60882e3e5f71 Mon Sep 17 00:00:00 2001 From: James Cotton Date: Sat, 26 Nov 2011 22:19:42 -0600 Subject: [PATCH] Spektrum support for Revolution --- flight/PiOS/Boards/STM32F4xx_Revolution.h | 8 +- flight/PiOS/STM32F4xx/pios_dsm.c | 10 +- flight/PiOS/STM32F4xx/pios_usart.c | 64 +++++-- .../OpenPilotOSX.xcodeproj/project.pbxproj | 6 +- flight/Revolution/System/pios_board.c | 168 ++++++++++++++++-- 5 files changed, 215 insertions(+), 41 deletions(-) diff --git a/flight/PiOS/Boards/STM32F4xx_Revolution.h b/flight/PiOS/Boards/STM32F4xx_Revolution.h index 19b9b1ee8..b55ca78c4 100644 --- a/flight/PiOS/Boards/STM32F4xx_Revolution.h +++ b/flight/PiOS/Boards/STM32F4xx_Revolution.h @@ -117,7 +117,7 @@ extern uint32_t pios_i2c_mag_adapter_id; // // See also pios_board.c //------------------------- -#define PIOS_USART_MAX_DEVS 2 +#define PIOS_USART_MAX_DEVS 5 //------------------------- // PIOS_COM @@ -185,6 +185,12 @@ extern uint32_t pios_com_telem_usb_id; #define PIOS_SBUS_MAX_DEVS 1 #define PIOS_SBUS_NUM_INPUTS (16+2) +//------------------------- +// Receiver DSM input +//------------------------- +#define PIOS_DSM_MAX_DEVS 2 +#define PIOS_DSM_NUM_INPUTS 12 + //------------------------- // Servo outputs //------------------------- diff --git a/flight/PiOS/STM32F4xx/pios_dsm.c b/flight/PiOS/STM32F4xx/pios_dsm.c index cb838b56b..ecbab955a 100644 --- a/flight/PiOS/STM32F4xx/pios_dsm.c +++ b/flight/PiOS/STM32F4xx/pios_dsm.c @@ -34,6 +34,10 @@ #if defined(PIOS_INCLUDE_DSM) +#if !defined(PIOS_INCLUDE_RTC) +#error PIOS_INCLUDE_RTC must be used to use DSM +#endif + /* Forward Declarations */ static int32_t PIOS_DSM_Get(uint32_t rcvr_id, uint8_t channel); static uint16_t PIOS_DSM_RxInCallback(uint32_t context, @@ -113,10 +117,8 @@ static void PIOS_DSM_Bind(struct pios_dsm_dev *dsm_dev, uint8_t bind) { const struct pios_dsm_cfg *cfg = dsm_dev->cfg; - GPIO_InitTypeDef GPIO_InitStructure; - GPIO_InitStructure.GPIO_Pin = cfg->bind.init.GPIO_Pin; - GPIO_InitStructure.GPIO_Speed = cfg->bind.init.GPIO_Speed; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; + GPIO_InitTypeDef GPIO_InitStructure = cfg->bind.init; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; /* just to limit bind pulses */ if (bind > 10) diff --git a/flight/PiOS/STM32F4xx/pios_usart.c b/flight/PiOS/STM32F4xx/pios_usart.c index 3dc368ad8..5bb54b412 100644 --- a/flight/PiOS/STM32F4xx/pios_usart.c +++ b/flight/PiOS/STM32F4xx/pios_usart.c @@ -74,14 +74,18 @@ static bool PIOS_USART_validate(struct pios_usart_dev * usart_dev) return (usart_dev->magic == PIOS_USART_DEV_MAGIC); } -#if defined(PIOS_INCLUDE_FREERTOS) && 0 +#if defined(PIOS_INCLUDE_FREERTOS) static struct pios_usart_dev * PIOS_USART_alloc(void) { struct pios_usart_dev * usart_dev; - usart_dev = (struct pios_usart_dev *)malloc(sizeof(*usart_dev)); + usart_dev = (struct pios_usart_dev *)pvPortMalloc(sizeof(*usart_dev)); if (!usart_dev) return(NULL); + usart_dev->rx_in_cb = 0; + usart_dev->rx_in_context = 0; + usart_dev->tx_out_cb = 0; + usart_dev->tx_out_context = 0; usart_dev->magic = PIOS_USART_DEV_MAGIC; return(usart_dev); } @@ -111,21 +115,47 @@ static struct pios_usart_dev * PIOS_USART_alloc(void) */ static void PIOS_USART_generic_irq_handler(uint32_t usart_id); -#define USART_HANDLER(_n) \ - static uint32_t PIOS_USART_ ## _n ## _id; \ - void USART ## _n ## _IRQHandler(void) __attribute__ ((alias ("PIOS_USART_" #_n "_irq_handler"))); \ - static void PIOS_USART_ ## _n ## _irq_handler (void) \ - { \ - PIOS_USART_generic_irq_handler (PIOS_USART_ ## _n ## _id); \ - } \ - struct hack +static uint32_t PIOS_USART_1_id; +void USART1_IRQHandler(void) __attribute__ ((alias ("PIOS_USART_1_irq_handler"))); +static void PIOS_USART_1_irq_handler (void) +{ + PIOS_USART_generic_irq_handler (PIOS_USART_1_id); +} -USART_HANDLER(1); -USART_HANDLER(2); -USART_HANDLER(3); -USART_HANDLER(4); -USART_HANDLER(5); -USART_HANDLER(6); +static uint32_t PIOS_USART_2_id; +void USART2_IRQHandler(void) __attribute__ ((alias ("PIOS_USART_2_irq_handler"))); +static void PIOS_USART_2_irq_handler (void) +{ + PIOS_USART_generic_irq_handler (PIOS_USART_2_id); +} + +static uint32_t PIOS_USART_3_id; +void USART3_IRQHandler(void) __attribute__ ((alias ("PIOS_USART_3_irq_handler"))); +static void PIOS_USART_3_irq_handler (void) +{ + PIOS_USART_generic_irq_handler (PIOS_USART_3_id); +} + +static uint32_t PIOS_USART_4_id; +void USART4_IRQHandler(void) __attribute__ ((alias ("PIOS_USART_4_irq_handler"))); +static void PIOS_USART_4_irq_handler (void) +{ + PIOS_USART_generic_irq_handler (PIOS_USART_4_id); +} + +static uint32_t PIOS_USART_5_id; +void USART5_IRQHandler(void) __attribute__ ((alias ("PIOS_USART_5_irq_handler"))); +static void PIOS_USART_5_irq_handler (void) +{ + PIOS_USART_generic_irq_handler (PIOS_USART_5_id); +} + +static uint32_t PIOS_USART_6_id; +void USART6_IRQHandler(void) __attribute__ ((alias ("PIOS_USART_6_irq_handler"))); +static void PIOS_USART_6_irq_handler (void) +{ + PIOS_USART_generic_irq_handler (PIOS_USART_6_id); +} /** * Initialise a single USART device @@ -185,6 +215,8 @@ int32_t PIOS_USART_Init(uint32_t * usart_id, const struct pios_usart_cfg * cfg) break; } NVIC_Init((NVIC_InitTypeDef *)&(usart_dev->cfg->irq.init)); + USART_ITConfig(usart_dev->cfg->regs, USART_IT_RXNE, ENABLE); + USART_ITConfig(usart_dev->cfg->regs, USART_IT_TXE, ENABLE); // FIXME XXX Clear / reset uart here - sends NUL char else diff --git a/flight/Project/OpenPilotOSX/OpenPilotOSX.xcodeproj/project.pbxproj b/flight/Project/OpenPilotOSX/OpenPilotOSX.xcodeproj/project.pbxproj index ee007e46f..9cd1572f5 100644 --- a/flight/Project/OpenPilotOSX/OpenPilotOSX.xcodeproj/project.pbxproj +++ b/flight/Project/OpenPilotOSX/OpenPilotOSX.xcodeproj/project.pbxproj @@ -3340,6 +3340,7 @@ 65FAA03F133B669400F6F5CD /* GTOP_BIN.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = GTOP_BIN.c; sourceTree = ""; }; 65FAB8FC1480DA19000FF8B2 /* pios_dsm.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = pios_dsm.c; sourceTree = ""; }; 65FAB8FD1480DA19000FF8B2 /* pios_pwm.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = pios_pwm.c; sourceTree = ""; }; + 65FAB8FE1481A5C5000FF8B2 /* pios_rtc.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = pios_rtc.c; sourceTree = ""; }; 65FBE14412E7C98100176B5A /* pios_servo_priv.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = pios_servo_priv.h; sourceTree = ""; }; 65FC66AA123F30F100B04F74 /* ahrs_timer.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ahrs_timer.c; path = ../../AHRS/ahrs_timer.c; sourceTree = SOURCE_ROOT; }; 65FC66AB123F312A00B04F74 /* ahrs_timer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ahrs_timer.h; sourceTree = ""; }; @@ -3862,8 +3863,6 @@ 65904E5D14613B6100FD9482 /* STM32F4xx */ = { isa = PBXGroup; children = ( - 65FAB8FC1480DA19000FF8B2 /* pios_dsm.c */, - 65FAB8FD1480DA19000FF8B2 /* pios_pwm.c */, 65904E5E14613B6100FD9482 /* inc */, 65904E6214613B6100FD9482 /* Libraries */, 65904EC114613B6100FD9482 /* library.mk */, @@ -3875,6 +3874,7 @@ 65904EC714613B6100FD9482 /* pios_bmp085.c */, 65904EC814613B6100FD9482 /* pios_debug.c */, 65904EC914613B6100FD9482 /* pios_delay.c */, + 65FAB8FC1480DA19000FF8B2 /* pios_dsm.c */, 65904ECA14613B6100FD9482 /* pios_exti.c */, 65904ECB14613B6100FD9482 /* pios_gpio.c */, 65904ECC14613B6100FD9482 /* pios_hmc5883.c */, @@ -3886,6 +3886,8 @@ 6534B5581474F7B1003DF47C /* pios_mpu6000.c */, 65FA9B7A14709E700019A260 /* pios_mpu6050.c */, 65904ED214613B6100FD9482 /* pios_ppm.c */, + 65FAB8FD1480DA19000FF8B2 /* pios_pwm.c */, + 65FAB8FE1481A5C5000FF8B2 /* pios_rtc.c */, 65904ED314613B6100FD9482 /* pios_servo.c */, 65904ED414613B6100FD9482 /* pios_spi.c */, 65904ED514613B6100FD9482 /* pios_sys.c */, diff --git a/flight/Revolution/System/pios_board.c b/flight/Revolution/System/pios_board.c index e9dd1e034..127504df6 100644 --- a/flight/Revolution/System/pios_board.c +++ b/flight/Revolution/System/pios_board.c @@ -517,8 +517,7 @@ static uint8_t pios_com_gps_rx_buffer[PIOS_COM_GPS_RX_BUF_LEN]; */ static const struct pios_usart_cfg pios_usart_telem_main_cfg = { .regs = USART6, - .regs = USART1, - .remap = GPIO_AF_USART1, + .remap = GPIO_AF_USART6, .init = { .USART_BaudRate = 57600, .USART_WordLength = USART_WordLength_8b, @@ -530,12 +529,64 @@ static const struct pios_usart_cfg pios_usart_telem_main_cfg = { }, .irq = { .init = { - .NVIC_IRQChannel = USART1_IRQn, + .NVIC_IRQChannel = USART6_IRQn, .NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_MID, .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 + }, + }, + .tx = { + .gpio = GPIOC, + .init = { + .GPIO_Pin = GPIO_Pin_6, + .GPIO_Speed = GPIO_Speed_2MHz, + .GPIO_Mode = GPIO_Mode_AF, + .GPIO_OType = GPIO_OType_PP, + .GPIO_PuPd = GPIO_PuPd_UP + }, + }, +}; + +#define PIOS_COM_TELEM_RF_RX_BUF_LEN 512 +#define PIOS_COM_TELEM_RF_TX_BUF_LEN 512 + +#endif /* PIOS_COM_TELEM */ + +#if defined(PIOS_INCLUDE_DSM) +/* + * Spektrum/JR DSM USART + */ +#include + +static const struct pios_usart_cfg pios_usart_dsm_main_cfg = { + .regs = USART1, + .remap = GPIO_AF_USART1, + .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 = USART1_IRQn, + .NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_HIGH, + .NVIC_IRQChannelSubPriority = 0, + .NVIC_IRQChannelCmd = ENABLE, + }, + }, .rx = { .gpio = GPIOA, .init = { @@ -558,10 +609,76 @@ static const struct pios_usart_cfg pios_usart_telem_main_cfg = { }, }; -#define PIOS_COM_TELEM_RF_RX_BUF_LEN 512 -#define PIOS_COM_TELEM_RF_TX_BUF_LEN 512 +static const struct pios_dsm_cfg pios_dsm_main_cfg = { + .bind = { + .gpio = GPIOA, + .init = { + .GPIO_Pin = GPIO_Pin_10, + .GPIO_Speed = GPIO_Speed_2MHz, + .GPIO_Mode = GPIO_Mode_OUT, + .GPIO_OType = GPIO_OType_PP, + .GPIO_PuPd = GPIO_PuPd_NOPULL + }, + }, +}; -#endif /* PIOS_COM_TELEM */ + +static const struct pios_usart_cfg pios_usart_dsm_flexi_cfg = { + .regs = USART3, + .remap = GPIO_AF_USART3, + .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 = USART3_IRQn, + .NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_HIGH, + .NVIC_IRQChannelSubPriority = 0, + .NVIC_IRQChannelCmd = ENABLE, + }, + }, + .rx = { + .gpio = GPIOB, + .init = { + .GPIO_Pin = GPIO_Pin_11, + .GPIO_Speed = GPIO_Speed_2MHz, + .GPIO_Mode = GPIO_Mode_AF, + .GPIO_OType = GPIO_OType_PP, + .GPIO_PuPd = GPIO_PuPd_UP + }, + }, + .tx = { + .gpio = GPIOB, + .init = { + .GPIO_Pin = GPIO_Pin_10, + .GPIO_Speed = GPIO_Speed_2MHz, + .GPIO_Mode = GPIO_Mode_AF, + .GPIO_OType = GPIO_OType_PP, + .GPIO_PuPd = GPIO_PuPd_UP + }, + }, +}; + +static const struct pios_dsm_cfg pios_dsm_flexi_cfg = { + .bind = { + .gpio = GPIOB, + .init = { + .GPIO_Pin = GPIO_Pin_11, + .GPIO_Speed = GPIO_Speed_2MHz, + .GPIO_Mode = GPIO_Mode_OUT, + .GPIO_OType = GPIO_OType_PP, + .GPIO_PuPd = GPIO_PuPd_NOPULL + }, + }, +}; + + +#endif /* PIOS_INCLUDE_DSM */ #if defined(PIOS_INCLUDE_COM) @@ -1388,10 +1505,6 @@ static const struct pios_mpu6000_cfg pios_mpu6000_cfg = { * initializes all the core subsystems on this specific hardware * called from System/openpilot.c */ -int32_t test_val; -uint8_t buf[4]; -uint8_t rec[4]; -struct pios_mpu6000_data test_gyro_data; void PIOS_Board_Init(void) { /* Delay system */ @@ -1486,16 +1599,39 @@ void PIOS_Board_Init(void) { #endif /* PIOS_INCLUDE_COM */ + // Set up spektrum receiver + enum pios_dsm_proto proto; + proto = PIOS_DSM_PROTO_DSM2; + uint32_t pios_usart_dsm_id; + if (PIOS_USART_Init(&pios_usart_dsm_id, &pios_usart_dsm_main_cfg)) { + PIOS_Assert(0); + } + + uint32_t pios_dsm_id; + if (PIOS_DSM_Init(&pios_dsm_id, + &pios_dsm_main_cfg, + &pios_usart_com_driver, + pios_usart_dsm_id, + proto, 0)) { + PIOS_Assert(0); + } + + uint32_t pios_dsm_rcvr_id; + if (PIOS_RCVR_Init(&pios_dsm_rcvr_id, &pios_dsm_rcvr_driver, pios_dsm_id)) { + PIOS_Assert(0); + } + pios_rcvr_group_map[MANUALCONTROLSETTINGS_CHANNELGROUPS_DSMMAINPORT] = pios_dsm_rcvr_id; + /* Set up the receiver port. Later this should be optional */ - uint32_t pios_pwm_id; - PIOS_PWM_Init(&pios_pwm_id, &pios_pwm_cfg); + //uint32_t pios_pwm_id; + //PIOS_PWM_Init(&pios_pwm_id, &pios_pwm_cfg); - uint32_t pios_pwm_rcvr_id; + /*uint32_t pios_pwm_rcvr_id; if (PIOS_RCVR_Init(&pios_pwm_rcvr_id, &pios_pwm_rcvr_driver, pios_pwm_id)) { PIOS_Assert(0); } - pios_rcvr_group_map[MANUALCONTROLSETTINGS_CHANNELGROUPS_PWM] = pios_pwm_rcvr_id; + pios_rcvr_group_map[MANUALCONTROLSETTINGS_CHANNELGROUPS_PWM] = pios_pwm_rcvr_id;*/ /* Set up the servo outputs */ PIOS_Servo_Init(&pios_servo_cfg); @@ -1517,7 +1653,6 @@ void PIOS_Board_Init(void) { PIOS_DEBUG_Assert(0); } - PIOS_MPU6000_Attach(pios_spi_gyro_id); PIOS_MPU6000_Init(&pios_mpu6000_cfg); @@ -1525,9 +1660,6 @@ void PIOS_Board_Init(void) { PIOS_BMA180_Init(&pios_bma180_cfg); PIOS_HMC5883_Init(&pios_hmc5883_cfg); - - - } /**