TMC5160 Driver 0.1.0
API reference
Loading...
Searching...
No Matches
tmc5160_detail.hpp
Go to the documentation of this file.
1
11#ifndef TMC5160_DETAIL_HPP
12#define TMC5160_DETAIL_HPP
13
14#include <array>
15#include <concepts>
16#include <cstddef>
17#include <cstdint>
18#include <limits>
19#include <span>
20#include <type_traits>
21
22namespace tmc5160::detail {
23
28void LiteralOutOfRange() noexcept;
29
36consteval std::uint32_t operator""_u32(unsigned long long v) noexcept {
37 if (v > 0xFFFF'FFFFULL) {
38 LiteralOutOfRange();
39 }
40 return static_cast<std::uint32_t>(v);
41}
42
56template <typename Port, typename Sig>
57 requires std::is_class_v<Port> && std::is_function_v<Sig>
58struct ExactHook;
59
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;
68};
69
71template <typename T>
72concept ClassType = std::is_class_v<T>;
73
76template <ClassType>
77inline constexpr bool kAlwaysFalse = false;
78
80template <typename E>
81concept Enumeration = std::is_enum_v<E>;
82
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);
90}
91
96template <Enumeration E>
97struct EnumLast;
98
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));
107}
108
114template <typename Reg, uint32_t Shift, uint32_t Width>
115 requires std::unsigned_integral<Reg> && (Width > 0U) && (Width < 32U) &&
116 ((Shift + Width) <= static_cast<uint32_t>(std::numeric_limits<Reg>::digits))
117struct Field {
119 static constexpr uint32_t kMax = (uint32_t{1} << Width) - 1U;
121 static constexpr Reg kMask = static_cast<Reg>(kMax << Shift);
122
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));
127 }
129 [[nodiscard]] static constexpr uint32_t Extract(Reg reg) noexcept {
130 return (static_cast<uint32_t>(reg) >> Shift) & kMax;
131 }
132};
133
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;
142
149[[nodiscard]] constexpr uint8_t Crc8(std::span<const uint8_t> bytes) noexcept {
150 uint32_t crc = 0U;
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;
157 current >>= 1U;
158 }
159 }
160 return static_cast<uint8_t>(crc);
161}
162
164using UartFrame = std::array<uint8_t, kUartFrameLen>;
165
173[[nodiscard]] constexpr UartFrame UartWriteFrame(uint8_t node, uint8_t reg,
174 uint32_t value) noexcept {
175 UartFrame f{kUartSync,
176 node,
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),
182 0U};
183 f[7] = Crc8(std::span<const uint8_t>{f}.first(7));
184 return f;
185}
186
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,
195 0U, 0U};
196 f[3] = Crc8(std::span<const uint8_t>{f}.first(3));
197 return f;
198}
199
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))) {
211 return 1U;
212 }
213 if (Crc8(std::span<const uint8_t>{reply}.first(7)) != reply[7]) {
214 return 2U;
215 }
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]);
218 return 0U;
219}
220
221// The datagrams of the datasheet's initialization example (section 23.1 page 116).
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);
229
230} // namespace tmc5160::detail
231
232#endif // TMC5160_DETAIL_HPP