STM32 SPI总线

来源:互联网 发布:软件安全检测 编辑:程序博客网 时间:2024/05/29 18:56

数据手册关于SPI的介绍

2.3.18 Serial peripheral interface (SPI)
Up to two SPIs are able to communicate up to 18 Mbits/s in slave and master modes in fullduplexand simplex communication modes. The 3-bit prescaler gives 8 master modefrequencies and the frame is configurable to 8 bits or 16 bits. The hardware CRCgeneration/verification supports basic SD Card/MMC modes.
Both SPIs can be served by the DMA controller

=======================================================

关于SPI配置的简单例子

/* SPI1 configuration ------------------------------------------------------*/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
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_8;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);

/* Enable SPI1 CRC calculation */
SPI_CalculateCRC(SPI1, ENABLE);

/* Enable SPI1 */
SPI_Cmd(SPI1, ENABLE);

发送函数

/* Wait for SPI1 Tx buffer empty */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
/* Send SPI1 data */
SPI_I2S_SendData(SPI1, SPI1_Buffer_Tx[TxIdx++]);

接收函数

/* Wait for SPI1 data reception */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
/* Read SPI1 received data */
SPI1_Buffer_Rx[RxIdx++] = SPI_I2S_ReceiveData(SPI1);

原创粉丝点击