DAM不能工作

2019-07-20 16:46发布

我们首先来看下面一段代码

[mw_shl_code=c,true]void mcpwm_adc_dma_init(void)
{
        DMA_InitTypeDef DMA_InitStructure;
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
                       
        // DMA for the ADC
        DMA_InitStructure.DMA_Channel = DMA_Channel_0;
        DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC_Value;
        DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC->CDR;
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
        DMA_InitStructure.DMA_BufferSize = 12;
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
        DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
        DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
        DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
        DMA_InitStructure.DMA_Priority = DMA_Priority_High;
        DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
        DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
        DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
        DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
        DMA_Init(DMA2_Stream4, &DMA_InitStructure);

        // DMA2_Stream0 enable
        DMA_Cmd(DMA2_Stream4, ENABLE);       
}[/mw_shl_code]
这段代码似乎没有什么不正常的,当你只调用一次,代码完全能够正常运行。
但是如果你再调用第二次的时候,你就会发现,DMA再也不工作了。
通过调试发现,在第二次运行的时候DMA_Init(DMA2_Stream4, &DMA_InitStructure);会使DMA使能失效,相当于DMA_Cmd(DMA2_Stream4, DISABLE);       
而接下来的DMA_Cmd(DMA2_Stream4, ENABLE);也并没有使DMA有效成功,DMA就会一直处于无效状态。


下面是修改过的程序
[mw_shl_code=c,true]void mcpwm_adc_dma_init(void)
{
        DMA_InitTypeDef DMA_InitStructure;
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
       
        DMA_Cmd(DMA2_Stream4, DISABLE);               
        // DMA for the ADC
        DMA_InitStructure.DMA_Channel = DMA_Channel_0;
        DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC_Value;
        DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC->CDR;
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
        DMA_InitStructure.DMA_BufferSize = 12;
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
        DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
        DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
        DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
        DMA_InitStructure.DMA_Priority = DMA_Priority_High;
        DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
        DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
        DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
        DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
        DMA_Init(DMA2_Stream4, &DMA_InitStructure);

        // DMA2_Stream0 enable
        DMA_Cmd(DMA2_Stream4, ENABLE);       
}[/mw_shl_code]

此程序第二次调用则DMA依然工作正常。这里采用的思路是先使DMA无效,在无效状态下配置DMA,即DMA_Init(DMA2_Stream4, &DMA_InitStructure);,然后再使DMA有效。
可见,在失效状态下配置DMA非常重要,对于其他外设,当然也推荐先失效外设,再配置外设。
0条回答

一周热门 更多>