SPI (串行外设接口)
简介
使用特定同步协议与外部器件进行半双工、全双工和单工同步串行通信。 一个 SPI 总线只支持一个 Master,但可以有一个或多个 Slave。 在 SPI 应用架构中,SPI Master 作为主机,负责输出时钟和片选信号给 Slave 器件。
ET6001提供 3 组 1 线 SPI Master 接口 和 1 组 1 线 SPI Slave 接口。
功能概述
支持接收端采样延迟
支持通过软件配置输出时钟的有效相位和极性
数据帧长度可配置,最大支持16bit传输数据帧
支持硬件DMA握手
支持中断上报
支持 TX-RX loopback 测试
SPI Master 最高写时钟和最高读时钟均为 25MHz
SPI Slave 最高写时钟和最高读时钟均为 12.5MHz
SPI Master0 接收和发送FIFO 深度为 32,SPI Master1、2 和 SPI Slave 接收和发送 FIFO 深度为 16,FIFO 宽度均为 16bit
软件配置流程
基础收发配置流程如下:
配置IOMUX 配置对应串口引脚为SPI模式
配置SPI Master 调用
SPI_setConfig()传入SPIx和参数结构体指针使能SPI 调用
SPI_enableModule()传入SPIx使能相应SPI数据收发 调用
SPI_writeDataBlockingFIFO()或SPI_readDataBlockingFIFO()传入SPIx和数据/数据指针,数据指针指向要接收的数据
SPI Master 时钟速率配置说明
SPI 的输入时钟来自APB3总线时钟,可通过 CRG_getAPB3Clock() 获取, 默认配置为100MHz。
配置SPI Master 时钟速率的公式如下:
注意
DIV 的取值范围为 2~65535,且必须为偶数。
配置SPI Master 时钟速率的方法有两种:
通过
SPI_setConfig()配置 SPI Master 时钟速率,传入参数结构体指针中的 baudrate 字段即可。通过
SPI_setBaudRate()配置 SPI Master 时钟速率。
应用示例
SPI-NorFlash(W25Q32JVSSIQ)读写示例:SPI/SPI_Device。
API Reference
Header File
Functions
-
void SPI_enableModule(SPI_Type *SPIx)
enable module
- 参数:
SPIx –
-
void SPI_disableModule(SPI_Type *SPIx)
disable module
- 参数:
SPIx –
-
void SPI_setcharLength(SPI_Type *SPIx, uint16_t charLength)
set data length
- 参数:
SPIx –
charLength –
-
void SPI_setCSLow(SPI_Type *SPIx)
set the selected chip signal to retain LOW
- 参数:
SPIx –
-
void SPI_setCSHigh(SPI_Type *SPIx)
set the selected chip signal to retain HIGH
- 参数:
SPIx –
-
void SPI_selectChip(SPI_Type *SPIx, uint8_t slaveIndex)
choose or change the slave for master to transmit data
- 参数:
SPIx –
slaveIndex – parameters are in the “ext6x_spi.h” SPI_CS_NONE SPI_CS0 SPI_CS1 SPI_CS2 SPI_CS3
-
void SPI_configFrameNum(SPI_Type *SPIx, uint16_t num)
Configure frame number to Receive when Transmit mode is SPI_RECEIVE or SPI_EEPROM_READ.
- 参数:
SPIx –
num –
-
void SPI_configTransMode(SPI_Type *SPIx, SPI_TransferMode mode)
Configure Transfer mode.
- 参数:
SPIx –
mode –
-
void SPI_resetFIFO(SPI_Type *SPIx)
reset fifo
- 参数:
SPIx –
-
void SPI_setFIFOInterruptLevel(SPI_Type *SPIx, uint8_t txLevel, uint8_t rxLevel)
Set FIFO interrupt thresholds for transmit and receive.
备注
Threshold values are checked by IS_SPI_FIFO_THRESHOLD macro to ensure they are less than SPI_MAX_FIFO_DEPTH
- 参数:
SPIx – Pointer to SPI peripheral
txLevel – Transmit FIFO interrupt threshold (valid range: 0 to SPI_MAX_FIFO_DEPTH)
rxLevel – Receive FIFO interrupt threshold (valid range: 0 to SPI_MAX_FIFO_DEPTH)
-
void SPI_getFIFOInterruptLevel(SPI_Type *SPIx, uint8_t *txLevel, uint8_t *rxLevel)
get FIFO interrupt level
- 参数:
SPIx –
txLevel –
rxLevel –
-
uint8_t SPI_getTxFIFOStatus(SPI_Type *SPIx)
get transmit data num in TX FIFO
- 参数:
SPIx –
- 返回:
SPI TxFIFO Level
-
uint8_t SPI_getRxFIFOStatus(SPI_Type *SPIx)
get received data num in RX FIFO
- 参数:
SPIx –
- 返回:
SPI RxFIFO Level
-
bool SPI_isBusy(SPI_Type *SPIx)
get spi is busy or not
- 参数:
SPIx –
- 返回:
true
- 返回:
false
-
void SPI_writeDataNonBlocking(SPI_Type *SPIx, uint16_t data)
write data in non-blocking mode
- 参数:
SPIx –
data –
-
uint16_t SPI_readDataNonBlocking(SPI_Type *SPIx)
read data in non-blocking mode
- 参数:
SPIx –
- 返回:
uint16_t
-
void SPI_writeDataBlockingFIFO(SPI_Type *SPIx, uint16_t data)
write data in blocking mode with FIFO status
- 参数:
SPIx –
data –
-
uint16_t SPI_readDataBlockingFIFO(SPI_Type *SPIx)
read data in blocking mode with FIFO status
- 参数:
SPIx –
- 返回:
uint16_t
-
void SPI_writeDataBlockingNonFIFO(SPI_Type *SPIx, uint16_t data)
write data in blocking mode with register status
- 参数:
SPIx –
data –
-
uint16_t SPI_readDataBlockingNonFIFO(SPI_Type *SPIx)
read data in blocking mode with register status
- 参数:
SPIx –
- 返回:
uint16_t
-
void SPI_setConfig(SPI_Type *SPIx, SPI_Config *config)
set spi configuration
- 参数:
SPIx –
config –
-
void SPI_setBaudRate(SPI_Type *SPIx, uint32_t bitRate)
set spi baud rate
- 参数:
SPIx –
bitRate –
-
void SPI_enableInterrupt(SPI_Type *SPIx, uint32_t intFlags)
enable interrupt
- 参数:
SPIx –
intFlags – SPI0_IMR_RSVD_IMR_Msk SPI0_IMR_MSTIM_Msk SPI0_IMR_RXFIM_Msk SPI0_IMR_RXOIM_Msk SPI0_IMR_RXUIM_Msk SPI0_IMR_TXOIM_Msk SPI0_IMR_TXEIM_Msk
-
void SPI_disableInterrupt(SPI_Type *SPIx, uint32_t intFlags)
disable interrupt
- 参数:
SPIx –
intFlags – parameters are the same as those used in SPI_enableInterrupt.
-
uint32_t SPI_getInterruptStatus(SPI_Type *SPIx)
get interrupt status
- 参数:
SPIx –
- 返回:
uint32_t return value is interrupt mask
-
void SPI_clearInterruptStatus(SPI_Type *SPIx, uint32_t intFlags)
clear interrupt status
- 参数:
SPIx –
intFlags – parameters are the same as those used in SPI_enableInterrupt.
-
void SPI_enableDMA(SPI_Type *SPIx)
enable DMA interface
- 参数:
SPIx –
-
void SPI_disableDMA(SPI_Type *SPIx)
disable DMA interface
- 参数:
SPIx –
-
void SPI_transmit24Bits(SPI_Type *SPIx, uint32_t data)
transmit 24bits data with 8bits length data
- 参数:
SPIx –
data –
-
void SPI_transmit32Bits(SPI_Type *SPIx, uint32_t data)
transmit 32bits data with 16bits length data
- 参数:
SPIx –
data –
-
uint16_t SPI_receive16Bits(SPI_Type *SPIx, SPI_endianess endianness, uint16_t dummyData)
receive 16bit with 8bit length data
- 参数:
SPIx –
endianness –
dummyData –
- 返回:
uint16_t
-
uint32_t SPI_receive24Bits(SPI_Type *SPIx, SPI_endianess endianness, uint16_t dummyData)
receive 24bit with 8bit length data
- 参数:
SPIx –
endianness –
dummyData –
- 返回:
uint32_t
-
uint32_t SPI_receive32Bits(SPI_Type *SPIx, SPI_endianess endianness, uint16_t dummyData)
receive 32bit with 8bit length data
- 参数:
SPIx –
endianness –
dummyData –
- 返回:
uint32_t
-
uint16_t SPI_pollingNonFIFOTransaction(SPI_Type *SPIx, uint16_t charLength, uint16_t data)
transmit and receive 1 data in polling mode with register status
- 参数:
SPIx –
charLength –
data –
- 返回:
uint16_t
-
void SPI_pollingFIFOTransaction(SPI_Type *SPIx, uint16_t charLength, void *pTxBuffer, void *pRxBuffer, uint16_t numOfWords)
transmit and receive [numOfWords] data in polling mode with register status
- 参数:
SPIx –
charLength –
pTxBuffer –
pRxBuffer –
numOfWords –
-
void SPI_enableLoopback(SPI_Type *SPIx)
Enables the loopback mode for the SPI interface.
In loopback mode, the transmitted data is internally connected to the receiver input, allowing for testing and debugging without the need for an external device.
备注
This function should be called only when the SPI interface is disabled.
- 参数:
SPIx – Pointer to the SPI peripheral, where x can be 0, 1, 2, or 3 to select the SPI peripheral.
-
void SPI_disableLoopback(SPI_Type *SPIx)
Disables the loopback mode for the SPI interface.
In loopback mode, the transmitted data is internally connected to the receiver input, allowing for testing and debugging without the need for an external device.
备注
This function should be called only when the SPI interface is disabled.
- 参数:
SPIx – Pointer to the SPI peripheral, where x can be 0, 1, 2, or 3 to select the SPI peripheral.
-
void SPI_initStruct(SPI_Config *cfg)
Initializes the SPI configuration structure.
备注
This function is used to initialize each member variable of the SPI configuration structure
- 参数:
cfg – Pointer to the SPI configuration structure
Structures
-
struct SPI_Config
Macros
-
SPI_INTERRUPT_ALL
-
SPI_CS_NONE
-
SPI_CS0
-
SPI_CS1
-
SPI_CS2
-
SPI_CS3
-
SPI_transmitByte(SPIx, txData)
-
SPI_transmit16Bits(SPIx, txData)
-
SPI_transmitNBytes(SPIx, txBuffer, numOfWords)
-
SPI_transmitN16BitWord(SPIx, txBuffer, numOfWords)
-
SPI_transmitNWordsWithCharLength(SPIx, charLength, txBuffer, numOfWords)
-
SPI_receiveByte(SPIx, dummyData)
-
SPI_receiveNBytes(SPIx, rxBuffer, numOfWords)
-
SPI_receiveN16BitWord(SPIx, rxBuffer, numOfWords)
-
SPI_receiveNWordsWithcharLength(SPIx, charLength, rxBuffer, numOfWords)
Enumerations
-
enum SPI_TransferProtocol
Values:
-
enumerator SPI_PROT_POL0PHA0
-
enumerator SPI_PROT_POL0PHA1
-
enumerator SPI_PROT_POL1PHA0
-
enumerator SPI_PROT_POL1PHA1
-
enumerator SPI_PROT_POL0PHA0