4 #if defined(PIOS_INCLUDE_RCVR)
6 #include <pios_rcvr_priv.h>
8 enum pios_rcvr_dev_magic
{
9 PIOS_RCVR_DEV_MAGIC
= 0x99aabbcc,
12 struct pios_rcvr_dev
{
13 enum pios_rcvr_dev_magic magic
;
15 const struct pios_rcvr_driver
*driver
;
18 static bool PIOS_RCVR_validate(struct pios_rcvr_dev
*rcvr_dev
)
20 return rcvr_dev
->magic
== PIOS_RCVR_DEV_MAGIC
;
23 #if defined(PIOS_INCLUDE_FREERTOS) && 0
24 // static struct pios_rcvr_dev * PIOS_RCVR_alloc(void)
26 // struct pios_rcvr_dev * rcvr_dev;
28 // rcvr_dev = (struct pios_rcvr_dev *)pvPortMalloc(sizeof(*rcvr_dev));
29 // if (!rcvr_dev) return (NULL);
31 // rcvr_dev->magic = PIOS_RCVR_DEV_MAGIC;
35 static struct pios_rcvr_dev pios_rcvr_devs
[PIOS_RCVR_MAX_DEVS
];
36 static uint8_t pios_rcvr_num_devs
;
37 static uint32_t PIOS_RCVR_alloc(void)
39 struct pios_rcvr_dev
*rcvr_dev
;
41 if (pios_rcvr_num_devs
>= PIOS_RCVR_MAX_DEVS
) {
42 return PIOS_RCVR_MAX_DEVS
+ 1;
45 rcvr_dev
= &pios_rcvr_devs
[pios_rcvr_num_devs
++];
46 rcvr_dev
->magic
= PIOS_RCVR_DEV_MAGIC
;
48 return pios_rcvr_num_devs
;
50 static struct pios_rcvr_dev
*PIOS_RCVR_find_dev(uint32_t rcvr_dev_id
)
55 if (rcvr_dev_id
> pios_rcvr_num_devs
+ 1) {
58 return &pios_rcvr_devs
[rcvr_dev_id
- 1];
61 #endif /* if defined(PIOS_INCLUDE_FREERTOS) && 0 */
64 * Initialises RCVR layer
68 * \return < 0 if initialisation failed
70 int32_t PIOS_RCVR_Init(uint32_t *rcvr_id
, const struct pios_rcvr_driver
*driver
, uint32_t lower_id
)
72 PIOS_DEBUG_Assert(rcvr_id
);
73 PIOS_DEBUG_Assert(driver
);
76 struct pios_rcvr_dev
*rcvr_dev
;
78 rcvr_dev_id
= PIOS_RCVR_alloc();
79 rcvr_dev
= PIOS_RCVR_find_dev(rcvr_dev_id
);
84 rcvr_dev
->driver
= driver
;
85 rcvr_dev
->lower_id
= lower_id
;
87 *rcvr_id
= rcvr_dev_id
;
95 * @brief Reads an input channel from the appropriate driver
96 * @param[in] rcvr_id driver to read from
97 * @param[in] channel channel to read
98 * @returns Unitless input value
99 * @retval PIOS_RCVR_TIMEOUT indicates a failsafe or timeout from that channel
100 * @retval PIOS_RCVR_INVALID invalid channel for this driver (usually out of range supported)
101 * @retval PIOS_RCVR_NODRIVER driver was not initialized
103 int32_t PIOS_RCVR_Read(uint32_t rcvr_id
, uint8_t channel
)
105 // Publicly facing API uses channel 1 for first channel
107 return PIOS_RCVR_INVALID
;
113 return PIOS_RCVR_NODRIVER
;
116 struct pios_rcvr_dev
*rcvr_dev
= PIOS_RCVR_find_dev(rcvr_id
);
118 if (!PIOS_RCVR_validate(rcvr_dev
)) {
119 /* Undefined RCVR port for this board (see pios_board.c) */
123 PIOS_DEBUG_Assert(rcvr_dev
->driver
->read
);
125 return rcvr_dev
->driver
->read(rcvr_dev
->lower_id
, channel
);
128 #endif /* if defined(PIOS_INCLUDE_RCVR) */