libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::STM32I2C Class Reference
Inheritance diagram for LibXR::STM32I2C:
[legend]
Collaboration diagram for LibXR::STM32I2C:
[legend]

Data Structures

struct  HasClockSpeed
 
struct  HasClockSpeed< T, std::void_t< decltype(std::declval< T >() ->Init.ClockSpeed)> >
 

Public Member Functions

 STM32I2C (I2C_HandleTypeDef *hi2c, RawData dma_buff, uint32_t dma_enable_min_size=3)
 
ErrorCode Read (uint16_t slave_addr, RawData read_data, ReadOperation &op) override
 读取 I2C 设备的数据。 Reads data from an I2C device.
 
ErrorCode Write (uint16_t slave_addr, ConstRawData write_data, WriteOperation &op) override
 I2C 设备写入数据。 Writes data to an I2C device.
 
ErrorCode MemRead (uint16_t slave_addr, uint16_t mem_addr, RawData read_data, ReadOperation &op, MemAddrLength mem_addr_size) override
 I2C 设备指定寄存器读取数据。 Reads data from a specific register of an I2C device.
 
ErrorCode MemWrite (uint16_t slave_addr, uint16_t mem_addr, ConstRawData write_data, WriteOperation &op, MemAddrLength mem_addr_size) override
 I2C 设备指定寄存器写入数据。 Writes data to a specific register of an I2C device.
 
template<typename T >
std::enable_if<!HasClockSpeed< T >::value >::type SetClockSpeed (T &, const Configuration &)
 
template<typename T >
std::enable_if< HasClockSpeed< T >::value >::type SetClockSpeed (T &i2c_handle, const Configuration &config)
 
ErrorCode SetConfig (Configuration config) override
 配置 I2C 设备参数。 Configures the I2C device settings.
 
- Public Member Functions inherited from LibXR::I2C
 I2C ()
 默认构造函数。 Default constructor.
 

Data Fields

stm32_i2c_id_t id_
 
I2C_HandleTypeDef * i2c_handle_
 
uint32_t dma_enable_min_size_
 
RawData dma_buff_
 
ReadOperation read_op_
 
WriteOperation write_op_
 
RawData read_buff_
 
bool read_ = false
 

Static Public Attributes

static STM32I2Cmap [STM32_I2C_NUMBER] = {nullptr}
 

Additional Inherited Members

- Public Types inherited from LibXR::I2C
enum class  MemAddrLength : uint8_t { BYTE_8 , BYTE_16 }
 

Detailed Description

Definition at line 48 of file stm32_i2c.hpp.

Constructor & Destructor Documentation

◆ STM32I2C()

STM32I2C::STM32I2C ( I2C_HandleTypeDef * hi2c,
RawData dma_buff,
uint32_t dma_enable_min_size = 3 )

Definition at line 66 of file stm32_i2c.cpp.

68 : I2C(),
69 id_(STM32_I2C_GetID(hi2c->Instance)),
70 i2c_handle_(hi2c),
71 dma_enable_min_size_(dma_enable_min_size),
72 dma_buff_(dma_buff)
73{
74 map[id_] = this;
75}
I2C()
默认构造函数。 Default constructor.
Definition i2c.hpp:39

Member Function Documentation

◆ MemRead()

ErrorCode STM32I2C::MemRead ( uint16_t slave_addr,
uint16_t mem_addr,
RawData read_data,
ReadOperation & op,
MemAddrLength mem_addr_size )
overridevirtual

I2C 设备指定寄存器读取数据。 Reads data from a specific register of an I2C device.

该函数从指定 I2C 从设备的寄存器地址读取数据,并存储到 read_data 中。 This function reads data from the specified register of the I2C slave and stores it in read_data.

Parameters
slave_addrI2C 从设备的八位地址。 The eight-bit address of the I2C slave device.
mem_addr寄存器地址(通常为 8 位或 16 位)。 Register address (typically 8-bit or 16-bit).
read_data用于存储读取数据的 RawData 对象。 RawData object to store read data.
op异步或同步的读取操作对象。 Read operation object (sync or async).
mem_addr_size寄存器地址长度。 Size of register address in bytes.
Returns
返回 ErrorCode,表示是否读取成功。 Returns ErrorCode indicating success or failure.

Implements LibXR::I2C.

