cancel
Showing results for 
Search instead for 
Did you mean: 

Two STM32F407 carry out SPI master-slave communication. Error occurs when the slave receives and sends data.

Kevin.YIN
Associate II
SPI Master code
-------------------------------
u8 Transmit_Buffer[SIZE];
u8 Receive_Buffer[SIZE];
uint16_t timebase_1us = 0;
u16 i = 0;
u16 a = 0;
int main()
{
	delay_init(168);	//168MHZ	  
	SPI1_Init();		      
	for(a=0;a<1024;a++)
  {
		Transmit_Buffer[a] = a;
	}
	while(1)
	{		
		SPI1_ReadWriteByte();
		delay_us(250);
	}
}
 
void SPI1_Init(void)
{	 
        GPIO_InitTypeDef  GPIO_InitStructure;
        SPI_InitTypeDef  SPI_InitStructure;
 
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
 
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;	
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_SPI1); 
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_SPI1); 
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_SPI1); 
 
	//这里�?�针对SPI�?��?始化
	RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,ENABLE);
	RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,DISABLE);
 
	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;  
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;		
	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_16;		
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;	
	SPI_InitStructure.SPI_CRCPolynomial = 7;	
	SPI_Init(SPI1, &SPI_InitStructure); 
	SPI_Cmd(SPI1, ENABLE); 
}   
 
 
void SPI1_ReadWriteByte(void)
{		 			 
   
  uint16_t count;
  for (count = 0; count < SIZE; count++)
  {
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
    SPI_I2S_SendData(SPI1, Transmit_Buffer[count]);
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
    Receive_Buffer[count] = SPI_I2S_ReceiveData(SPI1);
  }	
 		    
}
 
 
 
SPI Slave code
-------------------------------
 
 #define SIZE 1024
uint8_t TxBuffer[SIZE];
uint8_t RxBuffer[SIZE];
static uint16_t tx_index = 0;
uint16_t i = 0 ;
uint8_t data =0;
 
int main()
{
	delay_init(168);	//168MHZ
	SPI1_Init();			
	while (1)
	{	
        }
}
 
 
void SPI1_Init(void)
{	 
  GPIO_InitTypeDef  GPIO_InitStructure;
  SPI_InitTypeDef  SPI_InitStructure;
  NVIC_InitTypeDef NVIC_Initstruct;
	
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
	
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_SPI1); 
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_SPI1); 
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_SPI1); 
	
  SPI_I2S_ITConfig(SPI1,SPI_I2S_IT_RXNE,ENABLE),
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  NVIC_Initstruct.NVIC_IRQChannel = SPI1_IRQn;
  NVIC_Initstruct.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Initstruct.NVIC_IRQChannelPreemptionPriority =1;
  NVIC_Initstruct.NVIC_IRQChannelSubPriority =1;
  NVIC_Init(&NVIC_Initstruct);
  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_High;		
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;	
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;		
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;		
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;	
  SPI_InitStructure.SPI_CRCPolynomial = 7;	
  SPI_Init(SPI1, &SPI_InitStructure);  
		
  SPI_Cmd(SPI1, ENABLE); 
}   
 
 
 
extern uint8_t TxBuffer[SIZE];
extern uint8_t RxBuffer[SIZE];
extern uint16_t i;
 
static uint16_t rx_index = 0;
static uint16_t tx_index = 0;
 
void SPI1_IRQHandler(void)
{
    if (SPI_I2S_GetITStatus(SPI1, SPI_I2S_IT_RXNE) == SET)
    {
         RxBuffer[rx_index] = SPI_I2S_ReceiveData(SPI1);
       	 rx_index++;
	/* If all data received, copy to TX buffer and start transmission */
       	if (rx_index >= SIZE)
	{
	   /* Reset buffer indices and start transmission */
	   rx_index = 0;
	    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET){}   
	    SPI_I2S_SendData(SPI1, TxBuffer[tx_index++]);				
	   /* Enable TXE interrupt to continue transmission */
	    SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, ENABLE);		
        }	
     }
   if (SPI_I2S_GetITStatus(SPI1, SPI_I2S_IT_TXE) == SET)
   {
      SPI_I2S_SendData(SPI1, TxBuffer[tx_index++]);
    
      /* Disable TXE interrupt if all data is transmitted */
      if (tx_index >= SIZE)
     {
       SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, DISABLE);
       tx_index =0;
     }
  }
}
 
 
 

Data needs to be sent every 250us.I don't know if it is a logic problem for receiving and sending codes from the slave machine. The main frequency set is 168M and SPI frequency division coefficient is 256 frequency division(My client needs an SPI rate of 4.8M/S, I tried to change the overfrequency coefficient, but it couldn't be achieved)

thank you for your help in advance!

0 REPLIES 0