TLE92466ED Driver 0.1.0-preview
Modern C++23 driver for Infineon TLE92466ED Six-Channel Low-Side Solenoid Driver
Loading...
Searching...
No Matches
Arduino_HAL.hpp
Go to the documentation of this file.
1
31#pragma once
32
33#include <Arduino.h>
34#include <SPI.h>
35#include "TLE92466ED_HAL.hpp"
36
45class Arduino_HAL : public TLE92466ED_HAL {
46public:
51 struct SPIConfig {
52 uint8_t cs_pin = 10;
53 uint8_t en_pin = 9;
54 uint8_t resn_pin = 8;
55 uint32_t spi_frequency = 1000000;
56 uint8_t spi_mode = SPI_MODE0;
57 uint8_t spi_bit_order = MSBFIRST;
58 };
59
64 explicit Arduino_HAL(const SPIConfig& config = SPIConfig())
65 : m_config(config)
66 , m_initialized(false)
67 {
68 // Constructor body - initialization happens in initialize()
69 }
70
74 ~Arduino_HAL() override {
75 if (m_initialized) {
76 // Set CS high
77 digitalWrite(m_config.cs_pin, HIGH);
78 // Disable device if EN pin is used
79 if (m_config.en_pin != 255) {
80 digitalWrite(m_config.en_pin, LOW);
81 }
82 }
83 }
84
89 auto initialize() noexcept -> std::expected<void, TLE92466ED_Error> override {
90 if (m_initialized) {
91 return {}; // Already initialized
92 }
93
94 // Configure CS pin
95 pinMode(m_config.cs_pin, OUTPUT);
96 digitalWrite(m_config.cs_pin, HIGH); // CS idle high
97
98 // Configure EN pin if specified
99 if (m_config.en_pin != 255) {
100 pinMode(m_config.en_pin, OUTPUT);
101 digitalWrite(m_config.en_pin, HIGH); // Enable device
102 }
103
104 // Configure RESN pin if specified
105 if (m_config.resn_pin != 255) {
106 pinMode(m_config.resn_pin, OUTPUT);
107 digitalWrite(m_config.resn_pin, HIGH); // Not in reset
108
109 // Perform hardware reset
110 digitalWrite(m_config.resn_pin, LOW);
111 delay(10); // Hold reset for 10ms
112 digitalWrite(m_config.resn_pin, HIGH);
113 delay(10); // Wait for device to come out of reset
114 }
115
116 // Initialize SPI
117 SPI.begin();
118
119 m_initialized = true;
120
121 return {};
122 }
123
129 auto spiTransfer(uint32_t tx_data) noexcept -> std::expected<uint32_t, TLE92466ED_Error> override {
130 if (!m_initialized) {
131 return std::unexpected(TLE92466ED_Error::NOT_INITIALIZED);
132 }
133
134 // Begin SPI transaction with configured settings
135 SPI.beginTransaction(SPISettings(
139 ));
140
141 // Assert CS (active LOW)
142 digitalWrite(m_config.cs_pin, LOW);
143
144 // Small delay for CS setup time
146
147 // Transfer 4 bytes (32 bits), MSB first
148 uint32_t rx_data = 0;
149 rx_data |= static_cast<uint32_t>(SPI.transfer((tx_data >> 24) & 0xFF)) << 24;
150 rx_data |= static_cast<uint32_t>(SPI.transfer((tx_data >> 16) & 0xFF)) << 16;
151 rx_data |= static_cast<uint32_t>(SPI.transfer((tx_data >> 8) & 0xFF)) << 8;
152 rx_data |= static_cast<uint32_t>(SPI.transfer(tx_data & 0xFF));
153
154 // Small delay before deasserting CS
156
157 // Deassert CS (idle HIGH)
158 digitalWrite(m_config.cs_pin, HIGH);
159
160 // End SPI transaction
161 SPI.endTransaction();
162
163 return rx_data;
164 }
165
170 auto delayMicroseconds(uint32_t microseconds) noexcept -> void override {
171 ::delayMicroseconds(microseconds);
172 }
173
178 auto isInitialized() const noexcept -> bool {
179 return m_initialized;
180 }
181
186 auto getConfig() const noexcept -> const SPIConfig& {
187 return m_config;
188 }
189
193 auto enableDevice() noexcept -> void {
194 if (m_config.en_pin != 255) {
195 digitalWrite(m_config.en_pin, HIGH);
196 }
197 }
198
202 auto disableDevice() noexcept -> void {
203 if (m_config.en_pin != 255) {
204 digitalWrite(m_config.en_pin, LOW);
205 }
206 }
207
211 auto resetDevice() noexcept -> void {
212 if (m_config.resn_pin != 255) {
213 digitalWrite(m_config.resn_pin, LOW);
214 delay(10);
215 digitalWrite(m_config.resn_pin, HIGH);
216 delay(10);
217 }
218 }
219
220private:
223};
224
230 return new Arduino_HAL();
231}
232
239 return new Arduino_HAL(config);
240}
241
auto createTLE92466ED_HAL() -> Arduino_HAL *
Create Arduino HAL instance with default configuration.
Definition Arduino_HAL.hpp:229
Hardware Abstraction Layer (HAL) base class for TLE92466ED driver.
Arduino implementation of TLE92466ED HAL interface.
Definition Arduino_HAL.hpp:45
~Arduino_HAL() override
Destructor - cleanup resources.
Definition Arduino_HAL.hpp:74
auto enableDevice() noexcept -> void
Enable the TLE92466ED device (if EN pin is configured)
Definition Arduino_HAL.hpp:193
SPIConfig m_config
SPI configuration.
Definition Arduino_HAL.hpp:221
Arduino_HAL(const SPIConfig &config=SPIConfig())
Construct Arduino HAL with configuration.
Definition Arduino_HAL.hpp:64
auto resetDevice() noexcept -> void
Reset the TLE92466ED device (if RESN pin is configured)
Definition Arduino_HAL.hpp:211
auto spiTransfer(uint32_t tx_data) noexcept -> std::expected< uint32_t, TLE92466ED_Error > override
Transfer 32-bit SPI frame to/from TLE92466ED.
Definition Arduino_HAL.hpp:129
auto isInitialized() const noexcept -> bool
Check if HAL is initialized.
Definition Arduino_HAL.hpp:178
bool m_initialized
Initialization state.
Definition Arduino_HAL.hpp:222
auto disableDevice() noexcept -> void
Disable the TLE92466ED device (if EN pin is configured)
Definition Arduino_HAL.hpp:202
auto delayMicroseconds(uint32_t microseconds) noexcept -> void override
Delay for specified microseconds.
Definition Arduino_HAL.hpp:170
auto getConfig() const noexcept -> const SPIConfig &
Get current SPI configuration.
Definition Arduino_HAL.hpp:186
auto initialize() noexcept -> std::expected< void, TLE92466ED_Error > override
Initialize the Arduino HAL.
Definition Arduino_HAL.hpp:89
SPI configuration for Arduino.
Definition Arduino_HAL.hpp:51
uint8_t spi_bit_order
Bit order (MSB first)
Definition Arduino_HAL.hpp:57
uint8_t cs_pin
Chip Select pin (SS)
Definition Arduino_HAL.hpp:52
uint8_t resn_pin
Reset pin (optional)
Definition Arduino_HAL.hpp:54
uint8_t en_pin
Enable pin (optional)
Definition Arduino_HAL.hpp:53
uint8_t spi_mode
SPI mode (MODE0 for TLE92466ED)
Definition Arduino_HAL.hpp:56
uint32_t spi_frequency
SPI frequency (Hz) - 1MHz default.
Definition Arduino_HAL.hpp:55