TMC9660 Driver 0.1.0
API reference
Loading...
Searching...
No Matches
tmc9660_detail.hpp
Go to the documentation of this file.
1
11#ifndef TMC9660_DETAIL_HPP
12#define TMC9660_DETAIL_HPP
13
14#include <concepts>
15#include <cstddef>
16#include <cstdint>
17#include <limits>
18#include <span>
19#include <type_traits>
20
21namespace tmc9660::detail {
22
28constexpr void CopyBytes(std::span<const std::uint8_t> src, std::span<std::uint8_t> dst) noexcept {
29 const std::size_t n = (src.size() < dst.size()) ? src.size() : dst.size();
30 for (std::size_t k = 0U; k < n; ++k) {
31 dst[k] = src[k];
32 }
33}
34
48template <typename Port, typename Sig>
49 requires std::is_class_v<Port> && std::is_function_v<Sig>
50struct ExactHook;
51
53template <typename Port, typename R, typename... Args>
54 requires std::is_class_v<Port>
55struct ExactHook<Port, R(Args...) noexcept> {
57 static void Take(R (Port::*hook)(Args...) noexcept) noexcept;
59 static void Take(R (Port::*hook)(Args...) const noexcept) noexcept;
60};
61
63template <typename T>
64concept ClassType = std::is_class_v<T>;
65
68template <ClassType>
69inline constexpr bool kAlwaysFalse = false;
70
72template <typename E>
73concept Enumeration = std::is_enum_v<E>;
74
79template <Enumeration E>
80[[nodiscard]] constexpr std::underlying_type_t<E> ToUnderlying(E e) noexcept {
81 return static_cast<std::underlying_type_t<E>>(e);
82}
83
88template <Enumeration E>
89struct EnumLast;
90
95template <Enumeration E>
96[[nodiscard]] constexpr bool IsEnumerator(E e) noexcept {
97 return static_cast<uint32_t>(ToUnderlying(e)) <=
98 static_cast<uint32_t>(ToUnderlying(EnumLast<E>::kValue));
99}
100
106template <typename Reg, uint32_t Shift, uint32_t Width>
107 requires std::unsigned_integral<Reg> && (Width > 0U) && (Width < 32U) &&
108 ((Shift + Width) <= static_cast<uint32_t>(std::numeric_limits<Reg>::digits))
109struct Field {
111 static constexpr uint32_t kMax = (uint32_t{1} << Width) - 1U;
113 static constexpr Reg kMask = static_cast<Reg>(kMax << Shift);
114
116 [[nodiscard]] static constexpr Reg Insert(Reg reg, uint32_t value) noexcept {
117 return static_cast<Reg>((static_cast<uint32_t>(reg) & ~static_cast<uint32_t>(kMask)) |
118 ((value & kMax) << Shift));
119 }
121 [[nodiscard]] static constexpr uint32_t Extract(Reg reg) noexcept {
122 return (static_cast<uint32_t>(reg) >> Shift) & kMax;
123 }
124};
125
126} // namespace tmc9660::detail
127
128#endif // TMC9660_DETAIL_HPP
detail::Field< uint32_t, 16U, 3U > Shift
Bits 18:16, RW: the manual's SHIFT.
Definition tmc9660_registers.hpp:1834