33 #if defined(PIOS_INCLUDE_COM_MSG)
37 #define PIOS_COM_MSG_MAX_LEN 63
39 struct pios_com_msg_dev {
43 uint8_t rx_msg_buffer[PIOS_COM_MSG_MAX_LEN];
44 volatile bool rx_msg_full;
46 uint8_t tx_msg_buffer[PIOS_COM_MSG_MAX_LEN];
47 volatile bool tx_msg_full;
50 static struct pios_com_msg_dev com_msg_dev;
52 static uint16_t PIOS_COM_MSG_TxOutCallback(uintptr_t context, uint8_t * buf, uint16_t buf_len, uint16_t * headroom,
bool * need_yield);
53 static uint16_t PIOS_COM_MSG_RxInCallback(uintptr_t context, uint8_t * buf, uint16_t buf_len, uint16_t * headroom,
bool * need_yield);
63 struct pios_com_msg_dev * com_dev = &com_msg_dev;
65 com_dev->driver = driver;
66 com_dev->lower_id = lower_id;
68 com_dev->rx_msg_full =
false;
69 (com_dev->driver->bind_rx_cb)(lower_id, PIOS_COM_MSG_RxInCallback, (uintptr_t)com_dev);
70 (com_dev->driver->rx_start)(com_dev->lower_id,
sizeof(com_dev->rx_msg_buffer));
72 com_dev->tx_msg_full =
false;
73 (com_dev->driver->bind_tx_cb)(lower_id, PIOS_COM_MSG_TxOutCallback, (uintptr_t)com_dev);
75 *com_id = (uintptr_t)com_dev;
79 static uint16_t PIOS_COM_MSG_TxOutCallback(uintptr_t context, uint8_t * buf, uint16_t buf_len, uint16_t * headroom,
bool * need_yield)
81 struct pios_com_msg_dev * com_dev = (
struct pios_com_msg_dev *)context;
86 uint16_t bytes_from_fifo = 0;
88 if (com_dev->tx_msg_full && (buf_len >=
sizeof(com_dev->tx_msg_buffer))) {
90 memcpy(buf, com_dev->tx_msg_buffer,
sizeof(com_dev->tx_msg_buffer));
91 bytes_from_fifo =
sizeof(com_dev->tx_msg_buffer);
92 com_dev->tx_msg_full =
false;
96 if (com_dev->tx_msg_full) {
97 *headroom =
sizeof(com_dev->tx_msg_buffer);
103 return (bytes_from_fifo);
106 static uint16_t PIOS_COM_MSG_RxInCallback(uintptr_t context, uint8_t * buf, uint16_t buf_len, uint16_t * headroom,
bool * need_yield)
108 struct pios_com_msg_dev * com_dev = (
struct pios_com_msg_dev *)context;
110 uint16_t bytes_into_fifo = 0;
112 if (!com_dev->rx_msg_full && (buf_len >=
sizeof(com_dev->rx_msg_buffer))) {
113 memcpy(com_dev->rx_msg_buffer, buf,
sizeof(com_dev->rx_msg_buffer));
114 bytes_into_fifo =
sizeof(com_dev->rx_msg_buffer);
115 com_dev->rx_msg_full =
true;
119 if (!com_dev->rx_msg_full) {
120 *headroom =
sizeof(com_dev->rx_msg_buffer);
126 return (bytes_into_fifo);
129 int32_t
PIOS_COM_MSG_Send(uintptr_t com_id,
const uint8_t *msg, uint16_t msg_len)
134 struct pios_com_msg_dev * com_dev = (
struct pios_com_msg_dev *)com_id;
136 PIOS_Assert(msg_len ==
sizeof(com_dev->tx_msg_buffer));
139 while (com_dev->tx_msg_full) {
141 if (com_dev->driver->tx_start) {
142 (com_dev->driver->tx_start)(com_dev->lower_id,
sizeof(com_dev->tx_msg_buffer));
146 memcpy((
void *) com_dev->tx_msg_buffer, msg, msg_len);
147 com_dev->tx_msg_full =
true;
150 if (com_dev->driver->tx_start) {
151 (com_dev->driver->tx_start)(com_dev->lower_id,
sizeof(com_dev->tx_msg_buffer));
162 struct pios_com_msg_dev * com_dev = (
struct pios_com_msg_dev *)com_id;
164 PIOS_Assert(msg_len ==
sizeof(com_dev->rx_msg_buffer));
166 if (!com_dev->rx_msg_full) {
168 (com_dev->driver->rx_start)(com_dev->lower_id,
sizeof(com_dev->rx_msg_buffer));
170 memcpy(msg, com_dev->rx_msg_buffer, msg_len);
171 com_dev->rx_msg_full =
false;
uint16_t PIOS_COM_MSG_Receive(uintptr_t com_id, uint8_t *buf, uint16_t buf_len)
int32_t PIOS_COM_MSG_Send(uintptr_t com_id, const uint8_t *msg, uint16_t msg_len)
Main PiOS header to include all the compiled in PiOS options.
void(* bind_tx_cb)(uintptr_t id, pios_com_callback tx_out_cb, uintptr_t context)
COM layer functions header.
int32_t PIOS_COM_MSG_Init(uintptr_t *com_id, const struct pios_com_driver *driver, uintptr_t lower_id)
void(* bind_rx_cb)(uintptr_t id, pios_com_callback rx_in_cb, uintptr_t context)
#define PIOS_Assert(test)