libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
LibXR::SPI Class Referenceabstract

串行外设接口(SPI)抽象类。 Abstract class for Serial Peripheral Interface (SPI). More...

#include <spi.hpp>

Inheritance diagram for LibXR::SPI:
[legend]

Classes

struct  Configuration
 存储 SPI 配置参数的结构体。 Structure for storing SPI configuration parameters. More...
 
struct  ReadWriteInfo
 存储 SPI 读写操作信息的结构体。 Structure for storing SPI read/write operation information. More...
 

Public Types

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...
 
enum class  Prescaler : uint8_t {
  DIV_1 = 0 , DIV_2 = 1 , DIV_4 = 2 , DIV_8 = 3 ,
  DIV_16 = 4 , DIV_32 = 5 , DIV_64 = 6 , DIV_128 = 7 ,
  DIV_256 = 8 , DIV_512 = 9 , DIV_1024 = 10 , DIV_2048 = 11 ,
  DIV_4096 = 12 , DIV_8192 = 13 , DIV_16384 = 14 , DIV_32768 = 15 ,
  DIV_65536 = 16 , UNKNOWN = 0xFF
}
 
using OperationRW = WriteOperation
 定义读写操作类型的别名 / Defines an alias for the read/write operation type.
 

Public Member Functions

 SPI (RawData rx_buffer, RawData tx_buffer)
 构造函数 / Constructor.
 
virtual ErrorCode ReadAndWrite (RawData read_data, ConstRawData write_data, OperationRW &op, bool in_isr=false)=0
 进行 SPI 读写操作 / Performs SPI read and write operations.
 
virtual ErrorCode Read (RawData read_data, OperationRW &op, bool in_isr=false)
 进行 SPI 读取操作 / Performs SPI read operation.
 
virtual ErrorCode Write (ConstRawData write_data, OperationRW &op, bool in_isr=false)
 进行 SPI 写入操作 / Performs SPI write operation.
 
virtual ErrorCode SetConfig (Configuration config)=0
 设置 SPI 配置参数 / Sets SPI configuration parameters.
 
virtual uint32_t GetMaxBusSpeed () const =0
 获取 SPI 设备的最大时钟速度 / Gets the maximum clock speed of the SPI device.
 
virtual Prescaler GetMaxPrescaler () const =0
 获取 SPI 设备的最大分频系数 / Gets the maximum prescaler of the SPI device.
 
uint32_t GetBusSpeed () const
 获取 SPI 设备的当前总线速度 / Gets the current bus speed of the SPI device.
 
Prescaler CalcPrescaler (uint32_t target_max_bus_speed, uint32_t target_min_bus_speed, bool increase)
 计算 SPI 分频系数 / Calculates the SPI prescaler.
 
RawData GetRxBuffer ()
 获取接收数据的缓冲区 / Gets the buffer for storing received data.
 
RawData GetTxBuffer ()
 获取发送数据的缓冲区 / Gets the buffer for storing data to be sent.
 
void SwitchBuffer ()
 切换缓冲区 / Switches the buffer.
 
void SetActiveLength (size_t len)
 设置缓冲区的有效数据长度 / Sets the length of valid data in the buffer.
 
size_t GetActiveLength () const
 获取缓冲区的有效数据长度 / Gets the length of valid data in the buffer.
 
virtual ErrorCode Transfer (size_t size, OperationRW &op, bool in_isr=false)=0
 进行一次SPI传输(使用当前缓冲区数据,零拷贝,支持双缓冲)。 Performs a SPI transfer (zero-copy, supports double buffering).
 
virtual ErrorCode MemWrite (uint16_t reg, ConstRawData write_data, OperationRW &op, bool in_isr=false)=0
 向 SPI 设备的寄存器写入数据。 Writes data to a specific register of the SPI device.
 
virtual ErrorCode MemRead (uint16_t reg, RawData read_data, OperationRW &op, bool in_isr=false)=0
 从 SPI 设备的寄存器读取数据。 Reads data from a specific register of the SPI device.
 
