libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::STM32I2C Class Reference

STM32 I2C 驱动实现 / STM32 I2C driver implementation. More...

#include <stm32_i2c.hpp>

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)
 构造 I2C 对象 / Construct I2C object
 
ErrorCode Read (uint16_t slave_addr, RawData read_data, ReadOperation &op, bool in_isr) override
 读取 I2C 设备的数据。 Reads data from an I2C device.
 
ErrorCode Write (uint16_t slave_addr, ConstRawData write_data, WriteOperation &op, bool in_isr) 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, bool in_isr) 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, bool in_isr) 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
 
bool recovering_ = false
 
AsyncBlockWait block_wait_ {}
 

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

STM32 I2C 驱动实现 / STM32 I2C driver implementation.

Definition at line 51 of file stm32_i2c.hpp.

Constructor & Destructor Documentation

◆ STM32I2C()

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

构造 I2C 对象 / Construct I2C object

Definition at line 156 of file stm32_i2c.cpp.

158 : I2C(),
159 id_(STM32_I2C_GetID(hi2c->Instance)),
160 i2c_handle_(hi2c),
161 dma_enable_min_size_(dma_enable_min_size),
162 dma_buff_(dma_buff)
163{
164 map[id_] = this;
165}
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,
bool in_isr )
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_addr目标 I2C 从设备地址,不带 R/W 位。 Target I2C slave address, no R/W bit included.
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.
in_isr是否在中断中进行操作。Whether the operation is performed in an ISR.
Returns
返回 ErrorCode,表示是否读取成功。 Returns ErrorCode indicating success or failure.

Implements LibXR::I2C.

Definition at line 281 of file stm32_i2c.cpp.

283{
284 ASSERT(read_data.size_ <= dma_buff_.size_);
285
286 if (i2c_handle_->State != HAL_I2C_STATE_READY)
287 {
288 return ErrorCode::BUSY;
289 }
290
291 read_ = true;
292
293 const uint16_t dev_addr = EncodeHalDevAddress(i2c_handle_, slave_addr);
294
295 if (read_data.size_ > dma_enable_min_size_)
296 {
297 read_op_ = op;
298 read_buff_ = read_data;
299 if (op.type == ReadOperation::OperationType::BLOCK)
300 {
301 // Arm the BLOCK waiter before HAL exposes completion to IRQ context.
302 block_wait_.Start(*op.data.sem_info.sem);
303 }
304 const HAL_StatusTypeDef st = HAL_I2C_Mem_Read_DMA(
305 i2c_handle_, dev_addr, mem_addr,
306 mem_addr_size == MemAddrLength::BYTE_8 ? I2C_MEMADD_SIZE_8BIT
307 : I2C_MEMADD_SIZE_16BIT,
308 reinterpret_cast<uint8_t*>(dma_buff_.addr_), read_data.size_);
309 if (st != HAL_OK)
310 {
311 if (op.type == ReadOperation::OperationType::BLOCK)
312 {
313 block_wait_.Cancel();
314 return MapHalStartFailure(i2c_handle_, st);
315 }
316 return ErrorCode::BUSY;
317 }
318 op.MarkAsRunning();
319 if (op.type == ReadOperation::OperationType::BLOCK)
320 {
321 return WaitBlockResultAndRecoverTimeout(this, op.data.sem_info.timeout);
322 }
323 return ErrorCode::OK;
324 }
325 else
326 {
327 auto ans =
328 HAL_I2C_Mem_Read(i2c_handle_, dev_addr, mem_addr,
329 mem_addr_size == MemAddrLength::BYTE_8 ? I2C_MEMADD_SIZE_8BIT
330 : I2C_MEMADD_SIZE_16BIT,
331 reinterpret_cast<uint8_t*>(read_data.addr_), read_data.size_,
332 20) == HAL_OK
335
336 if (op.type != ReadOperation::OperationType::BLOCK)
337 {
338 op.UpdateStatus(in_isr, ans);
339 }
340 return ans;
341 }
342}
union LibXR::Operation::@5 data
void UpdateStatus(bool in_isr, Status &&status)
Updates operation status based on type.
void MarkAsRunning()
标记操作为运行状态。 Marks the operation as running.
OperationType type
size_t size_
数据字节数 / Data size in bytes
void * addr_
数据起始地址 / Data start address
@ BUSY
忙碌 | Busy
@ OK
操作成功 | Operation successful