Definition at line 161 of file stm32_i2c.cpp.

163{
164 ASSERT(read_data.size_ <= dma_buff_.size_);
165
166 if (i2c_handle_->State != HAL_I2C_STATE_READY)
167 {
168 return ErrorCode::BUSY;
169 }
170
171 read_ = true;
172
173 if (read_data.size_ > dma_enable_min_size_)
174 {
175 read_op_ = op;
176 HAL_I2C_Mem_Read_DMA(i2c_handle_, slave_addr, mem_addr,
177 mem_addr_size == MemAddrLength::BYTE_8 ? I2C_MEMADD_SIZE_8BIT
178 : I2C_MEMADD_SIZE_16BIT,
179 reinterpret_cast<uint8_t *>(dma_buff_.addr_), read_data.size_);
180 read_buff_ = read_data;
181 op.MarkAsRunning();
182 if (op.type == ReadOperation::OperationType::BLOCK)
183 {
184 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
185 }
186 return ErrorCode::OK;
187 }
188 else
189 {
190 auto ans =
191 HAL_I2C_Mem_Read(i2c_handle_, slave_addr, mem_addr,
192 mem_addr_size == MemAddrLength::BYTE_8 ? I2C_MEMADD_SIZE_8BIT
193 : I2C_MEMADD_SIZE_16BIT,
194 reinterpret_cast<uint8_t *>(read_data.addr_), read_data.size_,
195 20) == HAL_OK
196 ? ErrorCode::OK
197 : ErrorCode::BUSY;
198
199 op.UpdateStatus(false, std::forward<ErrorCode>(ans));
200 if (op.type == ReadOperation::OperationType::BLOCK)
201 {
202 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
203 }
204 return ans;
205 }
206}
void MarkAsRunning()
标记操作为运行状态。 Marks the operation as running.
Definition libxr_rw.hpp:202
void UpdateStatus(bool in_isr, Status &&...status)
Updates operation status based on type.
Definition libxr_rw.hpp:171
union LibXR::Operation::@4 data
OperationType type
Definition libxr_rw.hpp:225
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:25

◆ MemWrite()

ErrorCode STM32I2C::MemWrite ( uint16_t slave_addr,
uint16_t mem_addr,
ConstRawData write_data,
WriteOperation & op,
MemAddrLength mem_addr_size )
overridevirtual

I2C 设备指定寄存器写入数据。 Writes data to a specific register of an I2C device.

该函数将 write_data 写入指定 I2C 从设备的寄存器地址。 This function writes write_data to the specified register of the I2C slave.

Parameters
slave_addrI2C 从设备的八位地址。 The eight-bit address of the I2C slave device.
mem_addr寄存器地址(通常为 8 位或 16 位)。 Register address (typically 8-bit or 16-bit).
write_data要写入的数据,ConstRawData 类型。 Data to be written, of type ConstRawData.
op异步或同步的写入操作对象。 Write operation object (sync or async).
mem_addr_size寄存器地址长度。 Size of register address in bytes.
Returns
返回 ErrorCode,表示是否写入成功。 Returns ErrorCode indicating success or failure.

Implements LibXR::I2C.

Definition at line 208 of file stm32_i2c.cpp.

211{
212 ASSERT(write_data.size_ <= dma_buff_.size_);
213
214 if (i2c_handle_->State != HAL_I2C_STATE_READY)
215 {
216 return ErrorCode::BUSY;
217 }
218
219 read_ = false;
220
221 memcpy(dma_buff_.addr_, write_data.addr_, write_data.size_);
222
223 if (write_data.size_ > dma_enable_min_size_)
224 {
225 write_op_ = op;
226#if __DCACHE_PRESENT
227 SCB_CleanDCache_by_Addr(reinterpret_cast<uint32_t *>(dma_buff_.addr_),
228 write_data.size_);
229#endif
230 HAL_I2C_Mem_Write_DMA(i2c_handle_, slave_addr, mem_addr,
231 mem_addr_size == MemAddrLength::BYTE_8 ? I2C_MEMADD_SIZE_8BIT
232 : I2C_MEMADD_SIZE_16BIT,
233 reinterpret_cast<uint8_t *>(dma_buff_.addr_), write_data.size_);
234 op.MarkAsRunning();
235 if (op.type == WriteOperation::OperationType::BLOCK)
236 {
237 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
238 }
239 return ErrorCode::OK;
240 }
241 else
242 {
243 auto ans =
244 HAL_I2C_Mem_Write(i2c_handle_, slave_addr, mem_addr,
245 mem_addr_size == MemAddrLength::BYTE_8 ? I2C_MEMADD_SIZE_8BIT
246 : I2C_MEMADD_SIZE_16BIT,
247 reinterpret_cast<uint8_t *>(dma_buff_.addr_), write_data.size_,
248 20) == HAL_OK
249 ? ErrorCode::OK
250 : ErrorCode::BUSY;
251
252 op.UpdateStatus(false, std::forward<ErrorCode>(ans));
253 if (op.type == WriteOperation::OperationType::BLOCK)
254 {
255 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
256 }
257 return ans;
258 }
259}
size_t size_
数据大小(字节)。 The size of the data (in bytes).
const void * addr_
数据存储地址(常量)。 The storage address of the data (constant).

