Decoding a Feetech Sync Read packet and checksum
Decode a 24-byte Feetech Sync Read request, calculate its checksum, and budget 16 replies on a 1 Mbit/s half-duplex servo bus.
The robot duck from kissqy's Microduck HD1910 replica has 15 Feetech servos and an IMU on a single wire. Every 20 ms, the Raspberry Pi ZERO sends one question and sixteen devices answer. This post walks through every byte of that question: a 24-byte Sync Read request with checksum 0x12. Source excerpts are from feetech.rs in Microduck-kissqy at b43e38e. The transaction described here is this robot's configuration, not a requirement for every Feetech installation.
A byte on the wire
The bus runs at a million bits per second:
// duck-control/src/feetech.rs
pub const BAUD: u32 = 1_000_000;
pub const IMU_ID: u8 = 200;
The UART sends each byte as 10 bits: a start bit (always 0), 8 data bits lowest first, then a stop bit (always 1). Between bytes the line rests high. At 1 Mbit/s one bit is 1 µs, so one byte takes 10 µs. That number does most of the timing math below.
The diagram shows numeric bit positions, most significant first. UART transmits the data bits in the opposite order: 0, 1, 0, 0, 0, 0, 0, 1, between the start and stop bits. (Microduck ep09a)
One wire, two directions
The ZERO talks, then the devices talk. Never both at once. Nobody switches the direction in software, though. The HAT does it in hardware, one bit at a time:
- The ZERO's TX pin drives transistor Q1. When TX goes low, Q1 turns on and pulls
DXL_DIRhigh. DXL_DIRhigh enables buffer U7, which copies the 0 onto the bus.- When TX goes high, Q1 turns off,
DXL_DIRfalls, U7 lets go, and a pull-up holds the bus high. - With
DXL_DIRlow, U6 passes the bus the other way, into the ZERO's RX pin.
So the ZERO never hears its own zeros, and it starts hearing the devices as soon as it stops talking. This is specific to the HAT circuit shown below; other half-duplex adapters may require explicit direction control. The firmware relies on this circuit:
The HAT flips bus direction from the TX pin itself: Q1, DXL_DIR, U7 out, U6 in. (Microduck ep09a)
// On the HAT, direction is automatic. Start receiving a request's replies
// immediately: Unix SerialPort::flush calls tcdrain and can delay the
// control thread after the small request has already left the UART.
The shape of a packet
A request packet has this layout: FF FF, ID, length, instruction, parameters, checksum. A status reply uses the corresponding byte for an error/status value instead of an instruction. The checksum covers that byte in either case.
Toy example: ask servo 1 for 11 bytes, starting at address 56.
| FF | FF | 01 | 04 | 02 | 38 | 0B | B5 |
|---|---|---|---|---|---|---|---|
| header | header | ID 1 | length | READ | addr 56 | 11 bytes | checksum |
- Length
04counts what follows it: instruction, two parameters, checksum. - Checksum: add from the ID through the last parameter: 1 + 4 + 2 + 56 + 11 = 74. Flip every bit: 255 − 74 = 181 =
B5. - A receiver adds everything after
FF FF, checksum included, modulo 256. A packet with a valid checksum sums toFFin that low byte. This is a checksum check, not a guarantee that every possible corruption can be detected.
Here's the function that builds every packet the duck sends:
// duck-control/src/feetech.rs, lines 52–61
pub fn packet(id: u8, instruction: u8, params: &[u8]) -> Result<Vec<u8>> {
if id == 255 || params.len() > 253 {
return Err(error("Feetech invalid ID or packet length"));
}
let mut out = vec![255, 255, id, (params.len() + 2) as u8, instruction];
out.extend_from_slice(params);
let sum = out[2..].iter().fold(0u8, |a, b| a.wrapping_add(*b));
out.push(!sum);
Ok(out)
}
wrapping_add keeps only the low byte and !sum flips the bits. The toy packet above is checked in the pinned source's test:
packet() in duck-control/src/feetech.rs, lines 52–61, next to the toy read packet. (Microduck ep09a)
// Excerpt from verified_hat_read_and_reply()
assert_eq!(
packet(1, READ, &[56, 11]).unwrap(),
[255, 255, 1, 4, 2, 56, 11, 181]
);
The real question: a 24-byte Sync Read
FF FF FE 14 82 38 0F C8 14 15 16 17 18 1E 1F 20 21 22 0A 0B 0C 0D 0E 12
FEis the broadcast ID, so every device reads it.82is Sync Read: one question addressed to many devices.38 0F: from address 56, read 15 bytes.- Then sixteen IDs:
C8= 200 is the IMU, followed by the joint IDs: left leg (20–24), neck and head (30–34), right leg (10–14). - Length
14= 20: eighteen parameters plus two. - Checksum: the bytes from
FEthrough the last ID add up to 1,005. Keep the low byte,ED(237), and flip it:12.
The 24-byte Sync Read and its checksum: 1,005 → ED → 12. (Microduck ep09a)
In this implementation, an ID list containing the IMU must put it first. This is the robot's implementation contract, not a universal Sync Read protocol rule. sync_read_packet rejects a list that violates it:
|| ids.len() > 251
|| ids.iter().any(|id| *id >= BROADCAST || !unique.insert(*id))
|| (ids.contains(&IMU_ID) && ids[0] != IMU_ID)
{
return Err(error("invalid Feetech Sync Read address/length/ID order"));
telemetry() builds that ID list on every tick:
pub fn telemetry(&mut self, servo_ids: &[u8]) -> Result<BTreeMap<u8, Vec<u8>>> {
if servo_ids.len() != NUM_JOINTS || servo_ids.contains(&IMU_ID) {
return Err(error("telemetry requires all 15 joints plus IMU 200"));
}
let mut ids = [IMU_ID; NUM_JOINTS + 1];
ids[1..].copy_from_slice(servo_ids);
self.sync_read(SYNC_ADDRESS, SYNC_LENGTH, &ids)
}
Sixteen answers, and the time budget
Each device replies with 21 bytes. 24 bytes out plus 16 × 21 back is 360 bytes, and at 10 µs per byte that's 3.6 ms of wire time out of a 20 ms budget (TELEMETRY_DEADLINE_MS: u64 = 20). That counts wire time only. Each device also waits a moment before it starts answering. Writing the targets back is one more packet: a 113-byte Sync Write, about 1.1 ms.
24 + 16 × 21 = 360 bytes, 3.6 ms of wire time out of 20 ms. (Microduck ep09a)
Try it yourself: what's the checksum when you ask servo 21 (0x15) the same toy question? 21 + 4 + 2 + 56 + 11 = 94; 255 − 94 = 161 = A1.
▶ Watch it with the HAT schematic and the bits animated: Microduck advanced course, episode 1a: One question, sixteen answers. Part B opens one answer and reads its 15 bytes.