Configuration & GetConfig ()
 获取 SPI 配置参数 / Gets the SPI configuration parameters.
 
bool IsDoubleBuffer () const
 检查是否使用双缓冲区 / Checks if double buffering is enabled.
 

Static Public Member Functions

static constexpr uint32_t PrescalerToDiv (Prescaler prescaler)
 将分频系数转换为除数 / Converts a prescaler to a divisor.
 

Detailed Description

串行外设接口(SPI)抽象类。 Abstract class for Serial Peripheral Interface (SPI).

Definition at line 14 of file spi.hpp.

Member Typedef Documentation

◆ OperationRW

定义读写操作类型的别名 / Defines an alias for the read/write operation type.

Definition at line 63 of file spi.hpp.

Member Enumeration Documentation

◆ ClockPhase

enum class LibXR::SPI::ClockPhase : uint8_t
strong

定义 SPI 时钟相位 / Defines the SPI clock phase.

Enumerator
EDGE_1 

在第一个时钟边沿采样数据 / Data sampled on the first clock edge.

EDGE_2 

在第二个时钟边沿采样数据 / Data sampled on the second clock edge.

Definition at line 31 of file spi.hpp.

32 {
33 EDGE_1 = 0,
34 EDGE_2 = 1
35 };
@ EDGE_2
在第二个时钟边沿采样数据 / Data sampled on the second clock edge.
@ EDGE_1
在第一个时钟边沿采样数据 / Data sampled on the first clock edge.

◆ ClockPolarity

enum class LibXR::SPI::ClockPolarity : uint8_t
strong

定义 SPI 时钟极性 / Defines the SPI clock polarity.

Enumerator
LOW 

时钟空闲时为低电平 / Clock idle low.

HIGH 

时钟空闲时为高电平 / Clock idle high.

Definition at line 21 of file spi.hpp.

22 {
23 LOW = 0,
24 HIGH = 1
25 };
@ LOW
时钟空闲时为低电平 / Clock idle low.
@ HIGH
时钟空闲时为高电平 / Clock idle high.

◆ Prescaler

enum class LibXR::SPI::Prescaler : uint8_t
strong
Enumerator
DIV_1 

分频系数为 1 / Division factor is 1.

DIV_2 

分频系数为 2 / Division factor is 2.

DIV_4 

分频系数为 4 / Division factor is 4.

DIV_8 

分频系数为 8 / Division factor is 8.

DIV_16 

分频系数为 16 / Division factor is 16.

DIV_32 

分频系数为 32 / Division factor is 32.

DIV_64 

分频系数为 64 / Division factor is 64.

DIV_128 

分频系数为 128 / Division factor is 128.

DIV_256 

分频系数为 256 / Division factor is 256.

DIV_512 

分频系数为 512 / Division factor is 512.

DIV_1024 

分频系数为 1024 / Division factor is 1024.

DIV_2048 

分频系数为 2048 / Division factor is 2048.

DIV_4096 

分频系数为 4096 / Division factor is 4096.

DIV_8192 

分频系数为 8192 / Division factor is 8192.

DIV_16384 

分频系数为 16384 / Division factor is 16384.

DIV_32768 

分频系数为 32768 / Division factor is 32768.

DIV_65536 

分频系数为 65536 / Division factor is 65536.

UNKNOWN 

未知分频系数 / Unknown prescaler.

Definition at line 37 of file spi.hpp.

