最近在研究SPI總線,至于協(xié)議和硬件描述就不多說(shuō)了
四線包括時(shí)鐘、片選、接收、發(fā)送
初始化SP
SPI_InitStructure.SPI_Direction = SPI_DirecTIon_2Lines_FullDuplex; //全雙工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //主模式
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b; //16bit寬度
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; //2--18MHz; 4--9MHz; 8--4.5MHz
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //高位在前
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPIx, &SPI_InitStructure);
SPI_Cmd(SPIx, ENABLE);
SPI不能硬件控制CS,只能軟件來(lái)控,就是通過(guò)將NSS設(shè)為外部GPIO來(lái)控制。
像我所做的項(xiàng)目是使用STM32與FPGA通信,而FPGA的SPI工作在這種一直狀態(tài)
作為主設(shè)備的STM32,CS在傳輸數(shù)據(jù)的時(shí)候?yàn)榈?,傳輸完畢后必須拉高,這樣FPGA可以判斷出SPI的傳輸起止?fàn)顟B(tài)。
FPGA的數(shù)據(jù)傳輸格式是16bit地址+16bit數(shù)據(jù)
對(duì)于讀16bit,實(shí)現(xiàn)如下
uint16_t spi_read(SPI_TypeDef* SPIx,uint32_t addr)
{
uint16_t value;
uint16_t spi_nss;
uint16_t add;
uint32_t level;
if(SPI1 == SPIx)
spi_nss = SPI1_PIN_NSS;
else if(SPI2 == SPIx)
spi_nss = SPI2_PIN_NSS;
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
GPIO_ResetBits(GPIOA, spi_nss);
SPI_I2S_SendData(SPIx, addr); //0xf014 》》 2
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPIx, 0x0);
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPIx);
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
GPIO_SetBits(GPIOA, spi_nss);
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
value = SPI_I2S_ReceiveData(SPIx);
return value;
}
寫函數(shù)
void spi_write(SPI_TypeDef* SPIx,uint32_t addr, uint16_t value)
{
uint16_t spi_nss;
uint32_t level;
if(SPI1 == SPIx)
spi_nss = SPI1_PIN_NSS;
else if(SPI2 == SPIx)
spi_nss = SPI2_PIN_NSS;
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
GPIO_ResetBits(GPIOA, spi_nss);
SPI_I2S_SendData(SPIx, addr);
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPIx, value);
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPIx);
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
GPIO_SetBits(GPIOA, spi_nss);
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPIx);
}
拿write函數(shù)舉例
只所以這么設(shè)計(jì)是因?yàn)?/p>
如果是函數(shù)一開始就將NSS腳拉低,然后再去send,如下
GPIO_ResetBits(GPIOA, spi_nss);
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPIx, addr);
這樣在CS拉低一段時(shí)間后(時(shí)間大概有16個(gè)時(shí)鐘周期),才有CLK,這樣延時(shí)就會(huì)降低SPI的傳輸效率
之前那種方式會(huì)在CS拉底后很快就有clk時(shí)鐘出來(lái)
之所以寫兩次再讀兩次而不是讀一次寫一次也是考慮到效率的問(wèn)題
如果先寫一次再讀一次,看波形每個(gè)數(shù)據(jù)之間有比較大的空隙是沒(méi)有clk的,就是說(shuō)在傳輸完一個(gè)數(shù)據(jù)后再
傳第二個(gè)會(huì)要等一段時(shí)間,這個(gè)對(duì)速度要求比較高的設(shè)備是不允許的
還有值得注意的是:
如果SPI是主模式,那么GPIO設(shè)置為
NSS是GPIO_Mode_Out_PP
CLK是GPIO_Mode_AF_PP
MOSI是GPIO_Mode_AF_PP
MISO是GPIO_Mode_IN_FLOATING
如果SPI是從模式,那么GPIO設(shè)置為
NSS是GPIO_Mode_Out_PP
CLK是GPIO_Mode_IN_FLOATING
MOSI是GPIO_Mode_IN_FLOATING
MISO是GPIO_Mode_AF_PP