BMP581 Driver 0.1.0
API reference
Loading...
Searching...
No Matches
bmp581_spi_interface.hpp
Go to the documentation of this file.
1// clang-format off: the table below must keep one row per line.
27// clang-format on
28#ifndef BMP581_SPI_INTERFACE_HPP
29#define BMP581_SPI_INTERFACE_HPP
30
31#include <array>
32#include <concepts>
33#include <cstddef>
34#include <cstdint>
35#include <span>
36
37#include "bmp581_detail.hpp"
38#include "bmp581_registers.hpp"
39#include "bmp581_types.hpp"
40
45namespace bmp581::spi {
48inline constexpr uint8_t kSpiMode = 0;
50inline constexpr bool kMsbFirst = true;
52inline constexpr uint32_t kMaxClockHz = 12'000'000;
54inline constexpr uint32_t kMaxClockLowVoltageHz = 7'000'000;
57inline constexpr uint32_t kMinCsHighNs = 80;
60inline constexpr uint32_t kCsSetupNs = 40;
62inline constexpr uint32_t kCsHoldNs = 40;
64inline constexpr bool kContinuousWord = false;
65} // namespace bmp581::spi
66
67namespace bmp581 {
68
78template <typename Derived>
80public:
82 SpiInterface(const SpiInterface&) = delete;
85
86protected:
88 SpiInterface() noexcept = default;
90 ~SpiInterface() noexcept = default;
91};
92
99template <typename Port>
100concept SpiPort = std::derived_from<Port, SpiInterface<Port>> && requires {
101 detail::ExactHook<Port, bool() noexcept>::Take(&Port::EnsureInitialized);
102 detail::ExactHook<Port, bool(const uint8_t*, uint8_t*, std::size_t) noexcept>::Take(
103 &Port::Transfer);
104};
105
106namespace detail {
107
109inline constexpr uint32_t kSpiReadFlag = 0x80U;
111inline constexpr uint32_t kSpiAddressMask = 0x7FU;
113inline constexpr std::size_t kSpiMaxBurst = kFifoMaxBytes;
114
121template <SpiPort Port>
122class SpiRegisterBus {
123public:
125 explicit SpiRegisterBus(Port& port) noexcept : port_(port) {}
126
128 Port& GetPort() & noexcept {
129 return port_;
130 }
131
133 [[nodiscard]] bool AddressValid() const noexcept {
134 return true;
135 }
136
139 Error SelectInterface() noexcept {
140 return Read(reg::kChipId, std::span<uint8_t>{rx_.data(), 1U});
141 }
142
145 Error Read(uint8_t reg, std::span<uint8_t> out) noexcept {
146 if (out.empty() || (out.size() > kSpiMaxBurst)) {
147 return Error::InvalidArgument;
148 }
149 const std::size_t count = out.size();
150 tx_.fill(0U);
151 tx_[0] = static_cast<uint8_t>(kSpiReadFlag | (static_cast<uint32_t>(reg) & kSpiAddressMask));
152 if (!port_.Transfer(tx_.data(), rx_.data(), count + 1U)) {
153 return Error::Bus;
154 }
155 for (std::size_t i = 0U; i < count; ++i) {
156 out[i] = rx_[1U + i];
157 }
158 return Error::Ok;
159 }
160
162 Error WriteByte(uint8_t reg, uint8_t value) noexcept {
163 const std::array<uint8_t, 2> frame{
164 static_cast<uint8_t>(static_cast<uint32_t>(reg) & kSpiAddressMask), value};
165 return port_.Transfer(frame.data(), nullptr, frame.size()) ? Error::Ok : Error::Bus;
166 }
167
169 Error WriteResetCommand() noexcept {
170 return WriteByte(reg::kCmd, reg::kCmdSoftReset);
171 }
172
173private:
174 Port& port_;
175 std::array<uint8_t, 1U + kSpiMaxBurst> tx_{};
176 std::array<uint8_t, 1U + kSpiMaxBurst> rx_{};
177};
178
179} // namespace detail
180
181} // namespace bmp581
182
183#endif // BMP581_SPI_INTERFACE_HPP
Compile-time helpers of the BMP581 driver: typed integer literals, exact hook matching,...
BMP581 register map: addresses, bit fields and reset values (datasheet section 7).
Public types of the BMP581 driver: errors, Result<T>, configuration, measurements and the datasheet's...
CRTP base of the platform SPI port.
Definition bmp581_spi_interface.hpp:79
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 bmp581_spi_interface.hpp:100
constexpr uint32_t kFifoMaxBytes
Bytes of a full FIFO, either frame size.
Definition bmp581_types.hpp:381
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition bmp581_types.hpp:40
What the BMP581 needs from the SPI bus (sections 5.2.1, 5.5, table 18). A port and a board configurat...
Definition bmp581_spi_interface.hpp:45
constexpr uint32_t kMinCsHighNs
Definition bmp581_spi_interface.hpp:57
constexpr uint32_t kCsHoldNs
Last SCK edge to CSB high, the CSB lag time (table 18, T_hold_csb).
Definition bmp581_spi_interface.hpp:62
constexpr uint32_t kMaxClockHz
Highest SCK frequency with VDDIO at or above 1.62 V and C_bus up to 80 pF (table 4,...
Definition bmp581_spi_interface.hpp:52
constexpr uint32_t kMaxClockLowVoltageHz
Highest SCK frequency below VDDIO 1.62 V with C_bus up to 40 pF (table 4, f_spi_lowv).
Definition bmp581_spi_interface.hpp:54
constexpr uint32_t kCsSetupNs
Definition bmp581_spi_interface.hpp:60
constexpr bool kMsbFirst
Each byte is clocked most significant bit first (figures 8 to 11).
Definition bmp581_spi_interface.hpp:50
constexpr uint8_t kSpiMode
Definition bmp581_spi_interface.hpp:48
constexpr bool kContinuousWord
false: SCK may pause between the bytes of one frame while CSB stays low (section 5....
Definition bmp581_spi_interface.hpp:64
The BMP581 driver: everything it declares lives here; internals in detail.
Definition bmp581.hpp:32