STM32:DMA方式接收SPI总线数据,并按照协议进行处理

一、前言

为满足高速数据传输的要求,采用SPI总线。MCU端(STM32F072  Cortex-M0)接收CPU发送的SPI数据(数据18个字节为一包,起始包为0xAA,最后一包为CheckSum校验),接收完成后,将校验正确的数据分配给RF发送给接收端。

二、硬件电路

如下图所示,SPI部分使用SPI2即PB12 PB13 PB14 PB15

三、程序流程

3.1 SPI初始化

 SPI初始化为从模式,代码如下:

void BSP_SPI2_Init(void)
{	SPI_InitTypeDef  SPI_InitStructure;
		GPIO_InitTypeDef GPIO_InitStructure;
		RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); 
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
		/*!< Configure SPI_FLASH_SPI pins: NSS */	
		GPIO_InitStructure.GPIO_Pin    = GPIO_Pin_12;
		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_NOPULL;
		GPIO_Init(GPIOB, &GPIO_InitStructure); 
		/*!< Configure SPI_FLASH_SPI pins: SCK */	
		GPIO_InitStructure.GPIO_Pin    = GPIO_Pin_13;
		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_NOPULL;
		GPIO_Init(GPIOB, &GPIO_InitStructure); 
      /*!< Configure SPI_FLASH_SPI pins: MISO */
		GPIO_InitStructure.GPIO_Pin    = GPIO_Pin_14;
		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_NOPULL;
		GPIO_Init(GPIOB, &GPIO_InitStructure);
		 /*!< Configure SPI_FLASH_SPI pins: MOSI */
		GPIO_InitStructure.GPIO_Pin    = GPIO_Pin_15;
		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_NOPULL;
		GPIO_Init(GPIOB, &GPIO_InitStructure); 
		/*!< PinAFConfig */
		GPIO_PinAFConfig(GPIOB,GPIO_PinSource12,GPIO_AF_0);  
    GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_0);  
    GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_0);
    GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_0); 
    /* Deselect the FLASH: Chip Select high */
    /* SPI2 configuration */
    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_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;////
    SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;////
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init(SPI2, &SPI_InitStructure);
    SPI_RxFIFOThresholdConfig(SPI2, SPI_RxFIFOThreshold_QF);
    SPI_NSSInternalSoftwareConfig(SPI2, SPI_NSSInternalSoft_Reset);
    SPI_NSSPulseModeCmd(SPI2,DISABLE);
    //SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);
    /* Configure IT */
    /* Enable SPI2  */
		
    SPI_Cmd(SPI2, ENABLE);
		
		
		 
}


 
   

3.2 DMA初始化

查阅数据手册,SPI2_RX的DMA通道为4,由于需要持续从SPI中获取数据,DMA设置为Circle模式。由于这里SPI接收的数据长度固定为18,所以我们可以把BufferSize也设置为18,这样做的好处是我们不需要去查询DMA传输完成的标志,只需要去查询BUFFER中的数据是否有变化,一旦有变化从头到尾直接Load数据就可以了。当然这样处理只适用于固定长度的数据接收,如果DMA接收的数据不是固定长度的,则需要通过DMA的计数值CNTR这个寄存器的值来比较。
void SPI2_DMA_Init(void)
 {
  DMA_Cmd(DMA1_Channel4, DISABLE);
  DMA_DeInit(DMA1_Channel4); 
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  //DMA_InitStructure.DMA_FIFOMode
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(SPI2->DR));
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)DstBuffer;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = 18;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//DMA_Mode_Normal DMA_Mode_Circular
  DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel4, &DMA_InitStructure);
	/* Enable DMA1 Channel1 Transfer Complete interrupt */
  #if 0
	DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
  /* Enable DMA1 channel4 IRQ Channel */
  NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_5_6_7_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
	#endif
  SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE);//
  /* Enable DMA1 Channel4 transfer */
  DMA_Cmd(DMA1_Channel4, ENABLE);
 }

3.3 数据比较与更新

这部分内容的作用是,循环读取DMABuffer中的数据,如果其中的数据发送变化,表面有新的数据产生,将这个数据取出并进行协议处理,如果没有数据变化,则不进行任何处理。
 /*******************************************************************************
* @Function    : UpdateBuff
* @Description : Compare 2 strings , if different return 1 and Update to Target
* @Input       : buf1 buf2 size
* @Output      : None
* @Return      : None
* @Auth        : Solen           2016/8/30
*******************************************************************************/
 uint8_t UpdateBuff(uint8_t *Src,uint8_t *Taget,uint8_t Size)
 {
	 uint8_t Data_Updated = 0;
	  for(uint8_t i = 0;i

3.4 协议分析

根据3.3的结果,如果有新数据,则进行协议分析。即以AA为数据头,获取包数据。数据处理结束。

/*******************************************************************************
* @Function    : Packet_Align
* @Description : Mainly Used for DBG to certificate SPI - DMA
* @Input       : None
* @Output      : None
* @Return      : None
* @Auth        : Solen           2016/8/30
*******************************************************************************/
 void Packet_Align(void)
 {
	 static uint8_t Packet_Index = 0;
	 static uint8_t Find_Header = 0;
    for(uint8_t i = 0; i < 18 ; i++)
	 {
		   if(Find_Header == 0) //Need to Find Headerk
			 {
				 if(TgtBuffer[i] == 0xAA)
				 {
					 Find_Header = 1;
					 Packet_Index = 0;
					 ThrDimen_Packet_Data[Packet_Index] = 0xAA;
				 }
			 }
			 else
			 {
					Packet_Index++;
				  ThrDimen_Packet_Data[Packet_Index] = TgtBuffer[i];
				  if(Packet_Index >=17)
					{
						Find_Header = 0;
						Data_Finished = 1;
					}
			 }
	 }
 }




你可能感兴趣的:(STM32)