◆ Read()

ErrorCode STM32I2C::Read ( uint16_t slave_addr,
RawData read_data,
ReadOperation & op )
overridevirtual

读取 I2C 设备的数据。 Reads data from an I2C device.

该函数从指定的 I2C 从设备地址读取数据,并存储到 read_data 中。 This function reads data from the specified I2C slave address and stores it in read_data.

Parameters
slave_addr目标 I2C 从设备的八位地址。 The eight-bit address of the target I2C slave device.
read_data存储读取数据的 RawData 对象。 A RawData object to store the read data.
op读取操作对象,包含同步或异步操作模式。 Read operation object containing synchronous or asynchronous operation mode.
Returns
返回 ErrorCode,指示操作是否成功。 Returns an ErrorCode indicating whether the operation was successful.

Implements LibXR::I2C.

Definition at line 77 of file stm32_i2c.cpp.

78{
79 if (i2c_handle_->State != HAL_I2C_STATE_READY)
80 {
81 return ErrorCode::BUSY;
82 }
83
84 read_ = true;
85
86 if (read_data.size_ > dma_enable_min_size_)
87 {
88 read_op_ = op;
89 HAL_I2C_Master_Receive_DMA(i2c_handle_, slave_addr,
90 reinterpret_cast<uint8_t *>(dma_buff_.addr_),
91 read_data.size_);
92 read_buff_ = read_data;
93 op.MarkAsRunning();
94 if (op.type == ReadOperation::OperationType::BLOCK)
95 {
96 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
97 }
98 return ErrorCode::OK;
99 }
100 else
101 {
102 auto ans = HAL_I2C_Master_Receive(i2c_handle_, slave_addr,
103 reinterpret_cast<uint8_t *>(read_data.addr_),
104 read_data.size_, 20) == HAL_OK
105 ? ErrorCode::OK
106 : ErrorCode::BUSY;
107 op.UpdateStatus(false, std::forward<ErrorCode>(ans));
108 if (op.type == ReadOperation::OperationType::BLOCK)
109 {
110 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
111 }
112 return ans;
113 }
114}

◆ SetClockSpeed() [1/2]

template<typename T >
std::enable_if<!HasClockSpeed< T >::value >::type LibXR::STM32I2C::SetClockSpeed ( T & ,
const Configuration &  )
inline

Definition at line 76 of file stm32_i2c.hpp.

78 {
79 }

◆ SetClockSpeed() [2/2]

template<typename T >
std::enable_if< HasClockSpeed< T >::value >::type LibXR::STM32I2C::SetClockSpeed ( T & i2c_handle,
const Configuration & config )
inline

Definition at line 82 of file stm32_i2c.hpp.

84 {
85 i2c_handle->Init.ClockSpeed = config.clock_speed;
86 }

◆ SetConfig()

ErrorCode STM32I2C::SetConfig ( Configuration config)
overridevirtual

配置 I2C 设备参数。 Configures the I2C device settings.

该函数用于设置 I2C 设备的参数,例如通信速率等。 This function sets the parameters of the I2C device, such as the communication speed.

Parameters
config包含 I2C 设置信息的 Configuration 结构体。 A Configuration structure containing I2C settings.
Returns
返回 ErrorCode,指示配置是否成功。 Returns an ErrorCode indicating whether the configuration was successful.

