Internal Interface Wrap
API reference
Loading...
Searching...
No Matches
StmI2c.h
Go to the documentation of this file.
1
32#pragma once
33
34#include "BaseI2c.h"
35#include "PlatformMutex.h"
36#include "StmTypes.h"
37#include <vector>
38#include <memory>
39
40class StmI2cBus;
41
48class StmI2cDevice : public BaseI2c {
49public:
50 StmI2cDevice(StmI2cBus* parent, const hf_i2c_device_config_t& config) noexcept;
51 ~StmI2cDevice() noexcept override;
52
53 bool Initialize() noexcept override;
54 bool Deinitialize() noexcept override;
55
56 hf_i2c_err_t Write(const hf_u8_t* data, hf_u16_t length,
57 hf_u32_t timeout_ms = 0) noexcept override;
58 hf_i2c_err_t Read(hf_u8_t* data, hf_u16_t length,
59 hf_u32_t timeout_ms = 0) noexcept override;
60 hf_i2c_err_t WriteRead(const hf_u8_t* tx_data, hf_u16_t tx_length,
61 hf_u8_t* rx_data, hf_u16_t rx_length,
62 hf_u32_t timeout_ms = 0) noexcept override;
63 hf_u16_t GetDeviceAddress() const noexcept override;
64
66 void SetDeviceAddress(hf_u16_t address_7bit) noexcept;
67
69 const hf_i2c_device_config_t& GetConfig() const noexcept { return config_; }
70
72 StmI2cBus* GetParentBus() const noexcept { return parent_bus_; }
73
75 uint32_t LastHalError() const noexcept { return last_hal_error_; }
76
77private:
79 void NoteHalFailure(const I2C_HandleTypeDef* hi2c) noexcept;
80 uint32_t last_hal_error_{0};
82 hf_u32_t GetEffectiveTimeout(hf_u32_t requested_ms) const noexcept;
83
85 static hf_i2c_err_t ConvertHalStatus(hf_u32_t hal_status) noexcept;
86
89};
90
113public:
114 explicit StmI2cBus(const hf_i2c_bus_config_t& config) noexcept;
115
117 explicit StmI2cBus(I2C_HandleTypeDef* hal_handle, hf_u32_t timeout_ms = 1000) noexcept;
118
119 ~StmI2cBus() noexcept;
120
121 bool Initialize() noexcept;
122 bool IsInitialized() const noexcept;
123 bool Deinitialize() noexcept;
124
126 int CreateDevice(const hf_i2c_device_config_t& device_config) noexcept;
127
129 BaseI2c* GetDevice(int device_index) noexcept;
130 const BaseI2c* GetDevice(int device_index) const noexcept;
131
133 std::size_t GetDeviceCount() const noexcept;
134
136 bool RemoveDevice(int device_index) noexcept;
137
139 const hf_i2c_bus_config_t& GetConfig() const noexcept;
140
142 I2C_HandleTypeDef* GetHalHandle() const noexcept;
143
148 bool LockBus(hf_u32_t timeout_ms) noexcept;
149 void UnlockBus() noexcept;
150
151private:
152 friend class StmI2cDevice;
153
155 bool initialized_{false};
157 std::vector<std::unique_ptr<StmI2cDevice>> devices_;
158};
Abstract base class for I2C device implementations in the HardFOC system.
hf_i2c_err_t
Definition BaseI2c.h:85
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
uint16_t hf_u16_t
Platform-agnostic 16-bit unsigned integer type.
Definition HardwareTypes.h:46
Cross-platform mutex and synchronization primitives with pluggable backend.
STM32 platform-specific type definitions for hardware abstraction.
Abstract base class for I2C device implementations.
Definition BaseI2c.h:196
STM32 I2C bus — manages the HAL handle and device collection.
Definition StmI2c.h:112
~StmI2cBus() noexcept
Definition StmI2c.cpp:363
hf_i2c_bus_config_t config_
Definition StmI2c.h:154
bool RemoveDevice(int device_index) noexcept
Remove a device by index.
Definition StmI2c.cpp:407
bool Initialize() noexcept
Definition StmI2c.cpp:367
std::vector< std::unique_ptr< StmI2cDevice > > devices_
Definition StmI2c.h:157
bool IsInitialized() const noexcept
Definition StmI2c.cpp:375
bool Deinitialize() noexcept
Definition StmI2c.cpp:377
BaseI2c * GetDevice(int device_index) noexcept
Get device by index (returns nullptr if invalid)
Definition StmI2c.cpp:393
int CreateDevice(const hf_i2c_device_config_t &device_config) noexcept
Create a device on this bus and return its index.
Definition StmI2c.cpp:387
std::size_t GetDeviceCount() const noexcept
Get number of devices on this bus.
Definition StmI2c.cpp:405
I2C_HandleTypeDef * GetHalHandle() const noexcept
Get the STM32 HAL I2C handle.
Definition StmI2c.cpp:417
PlatformMutex bus_mutex_
Definition StmI2c.h:156
bool initialized_
Definition StmI2c.h:155
void UnlockBus() noexcept
Definition StmI2c.cpp:423
bool LockBus(hf_u32_t timeout_ms) noexcept
Exclusive bus lock for HAL transfer / probe (StmSpi parity).
Definition StmI2c.cpp:419
const hf_i2c_bus_config_t & GetConfig() const noexcept
Get the bus configuration.
Definition StmI2c.cpp:415
STM32 I2C device — inherits BaseI2c, delegates I/O to parent bus HAL handle.
Definition StmI2c.h:48
void NoteHalFailure(const I2C_HandleTypeDef *hi2c) noexcept
Count a failed transfer by HAL cause before recovery clears it.
Definition StmI2c.cpp:155
void SetDeviceAddress(hf_u16_t address_7bit) noexcept
Update the 7-bit slave address (e.g. after PCAL strap probe).
Definition StmI2c.cpp:333
hf_i2c_device_config_t config_
Device configuration.
Definition StmI2c.h:88
const hf_i2c_device_config_t & GetConfig() const noexcept
Get the device configuration.
Definition StmI2c.h:69
bool Initialize() noexcept override
Initialize the I2C bus.
Definition StmI2c.cpp:51
uint32_t last_hal_error_
Definition StmI2c.h:80
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 override
Write then read data from the I2C device.
Definition StmI2c.cpp:241
StmI2cBus * GetParentBus() const noexcept
Get the parent bus.
Definition StmI2c.h:72
hf_i2c_err_t Write(const hf_u8_t *data, hf_u16_t length, hf_u32_t timeout_ms=0) noexcept override
Write data to the I2C device.
Definition StmI2c.cpp:168
hf_i2c_err_t Read(hf_u8_t *data, hf_u16_t length, hf_u32_t timeout_ms=0) noexcept override
Read data from the I2C device.
Definition StmI2c.cpp:206
StmI2cBus * parent_bus_
Parent bus reference.
Definition StmI2c.h:87
hf_u16_t GetDeviceAddress() const noexcept override
Get the device address for this I2C device.
Definition StmI2c.cpp:329
uint32_t LastHalError() const noexcept
HAL ErrorCode of the newest failed transfer (HAL_I2C_ERROR_*; 0 = none yet).
Definition StmI2c.h:75
bool Deinitialize() noexcept override
Deinitialize the I2C bus.
Definition StmI2c.cpp:65
hf_u32_t GetEffectiveTimeout(hf_u32_t requested_ms) const noexcept
Get effective timeout (device override or bus default)
Definition StmI2c.cpp:337
~StmI2cDevice() noexcept override
Definition StmI2c.cpp:47
static hf_i2c_err_t ConvertHalStatus(hf_u32_t hal_status) noexcept
Convert HAL status to I2C error.
Definition StmI2c.cpp:343
Platform-agnostic I2C bus configuration for STM32. Users pass their CubeMX-generated I2C_HandleTypeDe...
Definition StmTypes.h:293
I2C device configuration structure.
Definition EspTypes_I2C.h:225