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

Public Member Functions

 STM32SPI (SPI_HandleTypeDef *spi_handle, RawData dma_buff_rx, RawData dma_buff_tx, uint32_t dma_enable_min_size=3)
 
ErrorCode ReadAndWrite (RawData read_data, ConstRawData write_data, OperationRW &op) override
 进行 SPI 读写操作。Performs SPI read and write operations.
 
ErrorCode SetConfig (SPI::Configuration config) override
 设置 SPI 配置参数。Sets SPI configuration parameters.
 
ErrorCode MemRead (uint16_t reg, RawData read_data, OperationRW &op) override
 SPI 设备的寄存器读取数据。 Reads data from a specific register of the SPI device.
 
ErrorCode MemWrite (uint16_t reg, ConstRawData write_data, OperationRW &op) override
 SPI 设备的寄存器写入数据。 Writes data to a specific register of the SPI device.
 
- Public Member Functions inherited from LibXR::SPI
 SPI ()
 默认构造函数。Default constructor.
 
virtual ErrorCode Read (RawData read_data, OperationRW &op)
 进行 SPI 读取操作。Performs SPI read operation.
 
virtual ErrorCode Write (ConstRawData write_data, OperationRW &op)
 进行 SPI 写入操作。Performs SPI write operation.
 

Data Fields

RawData dma_buff_rx_
 
RawData dma_buff_tx_
 
SPI_HandleTypeDef * spi_handle_
 
stm32_spi_id_t id_ = STM32_SPI_ID_ERROR
 
uint32_t dma_enable_min_size_ = 3
 
OperationRW rw_op_
 
RawData read_buff_
 
bool mem_read_ = false
 

Static Public Attributes

static STM32SPImap [STM32_SPI_NUMBER] = {nullptr}
 

Additional Inherited Members

- Public Types inherited from LibXR::SPI
enum class  ClockPolarity : uint8_t { LOW = 0 , HIGH = 1 }
 定义 SPI 时钟极性。Defines the SPI clock polarity. More...
 
enum class  ClockPhase : uint8_t { EDGE_1 = 0 , EDGE_2 = 1 }
 定义 SPI 时钟相位。Defines the SPI clock phase. More...
 
using OperationRW = WriteOperation
 定义读写操作类型的别名。Defines an alias for the read/write operation type.
 

Detailed Description

Definition at line 51 of file stm32_spi.hpp.

Constructor & Destructor Documentation

◆ STM32SPI()

LibXR::STM32SPI::STM32SPI ( SPI_HandleTypeDef * spi_handle,
RawData dma_buff_rx,
RawData dma_buff_tx,
uint32_t dma_enable_min_size = 3 )
inline

Definition at line 54 of file stm32_spi.hpp.

56 : SPI(),
57 dma_buff_rx_(dma_buff_rx),
58 dma_buff_tx_(dma_buff_tx),
59 spi_handle_(spi_handle),
60 id_(STM32_SPI_GetID(spi_handle_->Instance)),
61 dma_enable_min_size_(dma_enable_min_size)
62 {
63 ASSERT(id_ != STM32_SPI_ID_ERROR);
64
65 map[id_] = this;
66 }
SPI()
默认构造函数。Default constructor.
Definition spi.hpp:66

Member Function Documentation

◆ MemRead()

ErrorCode LibXR::STM32SPI::MemRead ( uint16_t reg,
RawData read_data,
OperationRW & op )
inlineoverridevirtual

SPI 设备的寄存器读取数据。 Reads data from a specific register of the SPI device.

Parameters
reg寄存器地址。Register address.
read_data读取的数据缓冲区。Buffer to store read data.
op操作类型(同步/异步)。Operation mode (sync/async).
Returns
操作结果的错误码。Error code indicating success or failure.

Implements LibXR::SPI.

Definition at line 217 of file stm32_spi.hpp.