38 {
39 DIV_1 = 0,
40 DIV_2 = 1,
41 DIV_4 = 2,
42 DIV_8 = 3,
43 DIV_16 = 4,
44 DIV_32 = 5,
45 DIV_64 = 6,
46 DIV_128 = 7,
47 DIV_256 = 8,
48 DIV_512 = 9,
49 DIV_1024 = 10,
50 DIV_2048 = 11,
51 DIV_4096 = 12,
52 DIV_8192 = 13,
53 DIV_16384 = 14,
54 DIV_32768 = 15,
55 DIV_65536 = 16,
56 UNKNOWN = 0xFF
57 };
@ DIV_8192
分频系数为 8192 / Division factor is 8192.
@ DIV_16
分频系数为 16 / Division factor is 16.
@ DIV_2
分频系数为 2 / Division factor is 2.
@ DIV_4096
分频系数为 4096 / Division factor is 4096.
@ DIV_64
分频系数为 64 / Division factor is 64.
@ DIV_16384
分频系数为 16384 / Division factor is 16384.
@ DIV_1
分频系数为 1 / Division factor is 1.
@ DIV_2048
分频系数为 2048 / Division factor is 2048.
@ UNKNOWN
未知分频系数 / Unknown prescaler.
@ DIV_32768
分频系数为 32768 / Division factor is 32768.
@ DIV_65536
分频系数为 65536 / Division factor is 65536.
@ DIV_256
分频系数为 256 / Division factor is 256.
@ DIV_128
分频系数为 128 / Division factor is 128.
@ DIV_4
分频系数为 4 / Division factor is 4.
@ DIV_32
分频系数为 32 / Division factor is 32.
@ DIV_512
分频系数为 512 / Division factor is 512.
@ DIV_8
分频系数为 8 / Division factor is 8.
@ DIV_1024
分频系数为 1024 / Division factor is 1024.

Constructor & Destructor Documentation

◆ SPI()

LibXR::SPI::SPI ( RawData rx_buffer,
RawData tx_buffer )
inline

构造函数 / Constructor.

Parameters
rx_buffer存储接收数据的缓冲区 / Buffer to store received data.
tx_buffer存储发送数据的缓冲区 / Buffer to store data to be sent.

Definition at line 111 of file spi.hpp.

112 : rx_buffer_(rx_buffer),
113 tx_buffer_(tx_buffer),
114 double_buffer_rx_(rx_buffer),
115 double_buffer_tx_(tx_buffer)
116 {
117 }

Member Function Documentation

◆ CalcPrescaler()

Prescaler LibXR::SPI::CalcPrescaler ( uint32_t target_max_bus_speed,
uint32_t target_min_bus_speed,
bool increase )
inline

计算 SPI 分频系数 / Calculates the SPI prescaler.

Parameters
target_max_bus_speed目标最大总线速度(单位:Hz)。 Target maximum bus speed (in Hz).
target_min_bus_speed目标最小总线速度(单位:Hz)。 Target minimum bus speed (in Hz).
increase是否从最小分频系数开始 / Whether to start from the minimum prescaler.
Returns
计算得到的分频系数 / The calculated prescaler.

Definition at line 199 of file spi.hpp.

