HardFOC Flux V1 HAL
Unified HAL API for the HardFOC Flux valve-control platform (ESP32-S3)
Loading...
Searching...
No Matches
Flux.h
Go to the documentation of this file.
1
43#ifndef FLUX_API_H_
44#define FLUX_API_H_
45
46#include <array>
47#include <atomic>
48#include <cstddef>
49#include <cstring>
50
51#include "FluxVersion.h"
53#include "handlers/logger/Logger.h"
54
55// Forward declarations
57class NvsManager;
58class GpioManager;
59class ValveManager;
60class AdcManager;
61class LedManager;
63
65inline constexpr size_t kFluxManagerCount = 7;
66
67//==============================================================================
68// FLUX SYSTEM DIAGNOSTICS
69//==============================================================================
70
101
102//==============================================================================
103// MANAGER HEALTH SNAPSHOT
104//==============================================================================
105
110 const char* name;
114
116 : name(nullptr), initialized(false), has_error(false), last_error_code(0) {}
117};
118
146
147//==============================================================================
148// AGGREGATED DIAGNOSTICS (shared ManagerError catalog)
149//==============================================================================
150
158
166 bool initialized{false};
167 bool all_managers_up{false};
168 bool all_healthy{false};
170 const char* first_error_manager{nullptr};
171
172 std::array<ManagerDiagnostic, kFluxManagerCount> managers{};
173
174 bool max22200_ready{false};
175 bool tle92466_ready{false};
176 bool pressure_bus_ready{false};
177 bool ads7952_ready{false};
178 bool internal_adc_ready{false};
179 bool can_ready{false};
180 bool die_temp_ready{false};
181 bool ntc_ready{false};
182 bool mcp9700_ready{false};
183 bool status_led_ready{false};
184};
185
186//==============================================================================
187// FLUX API CLASS
188//==============================================================================
189
200class Flux {
201public:
202 //==========================================================================
203 // SINGLETON ACCESS
204 //==========================================================================
205
210 static Flux& GetInstance() noexcept;
211
212 Flux(const Flux&) = delete;
213 Flux& operator=(const Flux&) = delete;
214 Flux(Flux&&) = delete;
215 Flux& operator=(Flux&&) = delete;
216
217 //==========================================================================
218 // INITIALIZATION
219 //==========================================================================
220
225 bool EnsureInitialized() noexcept;
226
229 [[nodiscard]] bool IsInitialized() const noexcept { return initialized_; }
230
235 bool Shutdown() noexcept;
236
242 [[nodiscard]] bool GetSystemDiagnostics(FluxSystemDiagnostics& diagnostics) const noexcept;
243
248 [[nodiscard]] std::array<bool, kFluxManagerCount> GetComponentInitializationStatus() const noexcept;
249
256 [[nodiscard]] size_t GetFailedComponents(const char* out[], size_t max_count) const noexcept;
257
264 [[nodiscard]] size_t GetSystemWarnings(const char* out[], size_t max_count) const noexcept;
265
272 [[nodiscard]] bool CollectManagerHealth(ManagerHealthSnapshot& snapshot) noexcept;
273
282 bool CollectDiagnostics(FluxHalDiagnostics& out) const noexcept;
283
288 [[nodiscard]] flux::hal::ManagerError GetLastError() const noexcept {
289 return last_error_.load(std::memory_order_acquire);
290 }
291
292 //==========================================================================
293 // COMPONENT ACCESS (public references to singletons)
294 //==========================================================================
295
303
304 //==========================================================================
305 // UTILITY
306 //==========================================================================
307
309 [[nodiscard]] uint64_t GetSystemUptimeMs() const noexcept;
310
312 [[nodiscard]] uint64_t GetInitializationTimeMs() const noexcept;
313
314 //==========================================================================
315 // SYSTEM CONTROL (MCU-agnostic wrappers)
316 //==========================================================================
317
329 [[nodiscard]] uint32_t GetLastResetReason() const noexcept;
330
339 [[nodiscard]] uint32_t GetFreeHeapBytes() const noexcept;
340
349 [[nodiscard]] uint32_t GetLargestFreeHeapBlock() const noexcept;
350
360 [[noreturn]] void SystemRestart() const noexcept;
361
363 void DumpSystemStatistics() const noexcept;
364
369 [[nodiscard]] bool PerformHealthCheck() noexcept;
370
375 [[nodiscard]] const char* GetSystemVersion() const noexcept;
376
377private:
378 Flux() noexcept;
379 ~Flux() = default;
380
381 bool InitializeAllComponents() noexcept;
382 bool InitializeNvs() noexcept;
383 bool InitializeComms() noexcept;
384 bool InitializeGpio() noexcept;
385 bool InitializeValves() noexcept;
386 bool InitializeAdc() noexcept;
387 bool InitializeLeds() noexcept;
388 bool InitializeTemperature() noexcept;
389 void UpdateSystemDiagnostics() noexcept;
390
391 //==========================================================================
392 // STATE
393 //==========================================================================
394
395 std::atomic<bool> initialized_{false};
396 std::atomic<flux::hal::ManagerError> last_error_{flux::hal::ManagerError::None};
397 std::atomic<uint64_t> initialization_start_time_{0};
398 std::atomic<uint64_t> initialization_end_time_{0};
399
400 std::atomic<bool> nvs_initialized_{false};
401 std::atomic<bool> comms_initialized_{false};
402 std::atomic<bool> gpio_initialized_{false};
403 std::atomic<bool> valves_initialized_{false};
404 std::atomic<bool> adc_initialized_{false};
405 std::atomic<bool> leds_initialized_{false};
406 std::atomic<bool> temp_initialized_{false};
407
409
410 static constexpr size_t kMaxFailedComponents = kFluxManagerCount;
413
414 static constexpr size_t kMaxSystemWarnings = 16;
417
418 void AddSystemWarning(const char* warning) noexcept {
421 }
422 }
423};
424
425//==============================================================================
426// CONVENIENCE
427//==============================================================================
428
433[[nodiscard]] inline Flux& GetFlux() noexcept {
434 return Flux::GetInstance();
435}
436
437//==============================================================================
438// GLOBAL ACCESS MACROS
439//==============================================================================
440
442#define FLUX_API Flux::GetInstance()
444#define FLUX_API_INIT() (Flux::GetInstance().EnsureInitialized(), Flux::GetInstance())
445
446#endif // FLUX_API_H_
constexpr size_t kFluxManagerCount
Number of Flux subsystem managers exposed on Flux (incl. NVS).
Definition Flux.h:65
Flux & GetFlux() noexcept
Convenience accessor — equivalent to Flux::GetInstance().
Definition Flux.h:433
Version information for the HardFOC Flux V1 HAL.
Singleton manager for all analogue-to-digital channels on Flux V1.
Definition AdcManager.h:87
Singleton managing all board communication channels for Flux V1.
Definition CommChannelsManager.h:134
Unified singleton entry point for the HardFOC Flux V1 platform.
Definition Flux.h:200
flux::hal::ManagerError GetLastError() const noexcept
Why the last EnsureInitialized() failed: the error of the first required manager that did not come up...
Definition Flux.h:288
bool CollectDiagnostics(FluxHalDiagnostics &out) const noexcept
Fill the board-level bring-up snapshot (managers in init order, part readiness).
Definition Flux.cpp:339
bool InitializeAllComponents() noexcept
Definition Flux.cpp:132
size_t GetSystemWarnings(const char *out[], size_t max_count) const noexcept
Get accumulated system warnings.
Definition Flux.cpp:512
AdcManager & adc
ADC channels (NTC, VBUS)
Definition Flux.h:300
std::atomic< bool > comms_initialized_
Definition Flux.h:401
uint32_t GetLargestFreeHeapBlock() const noexcept
Largest contiguous free block in the default heap.
Definition Flux.cpp:282
const char * GetSystemVersion() const noexcept
Get the Flux HAL semantic version string.
Definition Flux.cpp:435
const char * system_warnings_[kMaxSystemWarnings]
Definition Flux.h:415
static constexpr size_t kMaxFailedComponents
Definition Flux.h:410
void SystemRestart() const noexcept
Trigger a software reset of the MCU. Does not return on success.
Definition Flux.cpp:273
bool Shutdown() noexcept
Shut down all subsystems in reverse initialisation order.
Definition Flux.cpp:472
std::atomic< uint64_t > initialization_start_time_
Definition Flux.h:397
bool InitializeTemperature() noexcept
Definition Flux.cpp:224
static constexpr size_t kMaxSystemWarnings
Definition Flux.h:414
static Flux & GetInstance() noexcept
Access the singleton instance (Meyers singleton, thread-safe).
Definition Flux.cpp:68
uint64_t GetInitializationTimeMs() const noexcept
Time in ms that the initialisation sequence took to complete.
Definition Flux.cpp:259
bool InitializeLeds() noexcept
Definition Flux.cpp:216
size_t system_warnings_count_
Definition Flux.h:416
std::atomic< bool > initialized_
Definition Flux.h:395
bool CollectManagerHealth(ManagerHealthSnapshot &snapshot) noexcept
Collect per-manager error health into a unified snapshot.
Definition Flux.cpp:286
size_t failed_components_count_
Definition Flux.h:412
bool EnsureInitialized() noexcept
Ensure all Flux subsystems are properly initialized.
Definition Flux.cpp:94
const char * failed_components_[kMaxFailedComponents]
Definition Flux.h:411
TemperatureManager & temp
Temperature sensors.
Definition Flux.h:302
std::atomic< bool > temp_initialized_
Definition Flux.h:406
GpioManager & gpio
ESP32 GPIO pins.
Definition Flux.h:298
uint64_t GetSystemUptimeMs() const noexcept
Milliseconds elapsed since EnsureInitialized() completed.
Definition Flux.cpp:255
CommChannelsManager & comms
SPI, CAN, UART (I²C not used on Flux V1)
Definition Flux.h:296
bool PerformHealthCheck() noexcept
Run a quick health check across all managers.
Definition Flux.cpp:408
bool InitializeComms() noexcept
Definition Flux.cpp:184
Flux(const Flux &)=delete
std::atomic< bool > leds_initialized_
Definition Flux.h:405
bool InitializeAdc() noexcept
Definition Flux.cpp:208
ValveManager & valves
Solenoid + proportional valve control.
Definition Flux.h:299
bool InitializeNvs() noexcept
Definition Flux.cpp:176
Flux(Flux &&)=delete
bool GetSystemDiagnostics(FluxSystemDiagnostics &diagnostics) const noexcept
Populate a full system diagnostics snapshot.
Definition Flux.cpp:236
std::array< bool, kFluxManagerCount > GetComponentInitializationStatus() const noexcept
Per-component init status as a bool array.
Definition Flux.cpp:243
std::atomic< bool > nvs_initialized_
Definition Flux.h:400
bool InitializeGpio() noexcept
Definition Flux.cpp:192
bool IsInitialized() const noexcept
Check whether the system is fully initialised.
Definition Flux.h:229
FluxSystemDiagnostics diagnostics_
Definition Flux.h:408
void UpdateSystemDiagnostics() noexcept
Definition Flux.cpp:439
uint32_t GetFreeHeapBytes() const noexcept
Free bytes available in the default heap.
Definition Flux.cpp:278
std::atomic< bool > valves_initialized_
Definition Flux.h:403
void DumpSystemStatistics() const noexcept
Log a comprehensive system summary to the console.
Definition Flux.cpp:385
uint32_t GetLastResetReason() const noexcept
Reset reason of the previous boot, opaque to callers.
Definition Flux.cpp:269
void AddSystemWarning(const char *warning) noexcept
Definition Flux.h:418
std::atomic< bool > gpio_initialized_
Definition Flux.h:402
LedManager & leds
WS2812 status LED.
Definition Flux.h:301
std::atomic< bool > adc_initialized_
Definition Flux.h:404
Flux & operator=(Flux &&)=delete
NvsManager & nvs
NVS (flux / flux_cal / flux_cfg)
Definition Flux.h:297
size_t GetFailedComponents(const char *out[], size_t max_count) const noexcept
Get the list of components that failed initialization.
Definition Flux.cpp:504
std::atomic< flux::hal::ManagerError > last_error_
Definition Flux.h:396
std::atomic< uint64_t > initialization_end_time_
Definition Flux.h:398
Flux & operator=(const Flux &)=delete
bool InitializeValves() noexcept
Definition Flux.cpp:200
Singleton that manages all MCU GPIO pins defined in the X-MACRO pin config.
Definition GpioManager.h:81
Singleton that drives the on-board WS2812 status LED.
Definition LedManager.h:202
Singleton owning the Flux board's persistent-storage namespaces.
Definition NvsManager.h:35
Singleton manager for all temperature sensors on Flux V1.
Definition TemperatureManager.h:168
Unified singleton for controlling all valve channels on the Flux board.
Definition ValveManager.h:296
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
@ None
No error recorded.
@ NotInitialized
Manager not initialised (or shut down)
Definition flux_hal_types.hpp:29
Board-level bring-up snapshot: every manager's state and every fitted part's readiness.
Definition Flux.h:165
bool die_temp_ready
MCU die sensor came up.
Definition Flux.h:180
bool can_ready
CAN controller object exists.
Definition Flux.h:179
bool ntc_ready
Board NTC came up.
Definition Flux.h:181
bool initialized
Flux::EnsureInitialized() succeeded (every required manager up)
Definition Flux.h:166
bool all_healthy
... and none reports an error (none degraded)
Definition Flux.h:168
bool all_managers_up
Every manager, required and optional, is initialised.
Definition Flux.h:167
bool mcp9700_ready
MCP9700 on the ADS7952 came up.
Definition Flux.h:182
bool max22200_ready
MAX22200 solenoid driver came up.
Definition Flux.h:174
bool ads7952_ready
ADS7952 device 0 came up.
Definition Flux.h:177
bool pressure_bus_ready
ADS7952 SPI bus and chip select are up.
Definition Flux.h:176
bool internal_adc_ready
MCU ADC (NTC, VBUS) came up.
Definition Flux.h:178
const char * first_error_manager
Manager that reported it.
Definition Flux.h:170
bool status_led_ready
WS2812 status LED came up.
Definition Flux.h:183
bool tle92466_ready
TLE92466ED proportional driver came up.
Definition Flux.h:175
std::array< ManagerDiagnostic, kFluxManagerCount > managers
In initialisation order.
Definition Flux.h:172
flux::hal::ManagerError first_error
First error in init order.
Definition Flux.h:169
Complete system diagnostics snapshot for the Flux API.
Definition Flux.h:72
bool valves_initialized
ValveManager ready.
Definition Flux.h:77
bool gpio_initialized
GpioManager ready.
Definition Flux.h:76
static constexpr size_t kMaxDiagEntries
Max failed-component names.
Definition Flux.h:89
bool comms_initialized
CommChannelsManager ready.
Definition Flux.h:75
const char * failed_components_list[kMaxDiagEntries]
Names of failed components.
Definition Flux.h:90
bool nvs_initialized
NvsManager ready.
Definition Flux.h:74
bool temp_initialized
TemperatureManager ready.
Definition Flux.h:80
uint64_t initialization_time_ms
Time taken to initialise all components.
Definition Flux.h:86
bool system_healthy
true when all components are initialised.
Definition Flux.h:73
size_t failed_components_list_count
Count of entries in list.
Definition Flux.h:91
uint32_t total_components
Total number of managed components (kFluxManagerCount).
Definition Flux.h:82
bool adc_initialized
AdcManager ready.
Definition Flux.h:78
FluxSystemDiagnostics() noexcept
Default constructor — all fields zeroed / false.
Definition Flux.h:94
uint32_t initialized_components
Components that initialised successfully.
Definition Flux.h:83
uint32_t failed_components
Components that failed to initialise.
Definition Flux.h:84
bool leds_initialized
LedManager ready.
Definition Flux.h:79
uint64_t system_uptime_ms
Milliseconds since first initialisation.
Definition Flux.h:87
One manager's lifecycle state in FluxHalDiagnostics.
Definition Flux.h:152
flux::hal::ManagerError error
GetManagerError()
Definition Flux.h:156
bool required
Its failure fails Flux::EnsureInitialized()
Definition Flux.h:154
const char * name
Manager class name.
Definition Flux.h:153
bool initialized
IsInitialized()
Definition Flux.h:155
Per-manager health entry for unified error aggregation.
Definition Flux.h:109
uint8_t last_error_code
Last error code (0 = success, enum-specific otherwise)
Definition Flux.h:113
bool has_error
Whether last error is non-zero.
Definition Flux.h:112
bool initialized
Whether manager is initialized.
Definition Flux.h:111
const char * name
Manager name (string literal)
Definition Flux.h:110
ManagerHealthEntry() noexcept
Definition Flux.h:115
Aggregated health snapshot across all Flux managers.
Definition Flux.h:135
static constexpr size_t kMaxManagers
Definition Flux.h:136
size_t managers_with_errors
Count of managers reporting errors.
Definition Flux.h:140
ManagerHealthSnapshot() noexcept
Definition Flux.h:143
ManagerHealthEntry entries[kMaxManagers]
Per-manager entries.
Definition Flux.h:138
bool all_healthy
True if no errors and all initialized.
Definition Flux.h:141
size_t count
Number of populated entries.
Definition Flux.h:139