1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
//! Pure protocol layer: frame construction and parsing. No I/O.
//!
//! Everything here is a deterministic function of its inputs, which is what
//! makes the wire format unit-testable byte-for-byte (see `tests/vectors.rs`,
//! whose expected bytes are hand-derived from the DFRobot protocol
//! documentation and an independent CRC-8/MAXIM implementation).
//!
//! Out-of-range drive values are **clamped**, not rejected — a setpoint that
//! is merely too large should saturate the wheel, never wrap it around to
//! full reverse. This is part of the crate's API contract.
use Duration;
use crate;
use crate;
/// RS485 baud rate. The format is fixed: 115200 8N1, half-duplex.
pub const BAUD: u32 = 115_200;
/// Every frame on the bus, in both directions, is exactly this long.
pub const FRAME_LEN: usize = 10;
/// Drive command: the 16-bit value is interpreted per the active [`Mode`].
pub const CMD_DRIVE: u8 = 0x64;
/// Feedback query command: the motor replies with a telemetry frame.
pub const CMD_QUERY: u8 = 0x74;
/// Mode-switch command. **Its last byte is the mode, not a CRC.**
pub const CMD_MODE: u8 = 0xA0;
/// Minimum velocity command, RPM.
pub const RPM_MIN: i16 = -330;
/// Maximum velocity command, RPM.
pub const RPM_MAX: i16 = 330;
/// Minimum current command; maps to roughly −8 A.
///
/// Note this is `-32767`, not [`i16::MIN`] — the range is symmetric, and
/// `-32768` is clamped away rather than sent.
pub const CUR_MIN: i16 = -32_767;
/// Maximum current command; maps to roughly +8 A.
pub const CUR_MAX: i16 = 32_767;
/// Maximum position command; `0..=32767` maps to 0°..360°.
pub const POS_MAX: u16 = 32_767;
/// Brake byte value: in velocity mode, `0xFF` in byte 7 engages the
/// electric brake.
pub const BRAKE_BYTE: u8 = 0xFF;
/// Minimum drive-frame repetition rate (Hz) that sustains motion.
///
/// The M0601 is a *polling* device: it keeps moving only while drive frames
/// keep arriving. Below ~50 Hz it coasts to a stop.
pub const DRIVE_HZ_MIN: u32 = 50;
/// Maximum command rate the motor accepts (Hz).
pub const CMD_HZ_MAX: u32 = 500;
/// A complete 10-byte bus frame.
pub type Frame = ;
/// Time on the wire for one [`FRAME_LEN`]-byte frame at [`BAUD`] — 8N1 sends
/// 10 bits per byte (1 start + 8 data + 1 stop). This is the unit every
/// bus-occupancy budget is built from (see [`crate::bus::bus_period`]); the
/// same wire time that sizes [`DEFAULT_MIN_GAP`](crate::DEFAULT_MIN_GAP).
///
/// ```
/// use m0601::protocol::frame_time;
/// // 10 bytes × 10 bits ÷ 115200 baud ≈ 868 µs.
/// assert_eq!(frame_time().as_micros(), 868);
/// ```
/// The longest a wheel may go between drive frames before it coasts: the
/// period of the [`DRIVE_HZ_MIN`] floor. A periodic control loop's cycle
/// must not exceed this, or every cycle the motor slips below the floor and
/// coasts a little.
///
/// ```
/// use m0601::protocol::drive_floor;
/// assert_eq!(drive_floor().as_millis(), 20); // 1 s / 50 Hz
/// ```
/// CRC-8/MAXIM (Dallas 1-Wire): polynomial x⁸+x⁵+x⁴+1, reflected (0x8C),
/// init 0.
///
/// Host→motor frames carry this over bytes 0–8 in byte 9 — except the
/// mode-switch ([`frame_mode`]) and set-ID ([`frame_set_id`]) frames, which
/// carry no CRC at all.
///
/// Motor replies carry it too, over the same bytes: a hardware capture
/// settled that question, and [`Feedback::crc_ok`] reports the result. It
/// stays informational by default — telemetry is not *rejected* on it —
/// because the reference implementations disagree and firmware revisions may
/// differ. Callers who need the opposite trade-off can opt in with
/// [`parse_feedback_strict`] or [`Bus::with_strict_crc`](crate::Bus::with_strict_crc).
/// See `PROTOCOL.md` in the repository.
///
/// ```
/// use m0601::protocol::crc8_maxim;
/// assert_eq!(crc8_maxim(&[]), 0x00);
/// assert_eq!(crc8_maxim(&[0, 1, 2, 3, 4, 5, 6, 7, 8]), 0x83);
/// ```
/// Build a standard frame: `[id, cmd, data..., crc]`.
/// Velocity drive frame. `rpm` is clamped to [`RPM_MIN`]`..=`[`RPM_MAX`].
///
/// `accel` sets how steeply the motor ramps toward the setpoint: `1` is the
/// *fastest* ramp, larger values ramp more gently, and `0` selects the
/// motor's own default. Only that direction is documented here — the vendor
/// sources state a unit for this byte ("1 RPM per 0.1 ms") whose sense
/// contradicts the ramp direction every source agrees on, and it has not
/// been resolved against hardware. See `PROTOCOL.md`.
///
/// Only sustains motion while resent at ≥[`DRIVE_HZ_MIN`] Hz.
///
/// ```
/// use m0601::protocol::frame_velocity;
/// assert_eq!(
/// frame_velocity(0x01, 100, 1),
/// [0x01, 0x64, 0x00, 0x64, 0x00, 0x00, 0x01, 0x00, 0x00, 0xE4],
/// );
/// // Out-of-range values clamp: 500 RPM becomes 330.
/// assert_eq!(
/// frame_velocity(0x01, 500, 1),
/// [0x01, 0x64, 0x01, 0x4A, 0x00, 0x00, 0x01, 0x00, 0x00, 0x7C],
/// );
/// ```
/// Current drive frame. `value` is clamped to [`CUR_MIN`]`..=`[`CUR_MAX`]
/// (`±32767`, roughly −8 A..+8 A).
///
/// The range is symmetric, so [`i16::MIN`] (`-32768`) is *not* a valid
/// setpoint and clamps up to `-32767`.
///
/// ```
/// use m0601::protocol::frame_current;
/// assert_eq!(
/// frame_current(0x01, -1234),
/// [0x01, 0x64, 0xFB, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07],
/// );
/// // i16::MIN clamps to -32767 rather than going out on the wire as 0x8000.
/// assert_eq!(
/// frame_current(0x01, i16::MIN),
/// [0x01, 0x64, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0],
/// );
/// ```
/// Position drive frame. `raw` is clamped to `0..=`[`POS_MAX`]
/// (0°..360°).
///
/// ```
/// use m0601::protocol::frame_position;
/// assert_eq!(
/// frame_position(0x01, 32767),
/// [0x01, 0x64, 0x7F, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97],
/// );
/// ```
/// Electric-brake frame (velocity mode only): value 0 with [`BRAKE_BYTE`]
/// in the brake position.
///
/// ```
/// use m0601::protocol::frame_brake;
/// assert_eq!(
/// frame_brake(0x01),
/// [0x01, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xD1],
/// );
/// ```
/// Mode-switch frame. **The last byte is the mode value, not a CRC** — this
/// is the protocol's one deliberate deviation from the standard frame shape.
/// Must be sent five times ([`M0601::set_mode`](crate::M0601::set_mode)
/// does so).
///
/// ```
/// use m0601::{protocol::frame_mode, Mode};
/// assert_eq!(
/// frame_mode(0x01, Mode::Velocity),
/// [0x01, 0xA0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02],
/// );
/// ```
/// Feedback query frame: the addressed motor replies with telemetry.
///
/// ```
/// use m0601::protocol::frame_feedback;
/// assert_eq!(
/// frame_feedback(0x01),
/// [0x01, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04],
/// );
/// ```
/// Broadcast ID-query frame (fixed bytes `C8 64 00×7 DE`). Any motor on the
/// bus answers with a frame starting with its own ID.
/// Set-ID frame (`AA 55 53 <new_id> 00×6`, **no CRC**). Persistent; must be
/// sent five times with only one motor on the bus
/// ([`Bus::set_id`](crate::Bus::set_id) handles both).
///
/// Returns [`Error::InvalidId`] outside `0x01..=0xFE`.
/// Check that `id` is an assignable motor ID (`0x01..=0xFE`).
///
/// `0xC8` is accepted but best avoided: it is the destination byte of the
/// broadcast ID query ([`frame_id_query`]), so a motor assigned that ID
/// cannot be told apart from the query itself when a half-duplex adapter
/// echoes the transmission back.
/// Build a [`Frame`] from raw bytes: 9 bytes get a CRC-8/MAXIM appended,
/// 10 bytes pass through untouched (byte 9 is *not* recomputed, so this can
/// send deliberately corrupt frames).
///
/// Returns [`Error::InvalidFrameLen`] for any other length.
///
/// ```
/// use m0601::protocol::frame_from_bytes;
/// // 9 bytes: CRC appended.
/// assert_eq!(
/// frame_from_bytes(&[0x01, 0x74, 0, 0, 0, 0, 0, 0, 0])?[9],
/// 0x04,
/// );
/// // 10 bytes: byte 9 kept verbatim, even when wrong.
/// assert_eq!(
/// frame_from_bytes(&[0x01, 0x74, 0, 0, 0, 0, 0, 0, 0, 0xFF])?[9],
/// 0xFF,
/// );
/// assert!(frame_from_bytes(&[0x01, 0x74]).is_err());
/// # Ok::<(), m0601::Error>(())
/// ```
/// Full-scale torque current in amps, at [`CUR_MAX`] (and −[`CUR_MAX`]).
pub const CUR_FULL_SCALE_A: f32 = 8.0;
/// Raw current value → amps (`raw × 8 / 32767`).
///
/// ```
/// use m0601::protocol::raw_to_amps;
/// assert_eq!(raw_to_amps(0), 0.0);
/// assert!((raw_to_amps(32767) - 8.0).abs() < 1e-6);
/// assert!((raw_to_amps(-4096) + 1.0).abs() < 1e-3);
/// ```
/// Amps → raw current setpoint, clamped to [`CUR_MIN`]`..=`[`CUR_MAX`].
///
/// Inverts [`raw_to_amps`], rounding to nearest. Non-finite input maps to
/// `0` — `f32::clamp` propagates NaN rather than clamping it, so it is ruled
/// out here instead of relying on the `as` cast's NaN-to-zero rule.
///
/// ```
/// use m0601::protocol::{amps_to_raw, CUR_MAX, CUR_MIN};
/// assert_eq!(amps_to_raw(0.0), 0);
/// assert_eq!(amps_to_raw(8.0), CUR_MAX);
/// assert_eq!(amps_to_raw(1.0), 4096);
/// // Beyond the reachable range it saturates rather than wrapping.
/// assert_eq!(amps_to_raw(100.0), CUR_MAX);
/// assert_eq!(amps_to_raw(-100.0), CUR_MIN);
/// assert_eq!(amps_to_raw(f32::NAN), 0);
/// ```
/// 16-bit position → degrees (`raw × 360 / 32767`), as carried by a
/// [`ReplyKind::Drive`] reply.
///
/// ```
/// use m0601::protocol::raw_to_deg;
/// assert_eq!(raw_to_deg(0), 0.0);
/// assert_eq!(raw_to_deg(32767), 360.0);
/// ```
/// 8-bit position → degrees (`raw × 360 / 255`), as carried by a
/// [`ReplyKind::Query`] reply.
///
/// The divisor is **255**, not 256, so `0xFF` reads as a full 360° (i.e. 0°,
/// wrapped); every known implementation divides by 255.
///
/// ```
/// use m0601::protocol::raw8_to_deg;
/// assert_eq!(raw8_to_deg(0), 0.0);
/// assert_eq!(raw8_to_deg(255), 360.0);
/// ```
/// Degrees → raw position setpoint, clamped to `0..=`[`POS_MAX`].
///
/// Clamps rather than wrapping: an angle slightly past 360° should hold at
/// the top of the range, not snap round to 0° and drive a full revolution.
/// Non-finite input maps to `0`.
///
/// Rounds to nearest, so an angle read back from a drive reply round-trips
/// to exactly the value it came from — truncating instead can land one step
/// low and turn "hold this angle" into a command to move.
///
/// ```
/// use m0601::protocol::{deg_to_raw, raw_to_deg, POS_MAX};
/// assert_eq!(deg_to_raw(0.0), 0);
/// assert_eq!(deg_to_raw(180.0), 16_384);
/// assert_eq!(deg_to_raw(360.0), POS_MAX);
/// // Out-of-band and non-finite inputs clamp rather than wrap or trap.
/// assert_eq!(deg_to_raw(-90.0), 0);
/// assert_eq!(deg_to_raw(720.0), POS_MAX);
/// assert_eq!(deg_to_raw(f32::NAN), 0);
/// // Round-trips exactly.
/// assert_eq!(deg_to_raw(raw_to_deg(20_000)), 20_000);
/// ```
/// Degrees → raw 8-bit position, clamped to `0..=255`, as carried in byte 7
/// of a [`ReplyKind::Query`] reply.
///
/// The inverse of [`raw8_to_deg`] (divisor **255**, so 360° maps to `0xFF`).
/// Like [`deg_to_raw`] it clamps rather than wrapping, rounds to nearest, and
/// maps non-finite input to `0`. Mainly of use to a simulator or test
/// synthesizing a query reply with [`frame_query_reply`].
///
/// ```
/// use m0601::protocol::{deg_to_raw8, raw8_to_deg};
/// assert_eq!(deg_to_raw8(0.0), 0);
/// assert_eq!(deg_to_raw8(360.0), 255);
/// assert_eq!(deg_to_raw8(-1.0), 0); // clamps, not wraps
/// assert_eq!(deg_to_raw8(720.0), 255);
/// assert_eq!(deg_to_raw8(f32::NAN), 0);
/// // Round-trips a value that came from raw8_to_deg exactly.
/// assert_eq!(deg_to_raw8(raw8_to_deg(200)), 200);
/// ```
/// Which command elicited a telemetry reply — and therefore how its
/// bytes 6–7 must be decoded.
///
/// The motor answers with **two different reply layouts** (verified against
/// the DDT vendor sample and the DFRobot wiki — see `PROTOCOL.md`). Bytes
/// 0–5, 8 and 9 are identical in both; only bytes 6–7 differ:
///
/// | Kind | Elicited by | Byte 6 | Byte 7 |
/// |---------|-------------------------|-------------------|---------------|
/// | `Query` | `0x74` feedback query | winding temp (°C) | position u8 |
/// | `Drive` | `0x64` drive, broadcast | position u16 BE (high) | (low) |
///
/// A `Drive` reply carries **no temperature**, but its 16-bit position is
/// ~128× finer than the `Query` reply's single byte (~0.011° vs ~1.4°).
/// Parse a telemetry frame from raw reply bytes, decoding bytes 6–7
/// according to `kind` — see [`ReplyKind`] for why the caller must know
/// which command the reply answers.
///
/// Returns `None` when fewer than [`FRAME_LEN`] bytes are supplied; longer
/// input parses its first 10 bytes. Frames are validated by *length only*:
/// [`Feedback::crc_ok`] reports whether byte 9 matches a CRC-8/MAXIM
/// (genuine replies do carry one — verified on hardware), but this
/// function never rejects on it — see `PROTOCOL.md`.
///
/// Common layout: `[id, mode, current_i16_be, speed_i16_be, .., faults,
/// chk]` with current scaled ×8/32767 A. `Query` replies put temperature
/// (°C) in byte 6 and an 8-bit position (×360/255°) in byte 7; `Drive`
/// replies put a 16-bit position (×360/32767°) in bytes 6–7 and no
/// temperature.
///
/// The `Query` position uses ×360/**255**, so byte 7 = `0xFF` reads as a
/// full 360° (i.e. 0°, wrapped); every known implementation divides by 255,
/// not 256.
///
/// ```
/// use m0601::protocol::{parse_feedback, ReplyKind};
/// let raw = [0x01, 0x02, 0xF8, 0x30, 0x00, 0x64, 0x28, 0x80, 0x03, 0x00];
/// let q = parse_feedback(&raw, ReplyKind::Query).unwrap();
/// assert_eq!(q.speed_rpm, 100);
/// assert_eq!(q.temp_c, Some(40));
/// assert_eq!(q.faults.to_string(), "SensorErr | Overcurrent");
/// assert!(!q.crc_ok);
/// // The very same bytes decode differently as a drive reply: bytes 6–7
/// // are one 16-bit position (0x2880 = 10368 → ~113.9°), no temperature.
/// let d = parse_feedback(&raw, ReplyKind::Drive).unwrap();
/// assert_eq!(d.temp_c, None);
/// assert!((d.position_deg - 113.91).abs() < 0.01);
/// ```
/// Like [`parse_feedback`], but **rejects** a frame whose byte 9 does not
/// match its CRC-8/MAXIM: a decoded [`Feedback`] with `crc_ok == false`
/// becomes `None`.
///
/// This is the pure-function form of the bus's opt-in strict-CRC mode
/// ([`Bus::with_strict_crc`](crate::Bus::with_strict_crc) /
/// [`M0601::with_strict_crc`](crate::M0601::with_strict_crc)). The default
/// [`parse_feedback`] stays advisory — it returns the telemetry and leaves
/// the CRC verdict in [`Feedback::crc_ok`] for the caller to weigh — because
/// genuine replies from some firmware revisions have been seen to disagree on
/// the checksum. Reach for the strict form only where a corrupt frame is
/// worse than a dropped one, e.g. before feeding an odometry integrator.
///
/// ```
/// use m0601::protocol::{parse_feedback, parse_feedback_strict, ReplyKind};
/// // Byte 9 is deliberately wrong (a good CRC here is 0x00).
/// let bad = [0x01, 0x02, 0x00, 0x00, 0x00, 0x64, 0x28, 0x00, 0x00, 0xFF];
/// assert!(parse_feedback(&bad, ReplyKind::Query).is_some_and(|fb| !fb.crc_ok));
/// assert!(parse_feedback_strict(&bad, ReplyKind::Query).is_none());
/// ```
/// Encode a synthetic **query-layout** reply frame (the layout elicited by a
/// [`CMD_QUERY`] / `0x74` request): the encode-side inverse of
/// [`parse_feedback`] for [`ReplyKind::Query`].
///
/// The `0x74` names the *TX command* that selects this layout, not a byte in
/// the frame: byte 1 of the reply is the [`Mode`], never the command byte.
///
/// This is what a simulator or a test needs to stand in for a real motor —
/// building the ten telemetry bytes (with a correct CRC-8/MAXIM in byte 9) by
/// hand is error-prone, and [`Feedback`] is deliberately `#[non_exhaustive]`
/// so it cannot be constructed by struct literal. Build a reply frame here,
/// then feed it to [`parse_feedback`] or a
/// [`MockTransport`](crate::MockTransport) exactly as a real reply would flow.
///
/// `current_a` is quantised through [`amps_to_raw`] and `position_deg` through
/// [`deg_to_raw8`] (the coarse ~1.4° byte-7 resolution of a query reply), so a
/// round-trip back through [`parse_feedback`] returns those two within one
/// quantisation step; `id`, `mode`, `speed_rpm`, `temp_c` and `faults`
/// round-trip exactly. The resulting frame's [`Feedback::crc_ok`] is `true`.
///
/// ```
/// use m0601::protocol::{frame_query_reply, parse_feedback, ReplyKind};
/// use m0601::{Faults, Mode};
/// let frame = frame_query_reply(0x01, Mode::Velocity, 1.0, 100, 40, 180.0, Faults(0));
/// let fb = parse_feedback(&frame, ReplyKind::Query).unwrap();
/// assert_eq!(fb.id, 0x01);
/// assert_eq!(fb.mode, Some(Mode::Velocity));
/// assert_eq!(fb.speed_rpm, 100);
/// assert_eq!(fb.temp_c, Some(40));
/// assert!(fb.crc_ok);
/// assert!((fb.current_a - 1.0).abs() < 0.01);
/// assert!((fb.position_deg - 180.0).abs() < 1.5); // coarse 8-bit position
/// ```
/// Encode a synthetic **drive-layout** reply frame (the layout elicited by a
/// [`CMD_DRIVE`] / `0x64` frame or the broadcast ID query): the encode-side
/// inverse of [`parse_feedback`] for [`ReplyKind::Drive`].
///
/// As with [`frame_query_reply`], the `0x64` is the *TX command* that selects
/// the layout, not a byte in the reply — byte 1 is the [`Mode`].
///
/// The counterpart to [`frame_query_reply`] for the drive-reply layout —
/// bytes 6–7 hold a hi-res 16-bit position (~0.011°) and there is no
/// temperature. See [`frame_query_reply`] for why this exists and how it round-
/// trips; here `position_deg` goes through the finer [`deg_to_raw`], so it
/// returns from [`parse_feedback`] within ~0.011°.
///
/// ```
/// use m0601::protocol::{frame_drive_reply, parse_feedback, ReplyKind};
/// use m0601::{Faults, Mode};
/// let frame = frame_drive_reply(0x02, Mode::Velocity, -2.0, -50, 113.9, Faults(Faults::STALL));
/// let fb = parse_feedback(&frame, ReplyKind::Drive).unwrap();
/// assert_eq!(fb.id, 0x02);
/// assert_eq!(fb.speed_rpm, -50);
/// assert_eq!(fb.temp_c, None); // drive replies carry no temperature
/// assert!(fb.faults.stall());
/// assert!(fb.crc_ok);
/// assert!((fb.current_a + 2.0).abs() < 0.01);
/// assert!((fb.position_deg - 113.9).abs() < 0.02);
/// ```
/// Assemble a reply frame from its already-encoded parts and seal it with a
/// correct CRC-8/MAXIM. Bytes 6–7 (`bytes_6_7`) are the one part that differs
/// between the two reply layouts.
/// Strip a leading half-duplex TX echo from a raw reply.
///
/// Some RS485 adapters loop the host's own transmission back, so a reply can
/// arrive as `<tx frame><telemetry>`. An exact `tx` prefix is always an echo
/// (a genuine reply can never byte-equal the frame that elicited it — its
/// byte 1 is a mode value, not the command), so it is removed unconditionally.
/// A *partial* echo cannot be matched and passes through untouched — that
/// misaligned case is what [`frames`] rejects; see its docs for why that
/// matters more than it looks.
pub
/// Strip a leading half-duplex TX echo ([`strip_echo`]) and split what remains
/// into whole frames. Returns `None` unless that is a non-empty exact multiple
/// of [`FRAME_LEN`].
///
/// # Why the length must divide evenly
///
/// [`strip_echo`] is all-or-nothing: if the echo is short by even one byte it
/// is not recognised, and offset 0 is then no longer a frame boundary. Parsing
/// from there anyway yields a frame *straddling* the tail of the echo and the
/// head of the real reply — and that garbage is not obviously garbage. It looks
/// like telemetry, it passes the per-motor ID check (a truncated echo begins
/// with the addressed motor's own ID, exactly as a genuine reply does), and it
/// decodes to plausible values. Measured across every cut point, a wheel
/// turning at 300 RPM read back as 0, 1, 258 or 512 RPM — and for seven of the
/// nine cuts that is under the `< 10 RPM` guard callers rely on before entering
/// position mode, which is the one place a wrong speed reading is actively
/// dangerous.
///
/// A well-formed transaction is always a whole number of frames — the reply
/// alone, or the echo plus the reply — so anything else means the stream is
/// misaligned and none of it can be trusted. Rejecting on that costs at most
/// one dropped reading, which every caller already tolerates.
pub