AS5047U Driver 0.1.0
API reference
Loading...
Searching...
No Matches
as5047u_spi_interface.hpp
Go to the documentation of this file.
1// clang-format off: the hook table below must keep one row per line.
24// clang-format on
25#ifndef AS5047U_SPI_INTERFACE_HPP
26#define AS5047U_SPI_INTERFACE_HPP
27
28#include <array>
29#include <concepts>
30#include <cstddef>
31#include <cstdint>
32
33#include "as5047u_detail.hpp"
34#include "as5047u_registers.hpp"
35#include "as5047u_types.hpp"
36
37namespace as5047u {
38
47inline constexpr uint8_t kSpiMode = 1;
49inline constexpr bool kMsbFirst = true;
51inline constexpr uint32_t kMaxClockHz = 10'000'000;
54inline constexpr uint32_t kMinCsHighNs = 350;
56inline constexpr uint32_t kCsSetupNs = 350;
58inline constexpr uint32_t kCsHoldNs = 50;
61inline constexpr bool kContinuousWord = false;
63inline constexpr std::array<uint8_t, 3> kFrameBits = {16, 24, 32};
76template <typename Derived>
78public:
80 SpiInterface(const SpiInterface&) = delete;
83
84protected:
86 SpiInterface() noexcept = default;
88 ~SpiInterface() noexcept = default;
89};
90
102template <typename Port>
103concept SpiPort = std::derived_from<Port, SpiInterface<Port>> && requires {
104 detail::ExactHook<Port, bool() noexcept>::Take(&Port::EnsureInitialized);
105 detail::ExactHook<Port, bool(const uint8_t*, uint8_t*, std::size_t) noexcept>::Take(
106 &Port::Transfer);
107};
108
109namespace detail {
110
113constexpr uint32_t Crc8Step(uint32_t crc, uint32_t byte) noexcept {
114 uint32_t c = (crc ^ byte) & 0xFFU;
115 for (uint32_t bit = 0U; bit < 8U; ++bit) {
116 c = ((c & 0x80U) != 0U) ? (((c << 1U) ^ reg::kCrcPolynomial) & 0xFFU) : ((c << 1U) & 0xFFU);
117 }
118 return c;
119}
120
122constexpr uint8_t Crc8Bitwise(uint16_t word) noexcept {
123 const uint32_t high =
124 Crc8Step(static_cast<uint32_t>(reg::kCrcInit), static_cast<uint32_t>(word) >> 8U);
125 const uint32_t low = Crc8Step(high, static_cast<uint32_t>(word) & 0xFFU);
126 return static_cast<uint8_t>(low ^ reg::kCrcFinalXor);
127}
128
130constexpr std::array<uint8_t, 256> MakeCrc8Table() noexcept {
131 std::array<uint8_t, 256> table{};
132 for (uint32_t i = 0U; i < 256U; ++i) {
133 table[i] = static_cast<uint8_t>(Crc8Step(0_u32, i));
134 }
135 return table;
136}
137
140inline constexpr std::array<uint8_t, 256> kCrc8Table = MakeCrc8Table();
141
142} // namespace detail
143
151constexpr uint8_t Crc8(uint16_t word) noexcept {
152 const auto high = static_cast<uint32_t>(detail::kCrc8Table[static_cast<std::size_t>(
153 (reg::kCrcInit ^ (static_cast<uint32_t>(word) >> 8U)) & 0xFFU)]);
154 const auto low = static_cast<uint32_t>(
155 detail::kCrc8Table[static_cast<std::size_t>((high ^ static_cast<uint32_t>(word)) & 0xFFU)]);
156 return static_cast<uint8_t>(low ^ reg::kCrcFinalXor);
157}
158
159namespace detail {
160
161static_assert((Crc8(0x0000_u16) == 0xF1U) && (Crc8(0xFFFF_u16) == 0x74U),
162 "CRC-8 of the line patterns");
163static_assert((Crc8(0x3FFF_u16) == Crc8Bitwise(0x3FFF_u16)) &&
164 (Crc8(0x4001_u16) == Crc8Bitwise(0x4001_u16)),
165 "the table agrees with the bitwise CRC");
166
168inline constexpr std::size_t kMaxFrameBytes = 4U;
169
171constexpr std::size_t FrameBytes(FrameFormat format) noexcept {
172 switch (format) {
173 case FrameFormat::Bits16:
174 return 2U;
175 case FrameFormat::Bits24:
176 return 3U;
177 case FrameFormat::Bits32:
178 return 4U;
179 default:
180 break;
181 }
182 return 3U;
183}
184static_assert(((FrameBytes(FrameFormat::Bits16) * 8U) == kFrameBits[0]) &&
185 ((FrameBytes(FrameFormat::Bits24) * 8U) == kFrameBits[1]) &&
186 ((FrameBytes(FrameFormat::Bits32) * 8U) == kFrameBits[2]));
187
191constexpr bool IsLinePattern(uint16_t reply, uint8_t crc) noexcept {
192 return ((reply == 0x0000U) && (crc == 0x00U)) || ((reply == 0xFFFFU) && (crc == 0xFFU));
193}
194static_assert(IsLinePattern(0x0000_u16, 0x00_u8) && IsLinePattern(0xFFFF_u16, 0xFF_u8) &&
195 !IsLinePattern(0x0000_u16, 0xFF_u8) && !IsLinePattern(0x4000_u16, 0x00_u8));
196
201template <SpiPort Port>
202class FrameBus {
203public:
204 explicit FrameBus(Port& port) noexcept : port_(port) {}
205
206 Port& GetPort() & noexcept {
207 return port_;
208 }
209
219 Result<uint16_t> Exchange(FrameFormat format, uint8_t pad, uint16_t word) noexcept {
220 if (resync_) {
221 (void)transfer(format, pad, kResyncWord); // its reply is the previous command's; unused
222 if (resync_) {
223 return Result<uint16_t>::Failure(Error::Bus); // still failing: send nothing else
224 }
225 }
226 return transfer(format, pad, word);
227 }
228
230 [[nodiscard]] bool NeedsResync() const noexcept {
231 return resync_;
232 }
233
236 [[nodiscard]] uint16_t LastReply() const noexcept {
237 return last_reply_;
238 }
239
240private:
242 static constexpr uint16_t kResyncWord = 0x4000U;
243
244 Result<uint16_t> transfer(FrameFormat format, uint8_t pad, uint16_t word) noexcept {
245 std::array<uint8_t, kMaxFrameBytes> tx{};
246 std::array<uint8_t, kMaxFrameBytes> rx{};
247 const std::size_t len = FrameBytes(format);
248 std::size_t at = 0U;
249 if (format == FrameFormat::Bits32) {
250 tx[0] = pad;
251 at = 1U;
252 }
253 tx[at] = static_cast<uint8_t>(static_cast<uint32_t>(word) >> 8U);
254 tx[at + 1U] = static_cast<uint8_t>(static_cast<uint32_t>(word) & 0xFFU);
255 if (format != FrameFormat::Bits16) {
256 tx[at + 2U] = Crc8(word);
257 }
258 resync_ = !port_.Transfer(tx.data(), rx.data(), len);
259 if (resync_) {
260 return Result<uint16_t>::Failure(Error::Bus);
261 }
262 const auto reply =
263 static_cast<uint16_t>((static_cast<uint32_t>(rx[0]) << 8U) | static_cast<uint32_t>(rx[1]));
264 last_reply_ = reply;
265 if ((format != FrameFormat::Bits16) && (rx[2] != Crc8(reply))) {
266 return Result<uint16_t>::Failure(IsLinePattern(reply, rx[2]) ? Error::NoDevice : Error::Crc);
267 }
268 return Result<uint16_t>::Success(reply);
269 }
270
271 Port& port_;
272 uint16_t last_reply_ = 0;
273 bool resync_ = false;
274};
275
276} // namespace detail
277
278} // namespace as5047u
279
280#endif // AS5047U_SPI_INTERFACE_HPP
Compile-time helpers of the AS5047U driver: exact hook matching, the dependent-false helper,...
AS5047U register map: addresses, field masks and shifts.
Public types of the AS5047U driver: errors, Result<T>, configuration, decoded registers.
CRTP base of the platform SPI port.
Definition as5047u_spi_interface.hpp:77
SpiInterface() noexcept=default
Only the derived port is constructed.
SpiInterface(const SpiInterface &)=delete
Ports are not copied: the driver holds a reference.
SpiInterface & operator=(const SpiInterface &)=delete
Ports are not copied: the driver holds a reference.
Satisfied by a class that derives from SpiInterface<Port> and implements the required hooks with exac...
Definition as5047u_spi_interface.hpp:103
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition as5047u_types.hpp:37
@ Crc
A reply's CRC was wrong, or the chip saw a wrong CRC (ERRFL.CRC_error).
constexpr uint8_t kCrcInit
Initial value.
Definition as5047u_registers.hpp:44
constexpr uint8_t kCrcFinalXor
XORed into the result.
Definition as5047u_registers.hpp:45
The AS5047U driver: everything it declares lives here; internals in detail.
Definition as5047u.hpp:24
constexpr uint32_t kMinCsHighNs
Definition as5047u_spi_interface.hpp:54
constexpr bool kContinuousWord
Definition as5047u_spi_interface.hpp:61
constexpr uint8_t kSpiMode
SPI mode 1: CPOL 0, CPHA 1. The chip samples MOSI on the falling edge of CLK.
Definition as5047u_spi_interface.hpp:47
constexpr uint8_t Crc8(uint16_t word) noexcept
CRC-8 of a 16-bit frame word: polynomial 0x1D, initial value 0xC4, final XOR 0xFF,...
Definition as5047u_spi_interface.hpp:151
constexpr bool kMsbFirst
Each frame is clocked most significant bit first.
Definition as5047u_spi_interface.hpp:49
constexpr uint32_t kCsSetupNs
CS falling edge to the first CLK rising edge, t_L.
Definition as5047u_spi_interface.hpp:56
constexpr uint32_t kCsHoldNs
Last CLK falling edge to CS rising edge, t_H: t_clk / 2, so 50 ns at kMaxClockHz.
Definition as5047u_spi_interface.hpp:58
constexpr uint32_t kMaxClockHz
Highest CLK frequency: t_clk min 100 ns, so 10 MHz.
Definition as5047u_spi_interface.hpp:51
constexpr std::array< uint8_t, 3 > kFrameBits
Frame lengths in bits the chip accepts: 16 (reads only, no CRC), 24 and 32 (Figures 20,...
Definition as5047u_spi_interface.hpp:63