STM32F10XX中SPI的DMA发送数据

参考资料:

http://blog.csdn.net/jdh99/article/details/7603029

http://www.openedv.com/posts/list/3159.htm

    上面提到的两篇博文比较详细深刻的说明了DMA的工作方式以及SPI的DMA传输方式的特点。结合对Stm32F103VET6中SPI的DMA传输方式的配置和学习谈谈感受,在看下面的内容之前请先看上面的两篇参考博文,这里就不在说明。

    要使用SPI的DMA功能,首先配置好SPI外设,这里以SPI1为例子。下面的代码初始化了SPI1对应的GPIO以及SPI1工作的模式。

void SpiCC3000Init(void)
{
     SPI_InitTypeDef   SPI_InitStructure;
    GPIO_InitTypeDef  GPIO_InitStructure;
  /*!< Disable SPI */
  SPI_Cmd(SPI_USED, DISABLE);
  
  /*!< DeInitializes the SPI */
  SPI_I2S_DeInit(SPI_USED);
  
  /*!< SPI Periph clock disable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 , DISABLE); 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 , DISABLE);   
    /*Enable SPI2 Clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
  RCC_APB2PeriphClockCmd(    RCC_APB2Periph_GPIOB,    ENABLE);
    /*Enable SPI1 Clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 |RCC_APB2Periph_GPIOA,ENABLE);
    /*Config  SCLK and MOSI */
    GPIO_InitStructure.GPIO_Pin = SPI_CLK_PIN| SPI_MOSI_PIN;// 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//AF_PP
    GPIO_Init(SPI_PORT, &GPIO_InitStructure);
  /*!< Configure SPI pins: MISO */
  GPIO_InitStructure.GPIO_Pin = SPI_MISO_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_Init(SPI_PORT, &GPIO_InitStructure);
    /*!< Configure SPI pins: CS output high */
    GPIO_InitStructure.GPIO_Pin = SPI_CS_PIN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(SPI_PORT, &GPIO_InitStructure);
    GPIO_SetBits(SPI_PORT,SPI_CS_PIN);
    
  /*!< CC3000 SPI Init */
  SPI_StructInit(&SPI_InitStructure);
  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_Low;  
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;/* The buadrate is a fraction of the 72MHz clock*/
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPI_USED, &SPI_InitStructure);        
  SPI_SSOutputCmd(SPI_USED,ENABLE);//NSS(CS) 
  SPI_Cmd(SPI_USED, ENABLE);
  
  /*Configure SPI for DMA Operation*/
#if defined(ENABLE_SPI_DMA)
  SpiCC3000DMAInit(); 
#else
  SpiRxInterruptClkInit();
#endif
}
    在配置好SPI1以后,配置DMA1功能(选择SPI1的Tx连接到DMA上),因为SPI1对应的DMA功能由DMA1来实现。  下面函数中的DMA时钟将在调用它的函数中开启。
void C3000_DMA_Config(SPI_DMADirection_TypeDef Direction, uint8_t* buffer, uint16_t NumData)
{
    
#if defined(ENABLE_SPI_DMA)
  DMA_InitTypeDef DMA_InitStructure;
  /* Initialize the DMA_PeripheralBaseAddr member */
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t )&SPI1->DR; //SPI_DR_BASE; // 
    //DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t )&SPI2->DR; //SPI_DR_BASE; // 
  /* Initialize the DMA_MemoryBaseAddr member */
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)buffer;
   /* Initialize the DMA_PeripheralInc member */
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  /* Initialize the DMA_MemoryInc member */
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  /* Initialize the DMA_PeripheralDataSize member */
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  /* Initialize the DMA_MemoryDataSize member */
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  /* Initialize the DMA_Mode member */
    //yichuan
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
//DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  /* Initialize the DMA_Priority member */
  DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  /* Initialize the DMA_M2M member */
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  
  /* If using DMA for Reception */
  if (Direction == SPI_DMA_RX)
  {
    /* Initialize the DMA_DIR member */
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
    
    /* Initialize the DMA_BufferSize member */
    DMA_InitStructure.DMA_BufferSize = NumData;
    
    DMA_DeInit(SPI_DMA_RX_CHANNEL);
    
    DMA_Init(SPI_DMA_RX_CHANNEL, &DMA_InitStructure);
  }
   /* If using DMA for Transmission */
  else if (Direction == SPI_DMA_TX)
  { 
    /* Initialize the DMA_DIR member */
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
    
    /* Initialize the DMA_BufferSize member */
    DMA_InitStructure.DMA_BufferSize = NumData;
    
    DMA_DeInit(SPI_DMA_TX_CHANNEL);
 
    DMA_Init(SPI_DMA_TX_CHANNEL, &DMA_InitStructure);
  }
