HardFOC Core
API reference
Loading...
Searching...
No Matches
Tle92466edHandler.h
Go to the documentation of this file.
1
30#ifndef COMPONENT_HANDLER_TLE92466ED_HANDLER_H_
31#define COMPONENT_HANDLER_TLE92466ED_HANDLER_H_
32
33#include <cstdint>
34#include <memory>
35#include <cstdarg>
36#include <span>
37#include <type_traits>
38#include <utility>
39#include "core/hf-core-drivers/external/hf-tle92466ed-driver/inc/tle92466ed.hpp"
40#include "core/hf-core-drivers/external/hf-tle92466ed-driver/inc/tle92466ed_spi_interface.hpp"
41#include "base/BaseSpi.h"
42#include "base/BaseGpio.h"
43#include "RtosMutex.h"
44
49
62class HalSpiTle92466edComm : public tle92466ed::SpiInterface<HalSpiTle92466edComm> {
63public:
71 HalSpiTle92466edComm(BaseSpi& spi, BaseGpio& resn, BaseGpio& en,
72 BaseGpio* faultn = nullptr) noexcept;
73
76
77 // Bring base class variadic Log into scope (avoids name hiding)
78 using tle92466ed::SpiInterface<HalSpiTle92466edComm>::Log;
79
84 tle92466ed::CommResult<void> Init() noexcept;
85
87 tle92466ed::CommResult<void> Deinit() noexcept;
88
95 tle92466ed::CommResult<uint32_t> Transfer32(uint32_t tx_data) noexcept;
96
106 tle92466ed::CommResult<void> TransferMulti(std::span<const uint32_t> tx_data,
107 std::span<uint32_t> rx_data) noexcept;
108
114 tle92466ed::CommResult<void> TransferMulti(std::span<const uint32_t> tx_data,
115 std::span<uint32_t> rx_data,
116 uint32_t gap_us) noexcept;
117
119 tle92466ed::CommResult<void> Delay(uint32_t microseconds) noexcept;
120
122 tle92466ed::CommResult<void> Configure(const tle92466ed::SPIConfig& config) noexcept;
123
125 [[nodiscard]] bool IsReady() const noexcept;
126
128 [[nodiscard]] tle92466ed::CommError GetLastError() const noexcept;
129
131 tle92466ed::CommResult<void> ClearErrors() noexcept;
132
138 tle92466ed::CommResult<void> GpioSet(tle92466ed::CtrlPin pin,
139 tle92466ed::GpioSignal signal) noexcept;
140
145 tle92466ed::CommResult<tle92466ed::GpioSignal> GpioRead(tle92466ed::CtrlPin pin) noexcept;
146
148 void Log(tle92466ed::LogLevel level, const char* tag,
149 const char* format, va_list args) noexcept;
150
152 [[nodiscard]] uint32_t LastRxWord() const noexcept { return last_rx_; }
153
162 [[nodiscard]] uint32_t LastRxWordFirst() const noexcept { return last_rx_first_; }
163
165
166private:
167 BaseSpi& spi_;
168 BaseGpio& resn_;
169 BaseGpio& en_;
170 BaseGpio* faultn_;
171 bool initialized_{false};
172 tle92466ed::CommError last_error_{tle92466ed::CommError::None};
173 uint32_t last_rx_{0};
174 uint32_t last_rx_first_{0};
175};
176
178
183
198public:
200 using DriverType = tle92466ed::Driver<HalSpiTle92466edComm>;
201
203 static constexpr uint8_t kNumChannels = 6;
204
205 //=========================================================================
206 // Construction
207 //=========================================================================
208
216 Tle92466edHandler(BaseSpi& spi, BaseGpio& resn, BaseGpio& en,
217 BaseGpio* faultn = nullptr) noexcept;
218
220 ~Tle92466edHandler() noexcept;
221
222 // Non-copyable
224 Tle92466edHandler& operator=(const Tle92466edHandler&) = delete;
225
226 // Non-movable
228 Tle92466edHandler& operator=(Tle92466edHandler&&) = delete;
229
230 //=========================================================================
231 // Initialization
232 //=========================================================================
233
241 tle92466ed::DriverResult<void> Initialize(bool perform_hardware_reset = true) noexcept;
242
248 [[nodiscard]] bool EnsureInitialized() noexcept;
249
262 tle92466ed::DriverResult<void> Initialize(const tle92466ed::GlobalConfig& config) noexcept;
263
269 tle92466ed::DriverResult<void> Deinitialize() noexcept;
270
272 [[nodiscard]] bool IsInitialized() const noexcept { return initialized_; }
273
274 //=========================================================================
275 // Channel Control
276 //=========================================================================
277
284 tle92466ed::DriverResult<void> ConfigureChannel(uint8_t channel, const tle92466ed::ChannelConfig& config) noexcept;
285
290 tle92466ed::DriverResult<void> EnableChannel(uint8_t channel) noexcept;
291
296 tle92466ed::DriverResult<void> DisableChannel(uint8_t channel) noexcept;
297
299 tle92466ed::DriverResult<void> EnableAllChannels() noexcept;
300
302 tle92466ed::DriverResult<void> DisableAllChannels() noexcept;
303
309 tle92466ed::DriverResult<void> SetChannelCurrent(uint8_t channel, uint16_t current_ma) noexcept;
310
316 tle92466ed::DriverResult<void> SetChannelCurrentUa(uint8_t channel, uint32_t current_ua) noexcept;
317
324 tle92466ed::DriverResult<uint16_t> GetChannelCurrentSetpoint(
325 uint8_t channel, bool parallel_mode = false) noexcept;
326
334 tle92466ed::DriverResult<void> ConfigurePwmRaw(uint8_t channel, uint8_t mantissa,
335 uint8_t exponent, bool low_freq_range = false) noexcept;
336
337 //=========================================================================
338 // Device Mode
339 //=========================================================================
340
345 tle92466ed::DriverResult<void> EnterMissionMode() noexcept;
346
348 tle92466ed::DriverResult<void> EnterConfigMode() noexcept;
349
351 [[nodiscard]] bool IsMissionMode() noexcept;
352
353 //=========================================================================
354 // Hardware Output Stage (EN pin)
355 //=========================================================================
356
370 tle92466ed::DriverResult<void> EnableOutputStage() noexcept;
371
377 tle92466ed::DriverResult<void> DisableOutputStage() noexcept;
378
393 tle92466ed::DriverResult<void> EnableFeedbackUpdates() noexcept;
394
395 //=========================================================================
396 // Status & Diagnostics
397 //=========================================================================
398
403 tle92466ed::DriverResult<void> GetStatus(tle92466ed::DeviceStatus& status) noexcept;
404
411 tle92466ed::DriverResult<void> GetChannelDiagnostics(uint8_t channel, tle92466ed::ChannelDiagnostics& diag) noexcept;
412
417 tle92466ed::DriverResult<void> GetFaultReport(tle92466ed::FaultReport& report) noexcept;
418
420 tle92466ed::DriverResult<void> ClearFaults() noexcept;
421
426 [[nodiscard]] bool HasFault() noexcept;
427
428 //=========================================================================
429 // Watchdog
430 //=========================================================================
431
436 tle92466ed::DriverResult<void> KickWatchdog(uint16_t reload_value = 1000) noexcept;
437
438 //=========================================================================
439 // Device Info
440 //=========================================================================
441
446 uint32_t GetChipId() noexcept;
447
452 uint32_t GetIcVersion() noexcept;
453
458 [[nodiscard]] uint32_t LastSpiRxWord() const noexcept {
459 return comm_ ? comm_->LastRxWord() : 0U;
460 }
461
466 [[nodiscard]] uint32_t LastSpiRxWordFirst() const noexcept {
467 return comm_ ? comm_->LastRxWordFirst() : 0U;
468 }
469
487 [[nodiscard]] bool RawSpiChain(std::span<const uint32_t> tx,
488 std::span<uint32_t> rx,
489 uint32_t gap_us = 0U) noexcept {
490 MutexLockGuard lock(mutex_);
491 if (!EnsureInitializedLocked() || !comm_) {
492 return false;
493 }
494 return comm_->TransferMulti(tx, rx, gap_us).has_value();
495 }
496
511 template <typename Fn>
512 auto WithLock(Fn&& fn) noexcept {
513 MutexLockGuard lock(mutex_);
514 return std::forward<Fn>(fn)();
515 }
516
517 //=========================================================================
518 // Direct Driver Access
519 //=========================================================================
520
540 template <typename Fn>
541 auto WithDriver(Fn&& fn) noexcept {
542 using R = std::invoke_result_t<Fn, DriverType&>;
543 if constexpr (std::is_void_v<R>) {
544 MutexLockGuard lock(mutex_);
545 if (!EnsureInitializedLocked() || !driver_) return false;
546 fn(*driver_);
547 return true;
548 } else {
549 return withDriver(std::forward<Fn>(fn));
550 }
551 }
552
563 [[nodiscard]] DriverType* GetDriver() noexcept;
564 [[nodiscard]] const DriverType* GetDriver() const noexcept;
565
570 void DumpDiagnostics() noexcept;
571
572private:
574 tle92466ed::DriverResult<void> InitializeLocked(bool perform_hardware_reset) noexcept;
575 bool EnsureInitializedLocked() noexcept;
576
578 template <typename T>
579 struct is_driver_result : std::false_type {};
580 template <typename T>
581 struct is_driver_result<tle92466ed::DriverResult<T>> : std::true_type {};
582
590 template <typename Fn>
591 auto withDriver(Fn&& fn) noexcept {
592 using R = std::invoke_result_t<Fn, DriverType&>;
593 static_assert(!std::is_void_v<R>, "withDriver requires non-void return");
594 MutexLockGuard lock(mutex_);
595 if (!EnsureInitializedLocked() || !driver_) {
596 if constexpr (is_driver_result<R>::value)
597 return R{tle::unexpected(tle92466ed::DriverError::NotInitialized)};
598 else
599 return R{};
600 }
601 return fn(*driver_);
602 }
603
604 bool initialized_{false};
605 mutable RtosMutex mutex_;
606 std::unique_ptr<HalSpiTle92466edComm> comm_;
607 std::unique_ptr<DriverType> driver_;
608};
609
611
612#endif // COMPONENT_HANDLER_TLE92466ED_HANDLER_H_
CRTP SPI communication adapter for TLE92466ED using BaseSpi and BaseGpio.
Definition Tle92466edHandler.h:62
tle92466ed::CommResult< void > TransferMulti(std::span< const uint32_t > tx_data, std::span< uint32_t > rx_data) noexcept
Pipelined multi-frame SPI transfer under one SPI2 bus lock.
Definition Tle92466edHandler.cpp:135
tle92466ed::CommResult< tle92466ed::GpioSignal > GpioRead(tle92466ed::CtrlPin pin) noexcept
Read RESN, EN, or FAULTN logical state.
Definition Tle92466edHandler.cpp:281
uint32_t LastRxWordFirst() const noexcept
RX word from the FIRST frame of the last TransferMulti chain.
Definition Tle92466edHandler.h:162
uint32_t last_rx_first_
Definition Tle92466edHandler.h:174
BaseGpio & resn_
Definition Tle92466edHandler.h:168
tle92466ed::CommError last_error_
Definition Tle92466edHandler.h:172
tle92466ed::CommResult< void > Deinit() noexcept
Mark adapter deinitialized; does not change GPIO or SPI state.
Definition Tle92466edHandler.cpp:93
tle92466ed::CommResult< void > Init() noexcept
Reset pulse, assert EN, then SPI identity / fault clear.
Definition Tle92466edHandler.cpp:47
tle92466ed::CommResult< void > GpioSet(tle92466ed::CtrlPin pin, tle92466ed::GpioSignal signal) noexcept
Drive RESN, EN, or FAULTN via configured BaseGpio active levels.
Definition Tle92466edHandler.cpp:240
bool initialized_
Definition Tle92466edHandler.h:171
uint32_t LastRxWord() const noexcept
Last 32-bit SPI RX word (bring-up / WrongDeviceID diagnostics).
Definition Tle92466edHandler.h:152
BaseGpio * faultn_
Definition Tle92466edHandler.h:170
tle92466ed::CommResult< uint32_t > Transfer32(uint32_t tx_data) noexcept
Single 32-bit SPI frame (Mode 1, MSB first).
Definition Tle92466edHandler.cpp:99
tle92466ed::CommError GetLastError() const noexcept
Last comm-layer error from Transfer32 or GPIO helpers.
Definition Tle92466edHandler.cpp:231
bool IsReady() const noexcept
True after Init succeeded (GPIO polarity parked, SPI ready).
Definition Tle92466edHandler.cpp:227
tle92466ed::CommResult< void > Configure(const tle92466ed::SPIConfig &config) noexcept
No-op; BaseSpi is pre-configured for Mode 1 / 32-bit frames.
Definition Tle92466edHandler.cpp:221
tle92466ed::CommResult< void > Delay(uint32_t microseconds) noexcept
Busy-wait delay in microseconds (datasheet timing gaps).
Definition Tle92466edHandler.cpp:216
BaseSpi & spi_
Definition Tle92466edHandler.h:167
BaseGpio & en_
Definition Tle92466edHandler.h:169
void Log(tle92466ed::LogLevel level, const char *tag, const char *format, va_list args) noexcept
Route driver log lines to the HAL Logger.
Definition Tle92466edHandler.cpp:316
uint32_t last_rx_
Definition Tle92466edHandler.h:173
tle92466ed::CommResult< void > ClearErrors() noexcept
Clear last_error_; does not clear chip fault registers.
Definition Tle92466edHandler.cpp:235
Unified handler for TLE92466ED six-channel solenoid driver.
Definition Tle92466edHandler.h:197
uint32_t LastSpiRxWord() const noexcept
Last SPI RX word from HalSpiTle92466edComm (init / WrongDeviceID diag).
Definition Tle92466edHandler.h:458
tle92466ed::DriverResult< void > GetStatus(tle92466ed::DeviceStatus &status) noexcept
Read global device status (mode, rail voltages, OT flags, …).
Definition Tle92466edHandler.cpp:556
std::unique_ptr< DriverType > driver_
Definition Tle92466edHandler.h:607
uint32_t GetIcVersion() noexcept
Read IC version register (ICVID).
Definition Tle92466edHandler.cpp:615
tle92466ed::DriverResult< void > EnterConfigMode() noexcept
Enter config mode so register writes are accepted.
Definition Tle92466edHandler.cpp:544
bool EnsureInitialized() noexcept
Lazy-init entrypoint: initialize on first use if needed.
Definition Tle92466edHandler.cpp:381
tle92466ed::DriverResult< void > KickWatchdog(uint16_t reload_value=1000) noexcept
Reload the SPI watchdog (must be kicked periodically in mission mode).
Definition Tle92466edHandler.cpp:600
tle92466ed::DriverResult< void > EnableAllChannels() noexcept
Set CH_CTRL enable for every channel.
Definition Tle92466edHandler.cpp:472
tle92466ed::DriverResult< void > EnableChannel(uint8_t channel) noexcept
Enable a channel in CH_CTRL (requires EnableOutputStage for load current).
Definition Tle92466edHandler.cpp:456
tle92466ed::Driver< HalSpiTle92466edComm > DriverType
Driver type alias (tle92466ed::Driver bound to HalSpiTle92466edComm).
Definition Tle92466edHandler.h:200
tle92466ed::DriverResult< void > DisableChannel(uint8_t channel) noexcept
Disable a channel in CH_CTRL.
Definition Tle92466edHandler.cpp:464
tle92466ed::DriverResult< void > GetChannelDiagnostics(uint8_t channel, tle92466ed::ChannelDiagnostics &diag) noexcept
Read per-channel diagnostics (faults, FB_I_AVG, duty cycle, …).
Definition Tle92466edHandler.cpp:565
tle92466ed::DriverResult< uint16_t > GetChannelCurrentSetpoint(uint8_t channel, bool parallel_mode=false) noexcept
Read back programmed current setpoint for SW-HAL-01 verification.
Definition Tle92466edHandler.cpp:500
bool RawSpiChain(std::span< const uint32_t > tx, std::span< uint32_t > rx, uint32_t gap_us=0U) noexcept
Clock tx out as one bus-ownership chain and capture every reply.
Definition Tle92466edHandler.h:487
uint32_t GetChipId() noexcept
Read combined chip ID words for identity checks.
Definition Tle92466edHandler.cpp:606
tle92466ed::DriverResult< void > SetChannelCurrent(uint8_t channel, uint16_t current_ma) noexcept
Set channel current setpoint.
Definition Tle92466edHandler.cpp:484
uint32_t LastSpiRxWordFirst() const noexcept
RX word from the first frame of the last pipelined SPI chain.
Definition Tle92466edHandler.h:466
bool EnsureInitializedLocked() noexcept
Definition Tle92466edHandler.cpp:421
bool IsMissionMode() noexcept
true when the device is in mission (non-config) mode.
Definition Tle92466edHandler.cpp:550
tle92466ed::DriverResult< void > DisableOutputStage() noexcept
Drive the chip's EN pin LOW to gate off the output stage. SPI access remains functional; only the low...
Definition Tle92466edHandler.cpp:531
bool initialized_
Definition Tle92466edHandler.h:604
std::unique_ptr< HalSpiTle92466edComm > comm_
Definition Tle92466edHandler.h:606
tle92466ed::DriverResult< void > Deinitialize() noexcept
Disable all channels and tear down driver_.
Definition Tle92466edHandler.cpp:433
tle92466ed::DriverResult< void > InitializeLocked(bool perform_hardware_reset) noexcept
Initialize while mutex_ is already held.
Definition Tle92466edHandler.cpp:354
tle92466ed::DriverResult< void > ConfigurePwmRaw(uint8_t channel, uint8_t mantissa, uint8_t exponent, bool low_freq_range=false) noexcept
Configure raw TP_MANT / TP_EXP PWM period fields.
Definition Tle92466edHandler.cpp:509
tle92466ed::DriverResult< void > ClearFaults() noexcept
Clear latched fault registers after the fault condition is removed.
Definition Tle92466edHandler.cpp:586
bool HasFault() noexcept
Check if any fault is present.
Definition Tle92466edHandler.cpp:592
tle92466ed::DriverResult< void > ConfigureChannel(uint8_t channel, const tle92466ed::ChannelConfig &config) noexcept
Configure a channel (PWM, dither, current limit, …).
Definition Tle92466edHandler.cpp:447
tle92466ed::DriverResult< void > DisableAllChannels() noexcept
Clear CH_CTRL enable for every channel.
Definition Tle92466edHandler.cpp:478
auto WithLock(Fn &&fn) noexcept
Run fn (no arguments) while mutex_ is held.
Definition Tle92466edHandler.h:512
tle92466ed::DriverResult< void > Initialize(bool perform_hardware_reset=true) noexcept
Initialize driver (hardware reset + SPI init + config mode).
Definition Tle92466edHandler.cpp:348
tle92466ed::DriverResult< void > EnableFeedbackUpdates() noexcept
Clear the FB_FRZ register so the chip updates FB_DC / FB_I_AVG / FB_VBAT on every channel.
Definition Tle92466edHandler.cpp:537
auto WithDriver(Fn &&fn) noexcept
Run fn against the raw driver with mutex_ held.
Definition Tle92466edHandler.h:541
DriverType * GetDriver() noexcept
Get the underlying driver for advanced operations.
Definition Tle92466edHandler.cpp:623
static constexpr uint8_t kNumChannels
Number of solenoid channels (0–5); only CH5 is populated on this board.
Definition Tle92466edHandler.h:203
tle92466ed::DriverResult< void > EnableOutputStage() noexcept
Drive the chip's hardware EN pin HIGH so the output stage can deliver current to the load.
Definition Tle92466edHandler.cpp:525
bool IsInitialized() const noexcept
true after a successful Initialize or lazy EnsureInitialized.
Definition Tle92466edHandler.h:272
tle92466ed::DriverResult< void > EnterMissionMode() noexcept
Enter mission mode after configuration (required before driving loads).
Definition Tle92466edHandler.cpp:519
auto withDriver(Fn &&fn) noexcept
Execute a lambda with a locked, initialized driver.
Definition Tle92466edHandler.h:591
RtosMutex mutex_
Definition Tle92466edHandler.h:605
tle92466ed::DriverResult< void > SetChannelCurrentUa(uint8_t channel, uint32_t current_ua) noexcept
Set channel ICC setpoint in microamperes (15-bit TARGET resolution, ~61 µA LSB).
Definition Tle92466edHandler.cpp:492
void DumpDiagnostics() noexcept
Log device status, per-channel diagnostics, and chip ID to Logger.
Definition Tle92466edHandler.cpp:639
tle92466ed::DriverResult< void > GetFaultReport(tle92466ed::FaultReport &report) noexcept
Aggregate fault snapshot across device and all channels.
Definition Tle92466edHandler.cpp:577
Type trait to detect tle92466ed::DriverResult<T> types.
Definition Tle92466edHandler.h:579