diff --git a/flight/PiOS/Common/pios_rcvr.c b/flight/PiOS/Common/pios_rcvr.c new file mode 100644 index 000000000..b74f7beb2 --- /dev/null +++ b/flight/PiOS/Common/pios_rcvr.c @@ -0,0 +1,88 @@ +/* Project Includes */ +#include "pios.h" + +#if defined(PIOS_INCLUDE_RCVR) + +#include + +static bool PIOS_RCVR_validate(struct pios_rcvr_dev * rcvr_dev) +{ + return (rcvr_dev->magic == PIOS_RCVR_DEV_MAGIC); +} + +#if defined(PIOS_INCLUDE_FREERTOS) && 0 +static struct pios_rcvr_dev * PIOS_RCVR_alloc(void) +{ + struct pios_rcvr_dev * rcvr_dev; + + rcvr_dev = (struct pios_rcvr_dev *)malloc(sizeof(*rcvr_dev)); + if (!rcvr_dev) return (NULL); + + rcvr_dev->magic = PIOS_RCVR_DEV_MAGIC; + return(rcvr_dev); +} +#else +static struct pios_rcvr_dev pios_rcvr_devs[PIOS_RCVR_MAX_DEVS]; +static uint8_t pios_rcvr_num_devs; +static struct pios_rcvr_dev * PIOS_RCVR_alloc(void) +{ + struct pios_rcvr_dev * rcvr_dev; + + if (pios_rcvr_num_devs >= PIOS_RCVR_MAX_DEVS) { + return (NULL); + } + + rcvr_dev = &pios_rcvr_devs[pios_rcvr_num_devs++]; + rcvr_dev->magic = PIOS_RCVR_DEV_MAGIC; + + return (rcvr_dev); +} +#endif + +/** + * Initialises RCVR layer + * \param[out] handle + * \param[in] driver + * \param[in] id + * \return < 0 if initialisation failed + */ +int32_t PIOS_RCVR_Init(uint32_t * rcvr_id, const struct pios_rcvr_driver * driver, const uint32_t lower_id) +{ + PIOS_DEBUG_Assert(rcvr_id); + PIOS_DEBUG_Assert(driver); + + struct pios_rcvr_dev * rcvr_dev; + + rcvr_dev = (struct pios_rcvr_dev *) PIOS_RCVR_alloc(); + if (!rcvr_dev) goto out_fail; + + rcvr_dev->driver = driver; + rcvr_dev->id = lower_id; + + *rcvr_id = (uint32_t)rcvr_dev; + return(0); + +out_fail: + return(-1); +} + +int32_t PIOS_RCVR_Read(uint32_t rcvr_id) +{ + struct pios_rcvr_dev * rcvr_dev = (struct pios_rcvr_dev *)rcvr_id; + + if (!PIOS_RCVR_validate(rcvr_dev)) { + /* Undefined RCVR port for this board (see pios_board.c) */ + PIOS_DEBUG_Assert(0); + } + + PIOS_DEBUG_Assert(rcvr_dev->driver->read); + + return rcvr_dev->driver->read(rcvr_dev->id); +} + +#endif + +/** + * @} + * @} + */ diff --git a/flight/PiOS/inc/pios_rcvr.h b/flight/PiOS/inc/pios_rcvr.h new file mode 100644 index 000000000..40e99c450 --- /dev/null +++ b/flight/PiOS/inc/pios_rcvr.h @@ -0,0 +1,50 @@ +/** + ****************************************************************************** + * @addtogroup PIOS PIOS Core hardware abstraction layer + * @{ + * @addtogroup PIOS_RCVR RCVR layer functions + * @brief Hardware communication layer + * @{ + * + * @file pios_rcvr.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * Parts by Thorsten Klose (tk@midibox.org) + * @brief COM layer 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_RCVR_H +#define PIOS_RCVR_H + +extern uint32_t pios_rcvr_channel_to_id_map[]; + +struct pios_rcvr_driver { + void (*init)(uint32_t id); + int32_t (*read)(uint32_t id); +}; + +/* Public Functions */ +extern int32_t PIOS_RCVR_Read(uint32_t rcvr_id); + +#endif /* PIOS_RCVR_H */ + +/** + * @} + * @} + */ diff --git a/flight/PiOS/inc/pios_rcvr_priv.h b/flight/PiOS/inc/pios_rcvr_priv.h new file mode 100644 index 000000000..332ab9331 --- /dev/null +++ b/flight/PiOS/inc/pios_rcvr_priv.h @@ -0,0 +1,58 @@ +/** + ****************************************************************************** + * @addtogroup PIOS PIOS Core hardware abstraction layer + * @{ + * @addtogroup PIOS_RCVR RCVR Functions + * @brief PIOS interface for RCVR drivers + * @{ + * + * @file pios_rcvr_priv.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * Parts by Thorsten Klose (tk@midibox.org) + * @brief USART 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_RCVR_PRIV_H +#define PIOS_RCVR_PRIV_H + +#include + +extern uint32_t pios_rcvr_max_channel; + +enum pios_rcvr_dev_magic { + PIOS_RCVR_DEV_MAGIC = 0x99aabbcc, +}; + +struct pios_rcvr_dev { + enum pios_rcvr_dev_magic magic; + uint32_t id; + const struct pios_rcvr_driver * driver; +}; + +extern int32_t PIOS_RCVR_Init(uint32_t * rcvr_id, const struct pios_rcvr_driver * driver, const uint32_t lower_id); + +extern void PIOS_RCVR_IRQ_Handler(uint32_t rcvr_id); + +#endif /* PIOS_RCVR_PRIV_H */ + +/** + * @} + * @} + */ diff --git a/flight/PiOS/pios.h b/flight/PiOS/pios.h index 39802a2a9..a675f93b1 100644 --- a/flight/PiOS/pios.h +++ b/flight/PiOS/pios.h @@ -78,6 +78,7 @@ #include #include #include +#include #include #include #include