11#ifndef TMC5160_DETAIL_HPP
12#define TMC5160_DETAIL_HPP
22namespace tmc5160::detail {
28void LiteralOutOfRange() noexcept;
36consteval std::uint32_t operator""_u32(
unsigned long long v) noexcept {
37 if (v > 0xFFFF'FFFFULL) {
40 return static_cast<std::uint32_t
>(v);
56template <
typename Port,
typename Sig>
57 requires std::is_class_v<Port> && std::is_function_v<Sig>
61template <
typename Port,
typename R,
typename... Args>
62 requires std::is_class_v<Port>
63struct ExactHook<Port, R(Args...) noexcept> {
65 static void Take(R (Port::*hook)(Args...) noexcept) noexcept;
67 static void Take(R (Port::*hook)(Args...) const noexcept) noexcept;
72concept ClassType = std::is_class_v<T>;
77inline constexpr bool kAlwaysFalse =
false;
81concept Enumeration = std::is_enum_v<E>;
87template <Enumeration E>
88[[nodiscard]]
constexpr std::underlying_type_t<E> ToUnderlying(E e)
noexcept {
89 return static_cast<std::underlying_type_t<E>
>(e);
96template <Enumeration E>
103template <Enumeration E>
104[[nodiscard]]
constexpr bool IsEnumerator(E e)
noexcept {
105 return static_cast<uint32_t
>(ToUnderlying(e)) <=
106 static_cast<uint32_t
>(ToUnderlying(EnumLast<E>::kValue));
114template <
typename Reg, u
int32_t Shift, u
int32_t W
idth>
115 requires std::unsigned_integral<Reg> && (Width > 0U) && (Width < 32U) &&
116 ((Shift + Width) <=
static_cast<uint32_t
>(std::numeric_limits<Reg>::digits))
119 static constexpr uint32_t kMax = (uint32_t{1} << Width) - 1U;
121 static constexpr Reg kMask =
static_cast<Reg
>(kMax << Shift);
124 [[nodiscard]]
static constexpr Reg Insert(Reg reg, uint32_t value)
noexcept {
125 return static_cast<Reg
>((
static_cast<uint32_t
>(reg) & ~
static_cast<uint32_t
>(kMask)) |
126 ((value & kMax) << Shift));
129 [[nodiscard]]
static constexpr uint32_t Extract(Reg reg)
noexcept {
130 return (
static_cast<uint32_t
>(reg) >> Shift) & kMax;
135inline constexpr uint8_t kUartSync = 0x05;
137inline constexpr uint8_t kUartMasterAddress = 0xFF;
139inline constexpr std::size_t kUartFrameLen = 8;
141inline constexpr std::size_t kUartRequestLen = 4;
149[[nodiscard]]
constexpr uint8_t Crc8(std::span<const uint8_t> bytes)
noexcept {
151 for (
const uint8_t
byte : bytes) {
152 uint32_t current = byte;
153 for (uint32_t bit = 0U; bit < 8U; ++bit) {
154 const uint32_t feedback = ((crc >> 7U) ^ current) & 1U;
155 crc = (crc << 1U) & 0xFFU;
156 crc ^= feedback * 0x07U;
160 return static_cast<uint8_t
>(crc);
164using UartFrame = std::array<uint8_t, kUartFrameLen>;
173[[nodiscard]]
constexpr UartFrame UartWriteFrame(uint8_t node, uint8_t reg,
174 uint32_t value)
noexcept {
175 UartFrame f{kUartSync,
177 static_cast<uint8_t
>(0x80U | (
static_cast<uint32_t
>(reg) & 0x7FU)),
178 static_cast<uint8_t
>(value >> 24U),
179 static_cast<uint8_t
>(value >> 16U),
180 static_cast<uint8_t
>(value >> 8U),
181 static_cast<uint8_t
>(value),
183 f[7] = Crc8(std::span<const uint8_t>{f}.first(7));
193[[nodiscard]]
constexpr UartFrame UartReadFrame(uint8_t node, uint8_t reg)
noexcept {
194 UartFrame f{kUartSync, node,
static_cast<uint8_t
>(
static_cast<uint32_t
>(reg) & 0x7FU), 0U, 0U, 0U,
196 f[3] = Crc8(std::span<const uint8_t>{f}.first(3));
207[[nodiscard]]
constexpr uint32_t ParseUartReply(
const UartFrame& reply, uint8_t reg,
208 uint32_t& value)
noexcept {
209 if ((reply[0] != kUartSync) || (reply[1] != kUartMasterAddress) ||
210 (reply[2] != (
static_cast<uint32_t
>(reg) & 0x7FU))) {
213 if (Crc8(std::span<const uint8_t>{reply}.first(7)) != reply[7]) {
216 value = (
static_cast<uint32_t
>(reply[3]) << 24U) | (
static_cast<uint32_t
>(reply[4]) << 16U) |
217 (
static_cast<uint32_t
>(reply[5]) << 8U) |
static_cast<uint32_t
>(reply[6]);
222static_assert(UartWriteFrame(0x01, 0x6C, 0x000100C5U)[7] == 0xD3U);
223static_assert(UartWriteFrame(0x01, 0x10, 0x00011405U)[7] == 0xD8U);
224static_assert(UartWriteFrame(0x01, 0x26, 0x00001388U)[7] == 0xB4U);
225static_assert(UartWriteFrame(0x01, 0x27, 0x00004E20U)[7] == 0x85U);
226static_assert(UartWriteFrame(0x01, 0x20, 0x00000001U)[7] == 0xA3U);
227static_assert(UartReadFrame(0x01, 0x21)[3] == 0x6BU);
228static_assert(UartReadFrame(0x01, 0x22)[3] == 0x25U);