#endif
}
   配置好DMA1和SPI1后,要做的事情就是把二者联合起来,且配置DMA1的发送完成中断。
void SpiCC3000DMAInit(void)

    
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
 
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); 
  DMA_DeInit(SPI_DMA_RX_CHANNEL);
  DMA_DeInit(SPI_DMA_TX_CHANNEL);
 
  /* Configure and enable SPI DMA TX Channel interrupt */ 
 // NVIC_RxInt_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn; //SPI2 TX
  NVIC_RxInt_InitStructure.NVIC_IRQChannel = DMA1_Channel3_IRQn;//SPI1 TX // not config the Rx channel
  NVIC_RxInt_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_RxInt_InitStructure.NVIC_IRQChannelSubPriority = 0;//0
  NVIC_RxInt_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_RxInt_InitStructure);
 
 
  /* Configure DMA Peripheral but don't send data*/
  C3000_DMA_Config(SPI_DMA_RX, (uint8_t*)wlan_rx_buffer,0); 
 //C3000_DMA_Config(SPI_DMA_TX, (uint8_t*)wlan_tx_buffer,0);    
  C3000_DMA_Config(SPI_DMA_TX, (uint8_t*)wlan_tx_buffer,1700);  //buffer is 1700
            
 
 
    /* Enable SPI DMA request */
  SPI_I2S_DMACmd(SPI_USED,SPI_I2S_DMAReq_Tx, ENABLE);
  SPI_I2S_DMACmd(SPI_USED,SPI_I2S_DMAReq_Rx, ENABLE);
  /* Enable the DMA Channels Interrupts */
  /*It should be put after the DMA config  */
  DMA_ITConfig(SPI_DMA_TX_CHANNEL, DMA_IT_TC, ENABLE); 
 
  /* Enable DMA RX Channel */
  DMA_Cmd(SPI_DMA_RX_CHANNEL, ENABLE);  
    /*Note: Enable the SPI_DMA Channel,it begin SPI translate*/
  /* Enable DMA TX Channel ,begin the dma translate*/  
  DMA_Cmd(SPI_DMA_TX_CHANNEL, ENABLE); 
}
  这里要强调的是:使能DMA中断标志的语句DMA_ITConfig(SPI_DMA_TX_CHANNEL, DMA_IT_TC, ENABLE); 应当放到DMA1配置完以后,不然发送数据完成以后,不会进入中断处理函数,就是相当于发送完成后产生中断的这个功能并没有配置成功,如果放到DMA1配置前面。下面的图片是参考手册中关于DMA的配置过程,其中中断的使能在第六步,所以应当在DMA初始化完成后,再使能中断功能。

 

    接下来就是中断处理函数了,响应传输完成中断的产生

void DMA1_Channel3_IRQHandler(void)
{
    if(DMA_GetITStatus(DMA1_IT_TC3)==SET)
    {
            GPIO_InitTypeDef GPIO_InitStructure;
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
            GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
            GPIO_Init(GPIOC, &GPIO_InitStructure);
            GPIO_ResetBits(GPIOC,GPIO_Pin_6);
 
    }
   DMA_ClearITPendingBit(DMA1_IT_TC3);
 
}
最后启动DMA传输是通过DMA_Cmd(SPI_DMA_TX_CHANNEL, ENABLE) 这句话实现,当DMA通道被使能以后,它就自动开始传输数据,而不影响CPU其干其它事情,直到DMA传输完数据产生中断时,CPU才会去处理中断函数。

 

你可能感兴趣的:(STM32)