HardFOC Vortex V1 HAL
Unified HAL API for the HardFOC Vortex motor-control platform (ESP32-C6, TMC9660)
Loading...
Searching...
No Matches
GpioManager.h
Go to the documentation of this file.
1
34#ifndef VORTEX_GPIO_MANAGER_H_
35#define VORTEX_GPIO_MANAGER_H_
36
37#include <array>
38#include <atomic>
39#include <cstdint>
40#include <optional>
41#include <string_view>
42
43#include "base/BaseGpio.h"
44#include "handlers/pcal95555/Pcal95555Handler.h"
45#include "RtosMutex.h"
46#include "core/hf-core-drivers/internal/hf-pincfg/src/hf_functional_pin_config_vortex_v1.hpp"
47
48//==============================================================================
49// FORWARD DECLARATIONS
50//==============================================================================
51
52class Pcal95555Handler;
53class Tmc9660Handler;
54class MotorController;
55
56//==============================================================================
57// GPIO ENTRY — one slot per pin in the fixed-size array
58//==============================================================================
59
66struct GpioEntry {
67 BaseGpio* driver;
68 HfFunctionalGpioPin functional_pin;
69 bool registered{false};
70 uint32_t access_count{0};
71 uint32_t error_count{0};
72
73 GpioEntry() noexcept
74 : driver(nullptr),
75 functional_pin(HfFunctionalGpioPin::HF_FUNCTIONAL_GPIO_COUNT) {}
76};
77
78//==============================================================================
79// DIAGNOSTICS STRUCTURE
80//==============================================================================
81
86 uint32_t pins_by_chip[static_cast<uint8_t>(HfGpioChipType::TMC9660_CONTROLLER) + 1];
87 uint32_t pins_by_category[4];
92 hf_gpio_err_t last_error;
93};
94
95//==============================================================================
96// GPIO MANAGER
97//==============================================================================
98
111public:
112 //==========================================================================
113 // SINGLETON AND LIFECYCLE
114 //==========================================================================
115
120 static GpioManager& GetInstance() noexcept;
121
122 GpioManager(const GpioManager&) = delete;
126
131 [[nodiscard]] bool EnsureInitialized() noexcept;
132
137 [[nodiscard]] bool Deinitialize() noexcept;
138
141 [[nodiscard]] bool IsInitialized() const noexcept { return initialized_.load(std::memory_order_acquire); }
142
143 //==========================================================================
144 // GPIO ACCESS — by string name or functional enum
145 //==========================================================================
146
152 [[nodiscard]] BaseGpio* Get(std::string_view name) noexcept;
153
159 [[nodiscard]] BaseGpio* Get(HfFunctionalGpioPin pin) noexcept;
160
162 [[nodiscard]] bool Contains(std::string_view name) const noexcept;
163
165 [[nodiscard]] size_t Size() const noexcept;
166
173 [[nodiscard]] hf_gpio_err_t DebugReadPcal95555Pin(uint8_t expander_pin_0_to_15,
174 bool& out_level) noexcept;
175
181 [[nodiscard]] hf_gpio_err_t DebugReadPcal95555InputSnapshot(uint16_t& out_bits) noexcept;
182
194 void RegisterTmc9660BridgePinsIfNeeded() noexcept;
195
201 void UnregisterTmc9660BridgePins() noexcept;
202
203 //==========================================================================
204 // BASIC GPIO OPERATIONS — string-based routing
205 //==========================================================================
206
214 [[nodiscard]] hf_gpio_err_t Set(std::string_view name, bool value) noexcept;
215
221 [[nodiscard]] hf_gpio_err_t SetActive(std::string_view name) noexcept;
222
228 [[nodiscard]] hf_gpio_err_t SetInactive(std::string_view name) noexcept;
229
239 [[nodiscard]] hf_gpio_err_t Read(std::string_view name, bool& state) noexcept;
240
246 [[nodiscard]] hf_gpio_err_t Toggle(std::string_view name) noexcept;
247
254 [[nodiscard]] hf_gpio_err_t IsActive(std::string_view name, bool& active) noexcept;
255
256 //==========================================================================
257 // PIN CONFIGURATION
258 //==========================================================================
259
266 [[nodiscard]] hf_gpio_err_t SetDirection(std::string_view name, hf_gpio_direction_t direction) noexcept;
267
274 [[nodiscard]] hf_gpio_err_t SetPullMode(std::string_view name, hf_gpio_pull_mode_t pull_mode) noexcept;
275
282 [[nodiscard]] hf_gpio_err_t SetOutputMode(std::string_view name, hf_gpio_output_mode_t mode) noexcept;
283
284 //==========================================================================
285 // INTERRUPT SUPPORT — forwarded to underlying BaseGpio driver
286 //==========================================================================
287
296 [[nodiscard]] hf_gpio_err_t ConfigureInterrupt(std::string_view name,
297 hf_gpio_interrupt_trigger_t trigger,
298 InterruptCallback callback,
299 void* user_data = nullptr) noexcept;
300
306 [[nodiscard]] hf_gpio_err_t EnableInterrupt(std::string_view name) noexcept;
307
313 [[nodiscard]] hf_gpio_err_t DisableInterrupt(std::string_view name) noexcept;
314
320 [[nodiscard]] bool SupportsInterrupts(std::string_view name) const noexcept;
321
322 //==========================================================================
323 // DIAGNOSTICS
324 //==========================================================================
325
331 [[nodiscard]] hf_gpio_err_t GetSystemDiagnostics(GpioSystemDiagnostics& diagnostics) const noexcept;
332
337 [[nodiscard]] hf_gpio_err_t GetLastError() const noexcept { return last_error_; }
338
340 void DumpStatistics() const noexcept;
341
342private:
343 GpioManager() noexcept = default;
344 ~GpioManager() = default;
345
346 [[nodiscard]] bool Initialize() noexcept;
347
349 void ReleaseLocked() noexcept;
350
352 [[nodiscard]] size_t FindByName(std::string_view name) const noexcept;
353
355 [[nodiscard]] bool EnsurePcal95555Handler() noexcept;
356
357
358 //--------------------------------------------------------------------------
359 // Storage
360 //--------------------------------------------------------------------------
361
362 static constexpr size_t kMaxEntries =
363 static_cast<size_t>(HfFunctionalGpioPin::HF_FUNCTIONAL_GPIO_COUNT);
364 static constexpr size_t kInvalidIndex = SIZE_MAX;
365
368 uint64_t init_time_ms_{0};
369
370 uint32_t total_ops_{0};
371 uint32_t successful_ops_{0};
372 uint32_t failed_ops_{0};
373 hf_gpio_err_t last_error_{hf_gpio_err_t::GPIO_SUCCESS};
374
375 std::atomic<bool> initialized_{false};
376 mutable RtosMutex mutex_;
377
378 //--------------------------------------------------------------------------
379 // Hardware handlers
380 //--------------------------------------------------------------------------
381
382 std::optional<Pcal95555Handler> pcal95555_handler_;
383 std::array<std::optional<Pcal95555GpioPin>, 16> pcal_pins_{};
385 BaseGpio* pcal_host_int_gpio_{nullptr};
386};
387
388//==============================================================================
389// CONVENIENCE
390//==============================================================================
391
396[[nodiscard]] inline GpioManager& GetGpioManager() noexcept {
398}
399
400#endif // VORTEX_GPIO_MANAGER_H_
GpioManager & GetGpioManager() noexcept
Convenience accessor — equivalent to GpioManager::GetInstance().
Definition GpioManager.h:396
Singleton managing GPIO pins across ESP32, PCAL95555 I/O expander, and TMC9660 controllers.
Definition GpioManager.h:110
bool Contains(std::string_view name) const noexcept
Check if a named pin exists in the registry.
Definition GpioManager.cpp:427
uint32_t failed_ops_
Definition GpioManager.h:372
uint64_t init_time_ms_
Definition GpioManager.h:368
std::optional< Pcal95555Handler > pcal95555_handler_
In place; emplaced at init.
Definition GpioManager.h:382
std::array< GpioEntry, kMaxEntries > entries_
Definition GpioManager.h:366
hf_gpio_err_t last_error_
Definition GpioManager.h:373
GpioManager(const GpioManager &)=delete
hf_gpio_err_t Read(std::string_view name, bool &state) noexcept
Read the logical (active) state of a pin.
Definition GpioManager.cpp:480
static constexpr size_t kMaxEntries
Definition GpioManager.h:362
bool IsInitialized() const noexcept
Check whether the manager is initialised.
Definition GpioManager.h:141
GpioManager(GpioManager &&)=delete
void UnregisterTmc9660BridgePins() noexcept
Remove the TMC9660 GPIO17/18 entries (their objects belong to the TMC9660 handler).
Definition GpioManager.cpp:382
GpioManager & operator=(GpioManager &&)=delete
hf_gpio_err_t SetOutputMode(std::string_view name, hf_gpio_output_mode_t mode) noexcept
Configure output driver mode (push-pull or open-drain).
Definition GpioManager.cpp:530
size_t registered_count_
Definition GpioManager.h:367
hf_gpio_err_t IsActive(std::string_view name, bool &active) noexcept
Check whether a pin is currently in its active state.
Definition GpioManager.cpp:506
hf_gpio_err_t SetDirection(std::string_view name, hf_gpio_direction_t direction) noexcept
Change pin direction (input / output / open-drain).
Definition GpioManager.cpp:514
size_t FindByName(std::string_view name) const noexcept
Find entry index by string name. Returns kInvalidIndex if not found.
Definition GpioManager.cpp:397
std::atomic< bool > initialized_
Definition GpioManager.h:375
hf_gpio_err_t DebugReadPcal95555InputSnapshot(uint16_t &out_bits) noexcept
Read INPUT_PORT_0 and INPUT_PORT_1 in one driver call (bits 0–7 = port0, 8–15 = port1).
Definition GpioManager.cpp:314
uint32_t successful_ops_
Definition GpioManager.h:371
bool SupportsInterrupts(std::string_view name) const noexcept
Check whether a pin's underlying driver supports interrupts.
Definition GpioManager.cpp:569
bool Deinitialize() noexcept
Reset GPIO manager to uninitialised state.
Definition GpioManager.cpp:70
std::array< std::optional< Pcal95555GpioPin >, 16 > pcal_pins_
Definition GpioManager.h:383
bool Initialize() noexcept
Definition GpioManager.cpp:101
hf_gpio_err_t DisableInterrupt(std::string_view name) noexcept
Disable the interrupt on a named pin.
Definition GpioManager.cpp:561
hf_gpio_err_t GetLastError() const noexcept
Get the most recent error code from any GPIO operation.
Definition GpioManager.h:337
hf_gpio_err_t Toggle(std::string_view name) noexcept
Toggle pin output between high and low.
Definition GpioManager.cpp:493
size_t Size() const noexcept
Number of registered GPIO pins.
Definition GpioManager.cpp:432
hf_gpio_err_t DebugReadPcal95555Pin(uint8_t expander_pin_0_to_15, bool &out_level) noexcept
Read one PCAL95555 expander pin by hardware index (0–15), independent of whether that line is registe...
Definition GpioManager.cpp:301
hf_gpio_err_t GetSystemDiagnostics(GpioSystemDiagnostics &diagnostics) const noexcept
Populate a diagnostics snapshot.
Definition GpioManager.cpp:581
bool EnsureInitialized() noexcept
Register all GPIO pins and initialise drivers (idempotent).
Definition GpioManager.cpp:57
RtosMutex mutex_
Definition GpioManager.h:376
hf_gpio_err_t Set(std::string_view name, bool value) noexcept
Drive a GPIO output to its active (true) or inactive (false) state.
Definition GpioManager.cpp:441
uint32_t total_ops_
Definition GpioManager.h:370
bool EnsurePcal95555Handler() noexcept
Lazy-init the PCAL95555 handler (needs I²C from CommChannelsManager).
Definition GpioManager.cpp:238
static constexpr size_t kInvalidIndex
Definition GpioManager.h:364
hf_gpio_err_t ConfigureInterrupt(std::string_view name, hf_gpio_interrupt_trigger_t trigger, InterruptCallback callback, void *user_data=nullptr) noexcept
Configure interrupt on a named GPIO pin.
Definition GpioManager.cpp:542
hf_gpio_err_t EnableInterrupt(std::string_view name) noexcept
Enable a previously configured interrupt on a named pin.
Definition GpioManager.cpp:553
void DumpStatistics() const noexcept
Log current GPIO state and statistics to the console.
Definition GpioManager.cpp:616
void RegisterTmc9660BridgePinsIfNeeded() noexcept
After MotorController::CreateOnboardDevice(), register TMC9660 GPIO17/18 in the table.
Definition GpioManager.cpp:325
hf_gpio_err_t SetPullMode(std::string_view name, hf_gpio_pull_mode_t pull_mode) noexcept
Configure internal pull-up / pull-down resistors.
Definition GpioManager.cpp:522
hf_gpio_err_t SetInactive(std::string_view name) noexcept
Drive a pin to its inactive state.
Definition GpioManager.cpp:467
BaseGpio * Get(std::string_view name) noexcept
Get the pin object of a named pin.
Definition GpioManager.cpp:406
void ReleaseLocked() noexcept
Drop every entry and destroy the pin objects and the expander handler. Caller holds mutex_.
Definition GpioManager.cpp:79
BaseGpio * pcal_host_int_gpio_
ESP32 GPIO for PCAL95555 /INT (COMM_I2C_PCAL95555_INT); not in the GPIO registry table.
Definition GpioManager.h:385
GpioManager & operator=(const GpioManager &)=delete
static GpioManager & GetInstance() noexcept
Access the singleton instance (Meyers singleton, thread-safe).
Definition GpioManager.cpp:48
hf_gpio_err_t SetActive(std::string_view name) noexcept
Drive a pin to its active state.
Definition GpioManager.cpp:454
Singleton managing multiple TMC9660 motor controllers on Vortex V1.
Definition MotorController.h:207
A single GPIO entry in the fixed-size registry.
Definition GpioManager.h:66
uint32_t error_count
Error counter.
Definition GpioManager.h:71
uint32_t access_count
Operation counter.
Definition GpioManager.h:70
bool registered
Whether this slot is active.
Definition GpioManager.h:69
BaseGpio * driver
Pin object (not owned; static storage elsewhere)
Definition GpioManager.h:67
GpioEntry() noexcept
Definition GpioManager.h:73
HfFunctionalGpioPin functional_pin
Functional pin enum.
Definition GpioManager.h:68
System-level GPIO diagnostics snapshot.
Definition GpioManager.h:83
uint32_t pins_by_chip[static_cast< uint8_t >(HfGpioChipType::TMC9660_CONTROLLER)+1]
Pin count per chip type.
Definition GpioManager.h:86
uint32_t successful_operations
Calls that returned GPIO_SUCCESS.
Definition GpioManager.h:89
hf_gpio_err_t last_error
Most recent error code.
Definition GpioManager.h:92
uint32_t total_pins_registered
Number of GPIO pins currently registered.
Definition GpioManager.h:85
bool system_healthy
true when manager is initialised and operational.
Definition GpioManager.h:84
uint32_t total_operations
Lifetime count of read/write/toggle calls.
Definition GpioManager.h:88
uint64_t system_uptime_ms
Milliseconds since manager initialisation.
Definition GpioManager.h:91
uint32_t pins_by_category[4]
Pin count per functional category.
Definition GpioManager.h:87
uint32_t failed_operations
Calls that returned an error.
Definition GpioManager.h:90