42#define HF_I2C_ERR_LIST(X) \
44 X(I2C_SUCCESS, 0, "Success") \
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") \
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") \
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") \
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") \
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") \
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") \
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")
86#define X(NAME, VALUE, DESC) NAME = VALUE,
98#define X(NAME, VALUE, DESC) \
99 case hf_i2c_err_t::NAME: \
265 hf_u32_t timeout_ms = 0) noexcept = 0;
365 hf_u8_t buffer[2] = {reg_addr, data};
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