ADS7952 Driver 0.1.0
API reference
Loading...
Searching...
No Matches
ads7952_spi_interface.hpp
Go to the documentation of this file.
1// clang-format off: the hook table below must keep one row per line.
28// clang-format on
29#ifndef ADS7952_SPI_INTERFACE_HPP
30#define ADS7952_SPI_INTERFACE_HPP
31
32#include <array>
33#include <concepts>
34#include <cstddef>
35#include <cstdint>
36
37#include "ads7952_detail.hpp"
38#include "ads7952_registers.hpp"
39#include "ads7952_types.hpp"
40
41namespace ads7952 {
42
52inline constexpr uint8_t kSpiMode = 0;
54inline constexpr bool kMsbFirst = true;
56inline constexpr uint32_t kMaxClockHz = 20'000'000;
58inline constexpr uint32_t kMinCsHighNs = 20;
60inline constexpr uint32_t kCsSetupNs = 8;
63inline constexpr uint32_t kCsHoldNs = 0;
67inline constexpr uint32_t kQuietNs = 40;
70inline constexpr uint32_t kSdoReleaseNs = 26;
74inline constexpr bool kContinuousWord = true;
76inline constexpr uint32_t kFrameBits = 16;
79static_assert(kFrameBits == (8U * kFrameBytes), "one frame is one 16-bit word");
80
91template <typename Derived>
93public:
95 SpiInterface(const SpiInterface&) = delete;
98
99protected:
101 SpiInterface() noexcept = default;
103 ~SpiInterface() noexcept = default;
104};
105
117template <typename Port>
118concept SpiPort = std::derived_from<Port, SpiInterface<Port>> && requires {
119 detail::ExactHook<Port, bool() noexcept>::Take(&Port::EnsureInitialized);
120 detail::ExactHook<Port, bool(const uint8_t*, uint8_t*, std::size_t) noexcept>::Take(
121 &Port::Transfer);
122};
123
124namespace detail {
125
129template <SpiPort Port>
130class FrameBus {
131public:
132 explicit FrameBus(Port& port) noexcept : port_(port) {}
133
134 Port& GetPort() & noexcept {
135 return port_;
136 }
137
140 [[nodiscard]] Error Frame(uint16_t word, uint16_t& reply) noexcept {
141 const uint32_t w = word;
142 tx_ = {static_cast<uint8_t>(w >> 8U), static_cast<uint8_t>(w & 0xFFU)};
143 rx_ = {0xFF, 0xFF};
144 if (!port_.Transfer(tx_.data(), rx_.data(), tx_.size())) {
145 reply = 0xFFFFU;
146 return Error::Bus;
147 }
148 reply = static_cast<uint16_t>((static_cast<uint32_t>(rx_[0]) << 8U) |
149 static_cast<uint32_t>(rx_[1]));
150 return Error::Ok;
151 }
152
153private:
154 Port& port_;
155 std::array<uint8_t, kFrameBytes> tx_{};
156 std::array<uint8_t, kFrameBytes> rx_{};
157};
158
159} // namespace detail
160
161} // namespace ads7952
162
163#endif // ADS7952_SPI_INTERFACE_HPP
Compile-time helpers of the ADS7952 driver: exact hook matching and enumeration checks.
ADS795x command and reply words: the bit fields of every frame (datasheet SLAS605C,...
Public types of the ADS7952 driver: errors, Result<T>, variants, ranges, GPIO and alarm settings,...
CRTP base of the platform SPI port.
Definition ads7952_spi_interface.hpp:92
SpiInterface(const SpiInterface &)=delete
Ports are not copied: the driver holds a reference.
SpiInterface() noexcept=default
Only the derived port is constructed.
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 ads7952_spi_interface.hpp:118
constexpr std::size_t kFrameBytes
Bytes per transfer: every frame is 16 bits with CS low throughout (section 8.1).
Definition ads7952_types.hpp:310
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition ads7952_types.hpp:38
The ADS7952 driver: everything it declares lives here; internals in detail.
Definition ads7952.hpp:30
constexpr uint32_t kMaxClockHz
Highest SCLK frequency, f(SCLK) max (sections 7.3 and 7.9).
Definition ads7952_spi_interface.hpp:56
constexpr uint32_t kCsSetupNs
CS low to the first rising SCLK edge, t_su1; the largest minimum, at +VBD = 1.8 V (section 7....
Definition ads7952_spi_interface.hpp:60
constexpr uint32_t kCsHoldNs
Definition ads7952_spi_interface.hpp:63
constexpr bool kContinuousWord
Definition ads7952_spi_interface.hpp:74
constexpr bool kMsbFirst
Each word is clocked most significant bit first (DI15 and DO15 first, Figure 2).
Definition ads7952_spi_interface.hpp:54
constexpr uint32_t kQuietNs
Definition ads7952_spi_interface.hpp:67
constexpr uint32_t kMinCsHighNs
Shortest time CS stays high between two frames, t_w1 (section 7.9).
Definition ads7952_spi_interface.hpp:58
constexpr uint8_t kSpiMode
Definition ads7952_spi_interface.hpp:52
constexpr uint32_t kFrameBits
SCLK cycles per frame: one 16-bit word with CS low throughout (section 8.1).
Definition ads7952_spi_interface.hpp:76
constexpr uint32_t kSdoReleaseNs
Definition ads7952_spi_interface.hpp:70