本周调了一周的SPI通信,今日有了一些进展,通过自发自收实现了方案的验证。
SPI的配置函数如下
void SPI_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/* Peripheral Clock Enable -------------------------------------------------*/
/* Enable the SPI clock */
SPIx_CLK_INIT(SPIx_CLK, ENABLE);
/* Enable GPIO clocks */
RCC_AHB1PeriphClockCmd(SPIx_SCK_GPIO_CLK | SPIx_MISO_GPIO_CLK | SPIx_MOSI_GPIO_CLK, ENABLE);
/* SPI GPIO Configuration --------------------------------------------------*/
/* Connect SPI pins to AF5 */
GPIO_PinAFConfig(SPIx_SCK_GPIO_PORT, SPIx_SCK_SOURCE, SPIx_SCK_AF);
GPIO_PinAFConfig(SPIx_MOSI_GPIO_PORT, SPIx_MOSI_SOURCE, SPIx_MOSI_AF);
GPIO_PinAFConfig(SPIx_MISO_GPIO_PORT, SPIx_MISO_SOURCE, SPIx_MISO_AF);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
/* SPI SCK pin configuration */
GPIO_InitStructure.GPIO_Pin = SPIx_SCK_PIN;
GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStructure);
/* SPI MOSI pin configuration */
GPIO_InitStructure.GPIO_Pin = SPIx_MOSI_PIN;
GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStructure);
/* SPI MISO pin configuration */
GPIO_InitStructure.GPIO_Pin = SPIx_MISO_PIN;
GPIO_Init(SPIx_MISO_GPIO_PORT,&GPIO_InitStructure);
/* SPI configuration -------------------------------------------------------*/
SPI_I2S_DeInit(SPIx);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPIx,&SPI_InitStructure);
SPI_Cmd(SPIx, ENABLE);
/* Configure the Priority Group to 1 bit */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* Configure the SPI interrupt priority */
NVIC_InitStructure.NVIC_IRQChannel = SPIx_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
SPI.H 的头文件的宏定义
/* SPIx Communication boards Interface */
#define SPIx SPI2
#define SPIx_CLK RCC_APB1Periph_SPI2
#define SPIx_CLK_INIT RCC_APB1PeriphClockCmd
#define SPIx_IRQn SPI2_IRQn
#define SPIx_IRQHANDLER SPI2_IRQHandler
#define SPIx_SCK_PIN GPIO_Pin_1
#define SPIx_SCK_GPIO_PORT GPIOI
#define SPIx_SCK_GPIO_CLK RCC_AHB1Periph_GPIOI
#define SPIx_SCK_SOURCE GPIO_PinSource1
#define SPIx_SCK_AF GPIO_AF_SPI2
#define SPIx_MISO_PIN GPIO_Pin_2
#define SPIx_MISO_GPIO_PORT GPIOI
#define SPIx_MISO_GPIO_CLK RCC_AHB1Periph_GPIOI
#define SPIx_MISO_SOURCE GPIO_PinSource2
#define SPIx_MISO_AF GPIO_AF_SPI2
#define SPIx_MOSI_PIN GPIO_Pin_3
#define SPIx_MOSI_GPIO_PORT GPIOI
#define SPIx_MOSI_GPIO_CLK RCC_AHB1Periph_GPIOI
#define SPIx_MOSI_SOURCE GPIO_PinSource3
#define SPIx_MOSI_AF GPIO_AF_SPI2
#define TXBUFFERSIZE (countof(TxBuffer) - 1)
#define RXBUFFERSIZE TXBUFFERSIZE
注意
两个地方:
SPI2的引脚如果被其他引脚复用,应关闭其相关设备。
MISO和MOSI两个引脚要关闭。
不建议打开SPI的接收中断。