HardFOC Vortex V1 HAL
Unified HAL API for the HardFOC Vortex motor-control platform (ESP32-C6, TMC9660)
Loading...
Searching...
No Matches
AdcManager.h
Go to the documentation of this file.
1
17#ifndef VORTEX_ADC_MANAGER_H_
18#define VORTEX_ADC_MANAGER_H_
19
20#include <array>
21#include <atomic>
22#include <cstdint>
23#include <optional>
24#include <string_view>
25
26#include "base/BaseAdc.h"
27#include "RtosMutex.h"
28#include "handlers/tmc9660/Tmc9660AdcWrapper.h"
29#include "core/hf-core-drivers/internal/hf-pincfg/src/hf_functional_pin_config_vortex_v1.hpp"
30
31// Forward declarations
32class MotorController;
33class Tmc9660Handler;
34
35//==============================================================================
36// ADC ENTRY
37//==============================================================================
38
42struct AdcEntry {
43 HfFunctionalAdcChannel functional_channel{HfFunctionalAdcChannel::HF_FUNCTIONAL_ADC_COUNT};
44 std::string_view name;
45 BaseAdc* driver{nullptr};
46 uint8_t adc_unit{0};
47 uint8_t hw_channel{0};
48 float voltage_divider{1.0f};
49 bool registered{false};
50
51 // Statistics
52 uint32_t access_count{0};
53 uint32_t error_count{0};
54};
55
56//==============================================================================
57// ADC SYSTEM DIAGNOSTICS
58//==============================================================================
59
64 bool system_healthy{false};
66 uint32_t channels_by_chip[2]{};
67 uint32_t total_operations{0};
69 uint32_t failed_operations{0};
71 uint32_t hardware_errors{0};
72 uint64_t system_uptime_ms{0};
73 hf_adc_err_t last_error{hf_adc_err_t::ADC_SUCCESS};
74};
75
76//==============================================================================
77// ADC MANAGER
78//==============================================================================
79
93public:
94 //==========================================================================
95 // SINGLETON AND LIFECYCLE
96 //==========================================================================
97
99 static AdcManager& GetInstance() noexcept;
100
101 AdcManager(const AdcManager&) = delete;
102 AdcManager& operator=(const AdcManager&) = delete;
105
110 [[nodiscard]] bool EnsureInitialized() noexcept;
111
116 [[nodiscard]] bool Deinitialize() noexcept;
117
122 [[nodiscard]] bool IsInitialized() const noexcept { return initialized_.load(std::memory_order_acquire); }
123
124 //==========================================================================
125 // CHANNEL ACCESS
126 //==========================================================================
127
133 [[nodiscard]] BaseAdc* Get(std::string_view name) noexcept;
134
140 [[nodiscard]] BaseAdc* Get(HfFunctionalAdcChannel channel) noexcept;
141
147 [[nodiscard]] bool Contains(std::string_view name) const noexcept;
148
153 [[nodiscard]] size_t Size() const noexcept;
154
155 //==========================================================================
156 // READING OPERATIONS
157 //==========================================================================
158
167 [[nodiscard]] hf_adc_err_t ReadVoltage(std::string_view name, float& voltage,
168 hf_u8_t samples = 1,
169 hf_time_t timeBetweenSamples = 0) noexcept;
170
179 [[nodiscard]] hf_adc_err_t ReadVoltage(HfFunctionalAdcChannel channel, float& voltage,
180 hf_u8_t samples = 1,
181 hf_time_t timeBetweenSamples = 0) noexcept;
182
191 [[nodiscard]] hf_adc_err_t ReadRaw(std::string_view name, uint32_t& raw_value,
192 hf_u8_t numOfSamplesToAvg = 1,
193 hf_time_t timeBetweenSamples = 0) noexcept;
194
203 [[nodiscard]] hf_adc_err_t ReadRaw(HfFunctionalAdcChannel channel, uint32_t& raw_value,
204 hf_u8_t numOfSamplesToAvg = 1,
205 hf_time_t timeBetweenSamples = 0) noexcept;
206
207 //==========================================================================
208 // DIAGNOSTICS
209 //==========================================================================
210
216 [[nodiscard]] hf_adc_err_t GetSystemDiagnostics(AdcSystemDiagnostics& diagnostics) const noexcept;
217
222 [[nodiscard]] hf_adc_err_t GetLastError() const noexcept { return last_error_.load(std::memory_order_acquire); }
223
225 void DumpStatistics() const noexcept;
226
227private:
228 AdcManager() noexcept = default;
230
231 bool Initialize() noexcept;
232
234 [[nodiscard]] size_t FindByName(std::string_view name) const noexcept;
235
237 [[nodiscard]] size_t FindByEnum(HfFunctionalAdcChannel channel) const noexcept;
238
239 //==========================================================================
240 // STORAGE — entries are fixed-size; the Tmc9660AdcWrapper lives in place
241 // (std::optional, emplaced at init, reset at deinit)
242 //==========================================================================
243
244 static constexpr size_t kMaxChannels =
245 static_cast<size_t>(HfFunctionalAdcChannel::HF_FUNCTIONAL_ADC_COUNT);
246 static constexpr size_t kInvalidIndex = SIZE_MAX;
247
250
252 std::optional<Tmc9660AdcWrapper> tmc9660_adc_;
253
256
257 std::atomic<bool> initialized_{false};
258 std::atomic<hf_adc_err_t> last_error_{hf_adc_err_t::ADC_SUCCESS};
261 std::atomic<bool> warned_motor_controller_not_ready_{false};
262 std::atomic<bool> warned_tmc_handler_missing_{false};
263 mutable RtosMutex mutex_;
264};
265
266//==============================================================================
267// CONVENIENCE
268//==============================================================================
269
274[[nodiscard]] inline AdcManager& GetAdcManager() noexcept {
276}
277
278#endif // VORTEX_ADC_MANAGER_H_
AdcManager & GetAdcManager() noexcept
Convenience accessor — equivalent to AdcManager::GetInstance().
Definition AdcManager.h:274
Singleton manager for all analogue-to-digital channels on Vortex V1.
Definition AdcManager.h:92
RtosMutex mutex_
Definition AdcManager.h:263
static constexpr size_t kInvalidIndex
Definition AdcManager.h:246
bool IsInitialized() const noexcept
Check whether the ADC subsystem has been initialised.
Definition AdcManager.h:122
size_t FindByEnum(HfFunctionalAdcChannel channel) const noexcept
Find entry index by functional enum. Returns kInvalidIndex if not found.
Definition AdcManager.cpp:157
hf_adc_err_t GetSystemDiagnostics(AdcSystemDiagnostics &diagnostics) const noexcept
Populate an AdcSystemDiagnostics snapshot.
Definition AdcManager.cpp:301
bool Initialize() noexcept
Definition AdcManager.cpp:63
hf_adc_err_t ReadRaw(std::string_view name, uint32_t &raw_value, hf_u8_t numOfSamplesToAvg=1, hf_time_t timeBetweenSamples=0) noexcept
Read raw ADC value from a channel by name.
Definition AdcManager.cpp:249
void DumpStatistics() const noexcept
Log ADC channel statistics and health info to the console.
Definition AdcManager.cpp:326
AdcManager & operator=(AdcManager &&)=delete
std::atomic< bool > warned_tmc_handler_missing_
Definition AdcManager.h:262
size_t FindByName(std::string_view name) const noexcept
Find entry index by string name. Returns kInvalidIndex if not found.
Definition AdcManager.cpp:150
std::optional< Tmc9660AdcWrapper > tmc9660_adc_
Single TMC9660 ADC wrapper — all channels route through this.
Definition AdcManager.h:252
std::atomic< bool > warned_motor_controller_not_ready_
Definition AdcManager.h:261
AdcManager(AdcManager &&)=delete
hf_adc_err_t GetLastError() const noexcept
Get the most recent error code from any ADC operation.
Definition AdcManager.h:222
std::array< AdcEntry, kMaxChannels > entries_
Definition AdcManager.h:248
bool EnsureInitialized() noexcept
Initialise the ADC subsystem if not already initialised.
Definition AdcManager.cpp:36
std::atomic< hf_adc_err_t > last_error_
Definition AdcManager.h:258
size_t registered_count_
Definition AdcManager.h:249
bool Deinitialize() noexcept
Release all ADC resources and reset to uninitialised state.
Definition AdcManager.cpp:45
BaseAdc * Get(std::string_view name) noexcept
Get the BaseAdc driver for a named channel.
Definition AdcManager.cpp:164
size_t Size() const noexcept
Get the number of registered ADC channels.
Definition AdcManager.cpp:188
AdcManager(const AdcManager &)=delete
MotorController * motor_controller_
MotorController dependency (non-owning pointer, set at init).
Definition AdcManager.h:255
static constexpr size_t kMaxChannels
Definition AdcManager.h:244
bool Contains(std::string_view name) const noexcept
Check whether a named ADC channel is registered.
Definition AdcManager.cpp:182
static AdcManager & GetInstance() noexcept
Return the singleton AdcManager instance.
Definition AdcManager.cpp:27
hf_adc_err_t ReadVoltage(std::string_view name, float &voltage, hf_u8_t samples=1, hf_time_t timeBetweenSamples=0) noexcept
Read voltage from a channel by name (already divided if divider > 1).
Definition AdcManager.cpp:197
AdcManager & operator=(const AdcManager &)=delete
std::atomic< bool > initialized_
Definition AdcManager.h:257
Singleton managing multiple TMC9660 motor controllers on Vortex V1.
Definition MotorController.h:207
A single ADC channel entry in the fixed-size registry.
Definition AdcManager.h:42
float voltage_divider
Divider ratio for scaled readings.
Definition AdcManager.h:48
uint8_t adc_unit
ADC unit on the chip.
Definition AdcManager.h:46
uint8_t hw_channel
Hardware channel within the unit.
Definition AdcManager.h:47
bool registered
Whether this entry is active.
Definition AdcManager.h:49
BaseAdc * driver
The ADC driver (the manager's in-place Tmc9660AdcWrapper)
Definition AdcManager.h:45
uint32_t error_count
Total failed read operations on this channel.
Definition AdcManager.h:53
std::string_view name
Human-readable name from pincfg.
Definition AdcManager.h:44
uint32_t access_count
Total successful read operations on this channel.
Definition AdcManager.h:52
HfFunctionalAdcChannel functional_channel
Functional enum for this channel.
Definition AdcManager.h:43
ADC system diagnostics (compatible with example code).
Definition AdcManager.h:63
bool system_healthy
True when all registered channels are operational.
Definition AdcManager.h:64
uint32_t hardware_errors
Errors attributed to hardware faults.
Definition AdcManager.h:71
uint64_t system_uptime_ms
Milliseconds since ADC subsystem initialisation.
Definition AdcManager.h:72
uint32_t total_operations
Cumulative read attempts across all channels.
Definition AdcManager.h:67
uint32_t total_channels_registered
Number of channels currently registered.
Definition AdcManager.h:65
uint32_t channels_by_chip[2]
[0]=ESP32, [1]=TMC9660
Definition AdcManager.h:66
hf_adc_err_t last_error
Most recent error code from any operation.
Definition AdcManager.h:73
uint32_t failed_operations
Cumulative failed reads.
Definition AdcManager.h:69
uint32_t communication_errors
Errors attributed to bus communication failures.
Definition AdcManager.h:70
uint32_t successful_operations
Cumulative successful reads.
Definition AdcManager.h:68