201 {
202 const uint32_t SRC = GetMaxBusSpeed();
203 if (SRC == 0u)
204 {
205 return Prescaler::UNKNOWN;
206 }
207
208 if (target_max_bus_speed && target_min_bus_speed &&
209 target_min_bus_speed > target_max_bus_speed)
210 {
211 uint32_t t = target_min_bus_speed;
212 target_min_bus_speed = target_max_bus_speed;
213 target_max_bus_speed = t;
214 }
215
216 const uint8_t MAX_IDX = static_cast<uint8_t>(GetMaxPrescaler());
217
218 ASSERT(MAX_IDX != static_cast<uint8_t>(Prescaler::UNKNOWN));
219
220 auto fits = [&](Prescaler p) -> bool
221 {
222 const uint32_t DIV = PrescalerToDiv(p);
223 if (DIV == 0u)
224 {
225 return false;
226 }
227 const uint32_t F = SRC / DIV;
228 if (target_max_bus_speed && F > target_max_bus_speed)
229 {
230 return false;
231 }
232 if (target_min_bus_speed && F < target_min_bus_speed)
233 {
234 return false;
235 }
236 return true;
237 };
238
239 if (increase)
240 {
241 for (uint8_t i = 0; i <= MAX_IDX; ++i)
242 {
243 Prescaler p = static_cast<Prescaler>(i);
244 if (fits(p))
245 {
246 return p;
247 }
248 }
249 }
250 else
251 {
252 for (int i = static_cast<int>(MAX_IDX); i >= 0; --i)
253 {
254 Prescaler p = static_cast<Prescaler>(i);
255 if (fits(p))
256 {
257 return p;
258 }
259 }
260 }
261
262 const uint32_t F_FASTEST = SRC / PrescalerToDiv(Prescaler::DIV_1);
263 const Prescaler P_SLOWEST = static_cast<Prescaler>(MAX_IDX);
264 const uint32_t F_SLOWEST = SRC / PrescalerToDiv(P_SLOWEST);
265
266 if (target_min_bus_speed && F_FASTEST < target_min_bus_speed)
267 {
268 return Prescaler::DIV_1;
269 }
270 if (target_max_bus_speed && F_SLOWEST > target_max_bus_speed)
271 {
272 return P_SLOWEST;
273 }
274
275 if (increase)
276 {
277 for (uint8_t i = 0; i <= MAX_IDX; ++i)
278 {
279 Prescaler p = static_cast<Prescaler>(i);
280 const uint32_t F = SRC / PrescalerToDiv(p);
281 if (!target_max_bus_speed || F <= target_max_bus_speed)
282 {
283 return p;
284 }
285 }
286 return P_SLOWEST;
287 }
288 else
289 {
290 for (int i = static_cast<int>(MAX_IDX); i >= 0; --i)
291 {
292 Prescaler p = static_cast<Prescaler>(i);
293 const uint32_t F = SRC / PrescalerToDiv(p);
294 if (!target_min_bus_speed || F >= target_min_bus_speed)
295 {
296 return p;
297 }
298 }
299 return Prescaler::DIV_1;
300 }
301 }
static constexpr uint32_t PrescalerToDiv(Prescaler prescaler)
将分频系数转换为除数 / Converts a prescaler to a divisor.
Definition spi.hpp:70
virtual Prescaler GetMaxPrescaler() const =0
获取 SPI 设备的最大分频系数 / Gets the maximum prescaler of the SPI device.
virtual uint32_t GetMaxBusSpeed() const =0
获取 SPI 设备的最大时钟速度 / Gets the maximum clock speed of the SPI device.

◆ GetActiveLength()

size_t LibXR::SPI::GetActiveLength ( ) const
inline

获取缓冲区的有效数据长度 / Gets the length of valid data in the buffer.

Definition at line 355 of file spi.hpp.

355{ return double_buffer_tx_.GetActiveLength(); }
size_t GetActiveLength() const
获取当前活动缓冲区中准备好的数据长度 Gets the size of valid data in active buffer

◆ GetBusSpeed()

uint32_t LibXR::SPI::GetBusSpeed ( ) const
inline

获取 SPI 设备的当前总线速度 / Gets the current bus speed of the SPI device.

Returns
SPI 设备的当前总线速度(单位:Hz)。 The current bus speed of the SPI device (in Hz).

Definition at line 179 of file spi.hpp.

180 {
181 const uint32_t DIV = PrescalerToDiv(config_.prescaler);
182 const uint32_t SRC = GetMaxBusSpeed();
183 if (DIV == 0u || SRC == 0u)
184 {
185 return 0u;
186 }
187 return SRC / DIV;
188 }
Prescaler prescaler
SPI 分频系数 / SPI prescaler.
Definition spi.hpp:90

◆ GetConfig()

Configuration & LibXR::SPI::GetConfig ( )
inline

获取 SPI 配置参数 / Gets the SPI configuration parameters.

Returns
SPI 配置参数 / The SPI configuration parameters.

Definition at line 397 of file spi.hpp.

397{ return config_; }

◆ GetMaxBusSpeed()

virtual uint32_t LibXR::SPI::GetMaxBusSpeed ( ) const
pure virtual

获取 SPI 设备的最大时钟速度 / Gets the maximum clock speed of the SPI device.

Returns
SPI 设备的最大时钟速度(单位:Hz)。 The maximum clock speed of the SPI device (in Hz).

