rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
sbus.c File Reference

S.BUS frame decoder: framing on the byte gap, footer check and channel unpacking. More...

#include "sbus.h"
#include <string.h>

Functions

static bool footer_ok (uint8_t b)
 A footer of zero is what the specification says.
void sbus_decoder_reset (sbus_decoder_t *d)
 Clear a decoder, including its diagnostic counters.
static void unpack (const uint8_t *p, uint16_t *out)
 Sixteen channels of eleven bits, packed little-endian end to end across the twenty-two payload bytes with no alignment.
bool sbus_decode_byte (sbus_decoder_t *d, uint8_t byte, uint32_t now_us, sbus_frame_t *out)
 Feed one byte with the time it arrived; true when out holds a frame.
uint16_t sbus_to_us (uint16_t raw)
 Microseconds for a raw channel value, clamped to a sane pulse.

Detailed Description

S.BUS frame decoder: framing on the byte gap, footer check and channel unpacking.

SPDX-License-Identifier: MIT

Function Documentation

◆ footer_ok()

bool footer_ok ( uint8_t b)
static

A footer of zero is what the specification says.

Receivers in the field also send 0x04, 0x14, 0x24 and 0x34 – the low nibble carries a frame counter on some of them – so the check is on the bits that are always clear rather than on the whole byte. Rejecting those would refuse frames from hardware that works.

◆ sbus_decode_byte()

bool sbus_decode_byte ( sbus_decoder_t * d,
uint8_t byte,
uint32_t now_us,
sbus_frame_t * out )

Feed one byte with the time it arrived; true when out holds a frame.

now_us paces the framing. A gap of SBUS_GAP_US or more starts a new frame, whatever was half-collected before it; the gap is the only reliable boundary this protocol has.

Parameters
dDecoder state. NULL returns false.
byteReceived byte.
now_usArrival time of the byte, in us.
outReceives the frame; may be NULL.
Returns
true when a frame with a valid footer completes.

◆ sbus_decoder_reset()

void sbus_decoder_reset ( sbus_decoder_t * d)

Clear a decoder, including its diagnostic counters.

Parameters
dDecoder to clear. NULL is ignored.

◆ sbus_to_us()

uint16_t sbus_to_us ( uint16_t raw)

Microseconds for a raw channel value, clamped to a sane pulse.

Parameters
rawRaw channel value.
Returns
Pulse width in us, clamped to 800 to 2200.

◆ unpack()

void unpack ( const uint8_t * p,
uint16_t * out )
static

Sixteen channels of eleven bits, packed little-endian end to end across the twenty-two payload bytes with no alignment.

Channel n starts at bit 11n, which lands mid-byte for all but the first, so each value is read as a three-byte window shifted down and masked.

Written as arithmetic rather than as sixteen expanded expressions: a transposed shift in an expanded form produces a channel that looks plausible and moves the wrong surface.