HF Interface Wrapper 0.1.0-dev
Embedded C++ hardware abstraction layer
Loading...
Searching...
No Matches
BaseI2c.h
Go to the documentation of this file.
1
20#pragma once
21
22#include "HardwareTypes.h"
23#include <cstdint>
24#include <string_view>
25
31//--------------------------------------
32// HardFOC I2C Error Codes (Table)
33//--------------------------------------
42#define HF_I2C_ERR_LIST(X) \
43 /* Success codes */ \
44 X(I2C_SUCCESS, 0, "Success") \
45 /* General errors */ \
46 X(I2C_ERR_FAILURE, 1, "General failure") \
47 X(I2C_ERR_NOT_INITIALIZED, 2, "Not initialized") \
48 X(I2C_ERR_ALREADY_INITIALIZED, 3, "Already initialized") \
49 X(I2C_ERR_INVALID_PARAMETER, 4, "Invalid parameter") \
50 X(I2C_ERR_NULL_POINTER, 5, "Null pointer") \
51 X(I2C_ERR_OUT_OF_MEMORY, 6, "Out of memory") \
52 /* Bus errors */ \
53 X(I2C_ERR_BUS_BUSY, 7, "Bus busy") \
54 X(I2C_ERR_BUS_ERROR, 8, "Bus error") \
55 X(I2C_ERR_BUS_ARBITRATION_LOST, 9, "Arbitration lost") \
56 X(I2C_ERR_BUS_NOT_AVAILABLE, 10, "Bus not available") \
57 X(I2C_ERR_BUS_TIMEOUT, 11, "Bus timeout") \
58 /* Device errors */ \
59 X(I2C_ERR_DEVICE_NOT_FOUND, 12, "Device not found") \
60 X(I2C_ERR_DEVICE_NACK, 13, "Device NACK") \
61 X(I2C_ERR_DEVICE_NOT_RESPONDING, 14, "Device not responding") \
62 X(I2C_ERR_INVALID_ADDRESS, 15, "Invalid device address") \
63 /* Data errors */ \
64 X(I2C_ERR_DATA_TOO_LONG, 16, "Data too long") \
65 X(I2C_ERR_READ_FAILURE, 17, "Read failure") \
66 X(I2C_ERR_WRITE_FAILURE, 18, "Write failure") \
67 X(I2C_ERR_TIMEOUT, 19, "Operation timeout") \
68 /* Hardware errors */ \
69 X(I2C_ERR_HARDWARE_FAULT, 20, "Hardware fault") \
70 X(I2C_ERR_COMMUNICATION_FAILURE, 21, "Communication failure") \
71 X(I2C_ERR_VOLTAGE_OUT_OF_RANGE, 22, "Voltage out of range") \
72 X(I2C_ERR_CLOCK_STRETCH_TIMEOUT, 23, "Clock stretch timeout") \
73 /* Configuration errors */ \
74 X(I2C_ERR_INVALID_CONFIGURATION, 24, "Invalid configuration") \
75 X(I2C_ERR_UNSUPPORTED_OPERATION, 25, "Unsupported operation") \
76 X(I2C_ERR_INVALID_CLOCK_SPEED, 26, "Invalid clock speed") \
77 X(I2C_ERR_PIN_CONFIGURATION_ERROR, 27, "Pin configuration error") \
78 /* System errors */ \
79 X(I2C_ERR_SYSTEM_ERROR, 28, "System error") \
80 X(I2C_ERR_PERMISSION_DENIED, 29, "Permission denied") \
81 X(I2C_ERR_OPERATION_ABORTED, 30, "Operation aborted") \
82 X(I2C_ERR_INVALID_STATE, 31, "Invalid state") \
83 X(I2C_ERR_UNKNOWN, 32, "Unknown error")
84
85enum class hf_i2c_err_t : hf_u8_t {
86#define X(NAME, VALUE, DESC) NAME = VALUE,
88#undef X
89};
90
96constexpr std::string_view HfI2CErrToString(hf_i2c_err_t err) noexcept {
97 switch (err) {
98#define X(NAME, VALUE, DESC) \
99 case hf_i2c_err_t::NAME: \
100 return DESC;
102#undef X
103 default:
105 }
106}
107
135
162
163//--------------------------------------
164// Abstract Base Class
165//--------------------------------------
166
196class BaseI2c {
197public:
201 virtual ~BaseI2c() noexcept = default;
202
203 // Non-copyable, non-movable (can be changed in derived classes if needed)
204 BaseI2c(const BaseI2c&) = delete;
205 BaseI2c& operator=(const BaseI2c&) = delete;
206 BaseI2c(BaseI2c&&) = delete;
207 BaseI2c& operator=(BaseI2c&&) = delete;
208
213 bool EnsureInitialized() noexcept {
214 if (!initialized_) {
216 }
217 return initialized_;
218 }
219
224 bool EnsureDeinitialized() noexcept {
225 if (initialized_) {
227 return !initialized_;
228 }
229 return true;
230 }
231
236 [[nodiscard]] bool IsInitialized() const noexcept {
237 return initialized_;
238 }
239
240 //==============================================//
241 // PURE VIRTUAL FUNCTIONS - MUST BE OVERRIDDEN //
242 //==============================================//
243
248 virtual bool Initialize() noexcept = 0;
249
254 virtual bool Deinitialize() noexcept = 0;
255
264 virtual hf_i2c_err_t Write(const hf_u8_t* data, hf_u16_t length,
265 hf_u32_t timeout_ms = 0) noexcept = 0;
266
275 virtual hf_i2c_err_t Read(hf_u8_t* data, hf_u16_t length, hf_u32_t timeout_ms = 0) noexcept = 0;
276
287 virtual hf_i2c_err_t WriteRead(const hf_u8_t* tx_data, hf_u16_t tx_length, hf_u8_t* rx_data,
288 hf_u16_t rx_length, hf_u32_t timeout_ms = 0) noexcept = 0;
289
294 virtual hf_u16_t GetDeviceAddress() const noexcept = 0;
295
296 //==============================================//
297 // CONVENIENCE METHODS WITH DEFAULT IMPLEMENTATIONS
298 //==============================================//
299
304 virtual bool Open() noexcept {
305 return Initialize();
306 }
307
312 virtual bool Close() noexcept {
313 return Deinitialize();
314 }
315
316 // Removed duplicate Write method to avoid overload conflicts
317
318 // Removed duplicate Read method to avoid overload conflicts
319
320 // Removed duplicate WriteRead method to avoid overload conflicts
321
326 virtual bool IsDevicePresent() noexcept {
327 // Try to read 1 byte from the device
328 hf_u8_t dummy;
329 return Read(&dummy, 1, 100) == hf_i2c_err_t::I2C_SUCCESS;
330 }
331
336 virtual bool ProbeDevice() noexcept {
337 return IsDevicePresent();
338 }
339
345 virtual bool WriteByte(hf_u8_t data) noexcept {
346 return Write(&data, 1) == hf_i2c_err_t::I2C_SUCCESS;
347 }
348
354 virtual bool ReadByte(hf_u8_t& data) noexcept {
355 return Read(&data, 1) == hf_i2c_err_t::I2C_SUCCESS;
356 }
357
364 virtual bool WriteRegister(hf_u8_t reg_addr, hf_u8_t data) noexcept {
365 hf_u8_t buffer[2] = {reg_addr, data};
366 return Write(buffer, 2) == hf_i2c_err_t::I2C_SUCCESS;
367 }
368
375 virtual bool ReadRegister(hf_u8_t reg_addr, hf_u8_t& data) noexcept {
376 return WriteRead(&reg_addr, 1, &data, 1) == hf_i2c_err_t::I2C_SUCCESS;
377 }
378
386 virtual bool ReadRegisters(hf_u8_t reg_addr, hf_u8_t* data, hf_u16_t length) noexcept {
387 return WriteRead(&reg_addr, 1, data, length) == hf_i2c_err_t::I2C_SUCCESS;
388 }
389
390 //==============================================//
391 // STATISTICS AND DIAGNOSTICS
392 //==============================================//
393
399 virtual hf_i2c_err_t ResetStatistics() noexcept {
400 statistics_ = hf_i2c_statistics_t{}; // Reset statistics to default values
402 }
403
409 virtual hf_i2c_err_t ResetDiagnostics() noexcept {
410 diagnostics_ = hf_i2c_diagnostics_t{}; // Reset diagnostics to default values
412 }
413
419 virtual hf_i2c_err_t GetStatistics(hf_i2c_statistics_t& statistics) const noexcept {
420 statistics = statistics_; // Return statistics by default
422 }
423
429 virtual hf_i2c_err_t GetDiagnostics(hf_i2c_diagnostics_t& diagnostics) const noexcept {
430 diagnostics = diagnostics_; // Return diagnostics by default
432 }
433
434protected:
439 explicit BaseI2c() noexcept : initialized_(false), statistics_{}, diagnostics_{} {}
440
444};
hf_i2c_err_t
Definition BaseI2c.h:85
@ I2C_ERR_UNSUPPORTED_OPERATION
#define X(NAME, VALUE, DESC)
Definition BaseI2c.h:86
constexpr std::string_view HfI2CErrToString(hf_i2c_err_t err) noexcept
Convert hf_i2c_err_t to human-readable string.
Definition BaseI2c.h:96
Platform-agnostic hardware type definitions for the HardFOC system.
uint32_t hf_u32_t
Platform-agnostic 32-bit unsigned integer type.
Definition HardwareTypes.h:52
uint8_t hf_u8_t
Platform-agnostic 8-bit unsigned integer type.
Definition HardwareTypes.h:40
uint64_t hf_u64_t
Platform-agnostic 64-bit unsigned integer type.
Definition HardwareTypes.h:58
uint16_t hf_u16_t
Platform-agnostic 16-bit unsigned integer type.
Definition HardwareTypes.h:46
Abstract base class for I2C device implementations.
Definition BaseI2c.h:196
virtual bool IsDevicePresent() noexcept
Check if this device is present on the bus.
Definition BaseI2c.h:326
virtual hf_i2c_err_t ResetStatistics() noexcept
Reset I2C operation statistics.
Definition BaseI2c.h:399
virtual bool ProbeDevice() noexcept
Probe if this device is present on the bus (alias for IsDevicePresent).
Definition BaseI2c.h:336
virtual hf_i2c_err_t GetStatistics(hf_i2c_statistics_t &statistics) const noexcept
Get I2C operation statistics.
Definition BaseI2c.h:419
virtual bool Deinitialize() noexcept=0
Deinitialize the I2C bus.
hf_i2c_statistics_t statistics_
I2C operation statistics.
Definition BaseI2c.h:442
bool EnsureDeinitialized() noexcept
Ensures that the I2C bus is deinitialized (lazy deinitialization).
Definition BaseI2c.h:224
virtual bool Close() noexcept
Close the I2C bus (alias for Deinitialize).
Definition BaseI2c.h:312
virtual hf_i2c_err_t WriteRead(const hf_u8_t *tx_data, hf_u16_t tx_length, hf_u8_t *rx_data, hf_u16_t rx_length, hf_u32_t timeout_ms=0) noexcept=0
Write then read data from the I2C device.
virtual bool WriteRegister(hf_u8_t reg_addr, hf_u8_t data) noexcept
Write to a register on the I2C device.
Definition BaseI2c.h:364
virtual hf_i2c_err_t GetDiagnostics(hf_i2c_diagnostics_t &diagnostics) const noexcept
Get I2C diagnostic information.
Definition BaseI2c.h:429
virtual ~BaseI2c() noexcept=default
Virtual destructor ensures proper cleanup in derived classes.
bool initialized_
Initialization status.
Definition BaseI2c.h:441
virtual hf_u16_t GetDeviceAddress() const noexcept=0
Get the device address for this I2C device.
virtual bool Open() noexcept
Open the I2C bus (alias for Initialize).
Definition BaseI2c.h:304
bool IsInitialized() const noexcept
Checks if the bus is initialized.
Definition BaseI2c.h:236
virtual hf_i2c_err_t Read(hf_u8_t *data, hf_u16_t length, hf_u32_t timeout_ms=0) noexcept=0
Read data from the I2C device.
bool EnsureInitialized() noexcept
Ensures that the I2C bus is initialized (lazy initialization).
Definition BaseI2c.h:213
virtual hf_i2c_err_t Write(const hf_u8_t *data, hf_u16_t length, hf_u32_t timeout_ms=0) noexcept=0
Write data to the I2C device.
virtual bool ReadRegisters(hf_u8_t reg_addr, hf_u8_t *data, hf_u16_t length) noexcept
Read multiple registers from the I2C device.
Definition BaseI2c.h:386
hf_i2c_diagnostics_t diagnostics_
I2C diagnostic information.
Definition BaseI2c.h:443
virtual bool ReadByte(hf_u8_t &data) noexcept
Read a single byte from the I2C device.
Definition BaseI2c.h:354
virtual bool Initialize() noexcept=0
Initialize the I2C bus.
virtual bool ReadRegister(hf_u8_t reg_addr, hf_u8_t &data) noexcept
Read from a register on the I2C device.
Definition BaseI2c.h:375
virtual hf_i2c_err_t ResetDiagnostics() noexcept
Reset I2C diagnostic information.
Definition BaseI2c.h:409
BaseI2c() noexcept
Protected default constructor. Initializes base I2C state with default values.
Definition BaseI2c.h:439
virtual bool WriteByte(hf_u8_t data) noexcept
Write a single byte to the I2C device.
Definition BaseI2c.h:345
#define HF_I2C_ERR_LIST(X)
HardFOC I2C error codes.
Definition BaseI2c.h:42
I2C diagnostic information.
Definition BaseI2c.h:139
hf_u32_t clock_stretching_events
Clock stretching event count.
Definition BaseI2c.h:150
bool bus_healthy
Overall bus health status.
Definition BaseI2c.h:140
hf_u32_t consecutive_errors
Consecutive error count.
Definition BaseI2c.h:146
bool bus_locked
Bus lock status.
Definition BaseI2c.h:143
hf_u32_t error_recovery_attempts
Bus recovery attempts.
Definition BaseI2c.h:147
hf_u32_t total_device_scans
Total device scan operations.
Definition BaseI2c.h:152
float bus_utilization_percent
Bus utilization percentage.
Definition BaseI2c.h:148
hf_u64_t last_error_timestamp_us
Timestamp of last error.
Definition BaseI2c.h:145
hf_u32_t devices_found_last_scan
Devices found in last scan.
Definition BaseI2c.h:153
hf_i2c_err_t last_error_code
Last error code encountered.
Definition BaseI2c.h:144
bool scl_line_state
Current SCL line state.
Definition BaseI2c.h:142
hf_u32_t average_response_time_us
Average device response time.
Definition BaseI2c.h:149
bool sda_line_state
Current SDA line state.
Definition BaseI2c.h:141
hf_i2c_diagnostics_t() noexcept
Definition BaseI2c.h:155
hf_u32_t active_device_count
Number of active devices on bus.
Definition BaseI2c.h:151
I2C operation statistics.
Definition BaseI2c.h:111
hf_u32_t devices_removed
Devices removed from bus.
Definition BaseI2c.h:126
hf_u64_t total_transaction_time_us
Total transaction time.
Definition BaseI2c.h:118
hf_u32_t min_transaction_time_us
Shortest transaction time.
Definition BaseI2c.h:120
hf_u64_t bytes_read
Total bytes read.
Definition BaseI2c.h:117
hf_u64_t failed_transactions
Failed transactions.
Definition BaseI2c.h:114
hf_u64_t successful_transactions
Successful transactions.
Definition BaseI2c.h:113
hf_u64_t timeout_count
Transaction timeouts.
Definition BaseI2c.h:115
hf_u32_t devices_added
Devices added to bus.
Definition BaseI2c.h:125
hf_u32_t nack_errors
NACK error count.
Definition BaseI2c.h:121
hf_u32_t max_transaction_time_us
Longest transaction time.
Definition BaseI2c.h:119
hf_u64_t bytes_written
Total bytes written.
Definition BaseI2c.h:116
hf_u64_t total_transactions
Total transactions attempted.
Definition BaseI2c.h:112
hf_i2c_statistics_t() noexcept
Definition BaseI2c.h:128
hf_u32_t clock_stretch_timeouts
Clock stretch timeouts.
Definition BaseI2c.h:124
hf_u32_t bus_errors
Bus error count.
Definition BaseI2c.h:122
hf_u32_t arbitration_lost_count
Arbitration lost count.
Definition BaseI2c.h:123