Implemented in LibXR::CH32SPI, LibXR::ESP32SPI, LibXR::MSPM0SPI, and LibXR::STM32SPI.

◆ GetMaxPrescaler()

virtual Prescaler LibXR::SPI::GetMaxPrescaler ( ) const
pure virtual

获取 SPI 设备的最大分频系数 / Gets the maximum prescaler of the SPI device.

Returns
SPI 设备的最大分频系数 / The maximum prescaler of the SPI device.

Implemented in LibXR::CH32SPI, LibXR::ESP32SPI, LibXR::MSPM0SPI, and LibXR::STM32SPI.

◆ GetRxBuffer()

RawData LibXR::SPI::GetRxBuffer ( )
inline

获取接收数据的缓冲区 / Gets the buffer for storing received data.

Returns
接收数据的缓冲区 / The buffer for storing received data.

Definition at line 307 of file spi.hpp.

308 {
309 if (IsDoubleBuffer())
310 {
311 return {double_buffer_rx_.ActiveBuffer(), double_buffer_rx_.Size()};
312 }
313 else
314 {
315 return rx_buffer_;
316 }
317 }
size_t Size() const
获取每个缓冲区的大小(单位:字节) Gets the size of each buffer in bytes
uint8_t * ActiveBuffer() const
获取当前正在使用的缓冲区指针 Returns the currently active buffer
bool IsDoubleBuffer() const
检查是否使用双缓冲区 / Checks if double buffering is enabled.
Definition spi.hpp:405

◆ GetTxBuffer()

RawData LibXR::SPI::GetTxBuffer ( )
inline

获取发送数据的缓冲区 / Gets the buffer for storing data to be sent.

Returns
发送数据的缓冲区 / The buffer for storing data to be sent.

Definition at line 323 of file spi.hpp.

324 {
325 if (IsDoubleBuffer())
326 {
327 return {double_buffer_tx_.ActiveBuffer(), double_buffer_tx_.Size()};
328 }
329 else
330 {
331 return tx_buffer_;
332 }
333 }

◆ IsDoubleBuffer()

bool LibXR::SPI::IsDoubleBuffer ( ) const
inline

检查是否使用双缓冲区 / Checks if double buffering is enabled.

Returns
true
false

Definition at line 405 of file spi.hpp.

405{ return config_.double_buffer; }
bool double_buffer
是否使用双缓冲区 / Whether to use double buffer.
Definition spi.hpp:91

◆ MemRead()

virtual ErrorCode LibXR::SPI::MemRead ( uint16_t reg,
RawData read_data,
OperationRW & op,
bool in_isr = false )
pure virtual

从 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).
in_isr是否在中断中进行操作 / Whether the operation is performed in an ISR.
Returns
操作结果的错误码 / Error code indicating success or failure.

Implemented in LibXR::CH32SPI, LibXR::ESP32SPI, LibXR::MSPM0SPI, and LibXR::STM32SPI.

◆ MemWrite()

virtual ErrorCode LibXR::SPI::MemWrite ( uint16_t reg,
ConstRawData write_data,
OperationRW & op,
bool in_isr = false )
pure virtual

向 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).
in_isr是否在中断中进行操作 / Whether the operation is performed in an ISR.
Returns
操作结果的错误码 / Error code indicating success or failure.

Implemented in LibXR::CH32SPI, LibXR::ESP32SPI, LibXR::MSPM0SPI, and LibXR::STM32SPI.

◆ PrescalerToDiv()

static constexpr uint32_t LibXR::SPI::PrescalerToDiv ( Prescaler prescaler)
inlinestaticconstexpr

将分频系数转换为除数 / Converts a prescaler to a divisor.

Parameters
prescaler分频系数 / Prescaler.
Returns
除数 / Divisor.

Definition at line 70 of file spi.hpp.

71 {
72 if (prescaler == Prescaler::UNKNOWN)
73 {
74 return 0u;
75 }
76 const uint8_t ORD = static_cast<uint8_t>(prescaler);
77 return (ORD <= 30) ? (1u << ORD) : 0u;
78 }

