SPI双机通讯,程序并不多,可实在找不出问题在哪里,才在此处求救!~~
主机是GD32F150的芯片,从机是GD103RG的芯片,双方用的都是SPI1,程序分别如下:
这是主机的程序,用的是GD的库,主机先等待接收,再发送;
int main()
{
usart1_init();
delay_init(72);
SpiInit();
led_test();
i=0;
while(1)
{
rxbuff[i] = spi_send(txbuff[i]);
i++;
if(i>SIZE)i=0;
printf("the data received are: %c \n",txbuff[i]);
delay_ms(10);
if(txbuff[i] != rxbuff[i])
{
printf("Eorr! data are not match! \n");
while(1);
}
}
}
uint16_t spi_send(uint8_t data)
{
while(SPI_I2S_GetBitState(SPI1, SPI_FLAG_RBNE)==RESET);
SPI_I2S_SendData(SPI1, txbuff[i] );
return SPI_I2S_ReceiveData(SPI1);
}
void SpiInit(void)
{ /* Initialization GPIO and SPI */
GPIO_InitPara GPIO_InitStructure;
SPI_InitPara SPI_InitStructure;
/* Enable Peripheral clock */
RCC_AHBPeriphClock_Enable( RCC_AHBPERIPH_GPIOA, ENABLE);
RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_SPI1,ENABLE);
/* Configure SPI1 pins: */
GPIO_PinAFConfig( GPIOA , GPIO_PINSOURCE5, GPIO_AF_0 );
GPIO_PinAFConfig( GPIOA , GPIO_PINSOURCE6, GPIO_AF_0 );
GPIO_PinAFConfig( GPIOA , GPIO_PINSOURCE7, GPIO_AF_0 );
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_5| GPIO_PIN_6 | GPIO_PIN_7;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* SPI1 configuration */
SPI_InitStructure.SPI_TransType = SPI_TRANSTYPE_FULLDUPLEX;
SPI_InitStructure.SPI_Mode = SPI_MODE_MASTER;
SPI_InitStructure.SPI_FrameFormat = SPI_FRAMEFORMAT_8BIT;
SPI_InitStructure.SPI_SCKPL = SPI_SCKPL_HIGH;//SPI_SCKPL_HIGH
SPI_InitStructure.SPI_SCKPH = SPI_SCKPH_2EDGE ;//SPI_SCKPH_2EDGE
SPI_InitStructure.SPI_SWNSSEN= SPI_SWNSS_SOFT;
SPI_InitStructure.SPI_PSC = SPI_PSC_8 ;
SPI_InitStructure.SPI_FirstBit = SPI_FIRSTBIT_MSB;
SPI_InitStructure.SPI_CRCPOL = 7;
SPI_Init(SPI1, &SPI_InitStructure);
/* Enable SPI AND SPI2 CRC calculation */
// SPI_CRC_Enable(SPI1, ENABLE);
/* Enable SPI1 */
SPI_Enable(SPI1, ENABLE);
}
以下是从机程序,用ST的库,从机先发送一个字节,再等待接收
int main(void)
{
LED_Init();
delay_init(72);
RCC_Configuration();
uart_init(115200);
GPIO_Configuration();
SPI_init();
i=0;
while(1)
{
SPI_I2S_SendData(SPI1, SPI_MASTER_Buffer_Tx[i]);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)==RESET);
SPI_SLAVE_Buffer_Rx[i] = SPI_I2S_ReceiveData(SPI1);
LED1 = !LED0;
delay_ms(200);
i++;
if(i>SIZE) i=0;
}
}
void RCC_Configuration(void)
{
RCC_AHBPeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*SPI1-Slave GPIO Init*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; //SF_PIN_SCK | SF_PIN_MISO | SF_PIN_MOSI;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* ÅäÖÃƬѡ¿ÚÏßΪÍÆÍìÊä³öģʽ */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
}
void SPI_init()
{
SPI_InitTypeDef SPI_InitStructure;
/*SPI1-Slave Init*/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //ÉèÖÃSPIµ¥Ïò»òÕßË«ÏòµÄÊý¾Ýģʽ:SPIÉèÖÃΪ˫ÏßË«ÏòÈ«Ë«¹¤
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; //ÉèÖÃSPI¹¤×÷ģʽ:ÉèÖÃΪÖ÷SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //ÉèÖÃSPIµÄÊý¾Ý´óС:SPI·¢ËͽÓÊÕ8λ֡½á¹¹
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //Ñ¡ÔñÁË´®ÐÐʱÖÓµÄÎÈ̬:ʱÖÓÐü¿Õ¸ß
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //Êý¾Ý²¶»ñÓÚµÚ¶þ¸öʱÖÓÑØ
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSSÐźÅÓÉÓ²¼þ£¨NSS¹Ü½Å£©»¹ÊÇÈí¼þ£¨Ê¹ÓÃSSI룩¹ÜÀí:ÄÚ²¿NSSÐźÅÓÐSSIλ¿ØÖÆ
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; //¶¨Ò岨ÌØÂÊÔ¤·ÖƵµÄÖµ:²¨ÌØÂÊÔ¤·ÖƵֵΪ256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //Ö¸¶¨Êý¾Ý´«Êä´ÓMSBλ»¹ÊÇLSBλ¿ªÊ¼:Êý¾Ý´«Êä´ÓMSBλ¿ªÊ¼
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRCÖµ¼ÆËãµÄ¶àÏîʽ
SPI_Init(SPI1, &SPI_InitStructure); //¸ù¾ÝSPI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèSPIx¼Ä´æÆ÷
SPI_Cmd(SPI1, ENABLE); //ʹÄÜSPIÍâÉè
//SPI1->DR = 0XFFff;
}
无论哪个先发送,另一方总是卡在等待接收的语句上,硬件连接应该没问题,双方的PA5,6,7分别连接PA5,6,7。
哪位大神能点醒一下,感激不尽!
|