Implements LibXR::I2C.

Definition at line 261 of file stm32_i2c.cpp.

262{
263 if (HasClockSpeed<decltype(i2c_handle_)>::value)
264 {
265 SetClockSpeed<decltype(i2c_handle_)>(i2c_handle_, config);
266 }
267 else
268 {
269 return ErrorCode::NOT_SUPPORT;
270 }
271
272 if (HAL_I2C_Init(i2c_handle_) != HAL_OK)
273 {
274 return ErrorCode::INIT_ERR;
275 }
276 return ErrorCode::OK;
277}

◆ Write()

ErrorCode STM32I2C::Write ( uint16_t slave_addr,
ConstRawData write_data,
WriteOperation & op )
overridevirtual

I2C 设备写入数据。 Writes data to an I2C device.

该函数将 write_data 写入指定的 I2C 从设备地址。 This function writes write_data to the specified I2C slave address.

Parameters
slave_addr目标 I2C 从设备的八位地址。 The eight-bit address of the target I2C slave device.
write_data需要写入的数据,ConstRawData 类型。 The data to be written, of type ConstRawData.
op写入操作对象,包含同步或异步操作模式。 Write operation object containing synchronous or asynchronous operation mode.
Returns
返回 ErrorCode,指示操作是否成功。 Returns an ErrorCode indicating whether the operation was successful.

Implements LibXR::I2C.

Definition at line 116 of file stm32_i2c.cpp.

118{
119 if (i2c_handle_->State != HAL_I2C_STATE_READY)
120 {
121 return ErrorCode::BUSY;
122 }
123
124 read_ = false;
125
126 memcpy(dma_buff_.addr_, write_data.addr_, write_data.size_);
127
128 if (write_data.size_ > dma_enable_min_size_)
129 {
130 write_op_ = op;
131#if __DCACHE_PRESENT
132 SCB_CleanDCache_by_Addr(reinterpret_cast<uint32_t *>(dma_buff_.addr_),
133 write_data.size_);
134#endif
135 HAL_I2C_Master_Transmit_DMA(i2c_handle_, slave_addr,
136 reinterpret_cast<uint8_t *>(dma_buff_.addr_),
137 write_data.size_);
138 op.MarkAsRunning();
139 if (op.type == WriteOperation::OperationType::BLOCK)
140 {
141 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
142 }
143 return ErrorCode::OK;
144 }
145 else
146 {
147 auto ans = HAL_I2C_Master_Transmit(i2c_handle_, slave_addr,
148 reinterpret_cast<uint8_t *>(dma_buff_.addr_),
149 write_data.size_, 20) == HAL_OK
150 ? ErrorCode::OK
151 : ErrorCode::BUSY;
152 op.UpdateStatus(false, std::forward<ErrorCode>(ans));
153 if (op.type == WriteOperation::OperationType::BLOCK)
154 {
155 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
156 }
157 return ans;
158 }
159}

Field Documentation

◆ dma_buff_

RawData LibXR::STM32I2C::dma_buff_

Definition at line 94 of file stm32_i2c.hpp.

◆ dma_enable_min_size_

uint32_t LibXR::STM32I2C::dma_enable_min_size_

Definition at line 92 of file stm32_i2c.hpp.

◆ i2c_handle_

I2C_HandleTypeDef* LibXR::STM32I2C::i2c_handle_

Definition at line 91 of file stm32_i2c.hpp.

◆ id_

stm32_i2c_id_t LibXR::STM32I2C::id_

Definition at line 90 of file stm32_i2c.hpp.

◆ map

STM32I2C * STM32I2C::map = {nullptr}
static

Definition at line 102 of file stm32_i2c.hpp.

◆ read_

bool LibXR::STM32I2C::read_ = false

Definition at line 100 of file stm32_i2c.hpp.

◆ read_buff_

RawData LibXR::STM32I2C::read_buff_

Definition at line 98 of file stm32_i2c.hpp.

◆ read_op_

ReadOperation LibXR::STM32I2C::read_op_

Definition at line 96 of file stm32_i2c.hpp.

◆ write_op_

WriteOperation LibXR::STM32I2C::write_op_

Definition at line 97 of file stm32_i2c.hpp.


The documentation for this class was generated from the following files: