|
HF-Core Platform 0.1.0-dev
Hardware-Agnostic Handler Layer & RTOS Utilities for HardFOC
|
Unified, non-templated handler for a single PCA9555 / PCAL9555A device. More...
#include <Pcal95555Handler.h>
Public Types | |
Type Aliases | |
| using | Pcal95555Driver = pcal95555::PCAL95555<HalI2cPcal95555Comm> |
| Typed PCAL95555 driver using our I2C adapter. | |
Public Member Functions | |
Construction and Destruction | |
| Pcal95555Handler (BaseI2c &i2c_device, BaseGpio *interrupt_pin=nullptr) noexcept | |
| Construct a handler for a PCAL95555 device. | |
| ~Pcal95555Handler () noexcept=default | |
| Destructor. Releases driver, adapter, and all pin wrappers. | |
| Pcal95555Handler (const Pcal95555Handler &)=delete | |
| Non-copyable. | |
| Pcal95555Handler & | operator= (const Pcal95555Handler &)=delete |
| Non-copyable. | |
| Pcal95555Handler (Pcal95555Handler &&)=delete | |
| Non-movable. | |
| Pcal95555Handler & | operator= (Pcal95555Handler &&)=delete |
| Non-movable. | |
Initialization and Lifecycle | |
| bool | EnsureInitialized () noexcept |
| Ensure the handler is initialized (lazy initialization). | |
| bool | EnsureDeinitialized () noexcept |
| Deinitialize the handler and release resources. | |
| bool | IsInitialized () const noexcept |
| Check if the handler has been initialized. | |
Basic GPIO Operations | |
Per-pin operations using pin numbers (0-15). | |
| hf_gpio_err_t | SetDirection (uint8_t pin, hf_gpio_direction_t direction) noexcept |
| Set the direction of a single pin. | |
| hf_gpio_err_t | SetOutput (uint8_t pin, bool active) noexcept |
| Write a logical level to an output pin. | |
| hf_gpio_err_t | ReadInput (uint8_t pin, bool &active) noexcept |
| Read the logical level of an input pin. | |
| hf_gpio_err_t | Toggle (uint8_t pin) noexcept |
| Toggle the output state of a pin. | |
| 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. | |
| 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). | |
Batch GPIO Operations | |
Operations on multiple pins using 16-bit masks. | |
| hf_gpio_err_t | SetDirections (uint16_t pin_mask, hf_gpio_direction_t direction) noexcept |
| Set direction for multiple pins at once. | |
| hf_gpio_err_t | SetOutputs (uint16_t pin_mask, bool active) noexcept |
| Write output level for multiple pins at once. | |
| 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. | |
PCAL9555A Advanced Features (Agile I/O) | |
These features require a PCAL9555A chip. They return false on PCA9555. | |
| bool | HasAgileIO () const noexcept |
| Check if the detected chip supports Agile I/O features. | |
| pcal95555::ChipVariant | GetChipVariant () const noexcept |
| Get the detected chip variant. | |
| hf_gpio_err_t | SetPolarityInversion (hf_pin_num_t pin, bool invert) noexcept |
| Set input polarity inversion for a pin. | |
| hf_gpio_err_t | SetInterruptMask (hf_pin_num_t pin, bool mask) noexcept |
| Set the per-pin interrupt mask. | |
| hf_gpio_err_t | GetInterruptStatus (hf_pin_num_t pin, bool &status) noexcept |
| Get interrupt status for a single pin. | |
| hf_gpio_err_t | SetDriveStrength (hf_pin_num_t pin, DriveStrength level) noexcept |
| Set output drive strength for a pin. | |
| hf_gpio_err_t | EnableInputLatch (hf_pin_num_t pin, bool enable) noexcept |
| Enable or disable input latch for a pin. | |
| hf_gpio_err_t | SetOutputMode (bool port0_open_drain, bool port1_open_drain) noexcept |
| Configure per-port output mode (push-pull or open-drain). | |
| hf_gpio_err_t | ResetToDefault () noexcept |
| Reset all registers to power-on default state. | |
Error Management | |
| uint16_t | GetErrorFlags () const noexcept |
| Get the driver's error flags. | |
| void | ClearErrorFlags (uint16_t mask=0xFFFF) noexcept |
| Clear driver error flags. | |
Diagnostics | |
| void | DumpDiagnostics () const noexcept |
| Dump comprehensive diagnostics to the system log. | |
Private Member Functions | |
| bool | EnsureInitializedLocked () noexcept |
| Check init state under already-held handler_mutex_. | |
| hf_gpio_err_t | Initialize () noexcept |
| Internal initialization (called by EnsureInitialized under mutex). | |
| hf_gpio_err_t | Deinitialize () noexcept |
| Internal deinitialization. | |
| bool | ValidatePin (uint8_t pin) const noexcept |
| Validate pin number is in range 0-15. | |
| 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). | |
| hf_gpio_err_t | UnregisterPinInterrupt (hf_pin_num_t pin) noexcept |
| Unregister a per-pin interrupt callback. | |
| hf_gpio_err_t | ConfigureHardwareInterrupt () noexcept |
| Configure the hardware interrupt pin (falling edge, active-low). | |
| void | ProcessInterrupts () noexcept |
| Process pending interrupts by reading status and dispatching callbacks. | |
Direct Driver Access | |
| Pcal95555Driver * | GetDriver () noexcept |
| Get the underlying PCAL95555 driver for advanced register-level operations. | |
| const Pcal95555Driver * | GetDriver () const noexcept |
Static Private Member Functions | |
| static void | HardwareInterruptCallback (BaseGpio *gpio, hf_gpio_interrupt_trigger_t trigger, void *user_data) noexcept |
| Static ISR callback for the hardware interrupt pin. | |
Private Attributes | |
Core Components | |
| BaseI2c & | i2c_device_ |
| I2C device reference (not owned). | |
| std::unique_ptr< HalI2cPcal95555Comm > | i2c_adapter_ |
| I2C adapter (created in Initialize). | |
| std::unique_ptr< Pcal95555Driver > | pcal95555_driver_ |
| Typed driver (created in Initialize). | |
| bool | initialized_ = false |
| Initialization state. | |
| RtosMutex | handler_mutex_ |
| Thread safety for handler operations. | |
Pin Registry | |
| std::array< std::shared_ptr< Pcal95555GpioPin >, 16 > | pin_registry_ |
| Created pin wrappers. | |
Internal Pull Mode Tracking | |
Tracks pull mode per-pin since the driver doesn't provide readback. | |
| std::array< hf_gpio_pull_mode_t, 16 > | pull_mode_cache_ |
Edge Detection State | |
Tracks previous input state for rising/falling edge filtering. | |
| uint16_t | prev_input_state_ = 0 |
| Last-read pin input levels (bitmask). | |
Friends | |
| class | Pcal95555GpioPin |
| Allow Pcal95555GpioPin to access private interrupt methods. | |
Interrupt Management | |
| BaseGpio * | interrupt_pin_ |
| Hardware INT pin (optional, not owned). | |
| bool | interrupt_configured_ |
| Whether hardware interrupt is active. | |
| std::atomic< bool > | interrupt_pending_ {false} |
| Deferred interrupt flag (set in ISR, cleared in task context). | |
| bool | HasInterruptSupport () const noexcept |
| Check if hardware interrupt support is available. | |
| bool | IsInterruptConfigured () const noexcept |
| Check if hardware interrupt is configured and enabled. | |
| bool | DrainPendingInterrupts () noexcept |
| Drain any pending interrupt that was deferred from ISR context. | |
| hf_gpio_err_t | GetAllInterruptMasks (uint16_t &mask) noexcept |
| Get the interrupt mask for all 16 pins. | |
| hf_gpio_err_t | GetAllInterruptStatus (uint16_t &status) noexcept |
| Get the interrupt status for all 16 pins. | |
Pin Factory | |
Create and manage BaseGpio-compatible pin wrapper instances. | |
| uint8_t | GetI2cAddress () const noexcept |
| Get the I2C address of the underlying device. | |
| 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) noexcept |
| Create or retrieve an existing BaseGpio pin wrapper. | |
| std::shared_ptr< BaseGpio > | GetGpioPin (hf_pin_num_t pin) noexcept |
| Get an existing GPIO pin wrapper by number. | |
| bool | IsPinCreated (hf_pin_num_t pin) const noexcept |
| Check if a pin wrapper has been created. | |
| uint16_t | GetCreatedPinMask () const noexcept |
| Get a bitmask of all created pin numbers. | |
| static constexpr uint8_t | PinCount () noexcept |
| Total number of GPIO pins on the expander. | |
Unified, non-templated handler for a single PCA9555 / PCAL9555A device.
Pcal95555Handler is the primary interface that application and manager code uses to interact with a PCAL95555 GPIO expander. It hides the template complexity of pcal95555::PCAL95555<I2cType> behind a clean public API.
| using Pcal95555Handler::Pcal95555Driver = pcal95555::PCAL95555<HalI2cPcal95555Comm> |
Typed PCAL95555 driver using our I2C adapter.
|
explicitnoexcept |
Construct a handler for a PCAL95555 device.
The I2C adapter and driver are not created until EnsureInitialized() or the first operation that requires them.
| i2c_device | Reference to a BaseI2c device with address pre-configured. Must outlive the handler. |
| interrupt_pin | Optional BaseGpio pin connected to the expander's INT output. Pass nullptr for polling mode (no hardware interrupts). |
|
defaultnoexcept |
Destructor. Releases driver, adapter, and all pin wrappers.
|
delete |
Non-copyable.
|
delete |
Non-movable.
|
noexcept |
Clear driver error flags.
| mask | Bitmask of flags to clear (default: all). |
|
privatenoexcept |
Configure the hardware interrupt pin (falling edge, active-low).
|
noexcept |
Create or retrieve an existing BaseGpio pin wrapper.
If a wrapper for the given pin already exists in the registry and allow_existing is true, the existing instance is returned. Otherwise a new Pcal95555GpioPin is created, initialized, and registered.
| pin | Pin number (0-15). |
| direction | Initial direction (ignored if pin exists). |
| active_state | Active polarity (ignored if pin exists). |
| output_mode | Output mode (ignored if pin exists). |
| pull_mode | Pull resistor mode (ignored if pin exists). |
| allow_existing | If true, returns existing pin; if false, fails if exists. |
|
privatenoexcept |
Internal deinitialization.
|
noexcept |
Drain any pending interrupt that was deferred from ISR context.
This method should be called from task context (e.g., a polling loop or a dedicated worker task). It checks the atomic interrupt_pending_ flag, and if set, performs the I2C reads and user callback dispatch safely.
|
noexcept |
Dump comprehensive diagnostics to the system log.
Logs initialization status, I2C address, chip variant, pin registry, interrupt configuration, and error flags at INFO level.
|
noexcept |
Enable or disable input latch for a pin.
| pin | Pin number (0-15). |
| enable | true to enable input latch, false to disable. |
|
noexcept |
Deinitialize the handler and release resources.
Disables hardware interrupt, clears the pin registry, and releases the driver and I2C adapter.
|
noexcept |
Ensure the handler is initialized (lazy initialization).
On first call, creates the I2C adapter and driver, runs the driver's EnsureInitialized() (which auto-detects chip variant), and optionally configures the hardware interrupt pin.
|
inlineprivatenoexcept |
Check init state under already-held handler_mutex_.
Check initialization under an already-held handler_mutex_.
|
noexcept |
Get the interrupt mask for all 16 pins.
| [out] | mask | 16-bit mask (0=enabled, 1=masked per PCAL9555A convention). |
|
noexcept |
Get the interrupt status for all 16 pins.
Reading the status clears the interrupt condition on the device.
| [out] | status | 16-bit mask (bit N set = pin N has pending interrupt). |
|
noexcept |
Get the detected chip variant.
|
noexcept |
Get a bitmask of all created pin numbers.
|
privatenoexcept |
|
privatenoexcept |
Get the underlying PCAL95555 driver for advanced register-level operations.
|
noexcept |
Get the driver's error flags.
|
noexcept |
Get an existing GPIO pin wrapper by number.
| pin | Pin number (0-15). |
|
noexcept |
Get the I2C address of the underlying device.
|
noexcept |
Get interrupt status for a single pin.
| pin | Pin number (0-15). | |
| [out] | status | true if pin has pending interrupt. |
|
noexcept |
Get the pull resistor mode for a single pin (tracked internally).
| pin | Pin number (0-15). | |
| [out] | pull_mode | Current pull mode. |
|
staticprivatenoexcept |
Static ISR callback for the hardware interrupt pin.
| gpio | The GPIO pin that triggered. |
| trigger | Trigger type. |
| user_data | Pointer to Pcal95555Handler instance. |
|
noexcept |
Check if the detected chip supports Agile I/O features.
|
inlinenoexcept |
Check if hardware interrupt support is available.
|
privatenoexcept |
Internal initialization (called by EnsureInitialized under mutex).
|
inlinenoexcept |
Check if the handler has been initialized.
|
inlinenoexcept |
Check if hardware interrupt is configured and enabled.
|
noexcept |
Check if a pin wrapper has been created.
| pin | Pin number (0-15). |
|
delete |
Non-copyable.
|
delete |
Non-movable.
|
inlinestaticconstexprnoexcept |
Total number of GPIO pins on the expander.
|
privatenoexcept |
Process pending interrupts by reading status and dispatching callbacks.
|
noexcept |
Read the logical level of an input pin.
| pin | Pin number (0-15). | |
| [out] | active | true if HIGH, false if LOW. |
|
privatenoexcept |
Register a per-pin interrupt callback (called by Pcal95555GpioPin).
| pin | Pin number (0-15). |
| trigger | Trigger type. |
| callback | Callback function. |
| user_data | User data for callback. |
|
noexcept |
Reset all registers to power-on default state.
|
noexcept |
Set the direction of a single pin.
| pin | Pin number (0-15). |
| direction | Input or output. |
|
noexcept |
Set direction for multiple pins at once.
| pin_mask | 16-bit mask (bit N = pin N). |
| direction | Common direction for all selected pins. |
|
noexcept |
Set output drive strength for a pin.
| pin | Pin number (0-15). |
| level | Drive strength (Level0=25%, Level1=50%, Level2=75%, Level3=100%). |
|
noexcept |
Set the per-pin interrupt mask.
| pin | Pin number (0-15). |
| mask | true to mask (disable) interrupt, false to unmask (enable). |
|
noexcept |
Write a logical level to an output pin.
| pin | Pin number (0-15). |
| active | true for HIGH, false for LOW. |
|
noexcept |
Configure per-port output mode (push-pull or open-drain).
| port0_open_drain | true for open-drain on port 0 (pins 0-7). |
| port1_open_drain | true for open-drain on port 1 (pins 8-15). |
|
noexcept |
Write output level for multiple pins at once.
| pin_mask | 16-bit mask (bit N = pin N). |
| active | Common level for all selected pins. |
|
noexcept |
Set input polarity inversion for a pin.
| pin | Pin number (0-15). |
| invert | true to invert input polarity, false for normal. |
|
noexcept |
Set the pull resistor mode for a single pin.
Maps HardFOC pull modes to PCAL9555A SetPullEnable/SetPullDirection:
| pin | Pin number (0-15). |
| pull_mode | Desired pull mode. |
|
noexcept |
Set pull mode for multiple pins at once.
| pin_mask | 16-bit mask (bit N = pin N). |
| pull_mode | Common pull mode for all selected pins. |
|
noexcept |
Toggle the output state of a pin.
| pin | Pin number (0-15). |
|
privatenoexcept |
Unregister a per-pin interrupt callback.
| pin | Pin number (0-15). |
|
inlineprivatenoexcept |
Validate pin number is in range 0-15.
|
friend |
Allow Pcal95555GpioPin to access private interrupt methods.
|
mutableprivate |
Thread safety for handler operations.
|
private |
I2C adapter (created in Initialize).
|
private |
I2C device reference (not owned).
|
private |
Initialization state.
|
private |
Whether hardware interrupt is active.
|
private |
Deferred interrupt flag (set in ISR, cleared in task context).
|
private |
Hardware INT pin (optional, not owned).
|
private |
Typed driver (created in Initialize).
|
private |
Created pin wrappers.
|
private |
Last-read pin input levels (bitmask).
|
private |