2011-07-28 14:22:47 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup PIOS PIOS Core hardware abstraction layer
|
|
|
|
* @{
|
|
|
|
* @addtogroup PIOS_GCSRCVR GCS Receiver Input Functions
|
|
|
|
* @brief Code to read the channels within the GCS Receiver UAVObject
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file pios_gcsrcvr.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief GCS Input functions (STM32 dependent)
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pios.h"
|
|
|
|
|
2013-03-15 15:28:53 +01:00
|
|
|
#ifdef PIOS_INCLUDE_GCSRCVR
|
2013-02-15 21:13:50 +01:00
|
|
|
|
2013-03-15 15:28:53 +01:00
|
|
|
#include "uavobjectmanager.h"
|
2011-07-28 14:22:47 +02:00
|
|
|
|
|
|
|
#include "pios_gcsrcvr_priv.h"
|
|
|
|
|
|
|
|
static GCSReceiverData gcsreceiverdata;
|
|
|
|
|
|
|
|
/* Provide a RCVR driver */
|
|
|
|
static int32_t PIOS_GCSRCVR_Get(uint32_t rcvr_id, uint8_t channel);
|
2012-02-03 03:58:42 +01:00
|
|
|
static void PIOS_gcsrcvr_Supervisor(uint32_t ppm_id);
|
2011-07-28 14:22:47 +02:00
|
|
|
|
|
|
|
const struct pios_rcvr_driver pios_gcsrcvr_rcvr_driver = {
|
2013-05-19 16:37:30 +02:00
|
|
|
.read = PIOS_GCSRCVR_Get,
|
2011-07-28 14:22:47 +02:00
|
|
|
};
|
|
|
|
|
2012-02-03 03:58:42 +01:00
|
|
|
/* Local Variables */
|
|
|
|
enum pios_gcsrcvr_dev_magic {
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_GCSRCVR_DEV_MAGIC = 0xe9da5c56,
|
2012-02-03 03:58:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct pios_gcsrcvr_dev {
|
2013-05-19 16:37:30 +02:00
|
|
|
enum pios_gcsrcvr_dev_magic magic;
|
2012-02-03 03:58:42 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
uint8_t supv_timer;
|
|
|
|
bool Fresh;
|
2012-02-03 03:58:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct pios_gcsrcvr_dev *global_gcsrcvr_dev;
|
|
|
|
|
|
|
|
static bool PIOS_gcsrcvr_validate(struct pios_gcsrcvr_dev *gcsrcvr_dev)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return gcsrcvr_dev->magic == PIOS_GCSRCVR_DEV_MAGIC;
|
2012-02-03 03:58:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(PIOS_INCLUDE_FREERTOS)
|
|
|
|
static struct pios_gcsrcvr_dev *PIOS_gcsrcvr_alloc(void)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_gcsrcvr_dev *gcsrcvr_dev;
|
2012-02-03 03:58:42 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
gcsrcvr_dev = (struct pios_gcsrcvr_dev *)pvPortMalloc(sizeof(*gcsrcvr_dev));
|
|
|
|
if (!gcsrcvr_dev) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-03 03:58:42 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
gcsrcvr_dev->magic = PIOS_GCSRCVR_DEV_MAGIC;
|
|
|
|
gcsrcvr_dev->Fresh = false;
|
|
|
|
gcsrcvr_dev->supv_timer = 0;
|
2012-02-03 03:58:42 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* The update callback cannot receive the device pointer, so set it in a global */
|
|
|
|
global_gcsrcvr_dev = gcsrcvr_dev;
|
2012-02-03 03:58:42 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return gcsrcvr_dev;
|
2012-02-03 03:58:42 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
static struct pios_gcsrcvr_dev pios_gcsrcvr_devs[PIOS_GCSRCVR_MAX_DEVS];
|
|
|
|
static uint8_t pios_gcsrcvr_num_devs;
|
|
|
|
static struct pios_gcsrcvr_dev *PIOS_gcsrcvr_alloc(void)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_gcsrcvr_dev *gcsrcvr_dev;
|
2012-02-03 03:58:42 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (pios_gcsrcvr_num_devs >= PIOS_GCSRCVR_MAX_DEVS) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-03 03:58:42 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
gcsrcvr_dev = &pios_gcsrcvr_devs[pios_gcsrcvr_num_devs++];
|
|
|
|
gcsrcvr_dev->magic = PIOS_GCSRCVR_DEV_MAGIC;
|
|
|
|
gcsrcvr_dev->Fresh = false;
|
|
|
|
gcsrcvr_dev->supv_timer = 0;
|
2012-02-03 03:58:42 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
global_gcsrcvr_dev = gcsrcvr_dev;
|
2012-02-03 03:58:42 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return gcsrcvr_dev;
|
2012-02-03 03:58:42 +01:00
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
#endif /* if defined(PIOS_INCLUDE_FREERTOS) */
|
2012-02-03 03:58:42 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static void gcsreceiver_updated(UAVObjEvent *ev)
|
2011-07-28 14:22:47 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_gcsrcvr_dev *gcsrcvr_dev = global_gcsrcvr_dev;
|
|
|
|
|
|
|
|
if (ev->obj == GCSReceiverHandle()) {
|
|
|
|
GCSReceiverGet(&gcsreceiverdata);
|
|
|
|
gcsrcvr_dev->Fresh = true;
|
|
|
|
}
|
2011-07-28 14:22:47 +02:00
|
|
|
}
|
|
|
|
|
2013-05-05 09:02:24 +02:00
|
|
|
extern int32_t PIOS_GCSRCVR_Init(__attribute__((unused)) uint32_t *gcsrcvr_id)
|
2011-07-28 14:22:47 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_gcsrcvr_dev *gcsrcvr_dev;
|
2012-02-03 03:58:42 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Allocate the device structure */
|
|
|
|
gcsrcvr_dev = (struct pios_gcsrcvr_dev *)PIOS_gcsrcvr_alloc();
|
|
|
|
if (!gcsrcvr_dev) {
|
|
|
|
return -1;
|
|
|
|
}
|
2012-02-03 03:58:42 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
for (uint8_t i = 0; i < GCSRECEIVER_CHANNEL_NUMELEM; i++) {
|
|
|
|
/* Flush channels */
|
|
|
|
gcsreceiverdata.Channel[i] = PIOS_RCVR_TIMEOUT;
|
|
|
|
}
|
2012-09-22 09:45:47 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Register uavobj callback */
|
|
|
|
GCSReceiverConnectCallback(gcsreceiver_updated);
|
2012-02-03 03:58:42 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Register the failsafe timer callback. */
|
|
|
|
if (!PIOS_RTC_RegisterTickCallback(PIOS_gcsrcvr_Supervisor, (uint32_t)gcsrcvr_dev)) {
|
|
|
|
PIOS_DEBUG_Assert(0);
|
|
|
|
}
|
2012-02-03 03:58:42 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return 0;
|
2011-07-28 14:22:47 +02:00
|
|
|
}
|
|
|
|
|
2012-09-22 10:02:15 +02:00
|
|
|
/**
|
|
|
|
* Get the value of an input channel
|
|
|
|
* \param[in] channel Number of the channel desired (zero based)
|
|
|
|
* \output PIOS_RCVR_INVALID channel not available
|
|
|
|
* \output PIOS_RCVR_TIMEOUT failsafe condition or missing receiver
|
|
|
|
* \output >=0 channel value
|
|
|
|
*/
|
2013-05-05 09:02:24 +02:00
|
|
|
static int32_t PIOS_GCSRCVR_Get(__attribute__((unused)) uint32_t rcvr_id, uint8_t channel)
|
2011-07-28 14:22:47 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (channel >= GCSRECEIVER_CHANNEL_NUMELEM) {
|
|
|
|
/* channel is out of range */
|
|
|
|
return PIOS_RCVR_INVALID;
|
|
|
|
}
|
2011-07-28 14:22:47 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return gcsreceiverdata.Channel[channel];
|
2011-07-28 14:22:47 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static void PIOS_gcsrcvr_Supervisor(uint32_t gcsrcvr_id)
|
|
|
|
{
|
|
|
|
/* Recover our device context */
|
|
|
|
struct pios_gcsrcvr_dev *gcsrcvr_dev = (struct pios_gcsrcvr_dev *)gcsrcvr_id;
|
|
|
|
|
|
|
|
if (!PIOS_gcsrcvr_validate(gcsrcvr_dev)) {
|
|
|
|
/* Invalid device specified */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RTC runs at 625Hz.
|
|
|
|
*/
|
|
|
|
if (++(gcsrcvr_dev->supv_timer) < (PIOS_GCSRCVR_TIMEOUT_MS * 1000 / 625)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gcsrcvr_dev->supv_timer = 0;
|
|
|
|
|
|
|
|
if (!gcsrcvr_dev->Fresh) {
|
|
|
|
for (int32_t i = 0; i < GCSRECEIVER_CHANNEL_NUMELEM; i++) {
|
|
|
|
gcsreceiverdata.Channel[i] = PIOS_RCVR_TIMEOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gcsrcvr_dev->Fresh = false;
|
2012-02-03 03:58:42 +01:00
|
|
|
}
|
|
|
|
|
2013-03-15 15:28:53 +01:00
|
|
|
#endif /* PIOS_INCLUDE_GCSRCVR */
|
2011-07-28 14:22:47 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|