HF Interface Wrapper 0.1.0-dev
Embedded C++ hardware abstraction layer
Loading...
Searching...
No Matches
StmI2c.h
Go to the documentation of this file.
1
32#pragma once
33
34#include "BaseI2c.h"
35#include "StmTypes.h"
36#include <vector>
37#include <memory>
38
39class StmI2cBus;
40
47class StmI2cDevice : public BaseI2c {
48public:
49 StmI2cDevice(StmI2cBus* parent, const hf_i2c_device_config_t& config) noexcept;
50 ~StmI2cDevice() noexcept override;
51
52 bool Initialize() noexcept override;
53 bool Deinitialize() noexcept override;
54
55 hf_i2c_err_t Write(const hf_u8_t* data, hf_u16_t length,
56 hf_u32_t timeout_ms = 0) noexcept override;
57 hf_i2c_err_t Read(hf_u8_t* data, hf_u16_t length,
58 hf_u32_t timeout_ms = 0) noexcept override;
59 hf_i2c_err_t WriteRead(const hf_u8_t* tx_data, hf_u16_t tx_length,
60 hf_u8_t* rx_data, hf_u16_t rx_length,
61 hf_u32_t timeout_ms = 0) noexcept override;
62 hf_u16_t GetDeviceAddress() const noexcept override;
63
65 const hf_i2c_device_config_t& GetConfig() const noexcept { return config_; }
66
68 StmI2cBus* GetParentBus() const noexcept { return parent_bus_; }
69
70private:
72 hf_u32_t GetEffectiveTimeout(hf_u32_t requested_ms) const noexcept;
73
75 static hf_i2c_err_t ConvertHalStatus(hf_u32_t hal_status) noexcept;
76
79};
80
87class StmI2cBus {
88public:
89 explicit StmI2cBus(const hf_i2c_bus_config_t& config) noexcept;
90
92 explicit StmI2cBus(I2C_HandleTypeDef* hal_handle, hf_u32_t timeout_ms = 1000) noexcept;
93
94 ~StmI2cBus() noexcept;
95
96 bool Initialize() noexcept;
97 bool IsInitialized() const noexcept;
98 bool Deinitialize() noexcept;
99
101 int CreateDevice(const hf_i2c_device_config_t& device_config) noexcept;
102
104 BaseI2c* GetDevice(int device_index) noexcept;
105 const BaseI2c* GetDevice(int device_index) const noexcept;
106
108 std::size_t GetDeviceCount() const noexcept;
109
111 bool RemoveDevice(int device_index) noexcept;
112
114 const hf_i2c_bus_config_t& GetConfig() const noexcept;
115
117 I2C_HandleTypeDef* GetHalHandle() const noexcept;
118
119private:
121 bool initialized_{false};
122 std::vector<std::unique_ptr<StmI2cDevice>> devices_;
123};
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
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:87
~StmI2cBus() noexcept
Definition StmI2c.cpp:181
hf_i2c_bus_config_t config_
Definition StmI2c.h:120
bool RemoveDevice(int device_index) noexcept
Remove a device by index.
Definition StmI2c.cpp:225
bool Initialize() noexcept
Definition StmI2c.cpp:185
std::vector< std::unique_ptr< StmI2cDevice > > devices_
Definition StmI2c.h:122
bool IsInitialized() const noexcept
Definition StmI2c.cpp:193
bool Deinitialize() noexcept
Definition StmI2c.cpp:195
BaseI2c * GetDevice(int device_index) noexcept
Get device by index (returns nullptr if invalid)
Definition StmI2c.cpp:211
int CreateDevice(const hf_i2c_device_config_t &device_config) noexcept
Create a device on this bus and return its index.
Definition StmI2c.cpp:205
std::size_t GetDeviceCount() const noexcept
Get number of devices on this bus.
Definition StmI2c.cpp:223
StmI2cBus(const hf_i2c_bus_config_t &config) noexcept
Definition StmI2c.cpp:175
I2C_HandleTypeDef * GetHalHandle() const noexcept
Get the STM32 HAL I2C handle.
Definition StmI2c.cpp:235
bool initialized_
Definition StmI2c.h:121
const hf_i2c_bus_config_t & GetConfig() const noexcept
Get the bus configuration.
Definition StmI2c.cpp:233
STM32 I2C device — inherits BaseI2c, delegates I/O to parent bus HAL handle.
Definition StmI2c.h:47
hf_i2c_device_config_t config_
Device configuration.
Definition StmI2c.h:78
const hf_i2c_device_config_t & GetConfig() const noexcept
Get the device configuration.
Definition StmI2c.h:65
bool Initialize() noexcept override
Initialize the I2C bus.
Definition StmI2c.cpp:42
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:112
StmI2cBus * GetParentBus() const noexcept
Get the parent bus.
Definition StmI2c.h:68
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:67
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:90
StmI2cBus * parent_bus_
Parent bus reference.
Definition StmI2c.h:77
hf_u16_t GetDeviceAddress() const noexcept override
Get the device address for this I2C device.
Definition StmI2c.cpp:151
bool Deinitialize() noexcept override
Deinitialize the I2C bus.
Definition StmI2c.cpp:62
hf_u32_t GetEffectiveTimeout(hf_u32_t requested_ms) const noexcept
Get effective timeout (device override or bus default)
Definition StmI2c.cpp:155
StmI2cDevice(StmI2cBus *parent, const hf_i2c_device_config_t &config) noexcept
Definition StmI2c.cpp:35
~StmI2cDevice() noexcept override
Definition StmI2c.cpp:38
static hf_i2c_err_t ConvertHalStatus(hf_u32_t hal_status) noexcept
Convert HAL status to I2C error.
Definition StmI2c.cpp:161
Platform-agnostic I2C bus configuration for STM32. Users pass their CubeMX-generated I2C_HandleTypeDe...
Definition StmTypes.h:249
I2C device configuration structure.
Definition EspTypes_I2C.h:225