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
 

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

94 : I2C(),
95 id_(STM32_I2C_GetID(hi2c->Instance)),
96 i2c_handle_(hi2c),
97 dma_enable_min_size_(dma_enable_min_size),
98 dma_buff_(dma_buff)
99{
100 map[id_] = this;
101}
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 194 of file stm32_i2c.cpp.

196{
197 ASSERT(read_data.size_ <= dma_buff_.size_);
198
199 if (i2c_handle_->State != HAL_I2C_STATE_READY)
200 {
201 return ErrorCode::BUSY;
202 }
203
204 read_ = true;
205
206 const uint16_t dev_addr = EncodeHalDevAddress(i2c_handle_, slave_addr);
207
208 if (read_data.size_ > dma_enable_min_size_)
209 {
210 read_op_ = op;
211 HAL_I2C_Mem_Read_DMA(i2c_handle_, dev_addr, mem_addr,
212 mem_addr_size == MemAddrLength::BYTE_8 ? I2C_MEMADD_SIZE_8BIT
213 : I2C_MEMADD_SIZE_16BIT,
214 reinterpret_cast<uint8_t*>(dma_buff_.addr_), read_data.size_);
215 read_buff_ = read_data;
216 op.MarkAsRunning();
217 if (op.type == ReadOperation::OperationType::BLOCK)
218 {
219 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
220 }
221 return ErrorCode::OK;
222 }
223 else
224 {
225 auto ans =
226 HAL_I2C_Mem_Read(i2c_handle_, dev_addr, mem_addr,
227 mem_addr_size == MemAddrLength::BYTE_8 ? I2C_MEMADD_SIZE_8BIT
228 : I2C_MEMADD_SIZE_16BIT,
229 reinterpret_cast<uint8_t*>(read_data.addr_), read_data.size_,
230 20) == HAL_OK
231 ? ErrorCode::OK
232 : ErrorCode::BUSY;
233
234 op.UpdateStatus(in_isr, ans);
235 if (op.type == ReadOperation::OperationType::BLOCK)
236 {
237 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
238 }
239 return ans;
240 }
241}
union LibXR::Operation::@5 data
void UpdateStatus(bool in_isr, Status &&status)
Updates operation status based on type.
Definition libxr_rw.hpp:172
void MarkAsRunning()
标记操作为运行状态。 Marks the operation as running.
Definition libxr_rw.hpp:205
OperationType type
Definition libxr_rw.hpp:229
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,
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 243 of file stm32_i2c.cpp.

246{
247 ASSERT(write_data.size_ <= dma_buff_.size_);
248
249 if (i2c_handle_->State != HAL_I2C_STATE_READY)
250 {
251 return ErrorCode::BUSY;
252 }
253
254 read_ = false;
255
256 const uint16_t dev_addr = EncodeHalDevAddress(i2c_handle_, slave_addr);
257
258 Memory::FastCopy(dma_buff_.addr_, write_data.addr_, write_data.size_);
259
260 if (write_data.size_ > dma_enable_min_size_)
261 {
262 write_op_ = op;
263#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
264 SCB_CleanDCache_by_Addr(reinterpret_cast<uint32_t*>(dma_buff_.addr_),
265 write_data.size_);
266#endif
267 HAL_I2C_Mem_Write_DMA(i2c_handle_, dev_addr, mem_addr,
268 mem_addr_size == MemAddrLength::BYTE_8 ? I2C_MEMADD_SIZE_8BIT
269 : I2C_MEMADD_SIZE_16BIT,
270 reinterpret_cast<uint8_t*>(dma_buff_.addr_), write_data.size_);
271 op.MarkAsRunning();
272 if (op.type == WriteOperation::OperationType::BLOCK)
273 {
274 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
275 }
276 return ErrorCode::OK;
277 }
278 else
279 {
280 auto ans =
281 HAL_I2C_Mem_Write(i2c_handle_, dev_addr, mem_addr,
282 mem_addr_size == MemAddrLength::BYTE_8 ? I2C_MEMADD_SIZE_8BIT
283 : I2C_MEMADD_SIZE_16BIT,
284 reinterpret_cast<uint8_t*>(dma_buff_.addr_), write_data.size_,
285 20) == HAL_OK
286 ? ErrorCode::OK
287 : ErrorCode::BUSY;
288
289 op.UpdateStatus(in_isr, ans);
290 if (op.type == WriteOperation::OperationType::BLOCK)
291 {
292 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
293 }
294 return ans;
295 }
296}
size_t size_
数据大小(字节)。 The size of the data (in bytes).
const void * addr_
数据存储地址(常量)。 The storage address of the data (constant).
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
Definition libxr_mem.cpp:3

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

