35 #if defined(PIOS_INCLUDE_SBUS)
38 static int32_t PIOS_SBus_Get(uintptr_t rcvr_id, uint8_t channel);
39 static uint16_t PIOS_SBus_RxInCallback(uintptr_t context,
44 static void PIOS_SBus_Supervisor(uintptr_t sbus_id);
49 .
read = PIOS_SBus_Get,
52 enum pios_sbus_dev_magic {
53 PIOS_SBUS_DEV_MAGIC = 0x53427573,
56 struct pios_sbus_state {
59 uint8_t receive_timer;
60 uint8_t failsafe_timer;
64 struct pios_sbus_dev {
65 enum pios_sbus_dev_magic
magic;
66 struct pios_sbus_state
state;
70 static struct pios_sbus_dev *PIOS_SBus_Alloc(
void)
72 struct pios_sbus_dev *sbus_dev;
74 sbus_dev = (
struct pios_sbus_dev *)
PIOS_malloc(
sizeof(*sbus_dev));
75 if (!sbus_dev)
return(NULL);
77 sbus_dev->magic = PIOS_SBUS_DEV_MAGIC;
82 static bool PIOS_SBus_Validate(
struct pios_sbus_dev *sbus_dev)
84 return (sbus_dev->magic == PIOS_SBUS_DEV_MAGIC);
88 static void PIOS_SBus_ResetChannels(
struct pios_sbus_state *
state)
96 static void PIOS_SBus_ResetState(
struct pios_sbus_state *state)
98 state->failsafe_timer = 0;
99 state->receive_timer = 0;
100 state->byte_count = 0;
101 PIOS_SBus_ResetChannels(state);
113 struct pios_sbus_dev *sbus_dev;
115 sbus_dev = (
struct pios_sbus_dev *)PIOS_SBus_Alloc();
116 if (!sbus_dev)
return -1;
118 PIOS_SBus_ResetState(&(sbus_dev->state));
120 *sbus_id = (uintptr_t)sbus_dev;
122 (driver->
bind_rx_cb)(lower_id, PIOS_SBus_RxInCallback, *sbus_id);
138 static int32_t PIOS_SBus_Get(uintptr_t rcvr_id, uint8_t channel)
140 struct pios_sbus_dev *sbus_dev = (
struct pios_sbus_dev *)rcvr_id;
142 if (!PIOS_SBus_Validate(sbus_dev))
150 return sbus_dev->state.channel_data[channel];
158 static void PIOS_SBus_UnrollChannels(
struct pios_sbus_state *state)
160 uint8_t *s = state->received_data;
161 uint16_t *
d = state->channel_data;
163 #define F(v,s) (((v) >> (s)) & 0x7ff)
166 d[0] =
F(s[0] | s[1] << 8, 0);
167 d[1] =
F(s[1] | s[2] << 8, 3);
168 d[2] =
F(s[2] | s[3] << 8 | s[4] << 16, 6);
169 d[3] =
F(s[4] | s[5] << 8, 1);
170 d[4] =
F(s[5] | s[6] << 8, 4);
171 d[5] =
F(s[6] | s[7] << 8 | s[8] << 16, 7);
172 d[6] =
F(s[8] | s[9] << 8, 2);
173 d[7] =
F(s[9] | s[10] << 8, 5);
176 d[8] =
F(s[11] | s[12] << 8, 0);
177 d[9] =
F(s[12] | s[13] << 8, 3);
178 d[10] =
F(s[13] | s[14] << 8 | s[15] << 16, 6);
179 d[11] =
F(s[15] | s[16] << 8, 1);
180 d[12] =
F(s[16] | s[17] << 8, 4);
181 d[13] =
F(s[17] | s[18] << 8 | s[19] << 16, 7);
182 d[14] =
F(s[19] | s[20] << 8, 2);
183 d[15] =
F(s[20] | s[21] << 8, 5);
191 static void PIOS_SBus_UpdateState(
struct pios_sbus_state *state, uint8_t b)
193 if (state->byte_count == 0) {
205 state->received_data[state->byte_count - 1] = b;
215 PIOS_SBus_ResetChannels(state);
218 PIOS_SBus_UnrollChannels(state);
219 state->failsafe_timer = 0;
227 state->byte_count = 0;
232 static uint16_t PIOS_SBus_RxInCallback(uintptr_t context,
238 struct pios_sbus_dev *sbus_dev = (
struct pios_sbus_dev *)context;
240 bool valid = PIOS_SBus_Validate(sbus_dev);
243 struct pios_sbus_state *state = &(sbus_dev->state);
246 for (uint8_t
i = 0;
i < buf_len;
i++) {
247 PIOS_SBus_UpdateState(state, buf[
i]);
250 state->receive_timer = 0;
274 static void PIOS_SBus_Supervisor(uintptr_t sbus_id)
276 struct pios_sbus_dev *sbus_dev = (
struct pios_sbus_dev *)sbus_id;
278 bool valid = PIOS_SBus_Validate(sbus_dev);
281 struct pios_sbus_state *state = &(sbus_dev->state);
285 if (++state->receive_timer > 2) {
286 state->byte_count = 0;
287 state->receive_timer = 0;
291 if (++state->failsafe_timer > 156) {
292 PIOS_SBus_ResetChannels(state);
293 state->failsafe_timer = 0;
int32_t(* read)(uintptr_t id, uint8_t channel)
Main PiOS header to include all the compiled in PiOS options.
int32_t PIOS_SBus_Init(uintptr_t *sbus_id, const struct pios_com_driver *driver, uintptr_t lower_id)
#define PIOS_DEBUG_Assert(test)
#define SBUS_R7008SB_EOF_COUNTER_MASK
void * PIOS_malloc(size_t size)
const struct pios_rcvr_driver pios_sbus_rcvr_driver
bool PIOS_RTC_RegisterTickCallback(void(*fn)(uintptr_t id), uintptr_t data)
void PIOS_RCVR_ActiveFromISR()
#define PIOS_SBUS_NUM_INPUTS
static struct flyingpicmd_cfg_fa cfg
#define SBUS_R7008SB_EOF_BYTE
Futaba S.Bus Private structures.
void(* bind_rx_cb)(uintptr_t id, pios_com_callback rx_in_cb, uintptr_t context)
#define SBUS_FRAME_LENGTH
#define PIOS_Assert(test)