我用一块F207的开发板做master,自己做的板子(F205)做slave,两机通过SPI通信。主机使用GPIOI的pin1、2、3,分别连接从机的GPIOB的pin13、14、15,也就是CLK-CLK,MISO-MISO,MOSI-MOSI。主机发送数据,从机可以正常收到。如果从机收到数据后,也发送数据,则两边收到的数据都不对了。比如,主机连续发送50个0xff,从机在这期间发送0xa0,主机收到都是0x83,0x0b等,从机收到的0xff变成了0x0b等。
接收使用中断方式,发送没有使用中断。
SPI初始化的代码如下,主从基本一样,只是一个是master,一个是slave
static void InitSPI(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/* Enable the SPI clock */
SPIx_CLK_INIT(SPIx_CLK, ENABLE);
/* Enable GPIO clocks */
RCC_AHB1PeriphClockCmd(SPIx_GPIO_CLK, ENABLE);
//RCC_AHB1PeriphClockCmd(SPIx_SCK_GPIO_CLK | SPIx_MISO_GPIO_CLK | SPIx_MOSI_GPIO_CLK, ENABLE);
/* Connect PC10 to SPI3_SCK */
GPIO_PinAFConfig(SPIx_GPIO_PORT, SPIx_SCK_SOURCE, SPIx_SCK_AF);
/* Connect PC11 to SPI1_MOSI */
GPIO_PinAFConfig(SPIx_GPIO_PORT, SPIx_MISO_SOURCE, SPIx_MISO_AF);
/* Connect PC12 to SPI1_MISO */
GPIO_PinAFConfig(SPIx_GPIO_PORT, SPIx_MOSI_SOURCE, SPIx_MOSI_AF);
/* Configure SPI1 pins as alternate function
* (No need to configure PA4 since NSS will be
* managed by software) */
GPIO_InitStructure.GPIO_Pin = SPIx_SCK_PIN | SPIx_MISO_PIN | SPIx_MOSI_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //GPIO_PuPd_UP
GPIO_Init(SPIx_GPIO_PORT, &GPIO_InitStructure);
/* SPI configuration */
SPI_I2S_DeInit(SPIx);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; // SPI_CPOL_High?
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //把数据锁定在第一个波形.才不会漏掉最高位 //SPI_CPHA_2Edge ?
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; // SPI_BaudRatePrescaler_256 ?
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //高位在前
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPIx, &SPI_InitStructure);
/* 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; // 2 ???
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the Rx buffer not empty interrupt */
SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_RXNE, ENABLE);
/* Enable the SPI peripheral */
SPI_Cmd(SPIx, ENABLE);
}
中断接收函数
void SPIx_IRQHANDLER(void)
{
uint8_t n;
/* SPI in Slave Receiver mode--------------------------------------- */
if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_RXNE) == SET)
{
RxBuffer[Rx_Idx++] = SPI_I2S_ReceiveData(SPIx);
if(Rx_Idx==BUFFERSIZE)Rx_Idx=0;
}
}
slave端在主循环中发送 SpiSendByte(0xa0); 函数如下:
uint8_t SpiSendByte(uint8_t ch)
{
while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPIx, ch);
for(int i=0;i<1000;i++){}
//while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET){}
return SPI_I2S_ReceiveData(SPIx);
}
调试时,先启动从机,从机会在 while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);停留,等待主机发送时钟过来。
然后启动主机,主机在主循环中发送50个0xff。结果,主机收到50个数据,从机也收到50个数据。但是,不幸地是,数据都是错的。泪奔啊。
请教香主,以及各位同道,这是咋回事???
谢谢
|