◆ Read()

virtual ErrorCode LibXR::SPI::Read ( RawData read_data,
OperationRW & op,
bool in_isr = false )
inlinevirtual

进行 SPI 读取操作 / Performs SPI read operation.

Parameters
read_data存储读取数据的缓冲区 / Buffer to store the read data.
op读写操作类型 / Type of read/write operation.
in_isr是否在中断中进行操作 / Whether the operation is performed in an ISR.
Returns
操作结果的错误码 / Error code indicating the result of the operation.

Definition at line 137 of file spi.hpp.

138 {
139 return ReadAndWrite(read_data, ConstRawData(nullptr, 0), op, in_isr);
140 }
virtual ErrorCode ReadAndWrite(RawData read_data, ConstRawData write_data, OperationRW &op, bool in_isr=false)=0
进行 SPI 读写操作 / Performs SPI read and write operations.

◆ ReadAndWrite()

virtual ErrorCode LibXR::SPI::ReadAndWrite ( RawData read_data,
ConstRawData write_data,
OperationRW & op,
bool in_isr = false )
pure virtual

进行 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.
in_isr是否在中断中进行操作 / Whether the operation is performed in an ISR.
Returns
操作结果的错误码 / Error code indicating the result of the operation.

Implemented in LibXR::CH32SPI, LibXR::ESP32SPI, LibXR::MSPM0SPI, and LibXR::STM32SPI.

◆ SetActiveLength()

void LibXR::SPI::SetActiveLength ( size_t len)
inline

设置缓冲区的有效数据长度 / Sets the length of valid data in the buffer.

Definition at line 350 of file spi.hpp.

350{ double_buffer_tx_.SetActiveLength(len); }
void SetActiveLength(size_t length)
设置当前活动缓冲区的数据长度 Sets the size of the active buffer

◆ SetConfig()

virtual ErrorCode LibXR::SPI::SetConfig ( Configuration config)
pure virtual

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

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

Implemented in LibXR::CH32SPI, LibXR::ESP32SPI, LibXR::MSPM0SPI, and LibXR::STM32SPI.

◆ SwitchBuffer()

void LibXR::SPI::SwitchBuffer ( )
inline

切换缓冲区 / Switches the buffer.

Definition at line 338 of file spi.hpp.

339 {
340 if (IsDoubleBuffer())
341 {
342 double_buffer_rx_.Switch();
343 double_buffer_tx_.Switch();
344 }
345 }
void Switch()
切换到备用缓冲区(若其有效) Switches to the pending buffer if it's valid

◆ Transfer()

virtual ErrorCode LibXR::SPI::Transfer ( size_t size,
OperationRW & op,
bool in_isr = false )
pure virtual

进行一次SPI传输(使用当前缓冲区数据,零拷贝,支持双缓冲)。 Performs a SPI transfer (zero-copy, supports double buffering).

Parameters
size需要传输的数据大小 / The size of the data to be transferred.
op读写操作类型 / Type of read/write operation.
in_isr是否在中断中进行操作 / Whether the operation is performed in an ISR.
Returns
操作结果的错误码 / Error code indicating the result of the operation.

Implemented in LibXR::CH32SPI, LibXR::ESP32SPI, LibXR::MSPM0SPI, and LibXR::STM32SPI.

◆ Write()

virtual ErrorCode LibXR::SPI::Write ( ConstRawData write_data,
OperationRW & op,
bool in_isr = false )
inlinevirtual

进行 SPI 写入操作 / Performs SPI write operation.

Parameters
write_data需要写入的数据缓冲区 / Buffer containing the data to be written.
op读写操作类型 / Type of read/write operation.
in_isr是否在中断中进行操作 / Whether the operation is performed in an ISR.
Returns
操作结果的错误码 / Error code indicating the result of the operation.

Definition at line 149 of file spi.hpp.

150 {
151 return ReadAndWrite(RawData(nullptr, 0), write_data, op, in_isr);
152 }

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