HardFOC Flux V1 HAL
Unified HAL API for the HardFOC Flux valve-control platform (ESP32-S3)
Loading...
Searching...
No Matches
GpioManager.h
Go to the documentation of this file.
1
18#ifndef FLUX_GPIO_MANAGER_H_
19#define FLUX_GPIO_MANAGER_H_
20
21#include <array>
22#include <atomic>
23#include <cstdint>
24#include <string_view>
25
26#include "core/hf-core-drivers/internal/hf-internal-interface-wrap/inc/base/BaseGpio.h"
27#include "RtosMutex.h"
29#include "core/hf-core-drivers/internal/hf-pincfg/src/hf_functional_pin_config_flux_v1.hpp"
30
31//==============================================================================
32// GPIO ENTRY
33//==============================================================================
34
42struct GpioEntry {
43 BaseGpio* driver;
44 HfFunctionalGpioPin functional_pin;
45 bool registered{false};
46 uint32_t access_count{0};
47 uint32_t error_count{0};
48
49 GpioEntry() noexcept
50 : driver(nullptr), functional_pin(HfFunctionalGpioPin::HF_FUNCTIONAL_GPIO_COUNT) {}
51};
52
53//==============================================================================
54// GPIO SYSTEM DIAGNOSTICS
55//==============================================================================
56
67
68//==============================================================================
69// GPIO MANAGER
70//==============================================================================
71
82public:
83 //==========================================================================
84 // SINGLETON AND LIFECYCLE
85 //==========================================================================
86
91 static GpioManager& GetInstance() noexcept;
92
93 GpioManager(const GpioManager&) = delete;
97
102 [[nodiscard]] bool EnsureInitialized() noexcept;
103
106 [[nodiscard]] bool IsInitialized() const noexcept { return initialized_.load(std::memory_order_acquire); }
107
112 [[nodiscard]] bool Deinitialize() noexcept;
113
118 [[nodiscard]] hf_gpio_err_t GetLastError() const noexcept { return last_error_; }
119
125 [[nodiscard]] flux::hal::ManagerError GetManagerError() const noexcept {
126 return manager_error_.load(std::memory_order_acquire);
127 }
128
129 //==========================================================================
130 // GPIO ACCESS — by string name
131 //==========================================================================
132
134 [[nodiscard]] BaseGpio* Get(std::string_view name) noexcept;
135
137 [[nodiscard]] BaseGpio* Get(HfFunctionalGpioPin pin) noexcept;
138
140 [[nodiscard]] bool Contains(std::string_view name) const noexcept;
141
143 [[nodiscard]] size_t Size() const noexcept;
144
145 //==========================================================================
146 // BASIC GPIO OPERATIONS — string-based routing
147 //==========================================================================
148
155 [[nodiscard]] hf_gpio_err_t Set(std::string_view name, bool value) noexcept;
156
162 [[nodiscard]] hf_gpio_err_t SetActive(std::string_view name) noexcept;
163
169 [[nodiscard]] hf_gpio_err_t SetInactive(std::string_view name) noexcept;
170
177 [[nodiscard]] hf_gpio_err_t Read(std::string_view name, bool& state) noexcept;
178
184 [[nodiscard]] hf_gpio_err_t Toggle(std::string_view name) noexcept;
185
192 [[nodiscard]] hf_gpio_err_t IsActive(std::string_view name, bool& active) noexcept;
193
194 //==========================================================================
195 // PIN CONFIGURATION
196 //==========================================================================
197
204 [[nodiscard]] hf_gpio_err_t SetDirection(std::string_view name, hf_gpio_direction_t direction) noexcept;
205
212 [[nodiscard]] hf_gpio_err_t SetPullMode(std::string_view name, hf_gpio_pull_mode_t pull_mode) noexcept;
213
220 [[nodiscard]] hf_gpio_err_t SetOutputMode(std::string_view name, hf_gpio_output_mode_t mode) noexcept;
221
222 //==========================================================================
223 // INTERRUPT SUPPORT — forwarded to underlying BaseGpio driver
224 //==========================================================================
225
234 [[nodiscard]] hf_gpio_err_t ConfigureInterrupt(std::string_view name,
235 hf_gpio_interrupt_trigger_t trigger,
236 InterruptCallback callback,
237 void* user_data = nullptr) noexcept;
243 [[nodiscard]] hf_gpio_err_t EnableInterrupt(std::string_view name) noexcept;
244
250 [[nodiscard]] hf_gpio_err_t DisableInterrupt(std::string_view name) noexcept;
251
257 [[nodiscard]] bool SupportsInterrupts(std::string_view name) const noexcept;
258
259 //==========================================================================
260 // DIAGNOSTICS
261 //==========================================================================
262
268 [[nodiscard]] hf_gpio_err_t GetSystemDiagnostics(GpioSystemDiagnostics& diagnostics) const noexcept;
269
271 void DumpStatistics() const noexcept;
272
273private:
274 GpioManager() noexcept = default;
275 ~GpioManager() = default;
276
277 bool Initialize() noexcept;
278 void ReleaseLocked() noexcept;
279
281 [[nodiscard]] size_t FindByName(std::string_view name) const noexcept;
282
284 static constexpr size_t kMaxEntries = static_cast<size_t>(HfFunctionalGpioPin::HF_FUNCTIONAL_GPIO_COUNT);
285 static constexpr size_t kInvalidIndex = SIZE_MAX;
286
289 uint64_t init_time_ms_{0};
290
291 uint32_t total_ops_{0};
292 uint32_t successful_ops_{0};
293 uint32_t failed_ops_{0};
294 hf_gpio_err_t last_error_{hf_gpio_err_t::GPIO_SUCCESS};
295
296 std::atomic<bool> initialized_{false};
297 std::atomic<flux::hal::ManagerError> manager_error_{flux::hal::ManagerError::NotInitialized};
298 mutable RtosMutex mutex_;
299};
300
301//==============================================================================
302// CONVENIENCE
303//==============================================================================
304
309[[nodiscard]] inline GpioManager& GetGpioManager() noexcept {
311}
312
313#endif // FLUX_GPIO_MANAGER_H_
GpioManager & GetGpioManager() noexcept
Convenience accessor — equivalent to GpioManager::GetInstance().
Definition GpioManager.h:309
Singleton that manages all MCU GPIO pins defined in the X-MACRO pin config.
Definition GpioManager.h:81
bool Contains(std::string_view name) const noexcept
Check if a named pin exists in the registry.
Definition GpioManager.cpp:170
uint32_t failed_ops_
Definition GpioManager.h:293
uint64_t init_time_ms_
Timestamp (ms) when Initialize() completed.
Definition GpioManager.h:289
std::array< GpioEntry, kMaxEntries > entries_
Definition GpioManager.h:287
hf_gpio_err_t last_error_
Definition GpioManager.h:294
GpioManager(const GpioManager &)=delete
hf_gpio_err_t Read(std::string_view name, bool &state) noexcept
Read the current digital level of a pin.
Definition GpioManager.cpp:215
static constexpr size_t kMaxEntries
Maximum registerable GPIO entries (all GPIO + USER pins from pincfg).
Definition GpioManager.h:284
bool IsInitialized() const noexcept
Check whether the manager is initialised.
Definition GpioManager.h:106
GpioManager(GpioManager &&)=delete
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:257
size_t registered_count_
Definition GpioManager.h:288
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:237
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:245
size_t FindByName(std::string_view name) const noexcept
Find entry index by string name. Returns kInvalidIndex if not found.
Definition GpioManager.cpp:147
std::atomic< bool > initialized_
Definition GpioManager.h:296
uint32_t successful_ops_
Definition GpioManager.h:292
bool SupportsInterrupts(std::string_view name) const noexcept
Check whether a pin's underlying driver supports interrupts.
Definition GpioManager.cpp:353
bool Deinitialize() noexcept
Reset GPIO manager to uninitialized state.
Definition GpioManager.cpp:301
bool Initialize() noexcept
Definition GpioManager.cpp:51
hf_gpio_err_t DisableInterrupt(std::string_view name) noexcept
Disable the interrupt on a named pin.
Definition GpioManager.cpp:347
hf_gpio_err_t GetLastError() const noexcept
Get the most recent error code.
Definition GpioManager.h:118
hf_gpio_err_t Toggle(std::string_view name) noexcept
Toggle pin output between high and low.
Definition GpioManager.cpp:226
size_t Size() const noexcept
Number of registered GPIO pins.
Definition GpioManager.cpp:174
hf_gpio_err_t GetSystemDiagnostics(GpioSystemDiagnostics &diagnostics) const noexcept
Populate a diagnostics snapshot.
Definition GpioManager.cpp:267
bool EnsureInitialized() noexcept
Register all GPIO pins and initialise drivers (idempotent).
Definition GpioManager.cpp:35
RtosMutex mutex_
Definition GpioManager.h:298
hf_gpio_err_t Set(std::string_view name, bool value) noexcept
Set a GPIO output to high (true) or low (false).
Definition GpioManager.cpp:182
uint32_t total_ops_
Definition GpioManager.h:291
std::atomic< flux::hal::ManagerError > manager_error_
Definition GpioManager.h:297
static constexpr size_t kInvalidIndex
Definition GpioManager.h:285
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:332
hf_gpio_err_t EnableInterrupt(std::string_view name) noexcept
Enable a previously configured interrupt on a named pin.
Definition GpioManager.cpp:341
void DumpStatistics() const noexcept
Log current GPIO state and statistics to the console.
Definition GpioManager.cpp:278
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:251
hf_gpio_err_t SetInactive(std::string_view name) noexcept
Drive a pin to its inactive state.
Definition GpioManager.cpp:204
flux::hal::ManagerError GetManagerError() const noexcept
Lifecycle / bind error of the last EnsureInitialized() (shared catalog).
Definition GpioManager.h:125
BaseGpio * Get(std::string_view name) noexcept
Get the BaseGpio driver for a named pin. Returns nullptr if not found.
Definition GpioManager.cpp:156
void ReleaseLocked() noexcept
Definition GpioManager.cpp:317
GpioManager & operator=(const GpioManager &)=delete
static GpioManager & GetInstance() noexcept
Access the singleton instance (Meyers singleton, thread-safe).
Definition GpioManager.cpp:26
hf_gpio_err_t SetActive(std::string_view name) noexcept
Drive a pin to its active state.
Definition GpioManager.cpp:193
Shared HAL types for Flux V1: the manager error catalog and the facade result.
ManagerError
Lifecycle / bind failure catalog shared by every manager (GetManagerError()).
Definition flux_hal_types.hpp:37
@ NotInitialized
Manager not initialised (or shut down)
A single GPIO entry in the fixed-size registry.
Definition GpioManager.h:42
uint32_t error_count
Number of errors.
Definition GpioManager.h:47
uint32_t access_count
Number of times accessed.
Definition GpioManager.h:46
bool registered
Whether this entry has been registered.
Definition GpioManager.h:45
BaseGpio * driver
GPIO object owned by the platform binding.
Definition GpioManager.h:43
GpioEntry() noexcept
Definition GpioManager.h:49
HfFunctionalGpioPin functional_pin
Functional pin enum value.
Definition GpioManager.h:44
System-level GPIO diagnostics snapshot.
Definition GpioManager.h:58
uint32_t successful_operations
Calls that returned GPIO_SUCCESS.
Definition GpioManager.h:62
hf_gpio_err_t last_error
Most recent error code.
Definition GpioManager.h:65
uint32_t total_pins_registered
Number of GPIO pins currently registered.
Definition GpioManager.h:60
bool system_healthy
true when manager is initialised and operational.
Definition GpioManager.h:59
uint32_t total_operations
Lifetime count of read/write/toggle calls.
Definition GpioManager.h:61
uint64_t system_uptime_ms
Milliseconds since manager initialisation.
Definition GpioManager.h:64
uint32_t failed_operations
Calls that returned an error.
Definition GpioManager.h:63