12#ifndef TMC9660_FRAMES_HPP
13#define TMC9660_FRAMES_HPP
23namespace tmc9660::detail {
26[[nodiscard]]
constexpr uint8_t ReverseBits(uint8_t value)
noexcept {
29 for (uint32_t i = 0U; i < 8U; ++i) {
30 out = (out << 1U) | (in & 1U);
33 return static_cast<uint8_t
>(out);
41[[nodiscard]]
constexpr uint8_t Crc8(std::span<const uint8_t> data,
bool reflect_output)
noexcept {
43 for (
const uint8_t
byte : data) {
44 crc ^= ReverseBits(
byte);
45 for (uint32_t bit = 0U; bit < 8U; ++bit) {
46 if ((crc & 0x80U) != 0U) {
47 crc = ((crc << 1U) ^ 0x07U) & 0xFFU;
49 crc = (crc << 1U) & 0xFFU;
53 const auto result =
static_cast<uint8_t
>(crc);
54 return reflect_output ? ReverseBits(result) : result;
59[[nodiscard]]
constexpr uint32_t Crc32(std::span<const uint8_t> data)
noexcept {
60 uint32_t crc = 0xFFFF'FFFFU;
61 for (
const uint8_t
byte : data) {
63 for (uint32_t bit = 0U; bit < 8U; ++bit) {
64 if ((crc & 1U) != 0U) {
65 crc = (crc >> 1U) ^ 0xEDB8'8320U;
75[[nodiscard]]
constexpr uint8_t Sum8(std::span<const uint8_t> data)
noexcept {
77 for (
const uint8_t
byte : data) {
78 sum = (sum + byte) & 0xFFU;
80 return static_cast<uint8_t
>(sum);
84constexpr void PutBe32(std::span<uint8_t> out, uint32_t value)
noexcept {
85 out[0] =
static_cast<uint8_t
>(value >> 24U);
86 out[1] =
static_cast<uint8_t
>(value >> 16U);
87 out[2] =
static_cast<uint8_t
>(value >> 8U);
88 out[3] =
static_cast<uint8_t
>(value);
92[[nodiscard]]
constexpr uint32_t GetBe32(std::span<const uint8_t> in)
noexcept {
93 return (
static_cast<uint32_t
>(in[0]) << 24U) | (
static_cast<uint32_t
>(in[1]) << 16U) |
94 (
static_cast<uint32_t
>(in[2]) << 8U) |
static_cast<uint32_t
>(in[3]);
102inline constexpr std::size_t kBlUartLen = 8;
104inline constexpr std::size_t kBlSpiLen = 5;
106inline constexpr uint8_t kBlSync = 0x55;
115[[nodiscard]]
constexpr std::array<uint8_t, kBlUartLen> BlUartRequest(uint8_t device,
118 bool crc_reflect)
noexcept {
119 std::array<uint8_t, kBlUartLen> f{};
123 PutBe32(std::span<uint8_t>{f}.subspan(3U, 4U), value);
124 f[7] = Crc8(std::span<const uint8_t>{f}.first(7U), crc_reflect);
130[[nodiscard]]
constexpr bool BlUartDecode(std::span<const uint8_t, kBlUartLen> f, uint8_t host,
131 uint8_t device,
bool crc_reflect, BlReply& out)
noexcept {
132 if (Crc8(f.first(7U), crc_reflect) != f[7]) {
135 if ((f[0] != host) || (f[1] != device)) {
139 out.value = GetBe32(f.subspan(3U, 4U));
144[[nodiscard]]
constexpr std::array<uint8_t, kBlSpiLen> BlSpiRequest(uint8_t command,
145 uint32_t value)
noexcept {
146 std::array<uint8_t, kBlSpiLen> f{};
148 PutBe32(std::span<uint8_t>{f}.subspan(1U, 4U), value);
153[[nodiscard]]
constexpr BlReply BlSpiDecode(std::span<const uint8_t, kBlSpiLen> f)
noexcept {
154 return BlReply{.status = f[0], .value = GetBe32(f.subspan(1U, 4U))};
162inline constexpr std::size_t kTmclSpiLen = 8;
164inline constexpr std::size_t kTmclUartLen = 9;
166inline constexpr uint8_t kTmclReplyRequestOp = 0xFF;
178 uint8_t spi_status{0};
188[[nodiscard]]
constexpr std::array<uint8_t, 2> TmclTypeMotorBytes(
const TmclCommand& c,
190 const uint32_t type =
static_cast<uint32_t
>(c.type) & 0x0FFFU;
191 if (c.motor > 0x0FU) {
192 return {
static_cast<uint8_t
>(type & 0xFFU), c.motor};
194 const uint32_t motor =
static_cast<uint32_t
>(c.motor) & 0x0FU;
195 const uint32_t type_hi = (type >> 8U) & 0x0FU;
198 return {
static_cast<uint8_t
>(type & 0xFFU),
static_cast<uint8_t
>((type_hi << 4U) | motor)};
204 return {
static_cast<uint8_t
>(type & 0xFFU),
static_cast<uint8_t
>((motor << 4U) | type_hi)};
208[[nodiscard]]
constexpr std::array<uint8_t, kTmclSpiLen> TmclSpiRequest(
209 const TmclCommand& c,
TmclLayout layout)
noexcept {
210 std::array<uint8_t, kTmclSpiLen> f{};
211 const std::array<uint8_t, 2> tm = TmclTypeMotorBytes(c, layout);
215 PutBe32(std::span<uint8_t>{f}.subspan(3U, 4U), c.value);
216 f[7] = Sum8(std::span<const uint8_t>{f}.first(7U));
222[[nodiscard]]
constexpr std::array<uint8_t, kTmclSpiLen> TmclSpiReplyRequest() noexcept {
223 std::array<uint8_t, kTmclSpiLen> f{};
224 f[0] = kTmclReplyRequestOp;
225 f[7] = kTmclReplyRequestOp;
231[[nodiscard]]
constexpr bool TmclSpiDecode(std::span<const uint8_t, kTmclSpiLen> f,
232 TmclReply& out)
noexcept {
233 if (Sum8(f.first(7U)) != f[7]) {
236 out.spi_status = f[0];
239 out.value = GetBe32(f.subspan(3U, 4U));
245[[nodiscard]]
constexpr std::array<uint8_t, kTmclUartLen> TmclUartRequest(
246 uint8_t module_address,
const TmclCommand& c,
TmclLayout layout)
noexcept {
247 std::array<uint8_t, kTmclUartLen> f{};
248 const std::array<uint8_t, 2> tm = TmclTypeMotorBytes(c, layout);
249 f[0] =
static_cast<uint8_t
>(
static_cast<uint32_t
>(module_address) | 1U);
253 PutBe32(std::span<uint8_t>{f}.subspan(4U, 4U), c.value);
254 f[8] = Sum8(std::span<const uint8_t>{f}.first(8U));
259[[nodiscard]]
constexpr bool TmclUartDecode(std::span<const uint8_t, kTmclUartLen> f, uint8_t host,
260 uint8_t module_address, TmclReply& out)
noexcept {
261 if (Sum8(f.first(8U)) != f[8]) {
264 if ((f[0] != host) ||
265 (f[1] !=
static_cast<uint8_t
>(
static_cast<uint32_t
>(module_address) | 1U))) {
271 out.value = GetBe32(f.subspan(4U, 4U));
280inline constexpr std::size_t kRegSpiLen = 6;
282inline constexpr std::size_t kRegUartLen = 9;
284inline constexpr uint8_t kRegUartWrite = 146;
286inline constexpr uint8_t kRegUartRead = 148;
288inline constexpr uint8_t kRegUartRamDebug = 142;
301[[nodiscard]]
constexpr bool RegSpiRequest(uint8_t block, uint16_t offset,
bool write,
302 uint32_t data,
bool wide,
303 std::array<uint8_t, kRegSpiLen>& out)
noexcept {
304 const uint32_t offset_bits = wide ? 10U : 9U;
305 if ((
static_cast<uint32_t
>(offset) >> offset_bits) != 0U) {
308 const uint32_t block_bits = wide ? 5U : 6U;
309 if ((
static_cast<uint32_t
>(block) >> block_bits) != 0U) {
314 high = (
static_cast<uint32_t
>(block) << 11U) | (write ? (1U << 10U) : 0U) | offset;
316 high = (
static_cast<uint32_t
>(block) << 10U) | (write ? (1U << 9U) : 0U) | offset;
318 out[0] =
static_cast<uint8_t
>(high >> 8U);
319 out[1] =
static_cast<uint8_t
>(high);
320 PutBe32(std::span<uint8_t>{out}.subspan(2U, 4U), data);
325[[nodiscard]]
constexpr RegSpiReply RegSpiDecode(std::span<const uint8_t, kRegSpiLen> f)
noexcept {
327 r.boot = (f[0] & 0x80U) != 0U;
328 r.counter =
static_cast<uint8_t
>((
static_cast<uint32_t
>(f[0]) >> 4U) & 0x07U);
329 r.ok = (f[1] & 0x01U) != 0U;
330 r.value = GetBe32(f.subspan(2U, 4U));
336 uint8_t module_address{0};
341 bool msb_fields{
false};
348[[nodiscard]]
constexpr bool RegUartRequest(
const RegUartArgs& args,
349 std::array<uint8_t, kRegUartLen>& out)
noexcept {
350 if (((
static_cast<uint32_t
>(args.offset) >> 11U) != 0U) ||
351 ((
static_cast<uint32_t
>(args.block) >> 5U) != 0U)) {
354 const uint32_t offset_high = (
static_cast<uint32_t
>(args.offset) >> 8U) & 0x07U;
355 out[0] =
static_cast<uint8_t
>(
static_cast<uint32_t
>(args.module_address) | 1U);
356 out[1] = args.command;
357 out[2] =
static_cast<uint8_t
>(args.offset & 0xFFU);
358 out[3] = args.msb_fields
359 ?
static_cast<uint8_t
>((offset_high << 5U) | args.block)
360 :
static_cast<uint8_t
>((
static_cast<uint32_t
>(args.block) << 3U) | offset_high);
361 PutBe32(std::span<uint8_t>{out}.subspan(4U, 4U), args.data);
362 out[8] = Sum8(std::span<const uint8_t>{out}.first(8U));
371inline constexpr std::array<uint8_t, 7> kRequestExample{0x55, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01};
372inline constexpr std::array<uint8_t, 7> kReplyExample{0xFF, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00};
373static_assert(Crc8(kRequestExample,
false) ==
static_cast<uint8_t
>(0x1D));
374static_assert(Crc8(kReplyExample,
false) ==
static_cast<uint8_t
>(0xA3));
375static_assert(BlUartRequest(
static_cast<uint8_t
>(1),
static_cast<uint8_t
>(0),
376 static_cast<uint32_t
>(1),
false)[7] ==
static_cast<uint8_t
>(0x1D));
377static_assert(Sum8(std::array<uint8_t, 2>{0xFF, 0x02}) ==
static_cast<uint8_t
>(0x01));
378static_assert((TmclSpiReplyRequest()[0] ==
static_cast<uint8_t
>(0xFF)) &&
379 (TmclSpiReplyRequest()[7] ==
static_cast<uint8_t
>(0xFF)));
TmclLayout
Where the type and the motor or bank bytes of a TMCL frame put their bits (ambiguity A2).
Definition tmc9660_types.hpp:428
@ Auto
Probe both layouts; start only when exactly one answers.
@ MotorHigh
Byte 3 (SPI byte 2): motor << 4 | type[11:8]; the byte before holds type[7:0].
@ TypeHigh
Byte 3 (SPI byte 2): type[11:8] << 4 | motor; the byte before holds type[7:0].
Compile-time helpers of the TMC9660 driver: byte copy, exact hook matching, typed register fields and...
Public types of the TMC9660 driver: errors, Result<T>, the protocol enumerations, Config and its mode...