45#define HF_GPIO_ERR_LIST(X) \
47 X(GPIO_SUCCESS, 0, "Success") \
50 X(GPIO_ERR_FAILURE, 1, "General failure") \
51 X(GPIO_ERR_NOT_INITIALIZED, 2, "Not initialized") \
52 X(GPIO_ERR_ALREADY_INITIALIZED, 3, "Already initialized") \
53 X(GPIO_ERR_INVALID_PARAMETER, 4, "Invalid parameter") \
54 X(GPIO_ERR_NULL_POINTER, 5, "Null pointer") \
55 X(GPIO_ERR_OUT_OF_MEMORY, 6, "Out of memory") \
58 X(GPIO_ERR_INVALID_PIN, 7, "Invalid pin") \
59 X(GPIO_ERR_PIN_NOT_FOUND, 8, "Pin not found") \
60 X(GPIO_ERR_PIN_NOT_CONFIGURED, 9, "Pin not configured") \
61 X(GPIO_ERR_PIN_ALREADY_REGISTERED, 10, "Pin already registered") \
62 X(GPIO_ERR_PIN_ACCESS_DENIED, 11, "Pin access denied") \
63 X(GPIO_ERR_PIN_BUSY, 12, "Pin busy") \
66 X(GPIO_ERR_HARDWARE_FAULT, 13, "Hardware fault") \
67 X(GPIO_ERR_COMMUNICATION_FAILURE, 14, "Communication failure") \
68 X(GPIO_ERR_DEVICE_NOT_RESPONDING, 15, "Device not responding") \
69 X(GPIO_ERR_TIMEOUT, 16, "Timeout") \
70 X(GPIO_ERR_VOLTAGE_OUT_OF_RANGE, 17, "Voltage out of range") \
73 X(GPIO_ERR_INVALID_CONFIGURATION, 18, "Invalid configuration") \
74 X(GPIO_ERR_UNSUPPORTED_OPERATION, 19, "Unsupported operation") \
75 X(GPIO_ERR_RESOURCE_BUSY, 20, "Resource busy") \
76 X(GPIO_ERR_RESOURCE_UNAVAILABLE, 21, "Resource unavailable") \
79 X(GPIO_ERR_READ_FAILURE, 22, "Read failure") \
80 X(GPIO_ERR_WRITE_FAILURE, 23, "Write failure") \
81 X(GPIO_ERR_DIRECTION_MISMATCH, 24, "Direction mismatch") \
82 X(GPIO_ERR_PULL_RESISTOR_FAILURE, 25, "Pull resistor failure") \
85 X(GPIO_ERR_INTERRUPT_NOT_SUPPORTED, 26, "Interrupt not supported") \
86 X(GPIO_ERR_INTERRUPT_ALREADY_ENABLED, 27, "Interrupt already enabled") \
87 X(GPIO_ERR_INTERRUPT_NOT_ENABLED, 28, "Interrupt not enabled") \
88 X(GPIO_ERR_INTERRUPT_HANDLER_FAILED, 29, "Interrupt handler failed") \
91 X(GPIO_ERR_SYSTEM_ERROR, 30, "System error") \
92 X(GPIO_ERR_PERMISSION_DENIED, 31, "Permission denied") \
93 X(GPIO_ERR_OPERATION_ABORTED, 32, "Operation aborted") \
96 X(GPIO_ERR_NOT_SUPPORTED, 33, "Operation not supported") \
97 X(GPIO_ERR_DRIVER_ERROR, 34, "Driver error") \
98 X(GPIO_ERR_INVALID_STATE, 35, "Invalid state") \
99 X(GPIO_ERR_INVALID_ARG, 36, "Invalid argument") \
100 X(GPIO_ERR_CALIBRATION_FAILURE, 37, "Calibration failure") \
101 X(GPIO_ERR_UNKNOWN, 38, "Unknown error")
109#define X(NAME, VALUE, DESC) NAME = VALUE,
123#define X(NAME, VALUE, DESC) \
124 case hf_gpio_err_t::NAME: \
430 [[nodiscard]]
bool IsInput() const noexcept;
436 [[nodiscard]]
bool IsOutput() const noexcept;
601 void* user_data =
nullptr) noexcept {
822 current_direction_ = direction;
891 current_state_ = state;
901 active_state_ = active_state;
970 current_state_ = state;
988 bool should_be_high =
1027 switch (active_state) {
1029 return "ActiveHigh";
1038 switch (direction) {
1049 switch (output_mode) {
1060 switch (pull_mode) {
1068 return "PullUpDown";
1079 return "RisingEdge";
1081 return "FallingEdge";
1098 if (!initialized_) {
1109 if (direction != current_direction_) {
1118 if (!initialized_) {
1129 if (mode != output_mode_) {
#define X(NAME, VALUE, DESC)
Definition BaseGpio.h:109
Platform-agnostic hardware type definitions for the HardFOC system.
uint32_t hf_u32_t
Platform-agnostic 32-bit unsigned integer type.
Definition HardwareTypes.h:52
uint8_t hf_u8_t
Platform-agnostic 8-bit unsigned integer type.
Definition HardwareTypes.h:40
hf_i32_t hf_pin_num_t
Platform-agnostic GPIO pin number type.
Definition HardwareTypes.h:99
Unified GPIO base class for all digital GPIO implementations.
Definition BaseGpio.h:294
hf_gpio_output_mode_t output_mode_
Output drive mode.
Definition BaseGpio.h:774
hf_gpio_state_t GetCurrentState() const noexcept
Get the current logical state of the pin.
Definition BaseGpio.h:877
virtual bool Deinitialize() noexcept
Deinitialize the GPIO pin.
Definition BaseGpio.h:716
void SetActiveState(hf_gpio_active_state_t active_state) noexcept
Set the active state polarity configuration.
Definition BaseGpio.h:900
BaseGpio(const BaseGpio ©)=delete
Copy constructor is deleted to avoid copying instances.
hf_gpio_err_t ValidateBasicOperation() const noexcept
Validate basic parameters before GPIO operations.
Definition BaseGpio.h:976
hf_gpio_err_t VerifyOutputMode(hf_gpio_output_mode_t &mode) const noexcept
Verify current output mode setting by reading from hardware registers.
Definition BaseGpio.h:1117
static const char * ToString(hf_gpio_state_t state) noexcept
Definition BaseGpio.h:1004
virtual hf_gpio_err_t GetDirectionImpl(hf_gpio_direction_t &direction) const noexcept=0
Hardware read-back of current pin direction from registers.
bool IsInitialized() const noexcept
Check if the pin is initialized.
Definition BaseGpio.h:786
hf_gpio_err_t SetInactive() noexcept
Set the GPIO to inactive state.
Definition BaseGpio.h:920
bool EnsureDeinitialized() noexcept
Ensures the pin is deinitialized (lazy deinitialization).
Definition BaseGpio.h:797
hf_pin_num_t GetPin() const noexcept
Get the GPIO pin number/identifier.
Definition BaseGpio.h:805
bool IsInput() const noexcept
Check if the pin is currently configured as input.
Definition BaseGpio.h:833
hf_gpio_err_t VerifyDirection(hf_gpio_direction_t &direction) const noexcept
Verify current direction setting by reading from hardware registers.
Definition BaseGpio.h:1097
bool EnsureInitialized() noexcept
Ensures the pin is initialized (lazy initialization).
Definition BaseGpio.h:790
virtual const char * GetDescription() const noexcept=0
Get human-readable description of this GPIO instance.
virtual hf_gpio_err_t EnableInterrupt() noexcept
Enable GPIO interrupt.
Definition BaseGpio.h:609
virtual hf_gpio_err_t WaitForInterrupt(hf_u32_t timeout_ms=0) noexcept
Wait for GPIO interrupt to occur.
Definition BaseGpio.h:626
hf_gpio_active_state_t GetActiveState() const noexcept
Get the active state polarity configuration.
Definition BaseGpio.h:896
virtual hf_gpio_err_t ResetDiagnostics() noexcept
Reset GPIO diagnostic information.
Definition BaseGpio.h:666
hf_gpio_direction_t current_direction_
Current pin direction.
Definition BaseGpio.h:772
hf_gpio_pull_mode_t pull_mode_
Pull resistor configuration.
Definition BaseGpio.h:775
virtual hf_gpio_err_t GetOutputModeImpl(hf_gpio_output_mode_t &mode) const noexcept=0
Hardware read-back of current output mode from registers.
virtual hf_gpio_err_t GetDiagnostics(hf_gpio_diagnostics_t &diagnostics) const noexcept
Get GPIO diagnostic information.
Definition BaseGpio.h:686
hf_gpio_err_t SetDirection(hf_gpio_direction_t direction) noexcept
Set the pin direction (input or output).
Definition BaseGpio.h:814
virtual bool IsPinAvailable() const noexcept=0
Check if the pin is available for GPIO operations.
virtual hf_gpio_err_t SetPinLevelImpl(hf_gpio_level_t level) noexcept=0
Platform-specific implementation for setting pin electrical level.
hf_gpio_state_t current_state_
Current logical state.
Definition BaseGpio.h:776
hf_gpio_err_t SetPullMode(hf_gpio_pull_mode_t mode) noexcept
Set the pull resistor mode.
Definition BaseGpio.h:863
virtual bool Initialize() noexcept=0
Initialize the GPIO pin with current configuration.
hf_gpio_err_t Toggle() noexcept
Toggle the GPIO state.
Definition BaseGpio.h:935
hf_gpio_err_t IsActive(bool &is_active) noexcept
Check if the GPIO is currently active.
Definition BaseGpio.h:959
BaseGpio & operator=(const BaseGpio ©)=delete
Assignment operator is deleted to avoid copying instances.
hf_gpio_err_t VerifyHardwareConfiguration() const noexcept
Perform comprehensive hardware verification of all pin settings.
Definition BaseGpio.h:1137
hf_gpio_diagnostics_t diagnostics_
GPIO diagnostic information.
Definition BaseGpio.h:778
virtual hf_gpio_err_t ClearInterruptStats() noexcept
Clear interrupt statistics.
Definition BaseGpio.h:643
virtual hf_gpio_err_t SetOutputModeImpl(hf_gpio_output_mode_t mode) noexcept=0
Platform-specific implementation for setting output mode.
virtual ~BaseGpio() noexcept=default
Virtual destructor for proper cleanup of derived classes.
virtual hf_gpio_err_t SupportsInterrupts() const noexcept
Check if this GPIO implementation supports interrupts.
Definition BaseGpio.h:588
hf_gpio_direction_t GetDirection() const noexcept
Get the current pin direction.
Definition BaseGpio.h:810
virtual hf_gpio_err_t GetPinLevelImpl(hf_gpio_level_t &level) noexcept=0
Platform-specific implementation for getting pin electrical level.
hf_gpio_err_t SetState(hf_gpio_state_t state) noexcept
Set the pin to a specific logical state.
Definition BaseGpio.h:881
hf_gpio_err_t SetActive() noexcept
Set the GPIO to active state.
Definition BaseGpio.h:905
hf_gpio_err_t SetOutputMode(hf_gpio_output_mode_t mode) noexcept
Set the output drive mode.
Definition BaseGpio.h:845
virtual hf_gpio_err_t ConfigureInterrupt(hf_gpio_interrupt_trigger_t trigger, InterruptCallback callback=nullptr, void *user_data=nullptr) noexcept
Configure GPIO interrupt settings.
Definition BaseGpio.h:599
hf_gpio_active_state_t active_state_
Active state polarity.
Definition BaseGpio.h:773
hf_gpio_pull_mode_t GetPullMode() const noexcept
Get the current pull resistor mode.
Definition BaseGpio.h:859
virtual hf_gpio_err_t GetInterruptStatus(InterruptStatus &status) noexcept
Get interrupt status information.
Definition BaseGpio.h:635
virtual hf_gpio_err_t DisableInterrupt() noexcept
Disable GPIO interrupt.
Definition BaseGpio.h:617
virtual hf_gpio_err_t ResetStatistics() noexcept
Reset GPIO operation statistics.
Definition BaseGpio.h:656
bool initialized_
Initialization state flag.
Definition BaseGpio.h:771
hf_gpio_statistics_t statistics_
GPIO operation statistics.
Definition BaseGpio.h:777
virtual hf_gpio_err_t SetDirectionImpl(hf_gpio_direction_t direction) noexcept=0
Platform-specific implementation for setting pin direction.
virtual hf_u8_t GetMaxPins() const noexcept=0
Get the maximum number of pins supported by this GPIO instance.
hf_gpio_level_t StateToLevel(hf_gpio_state_t state) const noexcept
Convert logical state to electrical level based on polarity.
Definition BaseGpio.h:986
const hf_pin_num_t pin_
GPIO pin number/identifier.
Definition BaseGpio.h:770
virtual hf_gpio_err_t SetPullModeImpl(hf_gpio_pull_mode_t mode) noexcept=0
Platform-specific implementation for setting pull resistor mode.
bool IsOutput() const noexcept
Check if the pin is currently configured as output.
Definition BaseGpio.h:837
virtual hf_gpio_pull_mode_t GetPullModeImpl() const noexcept=0
Hardware read-back of current pull resistor mode from registers.
hf_gpio_state_t LevelToState(hf_gpio_level_t level) const noexcept
Convert electrical level to logical state based on polarity.
Definition BaseGpio.h:993
virtual hf_gpio_err_t GetStatistics(hf_gpio_statistics_t &statistics) const noexcept
Get GPIO operation statistics.
Definition BaseGpio.h:676
hf_gpio_output_mode_t GetOutputMode() const noexcept
Get the output drive mode.
Definition BaseGpio.h:841
hf_gpio_level_t
GPIO pin electrical levels.
Definition BaseGpio.h:148
hf_gpio_err_t
HardFOC GPIO error codes.
Definition BaseGpio.h:108
hf_gpio_state_t
GPIO pin logical states.
Definition BaseGpio.h:138
hf_gpio_direction_t
GPIO pin direction/mode configuration.
Definition BaseGpio.h:168
hf_gpio_interrupt_trigger_t
GPIO interrupt trigger types.
Definition BaseGpio.h:200
hf_gpio_pull_mode_t
GPIO pull resistor configuration.
Definition BaseGpio.h:188
hf_gpio_output_mode_t
GPIO output drive modes.
Definition BaseGpio.h:178
void(*)(BaseGpio *gpio, hf_gpio_interrupt_trigger_t trigger, void *user_data) InterruptCallback
GPIO interrupt callback function type.
Definition BaseGpio.h:220
#define HF_GPIO_ERR_LIST(X)
HardFOC GPIO error codes macro list.
Definition BaseGpio.h:45
hf_gpio_active_state_t
GPIO active state polarity configuration.
Definition BaseGpio.h:158
constexpr std::string_view HfGpioErrToString(hf_gpio_err_t err) noexcept
Convert hf_gpio_err_t to human-readable string.
Definition BaseGpio.h:121
@ HF_GPIO_LEVEL_LOW
Electrical low level (0V)
@ HF_GPIO_LEVEL_HIGH
Electrical high level (VCC)
@ GPIO_ERR_UNSUPPORTED_OPERATION
@ GPIO_ERR_PIN_ACCESS_DENIED
@ GPIO_ERR_HARDWARE_FAULT
@ GPIO_ERR_NOT_INITIALIZED
@ HF_GPIO_STATE_ACTIVE
Logical active state.
@ HF_GPIO_STATE_INACTIVE
Logical inactive state.
@ HF_GPIO_DIRECTION_INPUT
Pin configured as input.
@ HF_GPIO_DIRECTION_OUTPUT
Pin configured as output.
@ HF_GPIO_INTERRUPT_TRIGGER_BOTH_EDGES
Trigger on both rising and falling edges.
@ HF_GPIO_INTERRUPT_TRIGGER_RISING_EDGE
Trigger on rising edge (low to high)
@ HF_GPIO_INTERRUPT_TRIGGER_NONE
No interrupt (disabled)
@ HF_GPIO_INTERRUPT_TRIGGER_FALLING_EDGE
Trigger on falling edge (high to low)
@ HF_GPIO_INTERRUPT_TRIGGER_HIGH_LEVEL
Trigger on high level.
@ HF_GPIO_INTERRUPT_TRIGGER_LOW_LEVEL
Trigger on low level.
@ HF_GPIO_PULL_MODE_UP
Internal pull-up resistor enabled.
@ HF_GPIO_PULL_MODE_UP_DOWN
Both pull-up and pull-down resistors enabled.
@ HF_GPIO_PULL_MODE_DOWN
Internal pull-down resistor enabled.
@ HF_GPIO_PULL_MODE_FLOATING
No pull resistor (floating/high-impedance)
@ HF_GPIO_OUTPUT_MODE_PUSH_PULL
Push-pull output (strong high and low)
@ HF_GPIO_OUTPUT_MODE_OPEN_DRAIN
Open-drain output (strong low, high-impedance high)
@ HF_GPIO_ACTIVE_LOW
Active state is electrical low.
@ HF_GPIO_ACTIVE_HIGH
Active state is electrical high.
GPIO interrupt status structure.
Definition BaseGpio.h:227
bool is_enabled
Interrupt currently enabled.
Definition BaseGpio.h:228
bool has_callback
Callback function is registered.
Definition BaseGpio.h:231
hf_u32_t interrupt_count
Number of interrupts occurred.
Definition BaseGpio.h:230
hf_gpio_interrupt_trigger_t trigger_type
Current trigger configuration.
Definition BaseGpio.h:229
GPIO diagnostic information.
Definition BaseGpio.h:259
hf_gpio_err_t lastErrorCode
Last error code.
Definition BaseGpio.h:261
hf_gpio_diagnostics_t()
Definition BaseGpio.h:269
hf_u32_t consecutiveErrors
Consecutive error count.
Definition BaseGpio.h:263
bool gpioHealthy
Overall GPIO health status.
Definition BaseGpio.h:260
bool pinAvailable
Pin availability status.
Definition BaseGpio.h:264
bool interruptEnabled
Interrupt enabled status.
Definition BaseGpio.h:266
hf_u32_t currentState
Current pin state.
Definition BaseGpio.h:267
hf_u32_t lastErrorTimestamp
Last error timestamp.
Definition BaseGpio.h:262
bool interruptSupported
Interrupt support status.
Definition BaseGpio.h:265
GPIO operation statistics.
Definition BaseGpio.h:238
hf_gpio_statistics_t()
Definition BaseGpio.h:249
hf_u32_t stateChanges
Number of state changes.
Definition BaseGpio.h:242
hf_u32_t averageOperationTimeUs
Average operation time (microseconds)
Definition BaseGpio.h:245
hf_u32_t minOperationTimeUs
Minimum operation time.
Definition BaseGpio.h:247
hf_u32_t interruptCount
Number of interrupts received.
Definition BaseGpio.h:244
hf_u32_t totalOperations
Total GPIO operations performed.
Definition BaseGpio.h:239
hf_u32_t maxOperationTimeUs
Maximum operation time.
Definition BaseGpio.h:246
hf_u32_t directionChanges
Number of direction changes.
Definition BaseGpio.h:243
hf_u32_t successfulOperations
Successful operations.
Definition BaseGpio.h:240
hf_u32_t failedOperations
Failed operations.
Definition BaseGpio.h:241