|
libxr
1.0
Want to be the best embedded framework
|
CH32 SPI 驱动实现 / CH32 SPI driver implementation. More...
#include <ch32_spi.hpp>
Public Member Functions | |
| CH32SPI (ch32_spi_id_t id, RawData dma_rx, RawData dma_tx, GPIO_TypeDef *sck_port, uint16_t sck_pin, GPIO_TypeDef *miso_port, uint16_t miso_pin, GPIO_TypeDef *mosi_port, uint16_t mosi_pin, uint32_t pin_remap=0, bool master_mode=true, bool firstbit_msb=true, uint16_t prescaler=SPI_BaudRatePrescaler_64, uint32_t dma_enable_min_size=3, SPI::Configuration config={SPI::ClockPolarity::LOW, SPI::ClockPhase::EDGE_1}) | |
| 构造 SPI 对象 / Construct SPI object | |
| ErrorCode | ReadAndWrite (RawData read_data, ConstRawData write_data, OperationRW &op, bool in_isr) override |
| SPI 接口实现 / SPI interface overrides. | |
| ErrorCode | SetConfig (SPI::Configuration config) override |
| 设置 SPI 配置参数。Sets SPI configuration parameters. | |
| ErrorCode | MemRead (uint16_t reg, RawData read_data, OperationRW &op, bool in_isr) override |
| 从 SPI 设备的寄存器读取数据。 Reads data from a specific register of the SPI device. | |
| ErrorCode | MemWrite (uint16_t reg, ConstRawData write_data, OperationRW &op, bool in_isr) override |
| 向 SPI 设备的寄存器写入数据。 Writes data to a specific register of the SPI device. | |
| ErrorCode | Transfer (size_t size, OperationRW &op, bool in_isr) override |
| 进行一次SPI传输(使用当前缓冲区数据,零拷贝,支持双缓冲)。 Performs a SPI transfer (zero-copy, supports double buffering). | |
| uint32_t | GetMaxBusSpeed () const override |
| 获取 SPI 设备的最大时钟速度。Gets the maximum clock speed of the SPI device. | |
| SPI::Prescaler | GetMaxPrescaler () const override |
| 获取 SPI 设备的最大分频系数。Gets the maximum prescaler of the SPI device. | |
| void | RxDmaIRQHandler () |
| DMA 中断回调 / DMA interrupt callbacks. | |
| void | TxDmaIRQHandler () |
| ErrorCode | PollingTransfer (uint8_t *rx, const uint8_t *tx, uint32_t len) |
| 轮询传输辅助函数 / Polling transfer helper | |
Public Member Functions inherited from LibXR::SPI | |
| SPI (RawData rx_buffer, RawData tx_buffer) | |
| 构造函数。Constructor. | |
| 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. | |
| 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. | |
| Configuration & | GetConfig () |
| 获取 SPI 配置参数。Gets the SPI configuration parameters. | |
| bool | IsDoubleBuffer () const |
| 检查是否使用双缓冲区。Checks if double buffering is enabled. | |
Data Fields | |
| SPI_TypeDef * | instance_ |
| DMA_Channel_TypeDef * | dma_rx_channel_ |
| DMA_Channel_TypeDef * | dma_tx_channel_ |
| ch32_spi_id_t | id_ |
| uint32_t | dma_enable_min_size_ |
| OperationRW | rw_op_ |
| 运行时状态 / Runtime state | |
| RawData | read_buff_ |
| bool | mem_read_ = false |
| bool | busy_ = false |
| uint16_t | mode_ |
| SPI 寄存器配置缓存 / Cached SPI register configuration. | |
| uint16_t | datasize_ |
| SPI_DataSize_8b by default. | |
| uint16_t | firstbit_ |
| SPI_FirstBit_MSB / SPI_FirstBit_LSB. | |
| uint16_t | prescaler_ |
| SPI_BaudRatePrescaler_x. | |
| uint16_t | nss_ = SPI_NSS_Soft |
| GPIO_TypeDef * | sck_port_ |
| GPIO 配置 / GPIO configuration. | |
| uint16_t | sck_pin_ |
| GPIO_TypeDef * | miso_port_ |
| uint16_t | miso_pin_ |
| GPIO_TypeDef * | mosi_port_ |
| uint16_t | mosi_pin_ |
Static Public Attributes | |
| static CH32SPI * | map_ [CH32_SPI_NUMBER] = {nullptr} |
Private Member Functions | |
| bool | DmaBusy () const |
| void | StartDmaDuplex (uint32_t count) |
| void | StopDma () |
| void | PrepareTxBuffer (ConstRawData write_data, uint32_t need_len, uint32_t prefix=0, uint8_t dummy=0x00) |
Static Private Member Functions | |
| static SPI::Prescaler | MapCH32PrescalerToEnum (uint16_t p) |
| static bool | MapEnumToCH32Prescaler (SPI::Prescaler p, uint16_t &out) |
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... | |
| 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. | |
Static Public Member Functions inherited from LibXR::SPI | |
| static constexpr uint32_t | PrescalerToDiv (Prescaler prescaler) |
| 将分频系数转换为除数。Converts a prescaler to a divisor. | |
CH32 SPI 驱动实现 / CH32 SPI driver implementation.
Definition at line 19 of file ch32_spi.hpp.
| CH32SPI::CH32SPI | ( | ch32_spi_id_t | id, |
| RawData | dma_rx, | ||
| RawData | dma_tx, | ||
| GPIO_TypeDef * | sck_port, | ||
| uint16_t | sck_pin, | ||
| GPIO_TypeDef * | miso_port, | ||
| uint16_t | miso_pin, | ||
| GPIO_TypeDef * | mosi_port, | ||
| uint16_t | mosi_pin, | ||
| uint32_t | pin_remap = 0, | ||
| bool | master_mode = true, | ||
| bool | firstbit_msb = true, | ||
| uint16_t | prescaler = SPI_BaudRatePrescaler_64, | ||
| uint32_t | dma_enable_min_size = 3, | ||
| SPI::Configuration | config = {SPI::ClockPolarity::LOW, SPI::ClockPhase::EDGE_1} ) |
构造 SPI 对象 / Construct SPI object
Definition at line 62 of file ch32_spi.cpp.
|
inlineprivate |
Definition at line 55 of file ch32_spi.hpp.
|
overridevirtual |
获取 SPI 设备的最大时钟速度。Gets the maximum clock speed of the SPI device.
Implements LibXR::SPI.
Definition at line 649 of file ch32_spi.cpp.
|
overridevirtual |
获取 SPI 设备的最大分频系数。Gets the maximum prescaler of the SPI device.
Implements LibXR::SPI.
Definition at line 666 of file ch32_spi.cpp.
|
staticprivate |
Definition at line 693 of file ch32_spi.cpp.
|
staticprivate |
Definition at line 13 of file ch32_spi.cpp.
|
overridevirtual |
从 SPI 设备的寄存器读取数据。 Reads data from a specific register of the SPI device.
| 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. |
Implements LibXR::SPI.
Definition at line 371 of file ch32_spi.cpp.
|
overridevirtual |
向 SPI 设备的寄存器写入数据。 Writes data to a specific register of the SPI device.
| 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. |
Implements LibXR::SPI.
Definition at line 437 of file ch32_spi.cpp.
| ErrorCode CH32SPI::PollingTransfer | ( | uint8_t * | rx, |
| const uint8_t * | tx, | ||
| uint32_t | len ) |
轮询传输辅助函数 / Polling transfer helper
Definition at line 268 of file ch32_spi.cpp.
|
private |
Definition at line 503 of file ch32_spi.cpp.
|
overridevirtual |
SPI 接口实现 / SPI interface overrides.
Implements LibXR::SPI.
Definition at line 289 of file ch32_spi.cpp.
| void CH32SPI::RxDmaIRQHandler | ( | ) |
DMA 中断回调 / DMA interrupt callbacks.
Definition at line 549 of file ch32_spi.cpp.
|
overridevirtual |
设置 SPI 配置参数。Sets SPI configuration parameters.
Implements LibXR::SPI.
Definition at line 232 of file ch32_spi.cpp.
|
private |
Definition at line 525 of file ch32_spi.cpp.
|
private |
Definition at line 543 of file ch32_spi.cpp.
|
overridevirtual |
进行一次SPI传输(使用当前缓冲区数据,零拷贝,支持双缓冲)。 Performs a SPI transfer (zero-copy, supports double buffering).
| 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. |
Implements LibXR::SPI.
Definition at line 596 of file ch32_spi.cpp.
| void CH32SPI::TxDmaIRQHandler | ( | ) |
Definition at line 583 of file ch32_spi.cpp.
| bool LibXR::CH32SPI::busy_ = false |
Definition at line 80 of file ch32_spi.hpp.
| uint16_t LibXR::CH32SPI::datasize_ |
SPI_DataSize_8b by default.
Definition at line 84 of file ch32_spi.hpp.
| uint32_t LibXR::CH32SPI::dma_enable_min_size_ |
Definition at line 74 of file ch32_spi.hpp.
| DMA_Channel_TypeDef* LibXR::CH32SPI::dma_rx_channel_ |
Definition at line 71 of file ch32_spi.hpp.
| DMA_Channel_TypeDef* LibXR::CH32SPI::dma_tx_channel_ |
Definition at line 72 of file ch32_spi.hpp.
| uint16_t LibXR::CH32SPI::firstbit_ |
SPI_FirstBit_MSB / SPI_FirstBit_LSB.
Definition at line 85 of file ch32_spi.hpp.
| ch32_spi_id_t LibXR::CH32SPI::id_ |
Definition at line 73 of file ch32_spi.hpp.
| SPI_TypeDef* LibXR::CH32SPI::instance_ |
Definition at line 70 of file ch32_spi.hpp.
|
static |
Definition at line 52 of file ch32_spi.hpp.
| bool LibXR::CH32SPI::mem_read_ = false |
Definition at line 79 of file ch32_spi.hpp.
| uint16_t LibXR::CH32SPI::miso_pin_ |
Definition at line 93 of file ch32_spi.hpp.
| GPIO_TypeDef* LibXR::CH32SPI::miso_port_ |
Definition at line 92 of file ch32_spi.hpp.
| uint16_t LibXR::CH32SPI::mode_ |
SPI 寄存器配置缓存 / Cached SPI register configuration.
SPI_Mode_Master / SPI_Mode_Slave
Definition at line 83 of file ch32_spi.hpp.
| uint16_t LibXR::CH32SPI::mosi_pin_ |
Definition at line 95 of file ch32_spi.hpp.
| GPIO_TypeDef* LibXR::CH32SPI::mosi_port_ |
Definition at line 94 of file ch32_spi.hpp.
| uint16_t LibXR::CH32SPI::nss_ = SPI_NSS_Soft |
Definition at line 87 of file ch32_spi.hpp.
| uint16_t LibXR::CH32SPI::prescaler_ |
SPI_BaudRatePrescaler_x.
Definition at line 86 of file ch32_spi.hpp.
| RawData LibXR::CH32SPI::read_buff_ |
Definition at line 78 of file ch32_spi.hpp.
| OperationRW LibXR::CH32SPI::rw_op_ |
运行时状态 / Runtime state
Definition at line 77 of file ch32_spi.hpp.
| uint16_t LibXR::CH32SPI::sck_pin_ |
Definition at line 91 of file ch32_spi.hpp.
| GPIO_TypeDef* LibXR::CH32SPI::sck_port_ |
Definition at line 90 of file ch32_spi.hpp.