HardFOC Flux V1 HAL
Unified HAL API for the HardFOC Flux valve-control platform (ESP32-S3)
Loading...
Searching...
No Matches
AdcManager.h
Go to the documentation of this file.
1
17#ifndef FLUX_ADC_MANAGER_H_
18#define FLUX_ADC_MANAGER_H_
19
20#include <array>
21#include <atomic>
22#include <cstdint>
23#include <optional>
24#include <string_view>
25
26#include "core/hf-core-drivers/internal/hf-internal-interface-wrap/inc/base/BaseAdc.h"
27#include "RtosMutex.h"
29#include "handlers/ads7952/Ads7952Handler.h"
30#include "core/hf-core-drivers/internal/hf-pincfg/src/hf_functional_pin_config_flux_v1.hpp"
31
32
33//==============================================================================
34// ADC ENTRY — fixed-size registry; the MCU ADC object lives in the platform
35// binding's static storage
36//==============================================================================
37
41struct AdcEntry {
42 HfFunctionalAdcChannel functional_channel{HfFunctionalAdcChannel::HF_FUNCTIONAL_ADC_COUNT};
43 std::string_view name;
44 BaseAdc* driver{nullptr};
45 uint8_t adc_unit{0};
46 uint8_t hw_channel{0};
47 float voltage_divider{1.0f};
48 bool registered{false};
49
50 // Statistics
51 uint32_t access_count{0};
52 uint32_t error_count{0};
53};
54
59 bool system_healthy{false};
61 uint32_t channels_by_chip[2]{};
62 uint32_t total_operations{0};
64 uint32_t failed_operations{0};
66 uint32_t hardware_errors{0};
67 uint64_t system_uptime_ms{0};
68 hf_adc_err_t last_error{hf_adc_err_t::ADC_SUCCESS};
69};
70
71//==============================================================================
72// ADC MANAGER
73//==============================================================================
74
88public:
89 //==========================================================================
90 // SINGLETON AND LIFECYCLE
91 //==========================================================================
92
94 static AdcManager& GetInstance() noexcept;
95
96 AdcManager(const AdcManager&) = delete;
97 AdcManager& operator=(const AdcManager&) = delete;
100
105 [[nodiscard]] bool EnsureInitialized() noexcept;
106
110 [[nodiscard]] bool IsInitialized() const noexcept { return initialized_.load(std::memory_order_acquire); }
111
116 [[nodiscard]] flux::hal::ManagerError GetManagerError() const noexcept {
117 return manager_error_.load(std::memory_order_acquire);
118 }
119
121 [[nodiscard]] bool IsInternalAdcReady() const noexcept { return internal_adc_ != nullptr; }
122
127 [[nodiscard]] bool IsAds7952Ready(uint8_t device_index) const noexcept;
128
133 [[nodiscard]] bool Deinitialize() noexcept;
134
135 //==========================================================================
136 // CHANNEL ACCESS
137 //==========================================================================
138
144 [[nodiscard]] BaseAdc* Get(std::string_view name) noexcept;
145
151 [[nodiscard]] BaseAdc* Get(HfFunctionalAdcChannel channel) noexcept;
152
158 [[nodiscard]] bool Contains(std::string_view name) const noexcept;
159
164 [[nodiscard]] size_t Size() const noexcept;
165
166 //==========================================================================
167 // READING OPERATIONS
168 //==========================================================================
169
178 [[nodiscard]] hf_adc_err_t ReadVoltage(std::string_view name, float& voltage,
179 uint8_t samples = 1,
180 uint16_t timeBetweenSamples = 0) noexcept;
181
190 [[nodiscard]] hf_adc_err_t ReadVoltage(HfFunctionalAdcChannel channel, float& voltage,
191 uint8_t samples = 1,
192 uint16_t timeBetweenSamples = 0) noexcept;
193
202 [[nodiscard]] hf_adc_err_t ReadRaw(std::string_view name, uint32_t& raw_count,
203 uint8_t samples = 1,
204 uint16_t timeBetweenSamples = 0) noexcept;
205
214 [[nodiscard]] hf_adc_err_t ReadRaw(HfFunctionalAdcChannel channel, uint32_t& raw_count,
215 uint8_t samples = 1,
216 uint16_t timeBetweenSamples = 0) noexcept;
217
218 //==========================================================================
219 // DIAGNOSTICS
220 //==========================================================================
221
226 [[nodiscard]] hf_adc_err_t GetLastError() const noexcept { return last_error_.load(std::memory_order_acquire); }
227
233 [[nodiscard]] hf_adc_err_t GetSystemDiagnostics(AdcSystemDiagnostics& diag) const noexcept;
234
236 void DumpStatistics() const noexcept;
237
238private:
239 AdcManager() noexcept = default;
240 ~AdcManager() = default;
241
242 bool Initialize() noexcept;
243
245 void ReleaseLocked() noexcept;
246
248 [[nodiscard]] size_t FindByName(std::string_view name) const noexcept;
249
251 [[nodiscard]] size_t FindByEnum(HfFunctionalAdcChannel channel) const noexcept;
252
254 [[nodiscard]] hf_adc_err_t ReadVoltageByIndex(size_t idx, float& voltage, uint8_t samples, uint16_t timeBetweenSamples) noexcept;
255 [[nodiscard]] hf_adc_err_t ReadRawByIndex(size_t idx, uint32_t& raw_count, uint8_t samples, uint16_t timeBetweenSamples) noexcept;
256
257 //==========================================================================
258 // STORAGE — entries are fixed-size in static storage; the MCU ADC is
259 // owned by the platform binding
260 //==========================================================================
261
262 static constexpr size_t kMaxChannels =
263 static_cast<size_t>(HfFunctionalAdcChannel::HF_FUNCTIONAL_ADC_COUNT);
264 static constexpr size_t kInvalidIndex = SIZE_MAX;
265
267 std::array<size_t, kMaxChannels> enum_to_index_{};
269 uint64_t init_time_ms_{0};
270
271 // MCU ADC unit (ADC1) — owned by the platform binding (bsp_bind), nullptr when down.
272 BaseAdc* internal_adc_{nullptr};
273
274 // ADS7952 external ADC handler(s) — members, constructed in place at bring-up
275 // (no heap). One slot per possible device on the pressure bus (one fitted on
276 // Flux V1, room for expansion). Unconditional, so every translation unit sees
277 // the same class layout whatever the HAL's private feature macros are.
278 static constexpr size_t kMaxAds7952Devices = 4;
279 std::array<std::optional<Ads7952Handler>, kMaxAds7952Devices> ads7952_handlers_{};
281
282 std::atomic<bool> initialized_{false};
283 std::atomic<flux::hal::ManagerError> manager_error_{flux::hal::ManagerError::NotInitialized};
284 std::atomic<hf_adc_err_t> last_error_{hf_adc_err_t::ADC_SUCCESS};
285 mutable RtosMutex mutex_;
286};
287
288//==============================================================================
289// CONVENIENCE
290//==============================================================================
291
296[[nodiscard]] inline AdcManager& GetAdcManager() noexcept {
298}
299
300#endif // FLUX_ADC_MANAGER_H_
AdcManager & GetAdcManager() noexcept
Convenience accessor — equivalent to AdcManager::GetInstance().
Definition AdcManager.h:296
Singleton manager for all analogue-to-digital channels on Flux V1.
Definition AdcManager.h:87
RtosMutex mutex_
Definition AdcManager.h:285
static constexpr size_t kInvalidIndex
Definition AdcManager.h:264
static constexpr size_t kMaxAds7952Devices
Definition AdcManager.h:278
bool IsInternalAdcReady() const noexcept
The MCU ADC unit came up during the last bring-up attempt.
Definition AdcManager.h:121
bool IsInitialized() const noexcept
Check whether the ADC subsystem has been initialised.
Definition AdcManager.h:110
size_t ads7952_device_count_
Definition AdcManager.h:280
size_t FindByEnum(HfFunctionalAdcChannel channel) const noexcept
Find entry index by functional enum. Returns kInvalidIndex if not found. O(1) direct index.
Definition AdcManager.cpp:277
BaseAdc * internal_adc_
Definition AdcManager.h:272
bool Initialize() noexcept
Definition AdcManager.cpp:72
void DumpStatistics() const noexcept
Log ADC channel statistics and health info to the console.
Definition AdcManager.cpp:429
AdcManager & operator=(AdcManager &&)=delete
size_t FindByName(std::string_view name) const noexcept
Find entry index by string name. Returns kInvalidIndex if not found. O(N) linear scan.
Definition AdcManager.cpp:270
hf_adc_err_t ReadRaw(std::string_view name, uint32_t &raw_count, uint8_t samples=1, uint16_t timeBetweenSamples=0) noexcept
Read raw ADC count from a channel by name.
Definition AdcManager.cpp:372
std::array< size_t, kMaxChannels > enum_to_index_
O(1) enum → entries index lookup.
Definition AdcManager.h:267
AdcManager(AdcManager &&)=delete
std::array< std::optional< Ads7952Handler >, kMaxAds7952Devices > ads7952_handlers_
Definition AdcManager.h:279
hf_adc_err_t GetLastError() const noexcept
Get the most recent error code.
Definition AdcManager.h:226
std::array< AdcEntry, kMaxChannels > entries_
Definition AdcManager.h:266
uint64_t init_time_ms_
Timestamp (ms) when Initialize() completed.
Definition AdcManager.h:269
bool EnsureInitialized() noexcept
Initialise the ADC subsystem if not already initialised.
Definition AdcManager.cpp:35
bool IsAds7952Ready(uint8_t device_index) const noexcept
ADS7952 device device_index came up during the last bring-up attempt.
Definition AdcManager.cpp:49
std::atomic< hf_adc_err_t > last_error_
Definition AdcManager.h:284
size_t registered_count_
Definition AdcManager.h:268
bool Deinitialize() noexcept
Release all ADC resources and reset to uninitialized state.
Definition AdcManager.cpp:449
flux::hal::ManagerError GetManagerError() const noexcept
Lifecycle / bind error of the last EnsureInitialized() (shared catalog).
Definition AdcManager.h:116
BaseAdc * Get(std::string_view name) noexcept
Get the BaseAdc driver for a named channel.
Definition AdcManager.cpp:283
size_t Size() const noexcept
Get the number of registered ADC channels.
Definition AdcManager.cpp:304
hf_adc_err_t ReadVoltageByIndex(size_t idx, float &voltage, uint8_t samples, uint16_t timeBetweenSamples) noexcept
Read from an entry by index — shared implementation for both name and enum overloads.
Definition AdcManager.cpp:312
AdcManager(const AdcManager &)=delete
void ReleaseLocked() noexcept
Release the MCU ADC, every ADS7952 handler and the registry (mutex held).
Definition AdcManager.cpp:53
hf_adc_err_t ReadRawByIndex(size_t idx, uint32_t &raw_count, uint8_t samples, uint16_t timeBetweenSamples) noexcept
Definition AdcManager.cpp:328
static constexpr size_t kMaxChannels
Definition AdcManager.h:262
std::atomic< flux::hal::ManagerError > manager_error_
Definition AdcManager.h:283
bool Contains(std::string_view name) const noexcept
Check whether a named ADC channel is registered.
Definition AdcManager.cpp:299
static AdcManager & GetInstance() noexcept
Return the singleton AdcManager instance.
Definition AdcManager.cpp:26
AdcManager & operator=(const AdcManager &)=delete
hf_adc_err_t GetSystemDiagnostics(AdcSystemDiagnostics &diag) const noexcept
Populate an AdcSystemDiagnostics snapshot.
Definition AdcManager.cpp:406
hf_adc_err_t ReadVoltage(std::string_view name, float &voltage, uint8_t samples=1, uint16_t timeBetweenSamples=0) noexcept
Read voltage from a channel by name (already divided if divider > 1).
Definition AdcManager.cpp:342
std::atomic< bool > initialized_
Definition AdcManager.h:282
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 ADC channel entry in the fixed-size registry.
Definition AdcManager.h:41
float voltage_divider
Divider ratio for scaled readings.
Definition AdcManager.h:47
uint8_t adc_unit
ADC unit (0 = ADC1, 1 = ADC2)
Definition AdcManager.h:45
uint8_t hw_channel
Hardware channel within the unit.
Definition AdcManager.h:46
bool registered
Whether this entry is active.
Definition AdcManager.h:48
BaseAdc * driver
ADC driver (MCU ADC in the binding, or an ADS7952 handler)
Definition AdcManager.h:44
uint32_t error_count
Total failed read operations on this channel.
Definition AdcManager.h:52
std::string_view name
Human-readable name from pincfg.
Definition AdcManager.h:43
uint32_t access_count
Total successful read operations on this channel.
Definition AdcManager.h:51
HfFunctionalAdcChannel functional_channel
Functional enum for this channel.
Definition AdcManager.h:42
ADC system diagnostics (cross-HAL compatible format).
Definition AdcManager.h:58
bool system_healthy
True when all registered channels are operational.
Definition AdcManager.h:59
uint32_t hardware_errors
Errors attributed to hardware faults.
Definition AdcManager.h:66
uint64_t system_uptime_ms
Milliseconds since ADC subsystem initialisation.
Definition AdcManager.h:67
uint32_t total_operations
Cumulative read attempts across all channels.
Definition AdcManager.h:62
uint32_t total_channels_registered
Number of channels currently registered.
Definition AdcManager.h:60
uint32_t channels_by_chip[2]
[0]=ESP32 internal, [1]=external (ADS7952 etc.)
Definition AdcManager.h:61
hf_adc_err_t last_error
Most recent error code from any operation.
Definition AdcManager.h:68
uint32_t failed_operations
Cumulative failed reads.
Definition AdcManager.h:64
uint32_t communication_errors
Errors attributed to bus communication failures.
Definition AdcManager.h:65
uint32_t successful_operations
Cumulative successful reads.
Definition AdcManager.h:63