From a0bdc58e5b08964d218ba8385cc88c9579fadd36 Mon Sep 17 00:00:00 2001 From: Brian Webb Date: Tue, 28 Feb 2012 21:30:06 -0700 Subject: [PATCH] Added framework for rfm22b com device. --- .../PiOS/Boards/STM32103CB_PIPXTREME_Rev1.h | 2 + flight/PiOS/Common/pios_rfm22b.c | 241 ++++++++++++++++++ flight/PiOS/inc/pios_rfm22b.h | 42 +++ flight/PiOS/inc/pios_rfm22b_priv.h | 51 ++++ flight/PipXtreme/Makefile | 1 + flight/PipXtreme/System/inc/pios_config.h | 9 +- flight/PipXtreme/System/pios_board.c | 31 +++ 7 files changed, 370 insertions(+), 7 deletions(-) create mode 100644 flight/PiOS/Common/pios_rfm22b.c create mode 100644 flight/PiOS/inc/pios_rfm22b.h create mode 100644 flight/PiOS/inc/pios_rfm22b_priv.h diff --git a/flight/PiOS/Boards/STM32103CB_PIPXTREME_Rev1.h b/flight/PiOS/Boards/STM32103CB_PIPXTREME_Rev1.h index 123c30237..54b6a2a01 100755 --- a/flight/PiOS/Boards/STM32103CB_PIPXTREME_Rev1.h +++ b/flight/PiOS/Boards/STM32103CB_PIPXTREME_Rev1.h @@ -154,11 +154,13 @@ extern uint32_t pios_com_vcp_usb_id; extern uint32_t pios_com_usart1_id; extern uint32_t pios_com_usart2_id; extern uint32_t pios_com_usart3_id; +extern uint32_t pios_com_rfm22b_id; #define PIOS_COM_TELEM_SERIAL (pios_com_usart1_id) #define PIOS_COM_DEBUG (pios_com_usart2_id) #define PIOS_COM_FLEXI (pios_com_usart3_id) #define PIOS_COM_TELEM_USB (pios_com_telem_usb_id) #define PIOS_COM_VCP_USB (pios_com_vcp_usb_id) +#define PIOS_COM_RFM22B_RF (pios_com_rfm22b_id) //------------------------ // PIOS_RCVR diff --git a/flight/PiOS/Common/pios_rfm22b.c b/flight/PiOS/Common/pios_rfm22b.c new file mode 100644 index 000000000..2af5084e3 --- /dev/null +++ b/flight/PiOS/Common/pios_rfm22b.c @@ -0,0 +1,241 @@ +/** + ****************************************************************************** + * @addtogroup PIOS PIOS Core hardware abstraction layer + * @{ + * @addtogroup PIOS_RFM22B Radio Functions + * @brief PIOS interface for for the RFM22B radio + * @{ + * + * @file pios_rfm22b.c + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012. + * @brief Implements a driver the the RFM22B driver + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * This program 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 3 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* Project Includes */ +#include "pios.h" + +#if defined(PIOS_INCLUDE_RFM22B) + +#include + +/* Provide a COM driver */ +static void PIOS_RFM22B_ChangeBaud(uint32_t rfm22b_id, uint32_t baud); +static void PIOS_RFM22B_RegisterRxCallback(uint32_t rfm22b_id, pios_com_callback rx_in_cb, uint32_t context); +static void PIOS_RFM22B_RegisterTxCallback(uint32_t rfm22b_id, pios_com_callback tx_out_cb, uint32_t context); +static void PIOS_RFM22B_TxStart(uint32_t rfm22b_id, uint16_t tx_bytes_avail); +static void PIOS_RFM22B_RxStart(uint32_t rfm22b_id, uint16_t rx_bytes_avail); + +static void PIOS_RFM22B_Timer_Callback(uint32_t dev_id); + +const struct pios_com_driver pios_rfm22b_com_driver = { + .set_baud = PIOS_RFM22B_ChangeBaud, + .tx_start = PIOS_RFM22B_TxStart, + .rx_start = PIOS_RFM22B_RxStart, + .bind_tx_cb = PIOS_RFM22B_RegisterTxCallback, + .bind_rx_cb = PIOS_RFM22B_RegisterRxCallback, +}; + +enum pios_rfm22b_dev_magic { + PIOS_RFM22B_DEV_MAGIC = 0x68e971b6, +}; + +struct pios_rfm22b_dev { + enum pios_rfm22b_dev_magic magic; + const struct pios_rfm22b_cfg *cfg; + + uint32_t countdown_timer; + + pios_com_callback rx_in_cb; + uint32_t rx_in_context; + pios_com_callback tx_out_cb; + uint32_t tx_out_context; + + uint32_t rx_dropped; +}; + +static bool PIOS_RFM22B_validate(struct pios_rfm22b_dev * rfm22b_dev) +{ + return (rfm22b_dev->magic == PIOS_RFM22B_DEV_MAGIC); +} + +#if defined(PIOS_INCLUDE_FREERTOS) +static struct pios_rfm22b_dev * PIOS_RFM22B_alloc(void) +{ + struct pios_rfm22b_dev * rfm22b_dev; + + rfm22b_dev = (struct pios_rfm22b_dev *)pvPortMalloc(sizeof(*rfm22b_dev)); + if (!rfm22b_dev) return(NULL); + + rfm22b_dev->magic = PIOS_RFM22B_DEV_MAGIC; + return(rfm22b_dev); +} +#else +static struct pios_rfm22b_dev pios_rfm22b_devs[PIOS_RFM22B_MAX_DEVS]; +static uint8_t pios_rfm22b_num_devs; +static struct pios_rfm22b_dev * PIOS_RFM22B_alloc(void) +{ + struct pios_rfm22b_dev * rfm22b_dev; + + if (pios_rfm22b_num_devs >= PIOS_RFM22B_MAX_DEVS) { + return (NULL); + } + + rfm22b_dev = &pios_rfm22b_devs[pios_rfm22b_num_devs++]; + rfm22b_dev->magic = PIOS_RFM22B_DEV_MAGIC; + + return (rfm22b_dev); +} +#endif + +/** +* Initialise an RFM22B device +*/ +int32_t PIOS_RFM22B_Init(uint32_t *rfm22b_id, const struct pios_rfm22b_cfg *cfg) +{ + PIOS_DEBUG_Assert(rfm22b_id); + PIOS_DEBUG_Assert(cfg); + + struct pios_rfm22b_dev * rfm22b_dev; + + rfm22b_dev = (struct pios_rfm22b_dev *) PIOS_RFM22B_alloc(); + if (!rfm22b_dev) + return(-1); + + /* Bind the configuration to the device instance */ + rfm22b_dev->cfg = cfg; + + /* Configure the countdown timer and register the tick callback. */ + rfm22b_dev->countdown_timer = (uint32_t)((float)(rfm22b_dev->cfg->send_timeout) / 0.625); + if (!PIOS_RTC_RegisterTickCallback(PIOS_RFM22B_Timer_Callback, (uint32_t)rfm22b_dev)) { + PIOS_DEBUG_Assert(0); + } + + *rfm22b_id = (uint32_t)rfm22b_dev; + + return(0); +} + +static void PIOS_RFM22B_RxStart(uint32_t rfm22b_id, uint16_t rx_bytes_avail) +{ + struct pios_rfm22b_dev * rfm22b_dev = (struct pios_rfm22b_dev *)rfm22b_id; + + bool valid = PIOS_RFM22B_validate(rfm22b_dev); + PIOS_Assert(valid); + +#ifdef NEVER + RFM22B_ITConfig(rfm22b_dev->cfg->regs, RFM22B_IT_RXNE, ENABLE); +#endif +} +static void PIOS_RFM22B_TxStart(uint32_t rfm22b_id, uint16_t tx_bytes_avail) +{ + struct pios_rfm22b_dev * rfm22b_dev = (struct pios_rfm22b_dev *)rfm22b_id; + + bool valid = PIOS_RFM22B_validate(rfm22b_dev); + PIOS_Assert(valid); + +#ifdef NEVER + RFM22B_ITConfig(rfm22b_dev->cfg->regs, RFM22B_IT_TXE, ENABLE); +#endif +} + +/** +* Changes the baud rate of the RFM22B peripheral without re-initialising. +* \param[in] rfm22b_id RFM22B name (GPS, TELEM, AUX) +* \param[in] baud Requested baud rate +*/ +static void PIOS_RFM22B_ChangeBaud(uint32_t rfm22b_id, uint32_t baud) +{ + struct pios_rfm22b_dev * rfm22b_dev = (struct pios_rfm22b_dev *)rfm22b_id; + + bool valid = PIOS_RFM22B_validate(rfm22b_dev); + PIOS_Assert(valid); + +#ifdef NEVER + RFM22B_InitTypeDef RFM22B_InitStructure; + + /* Start with a copy of the default configuration for the peripheral */ + RFM22B_InitStructure = rfm22b_dev->cfg->init; + + /* Adjust the baud rate */ + RFM22B_InitStructure.RFM22B_BaudRate = baud; + + /* Write back the new configuration */ + RFM22B_Init(rfm22b_dev->cfg->regs, &RFM22B_InitStructure); +#endif +} + +static void PIOS_RFM22B_RegisterRxCallback(uint32_t rfm22b_id, pios_com_callback rx_in_cb, uint32_t context) +{ +#ifdef NEVER + struct pios_rfm22b_dev * rfm22b_dev = (struct pios_rfm22b_dev *)rfm22b_id; + + bool valid = PIOS_RFM22B_validate(rfm22b_dev); + PIOS_Assert(valid); + + /* + * Order is important in these assignments since ISR uses _cb + * field to determine if it's ok to dereference _cb and _context + */ + rfm22b_dev->rx_in_context = context; + rfm22b_dev->rx_in_cb = rx_in_cb; +#endif +} + +static void PIOS_RFM22B_RegisterTxCallback(uint32_t rfm22b_id, pios_com_callback tx_out_cb, uint32_t context) +{ +#ifdef NEVER + struct pios_rfm22b_dev * rfm22b_dev = (struct pios_rfm22b_dev *)rfm22b_id; + + bool valid = PIOS_RFM22B_validate(rfm22b_dev); + PIOS_Assert(valid); + + /* + * Order is important in these assignments since ISR uses _cb + * field to determine if it's ok to dereference _cb and _context + */ + rfm22b_dev->tx_out_context = context; + rfm22b_dev->tx_out_cb = tx_out_cb; +#endif +} + +static void PIOS_RFM22B_Timer_Callback(uint32_t dev_id) { + /* Recover our device context */ + struct pios_rfm22b_dev *rfm22b_dev = (struct pios_rfm22b_dev *)dev_id; + + if (!PIOS_RFM22B_validate(rfm22b_dev)) { + /* Invalid device specified */ + return; + } + + /* + * RTC runs at 625Hz. + */ + if(--rfm22b_dev->countdown_timer > 0) + return; + rfm22b_dev->countdown_timer = (uint32_t)((float)(rfm22b_dev->cfg->send_timeout) / 0.625); + PIOS_COM_SendString(PIOS_COM_TELEM_SERIAL, "Hello Telem\n\r"); +} + +#endif + +/** + * @} + * @} + */ diff --git a/flight/PiOS/inc/pios_rfm22b.h b/flight/PiOS/inc/pios_rfm22b.h new file mode 100644 index 000000000..ab767d9af --- /dev/null +++ b/flight/PiOS/inc/pios_rfm22b.h @@ -0,0 +1,42 @@ +/** + ****************************************************************************** + * @addtogroup PIOS PIOS Core hardware abstraction layer + * @{ + * @addtogroup PIOS_RFM22B Radio Functions + * @brief PIOS interface for RFM22B Radio + * @{ + * + * @file pios_rfm22b.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012. + * @brief RFM22B functions header. + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * This program 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 3 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PIOS_RFM22B_H +#define PIOS_RFM22B_H + +/* Global Types */ +/* Public Functions */ + +#endif /* PIOS_RFM22B_H */ + +/** + * @} + * @} + */ diff --git a/flight/PiOS/inc/pios_rfm22b_priv.h b/flight/PiOS/inc/pios_rfm22b_priv.h new file mode 100644 index 000000000..d22c90164 --- /dev/null +++ b/flight/PiOS/inc/pios_rfm22b_priv.h @@ -0,0 +1,51 @@ +/** + ****************************************************************************** + * @addtogroup PIOS PIOS Core hardware abstraction layer + * @{ + * @addtogroup PIOS_RFM22B Radio Functions + * @brief PIOS interface for RFM22B Radio + * @{ + * + * @file pios_rfm22b_priv.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012. + * @brief RFM22B private definitions. + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * This program 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 3 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PIOS_RFM22B_PRIV_H +#define PIOS_RFM22B_PRIV_H + +#include +#include "fifo_buffer.h" +#include "pios_rfm22b.h" + +extern const struct pios_com_driver pios_rfm22b_com_driver; + +struct pios_rfm22b_cfg { + uint32_t send_timeout; +}; + +extern int32_t PIOS_RFM22B_Init(uint32_t *rfb22b_id, const struct pios_rfm22b_cfg *cfg); + +#endif /* PIOS_RFM22B_PRIV_H */ + +/** + * @} + * @} + */ diff --git a/flight/PipXtreme/Makefile b/flight/PipXtreme/Makefile index 7562d7d34..27d480f39 100755 --- a/flight/PipXtreme/Makefile +++ b/flight/PipXtreme/Makefile @@ -215,6 +215,7 @@ SRC += $(PIOSCOMMON)/pios_iap.c SRC += $(PIOSCOMMON)/pios_bl_helper.c SRC += $(PIOSCOMMON)/pios_rcvr.c SRC += $(PIOSCOMMON)/printf-stdarg.c +SRC += $(PIOSCOMMON)/pios_rfm22b.c ## Libraries for flight calculations SRC += $(FLIGHTLIB)/fifo_buffer.c SRC += $(FLIGHTLIB)/CoordinateConversions.c diff --git a/flight/PipXtreme/System/inc/pios_config.h b/flight/PipXtreme/System/inc/pios_config.h index 01902e9cc..7dfd203d7 100755 --- a/flight/PipXtreme/System/inc/pios_config.h +++ b/flight/PipXtreme/System/inc/pios_config.h @@ -34,13 +34,12 @@ #define PIOS_CONFIG_H /* Enable/Disable PiOS Modules */ -//#define PIOS_INCLUDE_ADC #define PIOS_INCLUDE_DELAY -//#define PIOS_INCLUDE_I2C +#define PIOS_INCLUDE_I2C #define PIOS_INCLUDE_IRQ #define PIOS_INCLUDE_LED #define PIOS_INCLUDE_IAP - +#define PIOS_INCLUDE_RFM22B #define PIOS_INCLUDE_RCVR /* Supported receiver interfaces */ @@ -69,10 +68,6 @@ //#define PIOS_INCLUDE_WDG #define PIOS_INCLUDE_BL_HELPER -#define PIOS_INCLUDE_FLASH - -#define PIOS_UAVTALK_DEBUG - /* A really shitty setting saving implementation */ //#define PIOS_INCLUDE_FLASH_SECTOR_SETTINGS diff --git a/flight/PipXtreme/System/pios_board.c b/flight/PipXtreme/System/pios_board.c index 3d8a72a72..28a281089 100755 --- a/flight/PipXtreme/System/pios_board.c +++ b/flight/PipXtreme/System/pios_board.c @@ -403,6 +403,9 @@ static const struct pios_usart_cfg pios_usart_telem_flexi_cfg = { #define PIOS_COM_VCP_USB_RX_BUF_LEN 192 #define PIOS_COM_VCP_USB_TX_BUF_LEN 192 +#define PIOS_COM_RFM22B_RF_RX_BUF_LEN 192 +#define PIOS_COM_RFM22B_RF_TX_BUF_LEN 192 + #if defined(PIOS_INCLUDE_RTC) /* * Realtime Clock (RTC) @@ -612,6 +615,7 @@ uint32_t pios_com_vcp_usb_id; uint32_t pios_com_usart1_id; uint32_t pios_com_usart2_id; uint32_t pios_com_usart3_id; +uint32_t pios_com_rfm22b_id; #if defined(PIOS_INCLUDE_USB) #include "pios_usb_priv.h" @@ -662,6 +666,14 @@ const struct pios_usb_cdc_cfg pios_usb_cdc_cfg = { }; #endif /* PIOS_INCLUDE_USB_CDC */ +#if defined(PIOS_INCLUDE_RFM22B) +#include + +const struct pios_rfm22b_cfg pios_rfm22b_cfg = { + .send_timeout = 15, /* ms */ +}; +#endif /* PIOS_INCLUDE_RFM22B */ + /** * PIOS_Board_Init() * initializes all the core subsystems on this specific hardware @@ -842,9 +854,28 @@ void PIOS_Board_Init(void) { PIOS_Assert(0); } } +#if defined(PIOS_INCLUDE_RFM22B) + /* Initalize the RFM22B radio COM device. */ + { + uint32_t pios_rfm22b_id; + if (PIOS_RFM22B_Init(&pios_rfm22b_id, &pios_rfm22b_cfg)) { + PIOS_Assert(0); + } + uint8_t * rx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_RFM22B_RF_RX_BUF_LEN); + uint8_t * tx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_RFM22B_RF_TX_BUF_LEN); + PIOS_Assert(rx_buffer); + PIOS_Assert(tx_buffer); + if (PIOS_COM_Init(&pios_com_rfm22b_id, &pios_rfm22b_com_driver, pios_rfm22b_id, + rx_buffer, PIOS_COM_RFM22B_RF_RX_BUF_LEN, + tx_buffer, PIOS_COM_RFM22B_RF_TX_BUF_LEN)) { + PIOS_Assert(0); + } + } +#endif /* PIOS_INCLUDE_RFM22B */ PIOS_COM_SendString(PIOS_COM_TELEM_SERIAL, "Hello Telem\n\r"); PIOS_COM_SendString(PIOS_COM_DEBUG, "Hello Debug\n\r"); PIOS_COM_SendString(PIOS_COM_FLEXI, "Hello Flexi\n\r"); + PIOS_COM_SendString(PIOS_COM_RFM22B_RF, "Hello RMF22B\n\r"); /* Remap AFIO pin */ GPIO_PinRemapConfig( GPIO_Remap_SWJ_NoJTRST, ENABLE);