24#include "pcal9555_version.h"
31[[nodiscard]]
constexpr uint16_t Apply(uint16_t value, uint16_t mask,
bool set)
noexcept {
32 const auto v =
static_cast<uint32_t
>(value);
33 const auto m =
static_cast<uint32_t
>(mask);
34 return static_cast<uint16_t
>(set ? (v | m) : (v & ~m));
39[[nodiscard]]
constexpr uint32_t ApplyDrive(uint32_t codes, uint16_t mask,
42 for (uint32_t pin = 0U; pin <
kPinCount; ++pin) {
43 if ((
static_cast<uint32_t
>(mask) & (uint32_t{1} << pin)) != 0U) {
52[[nodiscard]]
constexpr uint16_t PairValueOf(std::span<const uint8_t, 2> bytes)
noexcept {
53 return static_cast<uint16_t
>(
static_cast<uint32_t
>(bytes[0]) |
54 (
static_cast<uint32_t
>(bytes[1]) << 8U));
58[[nodiscard]]
constexpr uint16_t WithByte(uint16_t value, uint32_t port, uint8_t
byte)
noexcept {
59 const uint32_t shift = (port & 1U) * 8U;
60 return static_cast<uint16_t
>((
static_cast<uint32_t
>(value) & ~(0xFFU << shift)) |
61 (
static_cast<uint32_t
>(
byte) << shift));
71[[nodiscard]]
constexpr std::array<PairValue, 3> StandardPairs(
const Config& c)
noexcept {
78[[nodiscard]]
constexpr std::array<PairValue, 6> AgilePairs(
const Config& c)
noexcept {
90[[nodiscard]]
constexpr uint8_t OutputPortConfig(
const Config& config)
noexcept {
98 return static_cast<uint8_t
>(value);
103inline constexpr uint8_t kNoSentinel = 0x00U;
107struct ResetSentinel {
117[[nodiscard]]
constexpr uint32_t UncertainIndex(uint8_t command)
noexcept {
118 const auto c =
static_cast<uint32_t
>(command);
125 return (c / 2U) & 0x0FU;
129[[nodiscard]]
constexpr uint32_t UncertainBit(uint8_t command)
noexcept {
130 return uint32_t{1} << UncertainIndex(command);
134inline constexpr uint32_t kAllUncertain = 0x07FEU;
137[[nodiscard]]
constexpr uint32_t StrongerDrive(uint32_t a, uint32_t b)
noexcept {
139 for (uint32_t pin = 0U; pin <
kPinCount; ++pin) {
143 codes |= ((ca > cb) ? ca : cb) << shift;
155[[nodiscard]]
constexpr Config Riskier(
const Config& old,
const Config& asked)
noexcept {
170[[nodiscard]]
constexpr uint16_t RegisterValue(
const Config& c, uint8_t command)
noexcept {
191 v = OutputPortConfig(c);
195 return static_cast<uint16_t
>(v);
199constexpr void Adopt(
Config& c, uint8_t command, uint16_t value)
noexcept {
200 const auto v =
static_cast<uint32_t
>(value);
232[[nodiscard]]
constexpr ResetSentinel Candidate(uint8_t command, uint16_t value, uint16_t reset,
233 bool pair =
true)
noexcept {
234 return {.reg = command, .value = value, .reset = reset, .pair = pair, .only =
true};
249[[nodiscard]]
constexpr ResetSentinel FindResetSentinel(
const Config& c,
bool agile,
250 uint32_t uncertain = 0U)
noexcept {
254 const std::array<ResetSentinel, 10> candidates{{
266 ResetSentinel found{.reg = kNoSentinel, .value = 0U, .reset = 0U, .pair =
true, .only =
true};
267 for (
const ResetSentinel& candidate : candidates) {
268 if (!
reg::IsImplemented(candidate.reg, agile) || (candidate.value == candidate.reset) ||
269 ((uncertain & UncertainBit(candidate.reg)) != 0U)) {
272 if (found.reg != kNoSentinel) {
278 if (uncertain != 0U) {
289static_assert((AgilePairs(
Config{})[0].value == 0xFFFFU) &&
290 (AgilePairs(
Config{})[1].value == 0xFFFFU) &&
299static_assert(PairValueOf(std::array<uint8_t, 2>{0x34, 0x12}) == 0x1234U);
305static_assert(StrongerDrive(0x0000'00F0_u32, 0x0000'003C_u32) == 0x0000'00FCU);
309static_assert((WithByte(0x1234_u16, 1_u32, 0xAB_u8) == 0xAB34U) &&
310 (WithByte(0x1234_u16, 0_u32, 0xAB_u8) == 0x12ABU));
314static_assert((FindResetSentinel(
Config{},
true).reg == kNoSentinel) &&
318static_assert((Apply(0x00F0_u16, 0x0F0F_u16,
true) == 0x0FFFU) &&
319 (Apply(0xFFFF_u16, 0x0F0F_u16,
false) == 0xF0F0U));
383template <I2cPort Port>
385 static_assert(!detail::DeclaresLog<Port> || detail::HasLog<Port>,
386 "pcal9555: the optional Log hook must be `void Log(LogLevel, const char* message) "
388 static_assert(!detail::DeclaresGetLastFailure<Port> || detail::HasGetLastFailure<Port>,
389 "pcal9555: the optional GetLastFailure hook must be `bool "
390 "GetLastFailure(I2cFailure& failure) noexcept`");
402 : bus_(port, address), config_(config) {}
489 return bus_.GetAddress();
765 return latched_interrupts_;
770 latched_interrupts_ = 0U;
777 Result<
void> checkPresence() noexcept;
778 Result<
void> writeAll(const
Config& c, uint8_t& failed) noexcept;
779 void noteApplyFailure(const
Config& asked, uint8_t failed,
Error error) noexcept;
780 Result<
void> resolveAll() noexcept;
781 Result<
void> checkUnknown() noexcept;
782 Result<
void> writeStage() noexcept;
783 void forget(uint8_t reg) noexcept;
786 Result<
void> storeMask(uint8_t reg, uint16_t
Config::* field, uint16_t value,
788 Result<
void> checkReset() noexcept;
789 Result<
void> guardWrite(uint8_t first, uint8_t second) noexcept;
790 Result<
void> noteInterrupts() noexcept;
791 Result<
void> prepare(uint8_t first, uint8_t second) noexcept;
792 Result<
void> resolve(uint8_t reg) noexcept;
793 void markFailed(const
Config& asked, uint8_t reg,
Error error) noexcept;
794 [[nodiscard]]
bool isUncertain(uint8_t reg) const noexcept {
795 return (
static_cast<uint32_t
>(uncertain_) & detail::UncertainBit(reg)) != 0U;
797 Result<void> writeDrive(uint32_t codes,
Verify verify)
noexcept;
798 Result<void> storeDrive(uint8_t reg, uint32_t codes, uint16_t value,
Verify verify)
noexcept;
799 Result<uint16_t> readPair(uint8_t reg)
noexcept;
800 Result<uint8_t> readByte(uint8_t reg)
noexcept;
801 Result<void> writeByte(uint8_t reg, uint8_t value)
noexcept;
802 void notePair(uint8_t reg, uint16_t value)
noexcept;
803 void noteByte(uint8_t reg, uint8_t value)
noexcept;
804 Result<void> writePair(uint8_t reg, uint16_t value)
noexcept;
805 Result<void> writePairChecked(uint8_t reg, uint16_t value,
Verify verify)
noexcept;
806 Result<uint16_t> readStarted(uint8_t reg)
noexcept;
807 template <std::
size_t N>
808 Result<void> verifyPairs(
const std::array<detail::PairValue, N>& pairs)
noexcept;
809 Result<void> fail(
Error error,
const char* message)
noexcept;
811 [[nodiscard]]
bool isAgile() const noexcept {
815 detail::RegisterBus<Port> bus_;
817 uint16_t last_inputs_{0U};
819 uint16_t latched_interrupts_{0U};
820 uint16_t uncertain_{0U};
821 bool initialized_{
false};
829#include "../src/pcal9555.ipp"
Driver for one PCA9555 or PCAL9555A on a platform port.
Definition pcal9555.hpp:384
Result< void > SetOutputMode(Bank bank, OutputMode mode, Verify verify=Verify::None) noexcept
Select push-pull or open-drain outputs for a whole port (output port configuration).
Result< uint16_t > ReadPolarityInversion() noexcept
Read both polarity inversion registers.
Result< uint16_t > ReadInputs() noexcept
Read both input ports: the level of every pin, inputs and outputs, after polarity inversion....
Result< bool > ReadPin(uint8_t pin) noexcept
Read the level of one pin. Reads its port's input register only, which clears that port's interrupt....
Result< void > WriteOutputs(uint16_t high_mask, Verify verify=Verify::None) noexcept
Write both output port registers at once.
uint16_t GetLatchedInterrupts() const noexcept
The pins any read of the interrupt status registers has reported since ClearLatchedInterrupts(),...
Definition pcal9555.hpp:764
bool IsInitialized() const noexcept
Definition pcal9555.hpp:435
uint16_t GetLastInputs() const noexcept
Definition pcal9555.hpp:740
uint8_t GetAddress() const noexcept
Definition pcal9555.hpp:488
Result< void > SetPull(uint16_t mask, Pull pull, Verify verify=Verify::None) noexcept
Connect a pull-up or pull-down to the pins in mask, or disconnect it. Writes the selection pair,...
Result< uint16_t > ReadInterruptStatus() noexcept
Read which pins raised the interrupt (interrupt status registers). Masked pins read 0....
Result< void > ApplyConfig(const Config &config) noexcept
Replace the configuration. When initialised, check for a reset (one read, see CheckForReset()),...
Result< void > SetDirection(uint16_t mask, Direction direction, Verify verify=Verify::None) noexcept
Set the direction of the pins in mask (configuration registers).
Result< void > SetPolarityInversion(uint16_t mask, bool invert, Verify verify=Verify::None) noexcept
Invert (or stop inverting) how the input pins in mask read (polarity inversion).
void ClearLatchedInterrupts() noexcept
Empty the latch of GetLatchedInterrupts(). No I/O; the chip is not touched.
Definition pcal9555.hpp:769
PCAL9555 & operator=(const PCAL9555 &)=delete
One driver object per chip.
Result< void > Deinitialize() noexcept
Make every pin a high-impedance input (the power-up direction), the safe state, and mark the driver u...
Result< void > SetDriveStrength(uint16_t mask, DriveStrength level, Verify verify=Verify::None) noexcept
Set the output drive strength of the pins in mask. Writes both drive strength pairs.
PCAL9555(Port &port, uint8_t address=kI2cAddress, const Config &config={}) noexcept
Bind the driver to a port, a 7-bit I2C address and a configuration. No I/O, no checks: EnsureInitiali...
Definition pcal9555.hpp:401
PCAL9555(const PCAL9555 &)=delete
One driver object per chip.
uint16_t GetLastOutput() const noexcept
Definition pcal9555.hpp:750
Result< uint8_t > ReadRegister(uint8_t command) noexcept
Read one register, once: one 1-byte write-read, no retry, no check of the value (handbook KB-CPP-129)...
void GetConfig() const &&=delete
Not on a temporary driver: the reference would dangle (handbook KB-CPP20-044).
Result< void > CheckForReset() noexcept
Find out whether the chip went through a power-on reset since the start.
Result< void > SetInterruptEnabled(uint16_t mask, bool enable, Verify verify=Verify::None) noexcept
Let the input pins in mask raise INT on a change, or mask them (interrupt mask registers; every pin i...
Result< uint16_t > ReadOutputLatch() noexcept
Read both output port registers: the level each pin is told to drive.
Result< void > SetOutputs(uint16_t mask, bool high, Verify verify=Verify::None) noexcept
Drive the pins in mask high or low (output port registers). Pins configured as inputs keep the value ...
Result< void > EnsureInitialized() noexcept
Start the chip on the first call; Ok at once, with no I/O, once it has succeeded.
Result< void > SetInputLatch(uint16_t mask, bool latch, Verify verify=Verify::None) noexcept
Latch (or stop latching) input changes of the pins in mask: a latched pin keeps the level that raised...
Result< void > ToggleOutputs(uint16_t mask, Verify verify=Verify::None) noexcept
Invert the output value of the pins in mask.
const Config & GetConfig() const &noexcept
Definition pcal9555.hpp:481
Result< uint16_t > ReadInputMask() noexcept
Read both configuration registers.
constexpr uint8_t kOutputConfigOden0
0x4F ODEN0: port 0 open-drain.
Definition pcal9555_registers.hpp:49
constexpr bool IsImplemented(uint8_t command, bool agile) noexcept
Whether a part implements a command byte (table 4): 0x00 to 0x07 on both parts, and on the PCAL9555A ...
Definition pcal9555_registers.hpp:83
constexpr uint16_t kOutputReset
Output ports: every pin high.
Definition pcal9555_registers.hpp:64
constexpr uint8_t kPullSelect0
Pull-up/pull-down selection 0 (R/W): 1 = pull-up.
Definition pcal9555_registers.hpp:44
constexpr uint16_t kConfigReset
Definition pcal9555_registers.hpp:66
constexpr uint32_t kDriveStrengthReset
Drive strength: full on every pin (0x40 to 0x43 all 0xFF).
Definition pcal9555_registers.hpp:68
constexpr uint8_t kConfigPort0
Configuration 0 (R/W): 1 = input; 0x07 port 1.
Definition pcal9555_registers.hpp:37
constexpr uint16_t kPullEnableReset
Pull resistors: enabled.
Definition pcal9555_registers.hpp:70
constexpr uint8_t kOutputPortConfig
Output port configuration (R/W).
Definition pcal9555_registers.hpp:47
constexpr uint8_t kOutputConfigOden1
Definition pcal9555_registers.hpp:50
constexpr uint8_t kPullEnable0
Pull-up/pull-down enable 0 (R/W); 0x47 port 1.
Definition pcal9555_registers.hpp:43
constexpr uint16_t kInterruptMaskReset
Interrupts: every pin masked.
Definition pcal9555_registers.hpp:72
constexpr uint32_t kDriveCodeMask
Mask of one pin's drive strength code.
Definition pcal9555_registers.hpp:61
constexpr uint8_t kDriveStrength0
Drive strength 0 (R/W): P0_0-3; 0x41 P0_4-7.
Definition pcal9555_registers.hpp:40
constexpr uint8_t kInputLatch0
Input latch 0 (R/W): 1 = latched; 0x45 port 1.
Definition pcal9555_registers.hpp:42
constexpr uint8_t kInputPort0
Input port 0 (R): pin levels; 0x01 is port 1.
Definition pcal9555_registers.hpp:34
constexpr uint8_t kOutputPortConfigReset
Both ports push-pull.
Definition pcal9555_registers.hpp:73
constexpr uint16_t kPullSelectReset
Pull resistors: pull-up.
Definition pcal9555_registers.hpp:71
constexpr uint16_t kInputLatchReset
Input latch: off.
Definition pcal9555_registers.hpp:69
constexpr uint32_t kDriveBitsPerPin
Bits per pin in the drive strength registers: pin n of a port uses bits 2(n%4)+1.....
Definition pcal9555_registers.hpp:59
constexpr uint8_t kPolarityPort0
Polarity inversion 0 (R/W); 0x05 is port 1.
Definition pcal9555_registers.hpp:36
constexpr uint8_t kOutputPort0
Output port 0 (R/W); 0x03 is port 1.
Definition pcal9555_registers.hpp:35
constexpr uint8_t kDriveStrength2
Drive strength 2 (R/W): P1_0-3; 0x43 P1_4-7.
Definition pcal9555_registers.hpp:41
constexpr uint16_t kPolarityReset
Polarity inversion: none.
Definition pcal9555_registers.hpp:65
constexpr uint8_t kInterruptMask0
Interrupt mask 0 (R/W): 1 = masked.
Definition pcal9555_registers.hpp:45
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition pcal9555_types.hpp:36
constexpr uint8_t kPinCount
Number of pins.
Definition pcal9555_types.hpp:245
Direction
Direction of a pin (configuration register: 1 = input).
Definition pcal9555_types.hpp:196
Pull
Pull resistor of a pin, PCAL9555A only (pull enable and selection registers).
Definition pcal9555_types.hpp:226
constexpr uint32_t DriveStrengthAll(DriveStrength level) noexcept
Drive strength codes with every pin at one level, in the layout of Config.
Definition pcal9555_types.hpp:283
DriveStrength
Output drive strength of a pin, PCAL9555A only (data sheet section 6.2.6).
Definition pcal9555_types.hpp:202
OutputMode
Output stage of a whole port, PCAL9555A only (output port configuration register).
Definition pcal9555_types.hpp:233
Verify
Whether a pin call reads back what it wrote, chosen per call (the last parameter of every pin and Agi...
Definition pcal9555_types.hpp:220
Bank
One of the two 8-pin ports.
Definition pcal9555_types.hpp:239
constexpr uint16_t PinMask(uint8_t pin) noexcept
The mask of one pin.
Definition pcal9555_types.hpp:261
@ Quarter
0.25 of the maximum drive.
@ Full
The maximum drive. Power-up value.
@ OpenDrain
Low-side transistor only; the pull resistors of the port are disconnected.
@ PushPull
Both transistors active. Power-up value.
@ Pcal9555a
PCAL9555A: also the Agile I/O bank at 0x40 to 0x4F.
@ None
Write only. Default.
The PCAL9555 driver: everything it declares lives here; internals in detail.
Definition pcal9555.hpp:26
constexpr uint8_t kI2cAddress
Definition pcal9555_i2c_interface.hpp:49
Compile-time helpers of the PCAL9555 driver: exact hook matching and enumeration checks.
The I2C port of the PCAL9555 driver: the bus requirements, the CRTP base, the I2cPort concept,...
PCA9555 / PCAL9555A register map: command bytes, fields and power-up defaults.
Public types of the PCAL9555 driver: errors, Result<T>, pins and configuration.
Everything EnsureInitialized() and ApplyConfig() write to the chip.
Definition pcal9555_types.hpp:302
Variant variant
Driver side: which part is fitted. The Agile I/O calls need Variant::Pcal9555a.
Definition pcal9555_types.hpp:304
uint16_t interrupt_mask
PCAL9555A: 1 = the pin raises no interrupt.
Definition pcal9555_types.hpp:315
uint16_t polarity_invert_mask
Polarity inversion: 1 = input reads inverted.
Definition pcal9555_types.hpp:307
OutputMode port1_mode
PCAL9555A: output stage of port 1 (ODEN1).
Definition pcal9555_types.hpp:317
OutputMode port0_mode
PCAL9555A: output stage of port 0 (ODEN0).
Definition pcal9555_types.hpp:316
uint16_t pull_enable_mask
PCAL9555A: 1 = pull resistor connected.
Definition pcal9555_types.hpp:313
uint16_t input_mask
Configuration: 1 = input, 0 = output. Power-up: inputs.
Definition pcal9555_types.hpp:308
uint16_t input_latch_mask
PCAL9555A: 1 = input change latched until read.
Definition pcal9555_types.hpp:312
uint16_t output_high_mask
Output ports: 1 = drive high. Power-up: all high.
Definition pcal9555_types.hpp:306
uint32_t drive_strength_codes
PCAL9555A: drive strength codes (DriveStrength), two bits per pin, pin n at bits 2n+1....
Definition pcal9555_types.hpp:311
uint16_t pull_up_mask
PCAL9555A: 1 = pull-up, 0 = pull-down.
Definition pcal9555_types.hpp:314
The value of a call, or why there is none.
Definition pcal9555_types.hpp:119