218 {
219 uint32_t need_read = read_data.size_;
220
221 if (spi_handle_->State != HAL_SPI_STATE_READY)
222 {
223 return ErrorCode::BUSY;
224 }
225
226 ASSERT(dma_buff_rx_.size_ >= need_read + 1);
227 ASSERT(dma_buff_tx_.size_ >= need_read + 1);
228
229 uint8_t *dma_buffer_rx = reinterpret_cast<uint8_t *>(dma_buff_rx_.addr_);
230 uint8_t *dma_buffer_tx = reinterpret_cast<uint8_t *>(dma_buff_tx_.addr_);
231
232 if (need_read + 1 > dma_enable_min_size_)
233 {
234 mem_read_ = true;
235 memset(dma_buff_tx_.addr_, 0, need_read + 1);
236 dma_buffer_tx[0] = reg | 0x80;
237 rw_op_ = op;
238 read_buff_ = read_data;
239
240 HAL_SPI_TransmitReceive_DMA(spi_handle_, static_cast<uint8_t *>(dma_buff_tx_.addr_),
241 static_cast<uint8_t *>(dma_buff_rx_.addr_),
242 need_read + 1);
243
244 op.MarkAsRunning();
245 if (op.type == OperationRW::OperationType::BLOCK)
246 {
247 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
248 }
249 return ErrorCode::OK;
250 }
251 else
252 {
253 mem_read_ = false;
254 memset(dma_buff_tx_.addr_, 0, need_read + 1);
255 dma_buffer_tx[0] = reg | 0x80;
256 ErrorCode ans =
257 HAL_SPI_TransmitReceive(spi_handle_, static_cast<uint8_t *>(dma_buff_tx_.addr_),
258 static_cast<uint8_t *>(dma_buff_rx_.addr_),
259 need_read + 1, 20) == HAL_OK
260 ? ErrorCode::OK
261 : ErrorCode::BUSY;
262
263 memcpy(read_data.addr_, dma_buffer_rx + 1, read_data.size_);
264
265 op.UpdateStatus(false, std::forward<ErrorCode>(ans));
266
267 if (op.type == OperationRW::OperationType::BLOCK)
268 {
269 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
270 }
271
272 return ans;
273 }
274 }
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.

◆ MemWrite()

ErrorCode LibXR::STM32SPI::MemWrite ( uint16_t reg,
ConstRawData write_data,
OperationRW & op )
inlineoverridevirtual

SPI 设备的寄存器写入数据。 Writes data to a specific register of the SPI device.

Parameters
reg寄存器地址。Register address.
write_data写入的数据缓冲区。Buffer containing data to write.
op操作类型(同步/异步)。Operation mode (sync/async).
Returns
操作结果的错误码。Error code indicating success or failure.

Implements LibXR::SPI.

Definition at line 276 of file stm32_spi.hpp.

277 {
278 uint32_t need_write = write_data.size_;
279
280 if (spi_handle_->State != HAL_SPI_STATE_READY)
281 {
282 return ErrorCode::BUSY;
283 }
284
285 mem_read_ = false;
286
287 ASSERT(dma_buff_tx_.size_ >= need_write + 1);
288
289 uint8_t *dma_buffer_tx = reinterpret_cast<uint8_t *>(dma_buff_tx_.addr_);
290
291 if (need_write + 1 > dma_enable_min_size_)
292 {
293 memcpy(dma_buffer_tx + 1, write_data.addr_, need_write);
294 *dma_buffer_tx = reg & 0x7f;
295
296 rw_op_ = op;
297 read_buff_ = {nullptr, 0};
298
299 HAL_SPI_Transmit_DMA(spi_handle_, dma_buffer_tx, need_write + 1);
300
301 op.MarkAsRunning();
302 if (op.type == OperationRW::OperationType::BLOCK)
303 {
304 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
305 }
306 return ErrorCode::OK;
307 }
308 else
309 {
310 memcpy(dma_buffer_tx + 1, write_data.addr_, need_write);
311 *dma_buffer_tx = reg & 0x7f;
312 ErrorCode ans =
313 HAL_SPI_Transmit(spi_handle_, dma_buffer_tx, need_write + 1, 20) == HAL_OK
314 ? ErrorCode::OK
315 : ErrorCode::BUSY;
316
317 op.UpdateStatus(false, std::forward<ErrorCode>(ans));
318
319 if (op.type == OperationRW::OperationType::BLOCK)
320 {
321 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
322 }
323
324 return ans;
325 }
326 }

