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()

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

Definition at line 51 of file stm32_i2c.hpp.

52 : I2C(),
53 id_(STM32_I2C_GetID(hi2c->Instance)),
54 i2c_handle_(hi2c),
55 dma_enable_min_size_(dma_enable_min_size),
56 dma_buff_(dma_buff)
57 {
58 map[id_] = this;
59 }
I2C()
默认构造函数。 Default constructor.
Definition i2c.hpp:39

Member Function Documentation

◆ MemRead()

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

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

146 {
147 ASSERT(read_data.size_ <= dma_buff_.size_);
148
149 if (i2c_handle_->State != HAL_I2C_STATE_READY)
150 {
151 return ErrorCode::BUSY;
152 }
153
154 read_ = true;
155
156 if (read_data.size_ > dma_enable_min_size_)
157 {
158 read_op_ = op;
159 HAL_I2C_Mem_Read_DMA(i2c_handle_, slave_addr, mem_addr,
160 mem_addr_size == MemAddrLength::BYTE_8 ? I2C_MEMADD_SIZE_8BIT
161 : I2C_MEMADD_SIZE_16BIT,
162 reinterpret_cast<uint8_t *>(dma_buff_.addr_), read_data.size_);
163 read_buff_ = read_data;
164 op.MarkAsRunning();
165 if (op.type == ReadOperation::OperationType::BLOCK)
166 {
167 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
168 }
169 return ErrorCode::OK;
170 }
171 else
172 {
173 auto ans =
174 HAL_I2C_Mem_Read(i2c_handle_, slave_addr, mem_addr,
175 mem_addr_size == MemAddrLength::BYTE_8 ? I2C_MEMADD_SIZE_8BIT
176 : I2C_MEMADD_SIZE_16BIT,
177 reinterpret_cast<uint8_t *>(read_data.addr_), read_data.size_,
178 20) == HAL_OK
179 ? ErrorCode::OK
180 : ErrorCode::BUSY;
181
182 op.UpdateStatus(false, std::forward<ErrorCode>(ans));
183 if (op.type == ReadOperation::OperationType::BLOCK)
184 {
185 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
186 }
187 return ans;
188 }
189 }
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.

◆ MemWrite()

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

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