◆ MemWrite()

ErrorCode STM32I2C::MemWrite ( uint16_t slave_addr,
uint16_t mem_addr,
ConstRawData write_data,
WriteOperation & op,
MemAddrLength mem_addr_size,
bool in_isr )
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_addr目标 I2C 从设备地址,不带 R/W 位。 Target I2C slave address, no R/W bit included.
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.
in_isr是否在中断中进行操作。Whether the operation is performed in an ISR.
Returns
返回 ErrorCode,表示是否写入成功。 Returns ErrorCode indicating success or failure.

Implements LibXR::I2C.

Definition at line 344 of file stm32_i2c.cpp.

347{
348 ASSERT(write_data.size_ <= dma_buff_.size_);
349
350 if (i2c_handle_->State != HAL_I2C_STATE_READY)
351 {
352 return ErrorCode::BUSY;
353 }
354
355 read_ = false;
356
357 const uint16_t dev_addr = EncodeHalDevAddress(i2c_handle_, slave_addr);
358
359 Memory::FastCopy(dma_buff_.addr_, write_data.addr_, write_data.size_);
360
361 if (write_data.size_ > dma_enable_min_size_)
362 {
363 write_op_ = op;
364 if (op.type == WriteOperation::OperationType::BLOCK)
365 {
366 // Arm the BLOCK waiter before HAL exposes completion to IRQ context.
367 block_wait_.Start(*op.data.sem_info.sem);
368 }
369 STM32_CleanDCacheByAddr(dma_buff_.addr_, write_data.size_);
370 const HAL_StatusTypeDef st = HAL_I2C_Mem_Write_DMA(
371 i2c_handle_, dev_addr, mem_addr,
372 mem_addr_size == MemAddrLength::BYTE_8 ? I2C_MEMADD_SIZE_8BIT
373 : I2C_MEMADD_SIZE_16BIT,
374 reinterpret_cast<uint8_t*>(dma_buff_.addr_), write_data.size_);
375 if (st != HAL_OK)
376 {
377 if (op.type == WriteOperation::OperationType::BLOCK)
378 {
379 block_wait_.Cancel();
380 return MapHalStartFailure(i2c_handle_, st);
381 }
382 return ErrorCode::BUSY;
383 }
384 op.MarkAsRunning();
385 if (op.type == WriteOperation::OperationType::BLOCK)
386 {
387 return WaitBlockResultAndRecoverTimeout(this, op.data.sem_info.timeout);
388 }
389 return ErrorCode::OK;
390 }
391 else
392 {
393 auto ans =
394 HAL_I2C_Mem_Write(i2c_handle_, dev_addr, mem_addr,
395 mem_addr_size == MemAddrLength::BYTE_8 ? I2C_MEMADD_SIZE_8BIT
396 : I2C_MEMADD_SIZE_16BIT,
397 reinterpret_cast<uint8_t*>(dma_buff_.addr_), write_data.size_,
398 20) == HAL_OK
401
402 if (op.type != WriteOperation::OperationType::BLOCK)
403 {
404 op.UpdateStatus(in_isr, ans);
405 }
406 return ans;
407 }
408}
size_t size_
数据字节数 / Data size in bytes
const void * addr_
数据起始地址 / Data start address
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
Definition libxr_mem.cpp:5
void STM32_CleanDCacheByAddr(const void *addr, size_t size)
Cleans D-Cache lines covering the specified memory range.

◆ Read()

