HF-Core Platform 0.1.0-dev
Hardware-Agnostic Handler Layer & RTOS Utilities for HardFOC
Loading...
Searching...
No Matches
Tle92466edHandler.h
Go to the documentation of this file.
1
21#ifndef COMPONENT_HANDLER_TLE92466ED_HANDLER_H_
22#define COMPONENT_HANDLER_TLE92466ED_HANDLER_H_
23
24#include <cstdint>
25#include <memory>
26#include <cstdarg>
27#include <span>
28#include <type_traits>
29#include "core/hf-core-drivers/external/hf-tle92466ed-driver/inc/tle92466ed.hpp"
30#include "core/hf-core-drivers/external/hf-tle92466ed-driver/inc/tle92466ed_spi_interface.hpp"
31#include "base/BaseSpi.h"
32#include "base/BaseGpio.h"
33#include "RtosMutex.h"
34
39
46class HalSpiTle92466edComm : public tle92466ed::SpiInterface<HalSpiTle92466edComm> {
47public:
55 HalSpiTle92466edComm(BaseSpi& spi, BaseGpio& resn, BaseGpio& en,
56 BaseGpio* faultn = nullptr) noexcept;
57
60
61 // Bring base class variadic Log into scope (avoids name hiding)
62 using tle92466ed::SpiInterface<HalSpiTle92466edComm>::Log;
63
64 tle92466ed::CommResult<void> Init() noexcept;
65 tle92466ed::CommResult<void> Deinit() noexcept;
66 tle92466ed::CommResult<uint32_t> Transfer32(uint32_t tx_data) noexcept;
67 tle92466ed::CommResult<void> TransferMulti(std::span<const uint32_t> tx_data,
68 std::span<uint32_t> rx_data) noexcept;
69 tle92466ed::CommResult<void> Delay(uint32_t microseconds) noexcept;
70 tle92466ed::CommResult<void> Configure(const tle92466ed::SPIConfig& config) noexcept;
71 [[nodiscard]] bool IsReady() const noexcept;
72 [[nodiscard]] tle92466ed::CommError GetLastError() const noexcept;
73 tle92466ed::CommResult<void> ClearErrors() noexcept;
74 tle92466ed::CommResult<void> GpioSet(tle92466ed::CtrlPin pin,
75 tle92466ed::GpioSignal signal) noexcept;
76 tle92466ed::CommResult<tle92466ed::GpioSignal> GpioRead(tle92466ed::CtrlPin pin) noexcept;
77 void Log(tle92466ed::LogLevel level, const char* tag,
78 const char* format, va_list args) noexcept;
79
81
82private:
83 BaseSpi& spi_;
84 BaseGpio& resn_;
85 BaseGpio& en_;
86 BaseGpio* faultn_;
87 bool initialized_{false};
88 tle92466ed::CommError last_error_{tle92466ed::CommError::None};
89};
90
92
97
110public:
112 using DriverType = tle92466ed::Driver<HalSpiTle92466edComm>;
113
114 static constexpr uint8_t kNumChannels = 6;
115
116 //=========================================================================
117 // Construction
118 //=========================================================================
119
127 Tle92466edHandler(BaseSpi& spi, BaseGpio& resn, BaseGpio& en,
128 BaseGpio* faultn = nullptr) noexcept;
129
130 ~Tle92466edHandler() noexcept;
131
132 // Non-copyable
134 Tle92466edHandler& operator=(const Tle92466edHandler&) = delete;
135
136 // Non-movable
138 Tle92466edHandler& operator=(Tle92466edHandler&&) = delete;
139
140 //=========================================================================
141 // Initialization
142 //=========================================================================
143
146 tle92466ed::DriverResult<void> Initialize() noexcept;
147
152 bool EnsureInitialized() noexcept;
153
159 tle92466ed::DriverResult<void> Initialize(const tle92466ed::GlobalConfig& config) noexcept;
160
163 tle92466ed::DriverResult<void> Deinitialize() noexcept;
164
166 [[nodiscard]] bool IsInitialized() const noexcept { return initialized_; }
167
168 //=========================================================================
169 // Channel Control
170 //=========================================================================
171
177 tle92466ed::DriverResult<void> ConfigureChannel(uint8_t channel, const tle92466ed::ChannelConfig& config) noexcept;
178
180 tle92466ed::DriverResult<void> EnableChannel(uint8_t channel) noexcept;
181
183 tle92466ed::DriverResult<void> DisableChannel(uint8_t channel) noexcept;
184
186 tle92466ed::DriverResult<void> EnableAllChannels() noexcept;
187
189 tle92466ed::DriverResult<void> DisableAllChannels() noexcept;
190
196 tle92466ed::DriverResult<void> SetChannelCurrent(uint8_t channel, uint16_t current_ma) noexcept;
197
205 tle92466ed::DriverResult<void> ConfigurePwmRaw(uint8_t channel, uint8_t mantissa,
206 uint8_t exponent, bool low_freq_range = false) noexcept;
207
208 //=========================================================================
209 // Device Mode
210 //=========================================================================
211
213 tle92466ed::DriverResult<void> EnterMissionMode() noexcept;
214
216 tle92466ed::DriverResult<void> EnterConfigMode() noexcept;
217
219 [[nodiscard]] bool IsMissionMode() noexcept;
220
221 //=========================================================================
222 // Status & Diagnostics
223 //=========================================================================
224
229 tle92466ed::DriverResult<void> GetStatus(tle92466ed::DeviceStatus& status) noexcept;
230
236 tle92466ed::DriverResult<void> GetChannelDiagnostics(uint8_t channel, tle92466ed::ChannelDiagnostics& diag) noexcept;
237
242 tle92466ed::DriverResult<void> GetFaultReport(tle92466ed::FaultReport& report) noexcept;
243
245 tle92466ed::DriverResult<void> ClearFaults() noexcept;
246
248 bool HasFault() noexcept;
249
250 //=========================================================================
251 // Watchdog
252 //=========================================================================
253
258 tle92466ed::DriverResult<void> KickWatchdog(uint16_t reload_value = 1000) noexcept;
259
260 //=========================================================================
261 // Device Info
262 //=========================================================================
263
265 uint32_t GetChipId() noexcept;
266
268 uint32_t GetIcVersion() noexcept;
269
270 //=========================================================================
271 // Direct Driver Access
272 //=========================================================================
273
278 [[nodiscard]] DriverType* GetDriver() noexcept;
279 [[nodiscard]] const DriverType* GetDriver() const noexcept;
280
282 void DumpDiagnostics() noexcept;
283
284private:
285 bool EnsureInitializedLocked() noexcept;
286
288 template <typename T>
289 struct is_driver_result : std::false_type {};
290 template <typename T>
291 struct is_driver_result<tle92466ed::DriverResult<T>> : std::true_type {};
292
300 template <typename Fn>
301 auto withDriver(Fn&& fn) noexcept {
302 using R = std::invoke_result_t<Fn, DriverType&>;
303 static_assert(!std::is_void_v<R>, "withDriver requires non-void return");
304 MutexLockGuard lock(mutex_);
305 if (!EnsureInitializedLocked() || !driver_) {
306 if constexpr (is_driver_result<R>::value)
307 return R{tle::unexpected(tle92466ed::DriverError::NotInitialized)};
308 else
309 return R{};
310 }
311 return fn(*driver_);
312 }
313
314 bool initialized_{false};
315 mutable RtosMutex mutex_;
316 std::unique_ptr<HalSpiTle92466edComm> comm_;
317 std::unique_ptr<DriverType> driver_;
318};
319
321
322#endif // COMPONENT_HANDLER_TLE92466ED_HANDLER_H_
CRTP SPI communication adapter for TLE92466ED using BaseSpi and BaseGpio.
Definition Tle92466edHandler.h:46
tle92466ed::CommResult< void > TransferMulti(std::span< const uint32_t > tx_data, std::span< uint32_t > rx_data) noexcept
Definition Tle92466edHandler.cpp:104
tle92466ed::CommResult< tle92466ed::GpioSignal > GpioRead(tle92466ed::CtrlPin pin) noexcept
Definition Tle92466edHandler.cpp:189
BaseGpio & resn_
Definition Tle92466edHandler.h:84
tle92466ed::CommError last_error_
Definition Tle92466edHandler.h:88
tle92466ed::CommResult< void > Deinit() noexcept
Definition Tle92466edHandler.cpp:71
tle92466ed::CommResult< void > Init() noexcept
Definition Tle92466edHandler.cpp:22
tle92466ed::CommResult< void > GpioSet(tle92466ed::CtrlPin pin, tle92466ed::GpioSignal signal) noexcept
Definition Tle92466edHandler.cpp:149
bool initialized_
Definition Tle92466edHandler.h:87
BaseGpio * faultn_
Definition Tle92466edHandler.h:86
tle92466ed::CommResult< uint32_t > Transfer32(uint32_t tx_data) noexcept
Definition Tle92466edHandler.cpp:76
tle92466ed::CommError GetLastError() const noexcept
Definition Tle92466edHandler.cpp:140
bool IsReady() const noexcept
Definition Tle92466edHandler.cpp:136
tle92466ed::CommResult< void > Configure(const tle92466ed::SPIConfig &config) noexcept
Definition Tle92466edHandler.cpp:130
tle92466ed::CommResult< void > Delay(uint32_t microseconds) noexcept
Definition Tle92466edHandler.cpp:125
BaseSpi & spi_
Definition Tle92466edHandler.h:83
BaseGpio & en_
Definition Tle92466edHandler.h:85
void Log(tle92466ed::LogLevel level, const char *tag, const char *format, va_list args) noexcept
Definition Tle92466edHandler.cpp:224
HalSpiTle92466edComm(BaseSpi &spi, BaseGpio &resn, BaseGpio &en, BaseGpio *faultn=nullptr) noexcept
Construct the SPI adapter.
Definition Tle92466edHandler.cpp:17
tle92466ed::CommResult< void > ClearErrors() noexcept
Definition Tle92466edHandler.cpp:144
Unified handler for TLE92466ED six-channel solenoid driver.
Definition Tle92466edHandler.h:109
tle92466ed::DriverResult< void > GetStatus(tle92466ed::DeviceStatus &status) noexcept
Get device status.
Definition Tle92466edHandler.cpp:417
std::unique_ptr< DriverType > driver_
Definition Tle92466edHandler.h:317
uint32_t GetIcVersion() noexcept
Get IC version (returns 0 on error).
Definition Tle92466edHandler.cpp:476
tle92466ed::DriverResult< void > EnterConfigMode() noexcept
Enter config mode (for register writes).
Definition Tle92466edHandler.cpp:405
bool EnsureInitialized() noexcept
Ensure driver is initialized (lazy init entrypoint).
Definition Tle92466edHandler.cpp:279
tle92466ed::DriverResult< void > KickWatchdog(uint16_t reload_value=1000) noexcept
Reload the SPI watchdog.
Definition Tle92466edHandler.cpp:461
tle92466ed::DriverResult< void > EnableAllChannels() noexcept
Enable all channels.
Definition Tle92466edHandler.cpp:369
tle92466ed::DriverResult< void > EnableChannel(uint8_t channel) noexcept
Enable a channel.
Definition Tle92466edHandler.cpp:353
tle92466ed::Driver< HalSpiTle92466edComm > DriverType
Driver type alias.
Definition Tle92466edHandler.h:112
tle92466ed::DriverResult< void > DisableChannel(uint8_t channel) noexcept
Disable a channel.
Definition Tle92466edHandler.cpp:361
tle92466ed::DriverResult< void > GetChannelDiagnostics(uint8_t channel, tle92466ed::ChannelDiagnostics &diag) noexcept
Get channel diagnostics.
Definition Tle92466edHandler.cpp:426
uint32_t GetChipId() noexcept
Get chip ID (returns 0 on error).
Definition Tle92466edHandler.cpp:467
tle92466ed::DriverResult< void > SetChannelCurrent(uint8_t channel, uint16_t current_ma) noexcept
Set channel current setpoint.
Definition Tle92466edHandler.cpp:381
bool EnsureInitializedLocked() noexcept
Definition Tle92466edHandler.cpp:319
bool IsMissionMode() noexcept
Check if in mission mode.
Definition Tle92466edHandler.cpp:411
bool initialized_
Definition Tle92466edHandler.h:314
std::unique_ptr< HalSpiTle92466edComm > comm_
Definition Tle92466edHandler.h:316
tle92466ed::DriverResult< void > Deinitialize() noexcept
Deinitialize — disable channels and shut down.
Definition Tle92466edHandler.cpp:330
Tle92466edHandler(BaseSpi &spi, BaseGpio &resn, BaseGpio &en, BaseGpio *faultn=nullptr) noexcept
Construct TLE92466ED handler.
Definition Tle92466edHandler.cpp:241
tle92466ed::DriverResult< void > ConfigurePwmRaw(uint8_t channel, uint8_t mantissa, uint8_t exponent, bool low_freq_range=false) noexcept
Configure raw PWM period for a channel.
Definition Tle92466edHandler.cpp:389
tle92466ed::DriverResult< void > ClearFaults() noexcept
Clear all fault flags.
Definition Tle92466edHandler.cpp:447
bool HasFault() noexcept
Check if any fault is present.
Definition Tle92466edHandler.cpp:453
tle92466ed::DriverResult< void > ConfigureChannel(uint8_t channel, const tle92466ed::ChannelConfig &config) noexcept
Configure a channel.
Definition Tle92466edHandler.cpp:344
tle92466ed::DriverResult< void > DisableAllChannels() noexcept
Disable all channels.
Definition Tle92466edHandler.cpp:375
tle92466ed::DriverResult< void > Initialize() noexcept
Initialize driver (hardware reset + SPI init + enter config mode).
Definition Tle92466edHandler.cpp:254
DriverType * GetDriver() noexcept
Get the underlying driver for advanced operations.
Definition Tle92466edHandler.cpp:484
static constexpr uint8_t kNumChannels
Definition Tle92466edHandler.h:114
bool IsInitialized() const noexcept
Check if initialized.
Definition Tle92466edHandler.h:166
tle92466ed::DriverResult< void > EnterMissionMode() noexcept
Enter mission mode (enable outputs after configuration).
Definition Tle92466edHandler.cpp:399
auto withDriver(Fn &&fn) noexcept
Execute a lambda with a locked, initialized driver.
Definition Tle92466edHandler.h:301
RtosMutex mutex_
Definition Tle92466edHandler.h:315
void DumpDiagnostics() noexcept
Dump diagnostics to logger.
Definition Tle92466edHandler.cpp:497
tle92466ed::DriverResult< void > GetFaultReport(tle92466ed::FaultReport &report) noexcept
Get comprehensive fault report.
Definition Tle92466edHandler.cpp:438
Type trait to detect tle92466ed::DriverResult<T> types.
Definition Tle92466edHandler.h:289