◆ ReadAndWrite()

ErrorCode LibXR::STM32SPI::ReadAndWrite ( RawData read_data,
ConstRawData write_data,
OperationRW & op )
inlineoverridevirtual

进行 SPI 读写操作。Performs SPI read and write operations.

Parameters
read_data存储读取数据的缓冲区。Buffer to store the read data.
write_data需要写入的数据缓冲区。Buffer containing the data to be written.
op读写操作类型。Type of read/write operation.
Returns
操作结果的错误码。Error code indicating the result of the operation.

Implements LibXR::SPI.

Definition at line 68 of file stm32_spi.hpp.

70 {
71 uint32_t need_write = max(write_data.size_, read_data.size_);
72
73 if (dma_buff_rx_.size_ > 0)
74 {
75 ASSERT(need_write <= dma_buff_rx_.size_);
76 }
77
78 if (dma_buff_tx_.size_ > 0)
79 {
80 ASSERT(need_write <= dma_buff_tx_.size_);
81 }
82
83 if (spi_handle_->State != HAL_SPI_STATE_READY)
84 {
85 return ErrorCode::BUSY;
86 }
87
88 mem_read_ = false;
89
90 if (need_write > dma_enable_min_size_)
91 {
92 rw_op_ = op;
93
94 if (write_data.size_ > 0)
95 {
96 memcpy(dma_buff_tx_.addr_, write_data.addr_, need_write);
97
98#if __DCACHE_PRESENT
99 SCB_CleanDCache_by_Addr(static_cast<uint32_t *>(dma_buff_tx_.addr_),
100 static_cast<int32_t>(write_data.size_));
101#endif
102 }
103 if (read_data.size_ > 0)
104 {
105 read_buff_ = read_data;
106 }
107
108 if (write_data.size_ > 0 && read_data.size_ > 0)
109 {
110 HAL_SPI_TransmitReceive_DMA(
111 spi_handle_, static_cast<uint8_t *>(dma_buff_tx_.addr_),
112 static_cast<uint8_t *>(dma_buff_rx_.addr_), need_write);
113 }
114 else if (write_data.size_ > 0)
115 {
116 HAL_SPI_Transmit_DMA(spi_handle_, static_cast<uint8_t *>(dma_buff_tx_.addr_),
117 need_write);
118 }
119 else if (read_data.size_ > 0)
120 {
121 HAL_SPI_Receive_DMA(spi_handle_, static_cast<uint8_t *>(dma_buff_rx_.addr_),
122 need_write);
123 }
124 else
125 {
126 if (op.type != OperationRW::OperationType::BLOCK)
127 {
128 op.UpdateStatus(false, ErrorCode::OK);
129 }
130 return ErrorCode::OK;
131 }
132
133 op.MarkAsRunning();
134 if (op.type == OperationRW::OperationType::BLOCK)
135 {
136 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
137 }
138 return ErrorCode::OK;
139 }
140 else
141 {
142 if (write_data.size_ > 0)
143 {
144 memcpy(dma_buff_tx_.addr_, write_data.addr_, write_data.size_);
145 }
146
147 ErrorCode ans = ErrorCode::OK;
148 if (read_data.size_ > 0 && write_data.size_ > 0)
149 {
150 ans = HAL_SPI_TransmitReceive(
151 spi_handle_, static_cast<uint8_t *>(dma_buff_tx_.addr_),
152 static_cast<uint8_t *>(dma_buff_rx_.addr_), need_write, 20) == HAL_OK
153 ? ErrorCode::OK
154 : ErrorCode::BUSY;
155 }
156 else if (read_data.size_ > 0)
157 {
158 ans = HAL_SPI_Receive(spi_handle_, static_cast<uint8_t *>(dma_buff_rx_.addr_),
159 need_write, 20) == HAL_OK
160 ? ErrorCode::OK
161 : ErrorCode::BUSY;
162 }
163 else if (write_data.size_ > 0)
164 {
165 ans = HAL_SPI_Transmit(spi_handle_, static_cast<uint8_t *>(dma_buff_tx_.addr_),
166 need_write, 20) == HAL_OK
167 ? ErrorCode::OK
168 : ErrorCode::BUSY;
169 }
170 else
171 {
172 if (op.type != OperationRW::OperationType::BLOCK)
173 {
174 op.UpdateStatus(false, ErrorCode::OK);
175 }
176 return ErrorCode::OK;
177 }
178
179 memcpy(read_data.addr_, dma_buff_rx_.addr_, read_data.size_);
180
181 op.UpdateStatus(false, std::forward<ErrorCode>(ans));
182
183 if (op.type == OperationRW::OperationType::BLOCK)
184 {
185 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
186 }
187
188 return ans;
189 }
190 }
void MarkAsRunning()
标记操作为运行状态。 Marks the operation as running.
Definition libxr_rw.hpp:204
constexpr auto max(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最大值

◆ SetConfig()

ErrorCode LibXR::STM32SPI::SetConfig ( SPI::Configuration config)
inlineoverridevirtual

设置 SPI 配置参数。Sets SPI configuration parameters.

Parameters
config需要应用的 SPI 配置。The SPI configuration to apply.
Returns
操作结果的错误码。Error code indicating the result of the operation.

Implements LibXR::SPI.

Definition at line 192 of file stm32_spi.hpp.

193 {
194 switch (config.clock_polarity)
195 {
197 spi_handle_->Init.CLKPolarity = SPI_POLARITY_LOW;
198 break;
200 spi_handle_->Init.CLKPolarity = SPI_POLARITY_HIGH;
201 break;
202 }
203
204 switch (config.clock_phase)
205 {
207 spi_handle_->Init.CLKPhase = SPI_PHASE_1EDGE;
208 break;
210 spi_handle_->Init.CLKPhase = SPI_PHASE_2EDGE;
211 break;
212 }
213
214 return HAL_SPI_Init(spi_handle_) == HAL_OK ? ErrorCode::OK : ErrorCode::BUSY;
215 }
@ EDGE_2
在第二个时钟边沿采样数据。Data sampled on the second clock edge.
@ EDGE_1
在第一个时钟边沿采样数据。Data sampled on the first clock edge.
@ LOW
时钟空闲时为低电平。Clock idle low.
@ HIGH
时钟空闲时为高电平。Clock idle high.

Field Documentation

◆ dma_buff_rx_

RawData LibXR::STM32SPI::dma_buff_rx_

Definition at line 328 of file stm32_spi.hpp.

◆ dma_buff_tx_

RawData LibXR::STM32SPI::dma_buff_tx_

Definition at line 328 of file stm32_spi.hpp.

◆ dma_enable_min_size_

uint32_t LibXR::STM32SPI::dma_enable_min_size_ = 3

Definition at line 334 of file stm32_spi.hpp.

◆ id_

stm32_spi_id_t LibXR::STM32SPI::id_ = STM32_SPI_ID_ERROR

Definition at line 332 of file stm32_spi.hpp.

◆ map

STM32SPI * STM32SPI::map = {nullptr}
static

Definition at line 342 of file stm32_spi.hpp.

◆ mem_read_

bool LibXR::STM32SPI::mem_read_ = false

Definition at line 340 of file stm32_spi.hpp.

◆ read_buff_

RawData LibXR::STM32SPI::read_buff_

Definition at line 338 of file stm32_spi.hpp.

◆ rw_op_

OperationRW LibXR::STM32SPI::rw_op_

Definition at line 336 of file stm32_spi.hpp.

◆ spi_handle_

SPI_HandleTypeDef* LibXR::STM32SPI::spi_handle_

Definition at line 330 of file stm32_spi.hpp.


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