82#ifndef COMPONENT_HANDLER_PCAL95555_HANDLER_H_
83#define COMPONENT_HANDLER_PCAL95555_HANDLER_H_
90#include "base/BaseGpio.h"
91#include "base/BaseI2c.h"
92#include "core/hf-core-drivers/external/hf-pcal95555-driver/inc/pcal95555.hpp"
151 bool Write(uint8_t addr, uint8_t reg,
const uint8_t* data,
size_t len)
noexcept;
164 bool Read(uint8_t addr, uint8_t reg, uint8_t* data,
size_t len)
noexcept;
243 hf_gpio_direction_t direction = hf_gpio_direction_t::HF_GPIO_DIRECTION_INPUT,
244 hf_gpio_active_state_t active_state = hf_gpio_active_state_t::HF_GPIO_ACTIVE_HIGH,
245 hf_gpio_output_mode_t output_mode = hf_gpio_output_mode_t::HF_GPIO_OUTPUT_MODE_PUSH_PULL,
246 hf_gpio_pull_mode_t pull_mode = hf_gpio_pull_mode_t::HF_GPIO_PULL_MODE_FLOATING,
247 bool configure_hardware =
true)
noexcept;
291 InterruptCallback callback =
nullptr,
292 void* user_data =
nullptr) noexcept override;
331 hf_gpio_err_t
SetDirectionImpl(hf_gpio_direction_t direction) noexcept override;
337 hf_gpio_err_t
SetPullModeImpl(hf_gpio_pull_mode_t mode) noexcept override;
343 hf_gpio_err_t
SetPinLevelImpl(hf_gpio_level_t level) noexcept override;
346 hf_gpio_err_t
GetPinLevelImpl(hf_gpio_level_t& level) noexcept override;
349 hf_gpio_err_t
GetDirectionImpl(hf_gpio_direction_t& direction) const noexcept override;
352 hf_gpio_err_t
GetOutputModeImpl(hf_gpio_output_mode_t& mode) const noexcept override;
370 hf_gpio_interrupt_trigger_t::HF_GPIO_INTERRUPT_TRIGGER_NONE;
444 explicit Pcal95555Handler(BaseI2c& i2c_device, BaseGpio* interrupt_pin =
nullptr) noexcept;
507 hf_gpio_err_t
SetDirection(uint8_t pin, hf_gpio_direction_t direction)
noexcept;
515 hf_gpio_err_t
SetOutput(uint8_t pin,
bool active)
noexcept;
527 hf_gpio_err_t
ReadInput(uint8_t pin,
bool& active)
noexcept;
534 hf_gpio_err_t
Toggle(uint8_t pin)
noexcept;
550 hf_gpio_err_t
SetPullMode(uint8_t pin, hf_gpio_pull_mode_t pull_mode)
noexcept;
558 hf_gpio_err_t
GetPullMode(uint8_t pin, hf_gpio_pull_mode_t& pull_mode)
noexcept;
574 hf_gpio_err_t
SetDirections(uint16_t pin_mask, hf_gpio_direction_t direction)
noexcept;
582 hf_gpio_err_t
SetOutputs(uint16_t pin_mask,
bool active)
noexcept;
603 hf_gpio_err_t
SetPullModes(uint16_t pin_mask, hf_gpio_pull_mode_t pull_mode)
noexcept;
663 static constexpr uint8_t
PinCount() noexcept {
return 16; }
695 hf_gpio_direction_t direction = hf_gpio_direction_t::HF_GPIO_DIRECTION_INPUT,
696 hf_gpio_active_state_t active_state = hf_gpio_active_state_t::HF_GPIO_ACTIVE_HIGH,
697 hf_gpio_output_mode_t output_mode = hf_gpio_output_mode_t::HF_GPIO_OUTPUT_MODE_PUSH_PULL,
698 hf_gpio_pull_mode_t pull_mode = hf_gpio_pull_mode_t::HF_GPIO_PULL_MODE_FLOATING,
699 bool allow_existing = true,
700 bool configure_hardware = true) noexcept;
706 std::shared_ptr<BaseGpio>
GetGpioPin(hf_pin_num_t pin) noexcept;
731 uint16_t out_check_mask) noexcept;
737 uint16_t& output) noexcept;
756 uint16_t& config) noexcept;
770 uint16_t& input_high, uint16_t& latch_low,
771 uint16_t& input_low) noexcept;
848 hf_gpio_err_t
SetDriveStrength(hf_pin_num_t pin, pcal95555::DriveStrength level) noexcept;
866 hf_gpio_err_t
SetOutputMode(
bool port0_open_drain,
bool port1_open_drain) noexcept;
948 hf_gpio_interrupt_trigger_t trigger,
949 InterruptCallback callback,
950 void* user_data)
noexcept;
972 hf_gpio_interrupt_trigger_t trigger,
973 void* user_data) noexcept;
Concrete I2C communication adapter for PCAL95555 using BaseI2c.
Definition Pcal95555Handler.h:126
uint8_t cmd_scratch_
Register cmd byte for Read/Write framing.
Definition Pcal95555Handler.h:196
bool Read(uint8_t addr, uint8_t reg, uint8_t *data, size_t len) noexcept
Read data from a device register.
Definition Pcal95555Handler.cpp:87
bool Write(uint8_t addr, uint8_t reg, const uint8_t *data, size_t len) noexcept
Write data to a device register.
Definition Pcal95555Handler.cpp:60
std::function< void()> interrupt_handler_
Stored interrupt handler from driver.
Definition Pcal95555Handler.h:195
bool RegisterInterruptHandler(std::function< void()> handler) noexcept
Register an interrupt handler to be called when the INT pin fires.
Definition Pcal95555Handler.cpp:124
RtosMutex i2c_mutex_
Thread safety for I2C operations.
Definition Pcal95555Handler.h:194
uint8_t rx_scratch_[2]
Definition Pcal95555Handler.h:199
BaseI2c & i2c_device_
I2C device interface (not owned).
Definition Pcal95555Handler.h:193
bool EnsureInitialized() noexcept
Ensure the I2C bus is initialized and ready.
Definition Pcal95555Handler.cpp:120
uint8_t tx_scratch_[4]
Definition Pcal95555Handler.h:198
const std::function< void()> & GetInterruptHandler() const noexcept
Get the stored interrupt handler (if any).
Definition Pcal95555Handler.h:190
BaseGpio adapter for a single PCAL95555 GPIO expander pin.
Definition Pcal95555Handler.h:230
hf_gpio_err_t SetPullModeImpl(hf_gpio_pull_mode_t mode) noexcept override
Set pull mode (floating/up/down). PCAL9555A only.
Definition Pcal95555Handler.cpp:1534
bool configure_hardware_
If false, Initialize() skips I2C.
Definition Pcal95555Handler.h:363
hf_gpio_err_t SetPolarityInversion(bool invert) noexcept
Set input polarity inversion for this pin.
Definition Pcal95555Handler.cpp:1503
~Pcal95555GpioPin() noexcept override=default
Default destructor.
bool Deinitialize() noexcept override
Deinitialize (marks pin as uninitialized; expander state persists).
Definition Pcal95555Handler.cpp:1473
hf_gpio_err_t SetOutputModeImpl(hf_gpio_output_mode_t mode) noexcept override
Returns GPIO_ERR_UNSUPPORTED_OPERATION (use Pcal95555Handler::SetOutputMode for per-port control).
Definition Pcal95555Handler.cpp:1526
void * interrupt_user_data_
Definition Pcal95555Handler.h:368
hf_gpio_err_t SetPinLevelImpl(hf_gpio_level_t level) noexcept override
Write pin level via the PCAL95555 driver.
Definition Pcal95555Handler.cpp:1552
InterruptCallback interrupt_callback_
Definition Pcal95555Handler.h:367
bool IsPinAvailable() const noexcept override
Check if this pin number is valid (0-15).
Definition Pcal95555Handler.cpp:1478
bool Initialize() noexcept override
Initialize the pin (configures direction and pull mode on the expander).
Definition Pcal95555Handler.cpp:1440
hf_pin_num_t pin_
Pin number (0-15).
Definition Pcal95555Handler.h:360
hf_gpio_err_t SupportsInterrupts() const noexcept override
Check if this pin supports interrupt configuration.
Definition Pcal95555Handler.cpp:1482
char description_[32]
Human-readable description.
Definition Pcal95555Handler.h:362
hf_gpio_err_t GetInterruptStatus(bool &status) noexcept
Get the interrupt status for this pin.
Definition Pcal95555Handler.cpp:1513
hf_gpio_pull_mode_t GetPullModeImpl() const noexcept override
Get current pull mode (tracked internally).
Definition Pcal95555Handler.cpp:1540
bool interrupt_enabled_
Definition Pcal95555Handler.h:371
hf_gpio_err_t SetInterruptMask(bool mask) noexcept
Set the interrupt mask for this pin.
Definition Pcal95555Handler.cpp:1508
hf_gpio_err_t GetDirectionImpl(hf_gpio_direction_t &direction) const noexcept override
Get pin direction from the PCAL95555 driver.
Definition Pcal95555Handler.cpp:1571
Pcal95555Handler * parent_handler_
Owning handler (not owned).
Definition Pcal95555Handler.h:361
hf_gpio_err_t SetDirectionImpl(hf_gpio_direction_t direction) noexcept override
Set pin direction via the PCAL95555 driver.
Definition Pcal95555Handler.cpp:1520
hf_gpio_err_t ConfigureInterrupt(hf_gpio_interrupt_trigger_t trigger, InterruptCallback callback=nullptr, void *user_data=nullptr) noexcept override
Configure interrupt for this pin.
Definition Pcal95555Handler.cpp:1493
const char * GetDescription() const noexcept override
Returns a description string like "PCAL95555_PIN_5".
Definition Pcal95555Handler.cpp:1489
hf_gpio_err_t GetOutputModeImpl(hf_gpio_output_mode_t &mode) const noexcept override
Get output mode (tracked internally).
Definition Pcal95555Handler.cpp:1578
hf_gpio_interrupt_trigger_t interrupt_trigger_
Definition Pcal95555Handler.h:369
hf_gpio_err_t GetPinLevelImpl(hf_gpio_level_t &level) noexcept override
Read pin level via the PCAL95555 driver.
Definition Pcal95555Handler.cpp:1559
hf_u8_t GetMaxPins() const noexcept override
Returns 16 (total pins on the expander).
Definition Pcal95555Handler.h:265
Unified, non-templated handler for a single PCA9555 / PCAL9555A device.
Definition Pcal95555Handler.h:416
hf_gpio_err_t ReadConfigAndOutputPorts(uint16_t &config, uint16_t &output) noexcept
Read CONFIG (1=input) and OUTPUT latch port pairs (PCA pin bits).
Definition Pcal95555Handler.cpp:1040
uint8_t GetI2cAddress() const noexcept
Get the I2C address of the underlying device.
Definition Pcal95555Handler.cpp:839
hf_gpio_err_t GetInterruptStatus(hf_pin_num_t pin, bool &status) noexcept
Get interrupt status for a single pin.
Definition Pcal95555Handler.cpp:1252
uint16_t output_shadow_
PCA POR default OUTPUT.
Definition Pcal95555Handler.h:1035
hf_gpio_err_t GetPullMode(uint8_t pin, hf_gpio_pull_mode_t &pull_mode) noexcept
Get the pull resistor mode for a single pin (tracked internally).
Definition Pcal95555Handler.cpp:478
hf_gpio_err_t ReadStandardRegisterBank(uint16_t &input, uint16_t &output, uint16_t &polarity, uint16_t &config) noexcept
Read the full standard PCA9555 bank (0x00..0x07) as four 16-bit ports.
Definition Pcal95555Handler.cpp:1063
std::unique_ptr< Pcal95555Driver > pcal95555_driver_
Typed driver (created in Initialize).
Definition Pcal95555Handler.h:1002
uint16_t GetCreatedPinMask() const noexcept
Get a bitmask of all created pin numbers.
Definition Pcal95555Handler.cpp:939
pcal95555::ChipVariant GetChipVariant() const noexcept
Get the detected chip variant.
Definition Pcal95555Handler.cpp:1198
hf_gpio_err_t Deinitialize() noexcept
Internal deinitialization.
Definition Pcal95555Handler.cpp:303
bool EnsureInitialized() noexcept
Ensure the handler is initialized (lazy initialization).
Definition Pcal95555Handler.cpp:149
bool AttachStaticPin(Pcal95555GpioPin &pin) noexcept
Register a caller-owned pin wrapper (no heap allocation).
Definition Pcal95555Handler.cpp:859
std::unique_ptr< HalI2cPcal95555Comm > i2c_adapter_
I2C adapter (created in Initialize).
Definition Pcal95555Handler.h:1001
bool IsPinCreated(hf_pin_num_t pin) const noexcept
Check if a pin wrapper has been created.
Definition Pcal95555Handler.cpp:933
bool DrainPendingInterrupts() noexcept
Drain any pending interrupt that was deferred from ISR context.
Definition Pcal95555Handler.cpp:757
hf_gpio_err_t ReadAllInputLevels(uint16_t &levels) noexcept
Read both INPUT_PORT registers in one dual-port transaction.
Definition Pcal95555Handler.cpp:950
bool EnsureDeinitialized() noexcept
Deinitialize the handler and release resources.
Definition Pcal95555Handler.cpp:157
hf_gpio_err_t SetMultiplePolarityInversion(uint16_t pin_mask, bool invert) noexcept
Batch-set input polarity inversion for a pin mask.
Definition Pcal95555Handler.cpp:1215
hf_gpio_err_t SetPolarityInversion(hf_pin_num_t pin, bool invert) noexcept
Set input polarity inversion for a pin.
Definition Pcal95555Handler.cpp:1204
hf_gpio_err_t ReadInput(uint8_t pin, bool &active) noexcept
Read the physical level of a pin.
Definition Pcal95555Handler.cpp:401
BaseI2c & i2c_device_
I2C device reference (not owned).
Definition Pcal95555Handler.h:1000
bool interrupt_configured_
Whether hardware interrupt is active.
Definition Pcal95555Handler.h:1016
uint16_t prev_input_state_
Last-read pin input levels (bitmask).
Definition Pcal95555Handler.h:1029
hf_gpio_err_t SetOutput(uint8_t pin, bool active) noexcept
Write a logical level to an output pin.
Definition Pcal95555Handler.cpp:348
hf_gpio_err_t EnableInputLatch(hf_pin_num_t pin, bool enable) noexcept
Enable or disable input latch for a pin.
Definition Pcal95555Handler.cpp:1273
hf_gpio_err_t UnregisterPinInterrupt(hf_pin_num_t pin) noexcept
Unregister a per-pin interrupt callback.
Definition Pcal95555Handler.cpp:704
uint16_t GetErrorFlags() const noexcept
Get the driver's error flags.
Definition Pcal95555Handler.cpp:1306
hf_gpio_err_t ReadOutputLatchFromWire(uint16_t &output) noexcept
Read OUTPUT_PORT_0/1 from the chip (never the CM4 shadow).
Definition Pcal95555Handler.cpp:1026
bool HasInterruptSupport() const noexcept
Check if hardware interrupt support is available.
Definition Pcal95555Handler.h:616
hf_gpio_err_t RegisterPinInterrupt(hf_pin_num_t pin, hf_gpio_interrupt_trigger_t trigger, InterruptCallback callback, void *user_data) noexcept
Register a per-pin interrupt callback (called by Pcal95555GpioPin).
Definition Pcal95555Handler.cpp:647
hf_gpio_err_t Initialize() noexcept
Internal initialization (called by EnsureInitialized under mutex).
Definition Pcal95555Handler.cpp:171
hf_gpio_err_t SetDirection(uint8_t pin, hf_gpio_direction_t direction) noexcept
Set the direction of a single pin.
Definition Pcal95555Handler.cpp:334
BaseGpio * interrupt_pin_
Hardware INT pin (optional, not owned).
Definition Pcal95555Handler.h:1015
hf_gpio_err_t GetAllInterruptMasks(uint16_t &mask) noexcept
Get the interrupt mask for all 16 pins.
Definition Pcal95555Handler.cpp:617
hf_gpio_err_t SetDirections(uint16_t pin_mask, hf_gpio_direction_t direction) noexcept
Set direction for multiple pins at once.
Definition Pcal95555Handler.cpp:492
bool port_shadow_valid_
Definition Pcal95555Handler.h:1038
Pcal95555Driver * GetDriver() noexcept
Get the underlying PCAL95555 driver for advanced register-level operations.
Definition Pcal95555Handler.cpp:824
bool IsInitialized() const noexcept
Check if the handler has been initialized.
Definition Pcal95555Handler.h:491
hf_gpio_err_t SetInterruptMask(hf_pin_num_t pin, bool mask) noexcept
Set the per-pin interrupt mask.
Definition Pcal95555Handler.cpp:1241
hf_gpio_err_t SetOutputs(uint16_t pin_mask, bool active) noexcept
Write output level for multiple pins at once.
Definition Pcal95555Handler.cpp:519
hf_gpio_err_t SetPullModes(uint16_t pin_mask, hf_gpio_pull_mode_t pull_mode) noexcept
Set pull mode for multiple pins at once.
Definition Pcal95555Handler.cpp:572
pcal95555::PCAL95555< HalI2cPcal95555Comm > Pcal95555Driver
Typed PCAL95555 driver using our I2C adapter.
Definition Pcal95555Handler.h:424
Pcal95555GpioPin * GetGpioPinRaw(hf_pin_num_t pin) noexcept
Raw pointer to a registered pin (no shared_ptr control block).
Definition Pcal95555Handler.cpp:927
void ProcessInterrupts() noexcept
Process pending interrupts by reading status and dispatching callbacks.
Definition Pcal95555Handler.cpp:767
hf_gpio_err_t VerifyOutputPinEffect(uint8_t pin, uint16_t &latch_high, uint16_t &input_high, uint16_t &latch_low, uint16_t &input_low) noexcept
Drive one pin and verify OUTPUT latch + INPUT pad follow.
Definition Pcal95555Handler.cpp:1102
bool EnsureInitializedLocked() noexcept
Check init state under already-held handler_mutex_.
Definition Pcal95555Handler.cpp:166
void DumpDiagnostics() const noexcept
Dump comprehensive diagnostics to the system log.
Definition Pcal95555Handler.cpp:1320
bool ValidatePin(uint8_t pin) const noexcept
Validate pin number is in range 0-15.
Definition Pcal95555Handler.h:937
static constexpr uint8_t PinCount() noexcept
Total number of GPIO pins on the expander.
Definition Pcal95555Handler.h:663
uint16_t config_shadow_
PCA POR default CONFIG (all input)
Definition Pcal95555Handler.h:1036
static void HardwareInterruptCallback(BaseGpio *gpio, hf_gpio_interrupt_trigger_t trigger, void *user_data) noexcept
Static ISR callback for the hardware interrupt pin.
Definition Pcal95555Handler.cpp:746
std::shared_ptr< BaseGpio > CreateGpioPin(hf_pin_num_t pin, hf_gpio_direction_t direction=hf_gpio_direction_t::HF_GPIO_DIRECTION_INPUT, hf_gpio_active_state_t active_state=hf_gpio_active_state_t::HF_GPIO_ACTIVE_HIGH, hf_gpio_output_mode_t output_mode=hf_gpio_output_mode_t::HF_GPIO_OUTPUT_MODE_PUSH_PULL, hf_gpio_pull_mode_t pull_mode=hf_gpio_pull_mode_t::HF_GPIO_PULL_MODE_FLOATING, bool allow_existing=true, bool configure_hardware=true) noexcept
Create or retrieve an existing BaseGpio pin wrapper (heap).
Definition Pcal95555Handler.cpp:886
bool HasAgileIO() const noexcept
Check if the detected chip supports Agile I/O features.
Definition Pcal95555Handler.cpp:1194
std::atomic< bool > interrupt_pending_
Deferred interrupt flag (set in ISR, cleared in task context).
Definition Pcal95555Handler.h:1017
std::array< hf_gpio_pull_mode_t, 16 > pull_mode_cache_
Definition Pcal95555Handler.h:1023
hf_gpio_err_t SetOutputsVerified(uint16_t pin_mask, bool active) noexcept
Write output level for multiple pins in one transaction, then read the latch back and retry (up to 3×...
Definition Pcal95555Handler.cpp:544
hf_gpio_err_t SetPullMode(uint8_t pin, hf_gpio_pull_mode_t pull_mode) noexcept
Set the pull resistor mode for a single pin.
Definition Pcal95555Handler.cpp:439
hf_gpio_err_t SetOutputMode(bool port0_open_drain, bool port1_open_drain) noexcept
Configure per-port output mode (push-pull or open-drain).
Definition Pcal95555Handler.cpp:1283
hf_gpio_err_t ResetToDefault() noexcept
Reset all registers to power-on default state.
Definition Pcal95555Handler.cpp:1293
std::array< std::unique_ptr< Pcal95555GpioPin >, 16 > owned_pins_
Heap pins from CreateGpioPin.
Definition Pcal95555Handler.h:1010
RtosMutex handler_mutex_
Thread safety for handler operations.
Definition Pcal95555Handler.h:1004
hf_gpio_err_t Toggle(uint8_t pin) noexcept
Toggle the output state of a pin.
Definition Pcal95555Handler.cpp:429
bool IsInterruptConfigured() const noexcept
Check if hardware interrupt is configured and enabled.
Definition Pcal95555Handler.h:622
hf_gpio_err_t ConfigureHardwareInterrupt() noexcept
Configure the hardware interrupt pin (falling edge, active-low).
Definition Pcal95555Handler.cpp:731
hf_gpio_err_t ProgramPortsAbsolute(uint16_t config, uint16_t output, uint16_t out_check_mask) noexcept
Absolute program POL=0, OUTPUT latch, and CONFIG (no RMW).
Definition Pcal95555Handler.cpp:966
std::shared_ptr< BaseGpio > GetGpioPin(hf_pin_num_t pin) noexcept
Get an existing GPIO pin wrapper by number.
Definition Pcal95555Handler.cpp:923
uint16_t polarity_shadow_
POLARITY_INV.
Definition Pcal95555Handler.h:1037
std::array< Pcal95555GpioPin *, 16 > pin_registry_
Non-owning registry slots.
Definition Pcal95555Handler.h:1009
hf_gpio_err_t GetAllInterruptStatus(uint16_t &status) noexcept
Get the interrupt status for all 16 pins.
Definition Pcal95555Handler.cpp:634
hf_gpio_err_t SetDriveStrength(hf_pin_num_t pin, pcal95555::DriveStrength level) noexcept
Set output drive strength for a pin.
Definition Pcal95555Handler.cpp:1262
bool initialized_
Initialization state.
Definition Pcal95555Handler.h:1003
void ClearErrorFlags(uint16_t mask=0xFFFF) noexcept
Clear driver error flags.
Definition Pcal95555Handler.cpp:1310