52static constexpr uint8_t
tmclChecksum(
const uint8_t *bytes,
size_t n)
noexcept {
54 for (
size_t i = 0; i < n; ++i)
70 [[nodiscard]]
bool isOK() const noexcept {
78 r.spiStatus =
static_cast<SPIStatus>(in[0]);
83 r.value = (
static_cast<uint32_t
>(in[3]) << 24) | (
static_cast<uint32_t
>(in[4]) << 16) |
84 (
static_cast<uint32_t
>(in[5]) << 8) |
static_cast<uint32_t
>(in[6]);
92 if ((in[1] & 0x7F) != (addr & 0x7F))
99 r.value = (
static_cast<uint32_t
>(in[4]) << 24) | (
static_cast<uint32_t
>(in[5]) << 16) |
100 (
static_cast<uint32_t
>(in[6]) << 8) |
static_cast<uint32_t
>(in[7]);
120 void toSpi(std::span<uint8_t, 8> out)
const noexcept {
122 out[1] =
static_cast<uint8_t
>(
type >> 4);
123 out[2] =
static_cast<uint8_t
>((
type << 4) | (
motor & 0x0F));
124 out[3] =
static_cast<uint8_t
>(
value >> 24);
125 out[4] =
static_cast<uint8_t
>(
value >> 16);
126 out[5] =
static_cast<uint8_t
>(
value >> 8);
127 out[6] =
static_cast<uint8_t
>(
value);
136 void toUart(uint8_t addr, std::span<uint8_t, 9> out)
const noexcept {
137 out[0] = addr | 0x80;
139 out[2] =
static_cast<uint8_t
>(
type >> 4);
140 out[3] =
static_cast<uint8_t
>((
type << 4) | (
motor & 0x0F));
141 out[4] =
static_cast<uint8_t
>(
value >> 24);
142 out[5] =
static_cast<uint8_t
>(
value >> 16);
143 out[6] =
static_cast<uint8_t
>(
value >> 8);
144 out[7] =
static_cast<uint8_t
>(
value);
156 f.
type =
static_cast<uint16_t
>(in[1]) << 4 | (in[2] >> 4);
157 f.
motor = in[2] & 0x0F;
158 f.
value = (
static_cast<uint32_t
>(in[3]) << 24) | (
static_cast<uint32_t
>(in[4]) << 16) |
159 (
static_cast<uint32_t
>(in[5]) << 8) |
static_cast<uint32_t
>(in[6]);
173 outFrame.
opcode = reply.opcode;
174 outFrame.value = reply.value;
185 static bool fromUart(std::span<const uint8_t, 9> in, uint8_t expectedAddr,
190 outFrame.
opcode = rep.opcode;
191 outFrame.value = rep.value;
203 for (
size_t i = 0; i < n; ++i)
259 virtual bool spiTransfer(std::array<uint8_t, 8> &tx, std::array<uint8_t, 8> &rx)
noexcept = 0;
262 std::array<uint8_t, 8> txBuf, rxBuf;
264 if (!spiTransfer(txBuf, rxBuf))
297 std::array<uint8_t, 9> frame;
298 tx.toUart(address, frame);
299 if (!sendUartDatagram(frame))
301 if (!receiveUartDatagram(frame))
CommMode
Supported physical communication modes.
Definition TMC9660CommInterface.hpp:41
SPIStatus
SPI status codes as per TMC9660 Parameter Mode.
Definition TMC9660CommInterface.hpp:44
static constexpr uint8_t tmclChecksum(const uint8_t *bytes, size_t n) noexcept
Calculate 8-bit checksum (sum of bytes)
Definition TMC9660CommInterface.hpp:52
SPI implementation of TMC9660CommInterface.
Definition TMC9660CommInterface.hpp:248
bool transfer(const TMCLFrame &tx, TMCLReply &reply, uint8_t) noexcept override
Perform a full duplex TMCL transfer.
Definition TMC9660CommInterface.hpp:261
CommMode mode() const noexcept override
Return underlying communication mode.
Definition TMC9660CommInterface.hpp:250
virtual bool spiTransfer(std::array< uint8_t, 8 > &tx, std::array< uint8_t, 8 > &rx) noexcept=0
Low-level SPI transfer of 8 bytes.
SPI status codes as per TMC9660 Parameter Mode.
Definition TMC9660CommInterface.hpp:224
virtual CommMode mode() const noexcept=0
Return underlying communication mode.
virtual ~TMC9660CommInterface() noexcept=default
virtual bool transfer(const TMCLFrame &tx, TMCLReply &reply, uint8_t address) noexcept=0
Perform a full duplex TMCL transfer.
UART implementation of TMC9660CommInterface.
Definition TMC9660CommInterface.hpp:277
virtual bool receiveUartDatagram(std::array< uint8_t, 9 > &data) noexcept=0
Receive raw 9-byte UART TMCL datagram.
bool transfer(const TMCLFrame &tx, TMCLReply &reply, uint8_t address) noexcept override
Perform a full duplex TMCL transfer.
Definition TMC9660CommInterface.hpp:296
CommMode mode() const noexcept override
Return underlying communication mode.
Definition TMC9660CommInterface.hpp:279
virtual bool sendUartDatagram(const std::array< uint8_t, 9 > &data) noexcept=0
Send raw 9-byte UART TMCL datagram.
Frame structure for TMCL commands and replies.
Definition TMC9660CommInterface.hpp:110
void toUart(uint8_t addr, std::span< uint8_t, 9 > out) const noexcept
Serialize frame into 9-byte UART buffer, including sync bit and checksum.
Definition TMC9660CommInterface.hpp:136
uint16_t type
Parameter or command type (BYTE 1-2, bits 8-19).
Definition TMC9660CommInterface.hpp:112
void toSpi(std::span< uint8_t, 8 > out) const noexcept
Serialize frame into 8-byte SPI buffer.
Definition TMC9660CommInterface.hpp:120
static TMCLFrame fromSpi(std::span< const uint8_t, 8 > in) noexcept
Deserialize an SPI buffer into a TMCLFrame without status check.
Definition TMC9660CommInterface.hpp:153
uint8_t opcode
Operation code field (BYTE 0, bits 0-7).
Definition TMC9660CommInterface.hpp:111
uint32_t value
32-bit data value (BYTE 4-7, bits 24-55).
Definition TMC9660CommInterface.hpp:114
static constexpr uint8_t calculateChecksum(const uint8_t *bytes, size_t n) noexcept
Calculate 8-bit checksum (sum of bytes).
Definition TMC9660CommInterface.hpp:201
static bool fromSpiChecked(std::span< const uint8_t, 8 > in, TMCLFrame &outFrame) noexcept
Deserialize an SPI buffer with status and checksum validation.
Definition TMC9660CommInterface.hpp:169
static bool fromUart(std::span< const uint8_t, 9 > in, uint8_t expectedAddr, TMCLFrame &outFrame) noexcept
Deserialize a UART buffer with address and checksum validation.
Definition TMC9660CommInterface.hpp:185
uint8_t motor
Motor or bank identifier (BYTE 3, bits 20-23).
Definition TMC9660CommInterface.hpp:113
Reply structure returned by sendCommand()
Definition TMC9660CommInterface.hpp:64
SPIStatus spiStatus
SPI status byte.
Definition TMC9660CommInterface.hpp:65
uint8_t opcode
Echoed operation code.
Definition TMC9660CommInterface.hpp:67
uint32_t value
Optional returned value.
Definition TMC9660CommInterface.hpp:68
uint8_t status
TMCL status code (100=OK, 101=LOADED)
Definition TMC9660CommInterface.hpp:66
bool isOK() const noexcept
Definition TMC9660CommInterface.hpp:70
static bool fromSpi(std::span< const uint8_t, 8 > in, TMCLReply &r) noexcept
Decode reply from SPI datagram.
Definition TMC9660CommInterface.hpp:75
static bool fromUart(std::span< const uint8_t, 9 > in, uint8_t addr, TMCLReply &r) noexcept
Decode reply from UART datagram.
Definition TMC9660CommInterface.hpp:89