28#include "at25040b_version.h"
39template <
typename Port>
40concept DeclaresGpioSet =
44template <
typename Port>
46 requires { ExactHook<Port, bool(
CtrlPin,
GpioSignal)
noexcept>::Take(&Port::GpioSet); };
49template <
typename Port>
50concept DeclaresDelayUs =
requires(Port& port) { port.DelayUs(1U); };
54template <
typename Port>
55concept HasDelayUs =
requires { ExactHook<Port, void(uint32_t)
noexcept>::Take(&Port::DelayUs); };
58template <
typename Port>
59concept DeclaresLog =
requires(Port& port) { port.Log(
LogLevel::Info,
""); };
62template <
typename Port>
64 requires { ExactHook<Port, void(
LogLevel,
const char*)
noexcept>::Take(&Port::Log); };
68template <
typename Port>
69void DelayUs(Port& port, uint32_t us)
noexcept {
70 if constexpr (HasDelayUs<Port>) {
73 static_assert(kAlwaysFalse<Port>,
74 "at25040b: the port must implement `void DelayUs(uint32_t us) noexcept`");
79template <
typename Port>
80void Log([[maybe_unused]] Port& port, [[maybe_unused]]
LogLevel level,
81 [[maybe_unused]]
const char* message)
noexcept {
82 if constexpr (HasLog<Port>) {
83 port.Log(level, message);
90concept WriteAccessOp =
91 std::invocable<Op&> && std::same_as<std::invoke_result_t<Op&>,
Result<void>>;
94constexpr BlockProtect DecodeBlockProtect(uint8_t status)
noexcept {
95 return static_cast<BlockProtect>(reg::StatusBp::Extract(status));
99constexpr uint8_t EncodeBlockProtect(
BlockProtect level)
noexcept {
100 return reg::StatusBp::Insert(0_u8,
static_cast<uint32_t
>(ToUnderlying(level)));
106constexpr Error ToggleError(uint8_t got, uint8_t expected)
noexcept {
107 const bool only_wen =
109 ((
static_cast<uint32_t
>(got) ^
static_cast<uint32_t
>(expected)) ==
reg::kStatusWen);
157template <SpiPort Port>
159 static_assert(!detail::DeclaresGpioSet<Port> || detail::HasGpioSet<Port>,
160 "at25040b: the optional GpioSet hook must be `bool GpioSet(CtrlPin, GpioSignal) "
161 "noexcept`, so that a failed pin write is reported");
163 !detail::DeclaresDelayUs<Port> || detail::HasDelayUs<Port>,
164 "at25040b: the DelayUs hook must be `void DelayUs(uint32_t us) noexcept`, so that no "
165 "wait is truncated");
166 static_assert(!detail::DeclaresLog<Port> || detail::HasLog<Port>,
167 "at25040b: the optional Log hook must be `void Log(LogLevel, const char* message) "
176 explicit AT25040B(Port& port,
const Config& config = {})
noexcept : bus_(port), config_(config) {}
359 Result<
void> initialize() noexcept;
360 Result<
void> checkWriteEnableToggle(uint8_t idle) noexcept;
361 Result<
void> checkStableHead() noexcept;
362 Result<
void> expectAfter(uint8_t opcode, uint8_t expected) noexcept;
363 Result<uint8_t> readStatusByte() noexcept;
364 Result<uint8_t> waitIdle(
bool started) noexcept;
365 Result<
void> readRange(uint16_t address, std::span<uint8_t> out,
Verify verify) noexcept;
366 Result<
void> writeRange(uint16_t address, std::span<const uint8_t> data,
Verify verify) noexcept;
367 Result<
void> writePage(uint16_t address, std::span<const uint8_t> data,
Verify verify) noexcept;
369 Result<
void> enableWrite() noexcept;
370 Result<uint8_t> finishWriteCycle() noexcept;
371 void disableWrite() noexcept;
372 Result<
void> releaseWriteProtect() noexcept;
373 Result<
void> parkWriteProtect() noexcept;
374 template <detail::WriteAccessOp Op>
375 Result<
void> withWriteAccess(Op op) noexcept;
376 [[nodiscard]]
bool inRange(uint16_t address, std::
size_t length) const noexcept;
377 Result<
void> fail(
Error error, const
char* message) noexcept;
379 detail::EepromBus<Port> bus_;
382 bool initialized_{
false};
390#include "../src/at25040b.ipp"
Compile-time helpers of the AT25040B driver: exact hook matching, typed register fields and enumerati...
Instruction opcodes and status register bits of the AT25010B, AT25020B and AT25040B.
The SPI port of the AT25040B driver: the bus requirements, the CRTP base, its pin enums,...
Public types of the AT25040B driver: errors, Result<T>, variants, block protection and configuration.
Driver for one AT25010B, AT25020B or AT25040B on a platform port.
Definition at25040b.hpp:158
AT25040B(const AT25040B &)=delete
One driver object per chip.
Result< void > ApplyConfig(const Config &config) noexcept
Replace the configuration. Never any I/O: the chip has no setting to write. The variant and the check...
Result< void > Deinitialize() noexcept
Leave the chip safe and mark the driver uninitialised: WRDI, then WP parked.
Result< void > Write(uint16_t address, std::span< const uint8_t > data, Verify verify=Verify::None) noexcept
Write data from address: one WRITE and one write cycle per 8-byte page.
Result< uint8_t > ReadRawStatus() noexcept
Read the status register once: one RDSR transfer, no wait, no retry, no decoding and no check of the ...
Result< void > SetBlockProtection(BlockProtect level) noexcept
Set BP1:BP0 (WRSR, section 6, "Write Status Register").
Result< void > EnsureInitialized() noexcept
Check that an AT25xxxB answers, once: the only way to initialise the driver.
const Config & GetConfig() const &noexcept
Definition at25040b.hpp:234
void GetConfig() const &&=delete
No reference into a temporary driver: it would dangle.
Result< Status > ReadStatus() noexcept
One status read (RDSR), decoded. Does not wait: during a write cycle it returns busy = true.
Result< BlockProtect > GetBlockProtection() noexcept
The block protection level, from the status (Table 6-4). Waits for a running write cycle first.
std::size_t GetCapacityBytes() const noexcept
Definition at25040b.hpp:241
Result< void > Read(uint16_t address, std::span< uint8_t > out, Verify verify=Verify::None) noexcept
Read out.size() bytes from address (sequential READ, section 6, "Read Sequence").
uint8_t GetLastStatusByte() const noexcept
The last status byte the driver read, by any call, for logs: 0xFF before the first....
Definition at25040b.hpp:340
AT25040B & operator=(const AT25040B &)=delete
One driver object per chip.
AT25040B(Port &port, const Config &config={}) noexcept
Bind the driver to a port and store the configuration. No I/O, cannot fail.
Definition at25040b.hpp:176
bool IsInitialized() const noexcept
Definition at25040b.hpp:205
constexpr std::size_t CapacityBytes(Variant variant) noexcept
Array size of a variant.
Definition at25040b_types.hpp:287
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition at25040b_types.hpp:36
Verify
Extra verification for one Read() or Write() call, chosen per call.
Definition at25040b_types.hpp:226
LogLevel
Severity passed to the optional Log() hook.
Definition at25040b_types.hpp:186
BlockProtect
Block write protection level, BP1:BP0 in the status register (Table 6-4).
Definition at25040b_types.hpp:212
@ None
Each chunk read once; no page read back. The default.
@ Info
Lifecycle events: initialised.
@ UpperQuarter
01: the top quarter is read-only (0x180 to 0x1FF on the AT25040B).
@ All
11: the whole array is read-only.
@ UpperHalf
10: the top half is read-only (0x100 to 0x1FF on the AT25040B).
constexpr bool IsIdleStatus(uint8_t status) noexcept
True for a status byte an idle chip can return: bits 7 to 4 clear and RDY clear (Table 6-3)....
Definition at25040b_registers.hpp:62
constexpr uint8_t kStatusBp1
BP1: block protection, high bit.
Definition at25040b_registers.hpp:44
constexpr uint8_t kStatusWen
WEN: 1 when the write enable latch is set.
Definition at25040b_registers.hpp:42
constexpr uint8_t kStatusBusyByte
Every bit reads 1 during a write cycle.
Definition at25040b_registers.hpp:48
The AT25040B driver: everything it declares lives here; internals in detail.
Definition at25040b.hpp:30
CtrlPin
Control pins the port may drive through the optional GpioSet() hook.
Definition at25040b_spi_interface.hpp:143
@ WriteProtect
WP, active low on the package pin. ACTIVE (pin low) blocks every write.
GpioSignal
Logical level of a control pin; the port maps it to the physical polarity.
Definition at25040b_spi_interface.hpp:148
@ ACTIVE
The pin function is asserted (WP: pin low, writes blocked).
What the driver is told about the board. The chip itself has nothing to configure: its only setting,...
Definition at25040b_types.hpp:361
Variant variant
Definition at25040b_types.hpp:362
The value of a call, or why there is none.
Definition at25040b_types.hpp:126