105{
106 if (i2c_handle_->State != HAL_I2C_STATE_READY)
107 {
108 return ErrorCode::BUSY;
109 }
110
111 read_ = true;
112
113 const uint16_t dev_addr = EncodeHalDevAddress(i2c_handle_, slave_addr);
114
115 if (read_data.size_ > dma_enable_min_size_)
116 {
117 read_op_ = op;
118 HAL_I2C_Master_Receive_DMA(i2c_handle_, dev_addr,
119 reinterpret_cast<uint8_t*>(dma_buff_.addr_),
120 read_data.size_);
121 read_buff_ = read_data;
122 op.MarkAsRunning();
123 if (op.type == ReadOperation::OperationType::BLOCK)
124 {
125 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
126 }
127 return ErrorCode::OK;
128 }
129 else
130 {
131 auto ans = HAL_I2C_Master_Receive(i2c_handle_, dev_addr,
132 reinterpret_cast<uint8_t*>(read_data.addr_),
133 read_data.size_, 20) == HAL_OK
134 ? ErrorCode::OK
135 : ErrorCode::BUSY;
136 // BLOCK 模式下沿用统一状态更新路径。
137 // Reuse the same status update path for BLOCK mode.
138 op.UpdateStatus(in_isr, ans);
139 if (op.type == ReadOperation::OperationType::BLOCK)
140 {
141 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
142 }
143 return ans;
144 }
145}

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

299{
300 if (HasClockSpeed<decltype(i2c_handle_)>::value)
301 {
302 SetClockSpeed<decltype(i2c_handle_)>(i2c_handle_, config);
303 }
304 else
305 {
306 return ErrorCode::NOT_SUPPORT;
307 }
308
309 if (HAL_I2C_Init(i2c_handle_) != HAL_OK)
310 {
311 return ErrorCode::INIT_ERR;
312 }
313 return ErrorCode::OK;
314}

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

149{
150 if (i2c_handle_->State != HAL_I2C_STATE_READY)
151 {
152 return ErrorCode::BUSY;
153 }
154
155 read_ = false;
156
157 const uint16_t dev_addr = EncodeHalDevAddress(i2c_handle_, slave_addr);
158
159 Memory::FastCopy(dma_buff_.addr_, write_data.addr_, write_data.size_);
160
161 if (write_data.size_ > dma_enable_min_size_)
162 {
163 write_op_ = op;
164#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
165 SCB_CleanDCache_by_Addr(reinterpret_cast<uint32_t*>(dma_buff_.addr_),
166 write_data.size_);
167#endif
168 HAL_I2C_Master_Transmit_DMA(i2c_handle_, dev_addr,
169 reinterpret_cast<uint8_t*>(dma_buff_.addr_),
170 write_data.size_);
171 op.MarkAsRunning();
172 if (op.type == WriteOperation::OperationType::BLOCK)
173 {
174 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
175 }
176 return ErrorCode::OK;
177 }
178 else
179 {
180 auto ans = HAL_I2C_Master_Transmit(i2c_handle_, dev_addr,
181 reinterpret_cast<uint8_t*>(dma_buff_.addr_),
182 write_data.size_, 20) == HAL_OK
183 ? ErrorCode::OK
184 : ErrorCode::BUSY;
185 op.UpdateStatus(in_isr, ans);
186 if (op.type == WriteOperation::OperationType::BLOCK)
187 {
188 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
189 }
190 return ans;
191 }
192}

Field Documentation

◆ 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 110 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.

◆ 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: