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

The outbound half of DShot: sixteen bits and the map from travel onto them. More...

#include "dshot.h"

Functions

static uint16_t fold (uint16_t payload)
 The checksum is three nibbles of the twelve-bit payload folded together.
uint16_t dshot_frame (uint16_t value, bool telemetry, bool inverted)
 Build a frame.
uint16_t dshot_throttle (uint16_t command, uint16_t span)
 Map a command of 0..span onto the throttle range.

Detailed Description

The outbound half of DShot: sixteen bits and the map from travel onto them.

See dshot.h for the model.

SPDX-License-Identifier: MIT

Function Documentation

◆ dshot_frame()

uint16_t dshot_frame ( uint16_t value,
bool telemetry,
bool inverted )

Build a frame.

value is 0..2047 and is used as given: mapping travel onto it is dshot_throttle()'s job, and a caller that wants to send command 12 must be able to say 12. telemetry asks the ESC to answer on its telemetry wire (the separate serial one, not the signal line). inverted selects bidirectional DShot, whose checksum is the complement of the ordinary one so that a receiver cannot mistake one protocol for the other.

Returns 0 for a value above 2047: zero is motor-stop, which is the safe reading of a number this end could not encode.

Parameters
valueFrame value, 0..2047, used as given.
telemetrytrue asks the ESC to answer on its telemetry wire.
invertedtrue selects bidirectional DShot (complemented checksum).
Returns
The 16-bit frame. 0 for a value above 2047: zero is motor-stop, which is the safe reading of a number this end could not encode.

◆ dshot_throttle()

uint16_t dshot_throttle ( uint16_t command,
uint16_t span )

Map a command of 0..span onto the throttle range.

Zero is DSHOT_CMD_MOTOR_STOP and everything above it lands in 48..2047, so the command range is never entered by a throttle that is merely low. The discontinuity at the bottom is the protocol's, not this function's: an ESC has no idea of "barely turning".

A command above span returns DSHOT_CMD_MOTOR_STOP, the same reading of a fault that dshot_frame() applies to a value it cannot encode. A command equal to span is the top of the range and returns 2047.

Parameters
commandCommand or travel step, 0 to span.
spanLargest command, the top of the travel.
Returns
DSHOT_CMD_MOTOR_STOP for 0 or a command above span; otherwise a value from 48 to 2047.

◆ fold()

uint16_t fold ( uint16_t payload)
static

The checksum is three nibbles of the twelve-bit payload folded together.

It catches every single-bit error and every burst inside one nibble, which is what a bit period misread as its neighbour looks like. It is not a CRC (cyclic redundancy check) in any useful sense and the protocol does not claim one.

Parameters
payloadThe twelve-bit payload, value and telemetry bit.
Returns
The checksum, 0 to 15.