OP-1734 replace double with qreal
[librepilot.git] / flight / pios / common / pios_rcvr.c
blobb947c5142f4a1f42e80aa8b5ba5db611705cab58
1 #include "pios.h"
3 #ifdef PIOS_INCLUDE_RCVR
5 #include <pios_rcvr_priv.h>
7 enum pios_rcvr_dev_magic {
8 PIOS_RCVR_DEV_MAGIC = 0x99aabbcc,
9 };
11 struct pios_rcvr_dev {
12 enum pios_rcvr_dev_magic magic;
13 uint32_t lower_id;
14 const struct pios_rcvr_driver *driver;
17 static bool PIOS_RCVR_validate(struct pios_rcvr_dev *rcvr_dev)
19 return rcvr_dev->magic == PIOS_RCVR_DEV_MAGIC;
22 #if defined(PIOS_INCLUDE_FREERTOS)
23 static struct pios_rcvr_dev *PIOS_RCVR_alloc(void)
25 struct pios_rcvr_dev *rcvr_dev;
27 rcvr_dev = (struct pios_rcvr_dev *)pios_malloc(sizeof(*rcvr_dev));
28 if (!rcvr_dev) {
29 return NULL;
32 rcvr_dev->magic = PIOS_RCVR_DEV_MAGIC;
33 return rcvr_dev;
35 #else
36 static struct pios_rcvr_dev pios_rcvr_devs[PIOS_RCVR_MAX_DEVS];
37 static uint8_t pios_rcvr_num_devs;
38 static struct pios_rcvr_dev *PIOS_RCVR_alloc(void)
40 struct pios_rcvr_dev *rcvr_dev;
42 if (pios_rcvr_num_devs >= PIOS_RCVR_MAX_DEVS) {
43 return NULL;
46 rcvr_dev = &pios_rcvr_devs[pios_rcvr_num_devs++];
47 rcvr_dev->magic = PIOS_RCVR_DEV_MAGIC;
49 return rcvr_dev;
51 #endif /* if defined(PIOS_INCLUDE_FREERTOS) */
53 /**
54 * Initialises RCVR layer
55 * \param[out] handle
56 * \param[in] driver
57 * \param[in] id
58 * \return < 0 if initialisation failed
60 int32_t PIOS_RCVR_Init(uint32_t *rcvr_id, const struct pios_rcvr_driver *driver, uint32_t lower_id)
62 PIOS_DEBUG_Assert(rcvr_id);
63 PIOS_DEBUG_Assert(driver);
65 struct pios_rcvr_dev *rcvr_dev;
67 rcvr_dev = (struct pios_rcvr_dev *)PIOS_RCVR_alloc();
68 if (!rcvr_dev) {
69 goto out_fail;
72 rcvr_dev->driver = driver;
73 rcvr_dev->lower_id = lower_id;
75 *rcvr_id = (uint32_t)rcvr_dev;
76 return 0;
78 out_fail:
79 return -1;
82 /**
83 * @brief Reads an input channel from the appropriate driver
84 * @param[in] rcvr_id driver to read from
85 * @param[in] channel channel to read
86 * @returns Unitless input value
87 * @retval PIOS_RCVR_TIMEOUT indicates a failsafe or timeout from that channel
88 * @retval PIOS_RCVR_INVALID invalid channel for this driver (usually out of range supported)
89 * @retval PIOS_RCVR_NODRIVER driver was not initialized
91 int32_t PIOS_RCVR_Read(uint32_t rcvr_id, uint8_t channel)
93 // Publicly facing API uses channel 1 for first channel
94 if (channel == 0) {
95 return PIOS_RCVR_INVALID;
96 } else {
97 channel--;
100 if (rcvr_id == 0) {
101 return PIOS_RCVR_NODRIVER;
104 struct pios_rcvr_dev *rcvr_dev = (struct pios_rcvr_dev *)rcvr_id;
106 if (!PIOS_RCVR_validate(rcvr_dev)) {
107 /* Undefined RCVR port for this board (see pios_board.c) */
108 PIOS_Assert(0);
111 PIOS_DEBUG_Assert(rcvr_dev->driver->read);
113 return rcvr_dev->driver->read(rcvr_dev->lower_id, channel);
117 * @brief Get a semaphore that signals when a new sample is available.
118 * @param[in] rcvr_id driver to read from
119 * @param[in] channel channel to read
120 * @returns The semaphore, or NULL if not supported.
122 xSemaphoreHandle PIOS_RCVR_GetSemaphore(uint32_t rcvr_id, uint8_t channel)
124 // Publicly facing API uses channel 1 for first channel
125 if (channel == 0) {
126 return NULL;
127 } else {
128 channel--;
131 if (rcvr_id == 0) {
132 return NULL;
135 struct pios_rcvr_dev *rcvr_dev = (struct pios_rcvr_dev *)rcvr_id;
137 if (!PIOS_RCVR_validate(rcvr_dev)) {
138 /* Undefined RCVR port for this board (see pios_board.c) */
139 PIOS_Assert(0);
142 if (rcvr_dev->driver->get_semaphore) {
143 return rcvr_dev->driver->get_semaphore(rcvr_dev->lower_id, channel);
145 return NULL;
148 #endif /* PIOS_INCLUDE_RCVR */
151 * @}
152 * @}