如何配置STM32f405 SPI以使用DMA传输数据?

时间:2016-08-04 15:23:59

标签: arm embedded stm32 stm32f4

我有一台STM32f405,其任务是通过SPI传输数据,并通过DMA节省处理器时间。使用的SPI是SPI1,引脚PA4至PA7。我已经从DMA2通道3中选择了DMA的第3个流。想法是激活CS信号并将一些数据存储在存储器中,然后由DMA自动传输,然后再进行传输。完成DMA应触发中断处理程序以停用CS。这是代码:

static void SPI_Config(void) {
    GPIO_InitTypeDef GPIO_InitStructure;
    SPI_InitTypeDef  SPI_InitStructure;
    DMA_InitTypeDef DMA_Init_Structure;
    NVIC_InitTypeDef NVIC_InitStructure;
    /* Enable the SPI clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
    /* Enable GPIO clocks */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    /* Enable DMA clock */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
    /* SPI GPIO Configuration --------------------------------------------------*/
  /* GPIO Deinitialisation */
    GPIO_DeInit(GPIOA);
    /* Connect SPI pins to AF5 */ 
//  GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_SPI1); //SS
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1); //SCK   
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1); //MISO
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1); //MOSI

    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_InitStructure.GPIO_Pin =  GPIO_Pin_5; //SCK
  GPIO_Init(GPIOA, &GPIO_InitStructure); 

    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6; //MISO
  GPIO_Init(GPIOA, &GPIO_InitStructure); 

    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7; //MOSI
  GPIO_Init(GPIOA, &GPIO_InitStructure); 

    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4; //SS
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure); 

    //DMA Globul Interrupt
    NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

    //DMA Configuration
    DMA_DeInit(DMA2_Stream3);
    DMA_Cmd(DMA2_Stream3, DISABLE);
    while (DMA1_Stream0->CR & DMA_SxCR_EN);
    DMA_Init_Structure.DMA_BufferSize = 0;
    DMA_Init_Structure.DMA_Channel = DMA_Channel_3; 
    DMA_Init_Structure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
    DMA_Init_Structure.DMA_FIFOMode = DMA_FIFOMode_Disable;
    DMA_Init_Structure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
    DMA_Init_Structure.DMA_Memory0BaseAddr = (uint32_t)(&spi_tx_val);
    DMA_Init_Structure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
    DMA_Init_Structure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_Init_Structure.DMA_MemoryInc = DMA_MemoryInc_Disable;
    DMA_Init_Structure.DMA_Mode = DMA_Mode_Circular;
    DMA_Init_Structure.DMA_PeripheralBaseAddr = (uint32_t) (&(SPI1->DR));
    DMA_Init_Structure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
    DMA_Init_Structure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_Init_Structure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_Init_Structure.DMA_Priority = DMA_Priority_High;
    DMA_Init(DMA2_Stream3,&DMA_Init_Structure);

    //SPI Configuration
    SPI_I2S_DeInit(SPI1);
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //AD5620 doku page 18 falling edge of SCLK
    SPI_InitStructure.SPI_CRCPolynomial = 0; //x_8+x_2+x_1+1 in python hex(2**8+2**2+2+1)
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b; //AD5620 input register is 16 bit
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_Init(SPI1, &SPI_InitStructure);




}

int8_t Analog_Out_Config(uint32_t target_reg_val) {
    uint16_t power_on_status;
    target_reg_val = target_reg_val;
    SPI_Config();
//  SPI_Cmd(SPI1, ENABLE);
//  power_on_status=PowerOn_AD5750_OutDriver();
//  if(power_on_status) {
        //enable dma interrupt
//      SPI_Cmd(SPI1, DISABLE);
        DMA_ITConfig(DMA2_Stream3,DMA_IT_TC,ENABLE);
        DMA_ClearFlag(DMA2_Stream3, DMA_FLAG_FEIF3|DMA_FLAG_DMEIF3|DMA_FLAG_TEIF3|DMA_FLAG_HTIF3|DMA_FLAG_TCIF3);
        DMA_Cmd(DMA2_Stream3, ENABLE);
      SPI_I2S_DMACmd(SPI1,SPI_I2S_DMAReq_Tx, ENABLE);
        SPI_Cmd(SPI1, ENABLE);

        return power_on_status&0x07;
//  }else {
//      return -1;
//  }
}
void Analog_Output(uint32_t measured_reg_val) {
    val=0x7ff;
    ACTIVATE_CS_DAC();
    spi_tx_val=val; 
}

void DMA2_Stream3_IRQHandler(void) {
    if(DMA_GetITStatus(DMA2_Stream3,DMA_IT_TCIF3)!=RESET) {
        DMA_ClearITPendingBit(DMA2_Stream3,DMA_IT_TCIF0|DMA_IT_HTIF0);
        DEACTIVATE_CS_DAC();

    }
}

int main(void)
{
    target_reg_val=14;
    measured_reg_val=12;
    Analog_Out_Config(target_reg_val);
  while (1)
  {
        for(val=-target_reg_val;val<target_reg_val;val++) {
            Analog_Output(val);
            for(i=0;i<1000;i++);
        }

  }
}

使用调试器,我发现DMA2_Stream3_IRQHandler永远不会被激活。根据参考手册,当SPI_DR寄存器的TXE标志为1时,DMA应该传输数据。还设置了SPI_CR2的标志TXDMAEN。我也检查了DMA S3CR寄存器,并设置了标志TCIE和EN。另外函数DMA2_Stream3_IRQHandler对main函数可见。仍然没有激活DMA2_Stream3_IRQHandler。

UPDATE :当我手动复位DMA2_S3CR寄存器的EN位时,会触发DMA2_Stream3_IRQHandler。根据参考手册,该位由硬件清除:

  • 在传输的DMA端(准备好配置的流)
  • 如果在AHB主总线上发生传输错误
  • 当内存AHB端口上的FIFO阈值与之不兼容时 爆发的大小

我也改变了SPI_Config和Analog_Out_Config,但仍然没有调试器干扰DMA2_Stream3_IRQHandler永远不会被触发。看起来DMA没有触发传输,并且由于某种原因无法终止它。如何判断DMA是否触发任何传输?

1 个答案:

答案 0 :(得分:0)

您的DMA配置结构设置为DMA_ModeDMA_NORMAL。它可能应设置为DMA_PFCTRL,这是外围流量控制。这将导致DMA等待来自外设(已配置)的信号而不是连续运行。但是,我希望,如果没有这个设置,你会得到一个或两个字出来的SPI总线,因为SPI_DR应该在连续转发的DMA传输中获得所有内存设置(因为它正在转移出来)第一个字)。

要检查的另一件事是您的外围设备(SPI1DMA2)都没有重置。我看到你禁用了时钟,但是我不记得ST是否还在该呼叫中使外设没有复位。

注意:我使用STM32F2xx DMA外设作为参考,但STM32F4xx外设往往是超集。看起来您使用的是不同版本的ST外设库,而不是我用作参考的版本。