3 #ifdef PIOS_INCLUDE_RCVR
5 #include <pios_rcvr_priv.h>
7 enum pios_rcvr_dev_magic
{
8 PIOS_RCVR_DEV_MAGIC
= 0x99aabbcc,
11 struct pios_rcvr_dev
{
12 enum pios_rcvr_dev_magic magic
;
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
));
32 rcvr_dev
->magic
= PIOS_RCVR_DEV_MAGIC
;
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
) {
46 rcvr_dev
= &pios_rcvr_devs
[pios_rcvr_num_devs
++];
47 rcvr_dev
->magic
= PIOS_RCVR_DEV_MAGIC
;
51 #endif /* if defined(PIOS_INCLUDE_FREERTOS) */
54 * Initialises RCVR layer
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();
72 rcvr_dev
->driver
= driver
;
73 rcvr_dev
->lower_id
= lower_id
;
75 *rcvr_id
= (uint32_t)rcvr_dev
;
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
95 return PIOS_RCVR_INVALID
;
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) */
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
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) */
142 if (rcvr_dev
->driver
->get_semaphore
) {
143 return rcvr_dev
->driver
->get_semaphore(rcvr_dev
->lower_id
, channel
);
148 #endif /* PIOS_INCLUDE_RCVR */