ErrorCode STM32I2C::Read ( uint16_t slave_addr,
RawData read_data,
ReadOperation & op,
bool in_isr )
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 从设备地址,不带 R/W 位。 Target I2C slave address, no R/W bit included.
read_data存储读取数据的 RawData 对象。 A RawData object to store the read data.
op读取操作对象,包含同步或异步操作模式。 Read operation object containing synchronous or asynchronous operation mode.
in_isr是否在中断中进行操作。Whether the operation is performed in an ISR.
Returns
返回 ErrorCode,指示操作是否成功。 Returns an ErrorCode indicating whether the operation was successful.

Implements LibXR::I2C.

Definition at line 167 of file stm32_i2c.cpp.

169{
170 if (i2c_handle_->State != HAL_I2C_STATE_READY)
171 {
172 return ErrorCode::BUSY;
173 }
174
175 read_ = true;
176
177 const uint16_t dev_addr = EncodeHalDevAddress(i2c_handle_, slave_addr);
178
179 if (read_data.size_ > dma_enable_min_size_)
180 {
181 read_op_ = op;
182 read_buff_ = read_data;
183 if (op.type == ReadOperation::OperationType::BLOCK)
184 {
185 // Arm the BLOCK waiter before HAL exposes completion to IRQ context.
186 block_wait_.Start(*op.data.sem_info.sem);
187 }
188 const HAL_StatusTypeDef st = HAL_I2C_Master_Receive_DMA(
189 i2c_handle_, dev_addr, reinterpret_cast<uint8_t*>(dma_buff_.addr_),
190 read_data.size_);
191 if (st != HAL_OK)
192 {
193 if (op.type == ReadOperation::OperationType::BLOCK)
194 {
195 block_wait_.Cancel();
196 return MapHalStartFailure(i2c_handle_, st);
197 }
198 return ErrorCode::BUSY;
199 }
200 op.MarkAsRunning();
201 if (op.type == ReadOperation::OperationType::BLOCK)
202 {
203 return WaitBlockResultAndRecoverTimeout(this, op.data.sem_info.timeout);
204 }
205 return ErrorCode::OK;
206 }
207 else
208 {
209 auto ans = HAL_I2C_Master_Receive(i2c_handle_, dev_addr,
210 reinterpret_cast<uint8_t*>(read_data.addr_),
211 read_data.size_, 20) == HAL_OK
214 // BLOCK 模式下沿用统一状态更新路径。
215 // Reuse the same status update path for BLOCK mode.
216 if (op.type != ReadOperation::OperationType::BLOCK)
217 {
218 op.UpdateStatus(in_isr, ans);
219 }
220 return ans;
221 }
222}

◆ SetClockSpeed() [1/2]

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

Definition at line 84 of file stm32_i2c.hpp.

86 {
87 }

◆ 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 90 of file stm32_i2c.hpp.

92 {
93 i2c_handle->Init.ClockSpeed = config.clock_speed;
94 }

◆ 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 410 of file stm32_i2c.cpp.

411{
412 if (HasClockSpeed<decltype(i2c_handle_)>::value)
413 {
414 SetClockSpeed<decltype(i2c_handle_)>(i2c_handle_, config);
415 }
416 else
417 {
419 }
420
421 if (HAL_I2C_Init(i2c_handle_) != HAL_OK)
422 {
423 return ErrorCode::INIT_ERR;
424 }
425 return ErrorCode::OK;
426}
@ INIT_ERR
初始化错误 | Initialization error
@ NOT_SUPPORT
不支持 | Not supported

◆ Write()

ErrorCode STM32I2C::Write ( uint16_t slave_addr,
ConstRawData write_data,
WriteOperation & op,
bool in_isr )
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 从设备地址,不带 R/W 位。 Target I2C slave address, no R/W bit included.
write_data需要写入的数据,ConstRawData 类型。 The data to be written, of type ConstRawData.
op写入操作对象,包含同步或异步操作模式。 Write operation object containing synchronous or asynchronous operation mode.
in_isr是否在中断中进行操作。Whether the operation is performed in an ISR.
Returns
返回 ErrorCode,指示操作是否成功。 Returns an ErrorCode indicating whether the operation was successful.

