25#ifndef AS5047U_SPI_INTERFACE_HPP
26#define AS5047U_SPI_INTERFACE_HPP
63inline constexpr std::array<uint8_t, 3>
kFrameBits = {16, 24, 32};
76template <
typename Derived>
102template <typename Port>
104 detail::ExactHook<Port, bool()
noexcept>::Take(&Port::EnsureInitialized);
105 detail::ExactHook<Port, bool(
const uint8_t*, uint8_t*, std::size_t)
noexcept>::Take(
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);
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);
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));
140inline constexpr std::array<uint8_t, 256> kCrc8Table = MakeCrc8Table();
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)]);
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");
168inline constexpr std::size_t kMaxFrameBytes = 4U;
171constexpr std::size_t FrameBytes(FrameFormat format)
noexcept {
173 case FrameFormat::Bits16:
175 case FrameFormat::Bits24:
177 case FrameFormat::Bits32:
184static_assert(((FrameBytes(FrameFormat::Bits16) * 8U) == kFrameBits[0]) &&
185 ((FrameBytes(FrameFormat::Bits24) * 8U) == kFrameBits[1]) &&
186 ((FrameBytes(FrameFormat::Bits32) * 8U) == kFrameBits[2]));
191constexpr bool IsLinePattern(uint16_t reply, uint8_t crc)
noexcept {
192 return ((reply == 0x0000U) && (crc == 0x00U)) || ((reply == 0xFFFFU) && (crc == 0xFFU));
194static_assert(IsLinePattern(0x0000_u16, 0x00_u8) && IsLinePattern(0xFFFF_u16, 0xFF_u8) &&
195 !IsLinePattern(0x0000_u16, 0xFF_u8) && !IsLinePattern(0x4000_u16, 0x00_u8));
201template <SpiPort Port>
204 explicit FrameBus(Port& port) noexcept : port_(port) {}
206 Port& GetPort() &
noexcept {
219 Result<uint16_t> Exchange(FrameFormat format, uint8_t pad, uint16_t word)
noexcept {
221 (void)transfer(format, pad, kResyncWord);
223 return Result<uint16_t>::Failure(Error::Bus);
226 return transfer(format, pad, word);
230 [[nodiscard]]
bool NeedsResync() const noexcept {
236 [[nodiscard]] uint16_t LastReply() const noexcept {
242 static constexpr uint16_t kResyncWord = 0x4000U;
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);
249 if (format == FrameFormat::Bits32) {
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);
258 resync_ = !port_.Transfer(tx.data(), rx.data(), len);
260 return Result<uint16_t>::Failure(Error::Bus);
263 static_cast<uint16_t
>((
static_cast<uint32_t
>(rx[0]) << 8U) |
static_cast<uint32_t
>(rx[1]));
265 if ((format != FrameFormat::Bits16) && (rx[2] !=
Crc8(reply))) {
266 return Result<uint16_t>::Failure(IsLinePattern(reply, rx[2]) ? Error::NoDevice :
Error::
Crc);
268 return Result<uint16_t>::Success(reply);
272 uint16_t last_reply_ = 0;
273 bool resync_ =
false;
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