TMC9660 Driver 0.1.0
API reference
Loading...
Searching...
No Matches
tmc9660_frames.hpp
Go to the documentation of this file.
1
12#ifndef TMC9660_FRAMES_HPP
13#define TMC9660_FRAMES_HPP
14
15#include <array>
16#include <cstddef>
17#include <cstdint>
18#include <span>
19
20#include "tmc9660_detail.hpp"
21#include "tmc9660_types.hpp"
22
23namespace tmc9660::detail {
24
26[[nodiscard]] constexpr uint8_t ReverseBits(uint8_t value) noexcept {
27 uint32_t in = value;
28 uint32_t out = 0U;
29 for (uint32_t i = 0U; i < 8U; ++i) {
30 out = (out << 1U) | (in & 1U);
31 in >>= 1U;
32 }
33 return static_cast<uint8_t>(out);
34}
35
41[[nodiscard]] constexpr uint8_t Crc8(std::span<const uint8_t> data, bool reflect_output) noexcept {
42 uint32_t crc = 0U;
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;
48 } else {
49 crc = (crc << 1U) & 0xFFU;
50 }
51 }
52 }
53 const auto result = static_cast<uint8_t>(crc);
54 return reflect_output ? ReverseBits(result) : result;
55}
56
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) {
62 crc ^= byte;
63 for (uint32_t bit = 0U; bit < 8U; ++bit) {
64 if ((crc & 1U) != 0U) {
65 crc = (crc >> 1U) ^ 0xEDB8'8320U;
66 } else {
67 crc >>= 1U;
68 }
69 }
70 }
71 return ~crc;
72}
73
75[[nodiscard]] constexpr uint8_t Sum8(std::span<const uint8_t> data) noexcept {
76 uint32_t sum = 0U;
77 for (const uint8_t byte : data) {
78 sum = (sum + byte) & 0xFFU;
79 }
80 return static_cast<uint8_t>(sum);
81}
82
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);
89}
90
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]);
95}
96
97// -------------------------------------------------------------------------------------------------
98// Bootloader
99// -------------------------------------------------------------------------------------------------
100
102inline constexpr std::size_t kBlUartLen = 8;
104inline constexpr std::size_t kBlSpiLen = 5;
106inline constexpr uint8_t kBlSync = 0x55;
107
109struct BlReply {
110 uint8_t status{0};
111 uint32_t value{0};
112};
113
115[[nodiscard]] constexpr std::array<uint8_t, kBlUartLen> BlUartRequest(uint8_t device,
116 uint8_t command,
117 uint32_t value,
118 bool crc_reflect) noexcept {
119 std::array<uint8_t, kBlUartLen> f{};
120 f[0] = kBlSync;
121 f[1] = device;
122 f[2] = command;
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);
125 return f;
126}
127
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]) {
133 return false;
134 }
135 if ((f[0] != host) || (f[1] != device)) {
136 return false;
137 }
138 out.status = f[2];
139 out.value = GetBe32(f.subspan(3U, 4U));
140 return true;
141}
142
144[[nodiscard]] constexpr std::array<uint8_t, kBlSpiLen> BlSpiRequest(uint8_t command,
145 uint32_t value) noexcept {
146 std::array<uint8_t, kBlSpiLen> f{};
147 f[0] = command;
148 PutBe32(std::span<uint8_t>{f}.subspan(1U, 4U), value);
149 return f;
150}
151
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))};
155}
156
157// -------------------------------------------------------------------------------------------------
158// Parameter mode (TMCL)
159// -------------------------------------------------------------------------------------------------
160
162inline constexpr std::size_t kTmclSpiLen = 8;
164inline constexpr std::size_t kTmclUartLen = 9;
166inline constexpr uint8_t kTmclReplyRequestOp = 0xFF;
167
169struct TmclCommand {
170 uint8_t op{0};
171 uint16_t type{0};
172 uint8_t motor{0};
173 uint32_t value{0};
174};
175
177struct TmclReply {
178 uint8_t spi_status{0};
179 uint8_t status{0};
180 uint8_t op{0};
181 uint32_t value{0};
182};
183
188[[nodiscard]] constexpr std::array<uint8_t, 2> TmclTypeMotorBytes(const TmclCommand& c,
189 TmclLayout layout) noexcept {
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};
193 }
194 const uint32_t motor = static_cast<uint32_t>(c.motor) & 0x0FU;
195 const uint32_t type_hi = (type >> 8U) & 0x0FU;
196 switch (layout) {
198 return {static_cast<uint8_t>(type & 0xFFU), static_cast<uint8_t>((type_hi << 4U) | motor)};
199 case TmclLayout::Auto:
201 default:
202 break;
203 }
204 return {static_cast<uint8_t>(type & 0xFFU), static_cast<uint8_t>((motor << 4U) | type_hi)};
205}
206
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);
212 f[0] = c.op;
213 f[1] = tm[0];
214 f[2] = tm[1];
215 PutBe32(std::span<uint8_t>{f}.subspan(3U, 4U), c.value);
216 f[7] = Sum8(std::span<const uint8_t>{f}.first(7U));
217 return f;
218}
219
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;
226 return f;
227}
228
231[[nodiscard]] constexpr bool TmclSpiDecode(std::span<const uint8_t, kTmclSpiLen> f,
232 TmclReply& out) noexcept {
233 if (Sum8(f.first(7U)) != f[7]) {
234 return false;
235 }
236 out.spi_status = f[0];
237 out.status = f[1];
238 out.op = f[2];
239 out.value = GetBe32(f.subspan(3U, 4U));
240 return true;
241}
242
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);
250 f[1] = c.op;
251 f[2] = tm[0];
252 f[3] = tm[1];
253 PutBe32(std::span<uint8_t>{f}.subspan(4U, 4U), c.value);
254 f[8] = Sum8(std::span<const uint8_t>{f}.first(8U));
255 return f;
256}
257
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]) {
262 return false;
263 }
264 if ((f[0] != host) ||
265 (f[1] != static_cast<uint8_t>(static_cast<uint32_t>(module_address) | 1U))) {
266 return false;
267 }
268 out.spi_status = 0U;
269 out.status = f[2];
270 out.op = f[3];
271 out.value = GetBe32(f.subspan(4U, 4U));
272 return true;
273}
274
275// -------------------------------------------------------------------------------------------------
276// Register mode
277// -------------------------------------------------------------------------------------------------
278
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;
289
291struct RegSpiReply {
292 bool boot{false};
293 uint8_t counter{0};
294 bool ok{false};
295 uint32_t value{0};
296};
297
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) {
306 return false;
307 }
308 const uint32_t block_bits = wide ? 5U : 6U;
309 if ((static_cast<uint32_t>(block) >> block_bits) != 0U) {
310 return false;
311 }
312 uint32_t high = 0U; // bits 47..32
313 if (wide) {
314 high = (static_cast<uint32_t>(block) << 11U) | (write ? (1U << 10U) : 0U) | offset;
315 } else {
316 high = (static_cast<uint32_t>(block) << 10U) | (write ? (1U << 9U) : 0U) | offset;
317 }
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);
321 return true;
322}
323
325[[nodiscard]] constexpr RegSpiReply RegSpiDecode(std::span<const uint8_t, kRegSpiLen> f) noexcept {
326 RegSpiReply r{};
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));
331 return r;
332}
333
335struct RegUartArgs {
336 uint8_t module_address{0};
337 uint8_t command{0};
338 uint8_t block{0};
339 uint16_t offset{0};
340 uint32_t data{0};
341 bool msb_fields{false};
342};
343
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)) {
352 return false;
353 }
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));
363 return true;
364}
365
366// -------------------------------------------------------------------------------------------------
367// Proofs against the datasheet's worked examples
368// -------------------------------------------------------------------------------------------------
369
370namespace proof {
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)));
380} // namespace proof
381
382} // namespace tmc9660::detail
383
384#endif // TMC9660_FRAMES_HPP
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...