Implements LibXR::I2C.

Definition at line 224 of file stm32_i2c.cpp.

226{
227 if (i2c_handle_->State != HAL_I2C_STATE_READY)
228 {
229 return ErrorCode::BUSY;
230 }
231
232 read_ = false;
233
234 const uint16_t dev_addr = EncodeHalDevAddress(i2c_handle_, slave_addr);
235
236 Memory::FastCopy(dma_buff_.addr_, write_data.addr_, write_data.size_);
237
238 if (write_data.size_ > dma_enable_min_size_)
239 {
240 write_op_ = op;
241 if (op.type == WriteOperation::OperationType::BLOCK)
242 {
243 // Arm the BLOCK waiter before HAL exposes completion to IRQ context.
244 block_wait_.Start(*op.data.sem_info.sem);
245 }
246 STM32_CleanDCacheByAddr(dma_buff_.addr_, write_data.size_);
247 const HAL_StatusTypeDef st = HAL_I2C_Master_Transmit_DMA(
248 i2c_handle_, dev_addr, reinterpret_cast<uint8_t*>(dma_buff_.addr_),
249 write_data.size_);
250 if (st != HAL_OK)
251 {
252 if (op.type == WriteOperation::OperationType::BLOCK)
253 {
254 block_wait_.Cancel();
255 return MapHalStartFailure(i2c_handle_, st);
256 }
257 return ErrorCode::BUSY;
258 }
259 op.MarkAsRunning();
260 if (op.type == WriteOperation::OperationType::BLOCK)
261 {
262 return WaitBlockResultAndRecoverTimeout(this, op.data.sem_info.timeout);
263 }
264 return ErrorCode::OK;
265 }
266 else
267 {
268 auto ans = HAL_I2C_Master_Transmit(i2c_handle_, dev_addr,
269 reinterpret_cast<uint8_t*>(dma_buff_.addr_),
270 write_data.size_, 20) == HAL_OK
273 if (op.type != WriteOperation::OperationType::BLOCK)
274 {
275 op.UpdateStatus(in_isr, ans);
276 }
277 return ans;
278 }
279}

Field Documentation

◆ block_wait_

AsyncBlockWait LibXR::STM32I2C::block_wait_ {}

Definition at line 110 of file stm32_i2c.hpp.

110{};

◆ dma_buff_

RawData LibXR::STM32I2C::dma_buff_

Definition at line 102 of file stm32_i2c.hpp.

◆ dma_enable_min_size_

uint32_t LibXR::STM32I2C::dma_enable_min_size_

Definition at line 100 of file stm32_i2c.hpp.

◆ i2c_handle_

I2C_HandleTypeDef* LibXR::STM32I2C::i2c_handle_

Definition at line 99 of file stm32_i2c.hpp.

◆ id_

stm32_i2c_id_t LibXR::STM32I2C::id_

Definition at line 98 of file stm32_i2c.hpp.

◆ map

STM32I2C * STM32I2C::map = {nullptr}
static

Definition at line 112 of file stm32_i2c.hpp.

◆ read_

bool LibXR::STM32I2C::read_ = false

Definition at line 108 of file stm32_i2c.hpp.

◆ read_buff_

RawData LibXR::STM32I2C::read_buff_

Definition at line 106 of file stm32_i2c.hpp.

◆ read_op_

ReadOperation LibXR::STM32I2C::read_op_

Definition at line 104 of file stm32_i2c.hpp.

◆ recovering_

bool LibXR::STM32I2C::recovering_ = false

Definition at line 109 of file stm32_i2c.hpp.

◆ write_op_

WriteOperation LibXR::STM32I2C::write_op_

Definition at line 105 of file stm32_i2c.hpp.


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