The STM32 SPI and FPGA communication
2015-08-25 04:16
344 查看
The STM32 SPI and FPGA communication
STM32 spi bus communicationSPI bus in the study, the protocol and hardware description is not to say that the four-wire, including clock, chip select, receive, send
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; / / full-duplex SPI_InitStructure.SPI_Mode = SPI_Mode_Master; / / master mode SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b; / / 16bit width 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; / / high byte first SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init (SPIx, & SPI_InitStructure); SPI_Cmd (SPIx, ENABLE);
SPI has no hardware control CS, can only be controlled by the software is by NSS the external GPIO to control.
Like my projects STM32 communication with the FPGA, the FPGA SPI In this state as a master of the STM32,
CS transmission data is low after the transmission must be pulled so that the FPGA can be judged SPI Transfer start and end state.
FPGA data transfer format is 16bit address +16 bit data for read 16bit
uint16_t spi_read (SPI_TypeDef * SPIx, uint32_t addr) { uint16_t value; The 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); the value = SPI_I2S_ReceiveData (SPIx); return value; } Write function void spi_write (SPI_TypeDef * SPIx, uint32_t addr, uint16_t value) { The 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); }
Take the write function example only so many design
because if it is the beginning of the function will be the NSS pin is pulled low, and then go Send as follows
GPIO_ResetBits (GPIOA, spi_nss);
while (SPI_I2S_GetFlagStatus (SPIx, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData (SPIx, addr);
This CS low after a period of time (the time there are about 16 clock cycles),
only CLK, this delay will reduce the transmission efficiency of the SPI
That way before CS Euphrates will soon have clk clock out
The reason why I wrote twice read it twice instead of reading a written also taking into account the efficiency problem
If you write once read it again to see a relatively large gap between each data of the waveform is not clk,
that will have to wait a period of time, then transmit a data require a relatively high speed The device is not allowed
It is worth noting that:
If the SPI master mode, the GPIO is set to
NSS is GPIO_Mode_Out_PP
CLK is GPIO_Mode_AF_PP
MOSI is GPIO_Mode_AF_PP
MISO is GPIO_Mode_IN_FLOATING
If the SPI Slave mode, the GPIO is set to
NSS is GPIO_Mode_Out_PP
CLK is GPIO_Mode_IN_FLOATING
MOSI is GPIO_Mode_IN_FLOATING
MISO is GPIO_Mode_AF_PP
相关文章推荐
- servlet jsp filter listener el表达式 tag file
- HOWTO: Use STM32 SPI half duplex mode
- 当tomcat已启动,但打不开tomcat主页
- 关于STM32 SPI NSS的讨论
- OpenGL中投影矩阵的推导
- [poj2778 DNA Sequence]AC自动机,矩阵快速幂
- hdu 5420 Victor and Proposition(强连通+线段树建图)
- Ubuntu Server配置wireless连接WPA2网络(interfaces)
- 连连看游戏的实现
- php大力力 [014节] 八杆子打不着的非技术文章,哈哈
- Android Overview Screen -- 概览界面
- Unity3D NGUI屏幕摇杆
- 子网掩码与IP地址之间的关系
- package.path和package.cpath
- Open JTAG Project
- 设置某一个Activity为单例模式无法接收到onActivityResult
- What is OpenOCD?
- OpenOCD Debug Adapter Configuration
- HDU 2523 hash排序
- OpenOCD 0.9.0 release