193 {
194 ASSERT(write_data.size_ <= dma_buff_.size_);
195
196 if (i2c_handle_->State != HAL_I2C_STATE_READY)
197 {
198 return ErrorCode::BUSY;
199 }
200
201 read_ = false;
202
203 memcpy(dma_buff_.addr_, write_data.addr_, write_data.size_);
204
205 if (write_data.size_ > dma_enable_min_size_)
206 {
207 write_op_ = op;
208#if __DCACHE_PRESENT
209 SCB_CleanDCache_by_Addr(reinterpret_cast<uint32_t *>(dma_buff_.addr_), write_data.size_);
210#endif
211 HAL_I2C_Mem_Write_DMA(
212 i2c_handle_, slave_addr, mem_addr,
213 mem_addr_size == MemAddrLength::BYTE_8 ? I2C_MEMADD_SIZE_8BIT
214 : I2C_MEMADD_SIZE_16BIT,
215 reinterpret_cast<uint8_t *>(dma_buff_.addr_), write_data.size_);
216 op.MarkAsRunning();
217 if (op.type == WriteOperation::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 = HAL_I2C_Mem_Write(i2c_handle_, slave_addr, mem_addr,
226 mem_addr_size == MemAddrLength::BYTE_8
227 ? I2C_MEMADD_SIZE_8BIT
228 : I2C_MEMADD_SIZE_16BIT,
229 reinterpret_cast<uint8_t *>(dma_buff_.addr_),
230 write_data.size_, 20) == HAL_OK
231 ? ErrorCode::OK
232 : ErrorCode::BUSY;
233
234 op.UpdateStatus(false, std::forward<ErrorCode>(ans));
235 if (op.type == WriteOperation::OperationType::BLOCK)
236 {
237 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
238 }
239 return ans;
240 }
241 }

◆ Read()

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

读取 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 61 of file stm32_i2c.hpp.

62 {
63 if (i2c_handle_->State != HAL_I2C_STATE_READY)
64 {
65 return ErrorCode::BUSY;
66 }
67
68 read_ = true;
69
70 if (read_data.size_ > dma_enable_min_size_)
71 {
72 read_op_ = op;
73 HAL_I2C_Master_Receive_DMA(i2c_handle_, slave_addr,
74 reinterpret_cast<uint8_t *>(dma_buff_.addr_),
75 read_data.size_);
76 read_buff_ = read_data;
77 op.MarkAsRunning();
78 if (op.type == ReadOperation::OperationType::BLOCK)
79 {
80 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
81 }
82 return ErrorCode::OK;
83 }
84 else
85 {
86 auto ans = HAL_I2C_Master_Receive(i2c_handle_, slave_addr,
87 reinterpret_cast<uint8_t *>(read_data.addr_),
88 read_data.size_, 20) == HAL_OK
89 ? ErrorCode::OK
90 : ErrorCode::BUSY;
91 op.UpdateStatus(false, std::forward<ErrorCode>(ans));
92 if (op.type == ReadOperation::OperationType::BLOCK)
93 {
94 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
95 }
96 return ans;
97 }
98 }

◆ SetClockSpeed() [1/2]

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

Definition at line 255 of file stm32_i2c.hpp.

257 {
258 }

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

263 {
264 i2c_handle->Init.ClockSpeed = config.clock_speed;
265 }

◆ SetConfig()

ErrorCode LibXR::STM32I2C::SetConfig ( Configuration config)
inlineoverridevirtual

配置 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 267 of file stm32_i2c.hpp.

268 {
269 if (HasClockSpeed<decltype(i2c_handle_)>::value)
270 {
271 SetClockSpeed<decltype(i2c_handle_)>(i2c_handle_, config);
272 }
273 else
274 {
275 return ErrorCode::NOT_SUPPORT;
276 }
277
278 if (HAL_I2C_Init(i2c_handle_) != HAL_OK)
279 {
280 return ErrorCode::INIT_ERR;
281 }
282 return ErrorCode::OK;
283 }

◆ Write()

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

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

102 {
103 if (i2c_handle_->State != HAL_I2C_STATE_READY)
104 {
105 return ErrorCode::BUSY;
106 }
107
108 read_ = false;
109
110 memcpy(dma_buff_.addr_, write_data.addr_, write_data.size_);
111
112 if (write_data.size_ > dma_enable_min_size_)
113 {
114 write_op_ = op;
115#if __DCACHE_PRESENT
116 SCB_CleanDCache_by_Addr(reinterpret_cast<uint32_t *>(dma_buff_.addr_), write_data.size_);
117#endif
118 HAL_I2C_Master_Transmit_DMA(i2c_handle_, slave_addr,
119 reinterpret_cast<uint8_t *>(dma_buff_.addr_),
120 write_data.size_);
121 op.MarkAsRunning();
122 if (op.type == WriteOperation::OperationType::BLOCK)
123 {
124 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
125 }
126 return ErrorCode::OK;
127 }
128 else
129 {
130 auto ans = HAL_I2C_Master_Transmit(i2c_handle_, slave_addr,
131 reinterpret_cast<uint8_t *>(dma_buff_.addr_),
132 write_data.size_, 20) == HAL_OK
133 ? ErrorCode::OK
134 : ErrorCode::BUSY;
135 op.UpdateStatus(false, std::forward<ErrorCode>(ans));
136 if (op.type == WriteOperation::OperationType::BLOCK)
137 {
138 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
139 }
140 return ans;
141 }
142 }

Field Documentation

◆ dma_buff_

RawData LibXR::STM32I2C::dma_buff_

Definition at line 289 of file stm32_i2c.hpp.

◆ dma_enable_min_size_

uint32_t LibXR::STM32I2C::dma_enable_min_size_

Definition at line 287 of file stm32_i2c.hpp.

◆ i2c_handle_

I2C_HandleTypeDef* LibXR::STM32I2C::i2c_handle_

Definition at line 286 of file stm32_i2c.hpp.

◆ id_

stm32_i2c_id_t LibXR::STM32I2C::id_

Definition at line 285 of file stm32_i2c.hpp.

◆ map

STM32I2C * STM32I2C::map = {nullptr}
static

Definition at line 297 of file stm32_i2c.hpp.

◆ read_

bool LibXR::STM32I2C::read_ = false

Definition at line 295 of file stm32_i2c.hpp.

◆ read_buff_

RawData LibXR::STM32I2C::read_buff_

Definition at line 293 of file stm32_i2c.hpp.

◆ read_op_

ReadOperation LibXR::STM32I2C::read_op_

Definition at line 291 of file stm32_i2c.hpp.

◆ write_op_

WriteOperation LibXR::STM32I2C::write_op_

Definition at line 292 of file stm32_i2c.hpp.


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