ISF15ACP4 Driver 0.1.0
API reference
Loading...
Searching...
No Matches
isf15acp4_spi_interface.hpp
Go to the documentation of this file.
1// clang-format off: the hook table below must keep one row per line.
35// clang-format on
36#ifndef ISF15ACP4_SPI_INTERFACE_HPP
37#define ISF15ACP4_SPI_INTERFACE_HPP
38
39#include <array>
40#include <concepts>
41#include <cstddef>
42#include <cstdint>
43#include <span>
44
45#include "isf15acp4_detail.hpp"
47#include "isf15acp4_types.hpp"
48
49namespace isf15acp4 {
50
60inline constexpr uint8_t kSpiMode = 0;
62inline constexpr bool kMsbFirst = true;
64inline constexpr uint32_t kMaxClockHz = 6'600'000;
67inline constexpr uint32_t kMinCsHighNs = 150;
69inline constexpr uint32_t kCsSetupNs = 75;
71inline constexpr uint32_t kCsHoldNs = 60;
74inline constexpr bool kContinuousWord = false;
76inline constexpr uint32_t kDcSetupNs = 40;
78inline constexpr uint32_t kDcHoldNs = 40;
80inline constexpr bool kWriteOnly = true;
83static_assert((kMaxClockHz * 150ULL) <= 1'000'000'000ULL, "tcycle 150 ns minimum");
84
86enum class CtrlPin : uint8_t {
87 Reset = 0,
88 VccEnable =
89 1,
90};
91
93enum class GpioSignal : uint8_t {
94 INACTIVE = 0,
95 ACTIVE = 1,
96};
97
99enum class DataKind : uint8_t {
100 Command = 0,
101 Data = 1,
102};
103
114template <typename Derived>
116public:
118 SpiInterface(const SpiInterface&) = delete;
121
122protected:
124 SpiInterface() noexcept = default;
126 ~SpiInterface() noexcept = default;
127};
128
129namespace detail {
130
132template <typename Port>
133concept HasGpioSet =
134 requires { ExactHook<Port, bool(CtrlPin, GpioSignal) noexcept>::Take(&Port::GpioSet); };
135
136} // namespace detail
137
150template <typename Port>
151concept SpiPort =
152 std::derived_from<Port, SpiInterface<Port>> && detail::HasGpioSet<Port> && requires {
153 detail::ExactHook<Port, bool() noexcept>::Take(&Port::EnsureInitialized);
154 detail::ExactHook<Port, bool(DataKind, const uint8_t*, std::size_t) noexcept>::Take(
155 &Port::Write);
156 };
157
158namespace detail {
159
161inline constexpr std::size_t kMaxCommandBytes = 33;
163inline constexpr std::size_t kDataChunkBytes = 96;
164
169template <SpiPort Port>
170class WriteBus {
171public:
172 explicit WriteBus(Port& port) noexcept : port_(port) {}
173
174 Port& GetPort() & noexcept {
175 return port_;
176 }
177
180 Error Command(std::span<const uint8_t> bytes) noexcept {
181 if (bytes.empty() || (bytes.size() > kMaxCommandBytes)) {
182 return Error::InvalidArgument;
183 }
184 return port_.Write(DataKind::Command, bytes.data(), bytes.size()) ? Error::Ok : Error::Bus;
185 }
186
189 Error Data(std::span<const uint8_t> bytes) noexcept {
190 if (bytes.empty() || (bytes.size() > kDataChunkBytes)) {
191 return Error::InvalidArgument;
192 }
193 return port_.Write(DataKind::Data, bytes.data(), bytes.size()) ? Error::Ok : Error::Bus;
194 }
195
196private:
197 Port& port_;
198};
199
200} // namespace detail
201
202} // namespace isf15acp4
203
204#endif // ISF15ACP4_SPI_INTERFACE_HPP
CRTP base of the platform SPI port.
Definition isf15acp4_spi_interface.hpp:115
SpiInterface(const SpiInterface &)=delete
Ports are not copied: the driver holds a reference.
SpiInterface & operator=(const SpiInterface &)=delete
Ports are not copied: the driver holds a reference.
SpiInterface() noexcept=default
Only the derived port is constructed.
Satisfied by a class that derives from SpiInterface<Port> and implements the required hooks with exac...
Definition isf15acp4_spi_interface.hpp:151
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition isf15acp4_types.hpp:41
Compile-time helpers of the ISF15ACP4 driver: typed integer literals, exact hook matching,...
SSD1331 command set: opcodes, parameter fields, limits and timing (SSD1331 datasheet Rev 1....
Public types of the ISF15ACP4 driver: errors, Result<T>, the display and button configuration,...
The ISF15ACP4 driver: everything it declares lives here; internals in detail.
Definition isf15acp4.hpp:31
constexpr bool kMsbFirst
Each byte is clocked most significant bit first, D7 first (section 7.1.3).
Definition isf15acp4_spi_interface.hpp:62
DataKind
What a transfer carries: it sets the D/C# pin (section 7.2).
Definition isf15acp4_spi_interface.hpp:99
@ Command
D/C# low: an opcode or a parameter byte.
@ Data
D/C# high: GDDRAM data.
GpioSignal
Logical level of a control pin; the port maps it to the physical polarity.
Definition isf15acp4_spi_interface.hpp:93
@ ACTIVE
The pin function is asserted.
@ INACTIVE
The pin function is deasserted.
constexpr uint32_t kCsSetupNs
CS# low to the first SCK edge, tCSS (Table 21).
Definition isf15acp4_spi_interface.hpp:69
constexpr uint32_t kDcSetupNs
D/C# valid before the clock edge that starts a byte, tAS (Table 21).
Definition isf15acp4_spi_interface.hpp:76
constexpr bool kWriteOnly
There is no MISO: nothing can be read back.
Definition isf15acp4_spi_interface.hpp:80
constexpr bool kContinuousWord
Definition isf15acp4_spi_interface.hpp:74
constexpr uint32_t kDcHoldNs
D/C# held after the byte, tAH (Table 21).
Definition isf15acp4_spi_interface.hpp:78
constexpr uint32_t kMaxClockHz
Highest SCK frequency: tcycle 150 ns minimum (Table 21), 6.66 MHz.
Definition isf15acp4_spi_interface.hpp:64
constexpr uint8_t kSpiMode
Definition isf15acp4_spi_interface.hpp:60
constexpr uint32_t kMinCsHighNs
Definition isf15acp4_spi_interface.hpp:67
constexpr uint32_t kCsHoldNs
Last SCK edge to CS# high, tCSH (Table 21).
Definition isf15acp4_spi_interface.hpp:71
CtrlPin
The pins the port drives through GpioSet().
Definition isf15acp4_spi_interface.hpp:86
@ Reset
RES#, active low on the pin: ACTIVE holds the controller in reset.
@ VccEnable
The enable of the 15 V OLED supply (module pin 9), active high: ACTIVE = supply on.