ADS9324 Driver 0.1.0
API reference
Loading...
Searching...
No Matches
ads9324_spi_interface.hpp
Go to the documentation of this file.
1// clang-format off: the table below must keep one row per line.
22// clang-format on
23#ifndef ADS9324_SPI_INTERFACE_HPP
24#define ADS9324_SPI_INTERFACE_HPP
25
26#include <array>
27#include <concepts>
28#include <cstddef>
29#include <cstdint>
30#include <span>
31
32#include "ads9324_detail.hpp"
33#include "ads9324_registers.hpp"
34#include "ads9324_types.hpp"
35
36namespace ads9324 {
37
48inline constexpr uint8_t kSpiMode = 0;
50inline constexpr bool kMsbFirst = true;
52inline constexpr uint32_t kMaxClockHz = kSclkMaxHz;
55inline constexpr uint32_t kMaxSdoutClockHz = 29'000'000;
57inline constexpr uint32_t kMinCsHighNs = 16;
59inline constexpr uint32_t kCsSetupNs = 16;
61inline constexpr uint32_t kCsHoldNs = 16;
64inline constexpr bool kFrameIsAtomic = true;
68inline constexpr bool kContinuousWord = false;
70inline constexpr uint32_t kFrameBits = 24;
75enum class CtrlPin : uint8_t {
76 Reset = 0,
77 Convst = 1,
78 DrdyAlarm = 2,
79};
80
83enum class GpioSignal : uint8_t {
84 INACTIVE = 0,
85 ACTIVE = 1,
86};
87
99template <typename Derived>
101public:
103 SpiInterface(const SpiInterface&) = delete;
106
107protected:
109 SpiInterface() noexcept = default;
111 ~SpiInterface() noexcept = default;
112};
113
126template <typename Port>
127concept SpiPort = std::derived_from<Port, SpiInterface<Port>> && requires {
128 detail::ExactHook<Port, bool() noexcept>::Take(&Port::EnsureInitialized);
129 detail::ExactHook<Port, bool(const uint8_t*, uint8_t*, std::size_t) noexcept>::Take(
130 &Port::Transfer);
131};
132
133namespace detail {
134
136inline constexpr std::size_t kFrameBytes = 3;
137
139constexpr uint16_t ReadCommand(uint8_t address) noexcept {
140 return reg::GenCfg1ReadAddress::Insert(reg::kRegRdEn, address);
141}
142
148template <SpiPort Port>
149class RegisterBus {
150public:
151 explicit RegisterBus(Port& port) noexcept : port_(port) {}
152
153 Port& GetPort() & noexcept {
154 return port_;
155 }
156
158 Error Write(uint8_t address, uint16_t value) noexcept {
159 tx_[0] = address;
160 tx_[1] = static_cast<uint8_t>(value >> 8U);
161 tx_[2] = static_cast<uint8_t>(value & 0xFFU);
162 return port_.Transfer(tx_.data(), nullptr, kFrameBytes) ? Error::Ok : Error::Bus;
163 }
164
169 Result<uint16_t> Read(uint8_t address) noexcept {
170 if (const Error e = Write(reg::kGenCfg1, ReadCommand(address)); e != Error::Ok) {
171 return Result<uint16_t>::Failure(e);
172 }
173 tx_.fill(0U);
174 rx_.fill(0U);
175 if (!port_.Transfer(tx_.data(), rx_.data(), kFrameBytes)) {
176 return Result<uint16_t>::Failure(Error::Bus);
177 }
178 if (rx_[2] != 0U) {
179 return Result<uint16_t>::Failure(Error::Bus);
180 }
181 return Result<uint16_t>::Success(static_cast<uint16_t>((static_cast<uint32_t>(rx_[0]) << 8U) |
182 static_cast<uint32_t>(rx_[1])));
183 }
184
188 Error ReadStream(std::span<uint8_t> out) noexcept {
189 if (out.empty() || (out.size() > kMaxFrameBytes)) {
190 return Error::InvalidArgument;
191 }
192 zeros_.fill(0U);
193 return port_.Transfer(zeros_.data(), out.data(), out.size()) ? Error::Ok : Error::Bus;
194 }
195
196private:
197 Port& port_;
198 // The transfer buffers belong to the driver object, never to a caller's stack, so a port whose
199 // bus reads them by DMA reaches them (KB-HW-019).
200 std::array<uint8_t, kFrameBytes> tx_{};
201 std::array<uint8_t, kFrameBytes> rx_{};
202 std::array<uint8_t, kMaxFrameBytes> zeros_{};
203};
204
205} // namespace detail
206
207} // namespace ads9324
208
209#endif // ADS9324_SPI_INTERFACE_HPP
Compile-time helpers of the ADS9324 driver: typed integer literals, exact hook matching,...
ADS9324 register map: banks, addresses, bit fields, reset values and the channel layout (datasheet se...
Public types of the ADS9324 driver: errors, Result<T>, enumerations, configuration,...
CRTP base of the platform port.
Definition ads9324_spi_interface.hpp:100
SpiInterface() noexcept=default
Only the derived port is constructed.
SpiInterface & operator=(const SpiInterface &)=delete
Ports are not copied: the driver holds a reference.
SpiInterface(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 ads9324_spi_interface.hpp:127
constexpr uint32_t kSclkMaxHz
Highest SCLK frequency (section 5.6).
Definition ads9324_types.hpp:667
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition ads9324_types.hpp:45
@ Bus
A port hook returned false.
The ADS9324 driver: everything it declares lives here; internals in detail.
Definition ads9324.hpp:32
constexpr uint8_t kSpiMode
Definition ads9324_spi_interface.hpp:48
constexpr bool kMsbFirst
Each byte is clocked most significant bit first.
Definition ads9324_spi_interface.hpp:50
constexpr uint32_t kMinCsHighNs
Shortest time CS stays high between two frames, thi_CS (section 5.6).
Definition ads9324_spi_interface.hpp:57
constexpr uint32_t kCsHoldNs
Last SCLK edge to CS high, tht_CSCK.
Definition ads9324_spi_interface.hpp:61
CtrlPin
Control pins the port may drive or read through the optional hooks.
Definition ads9324_spi_interface.hpp:75
@ Reset
RESET, active low on the package pin; idles high (INACTIVE).
@ Convst
CONVST: ACTIVE is a high level, and the line idles high (ACTIVE) between pulses.
@ DrdyAlarm
DRDY/ALARM: an input, read with GpioGet().
constexpr bool kContinuousWord
Definition ads9324_spi_interface.hpp:68
GpioSignal
Logical level of a control pin; the port maps it to the physical polarity.
Definition ads9324_spi_interface.hpp:83
@ ACTIVE
The pin function is asserted.
@ INACTIVE
The pin function is deasserted.
constexpr uint32_t kFrameBits
Bits in a register frame: 8 address bits and 16 data bits.
Definition ads9324_spi_interface.hpp:70
constexpr bool kFrameIsAtomic
Definition ads9324_spi_interface.hpp:64
constexpr uint32_t kMaxClockHz
Highest SCLK frequency, f_SCLK (section 5.6), for frames that read nothing back.
Definition ads9324_spi_interface.hpp:52
constexpr uint32_t kCsSetupNs
CS low to the first SCLK edge, tsu_CSCK.
Definition ads9324_spi_interface.hpp:59
constexpr uint32_t kMaxSdoutClockHz
Definition ads9324_spi_interface.hpp:55