F103RCT6定时器TIM5-CH1 DMA无法传输

2019-07-21 03:54发布

求助!在用 stm32调试WS2812的时候,网上找的代码,用PA0口的TIM2-CH1,DMA方式输出PWM进行驱动,验证过可以成功。配置代码如下:
[mw_shl_code=c,true]void Timer2_init(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    DMA_InitTypeDef DMA_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    /* GPIOA Configuration: TIM2 Channel 1 as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    /* Compute the prescaler value */
    //PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Period = 90 - 1; // 800kHz
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    /* PWM1 Mode configuration: Channel1 */
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = 0;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC1Init(TIM2, &TIM_OCInitStructure);

    /* configure DMA */
    /* DMA clock enable */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

    /* DMA1 Channel6 Config */
    DMA_DeInit(DMA1_Channel2);

    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM2_CCR1_Address;        // physical address of Timer 3 CCR1
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)LED_BYTE_Buffer;                // this is the buffer memory
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;                                                // data shifted from memory to peripheral
    DMA_InitStructure.DMA_BufferSize = 42;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;                                        // automatically increase buffer index
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;                                                        // stop DMA feed after buffer size is reached
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

    DMA_Init(DMA1_Channel2, &DMA_InitStructure);DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE);

NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel2;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

    /* TIM3 CC1 DMA Request enable */
    TIM_DMACmd(TIM2, TIM_DMA_Update, ENABLE);
}
[/mw_shl_code]在主函数中调用的子程序如下:
[mw_shl_code=c,true]void WS2812_send(uint8_t (*color)[3], uint16_t len)
{

// 省略了逻辑处理的代码,主要是往数组中填充数据,和DMA无关

DMA_SetCurrDataCounter(DMA1_Channel2, buffersize);         // load number of bytes to be transferred
    DMA_Cmd(DMA1_Channel2, ENABLE);                         // enable DMA channel 6
    TIM_Cmd(TIM2, ENABLE);                                                 // enable Timer 3
}[/mw_shl_code]




在DMA的传输完成中断中,关闭DMA和定时器,中断服务函数如下:
[mw_shl_code=c,true]void DMA1_Channel2_IRQHandler()
{
    if(DMA_GetITStatus(DMA1_FLAG_TC2))
    {
        DMA_ClearFlag(DMA1_FLAG_GL2);
        TIM_Cmd(TIM2, DISABLE);         // disable Timer 2
        DMA_Cmd(DMA1_Channel2, DISABLE);
    }
}[/mw_shl_code]
以上代码经过测试,可以正常工作。
但是程序中还要用到USART3_TX的DMA,同样也是DMA1_Channel2,和上面的冲突了。

于是考虑到,用PA0引脚的TIM5-CH1产生PWM,这时候用到的是DMA2_Channel5.
于是将代码改成下面的样子:



配置代码如下:
[mw_shl_code=c,true]void LED_PWM_Init(void)
{
                TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    DMA_InitTypeDef DMA_InitStructure;
                NVIC_InitTypeDef NVIC_InitStructure;


    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    /* GPIOA Configuration: TIM2 Channel 1 as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
               

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
    /* Compute the prescaler value */
    //PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Period = 90 - 1; // 800kHz
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);

    /* PWM1 Mode configuration: Channel1 */
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = 0;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC1Init(TIM5, &TIM_OCInitStructure);
                /* TIM3 CC1 DMA Request enable */
    TIM_DMACmd(TIM5, TIM_DMA_Update, ENABLE);
    /* configure DMA */
    /* DMA clock enable */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);

    /* DMA1 Channel6 Config */
                DMA_Cmd(DMA1_Channel2, DISABLE);
    DMA_DeInit(DMA2_Channel5);
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM5_CCR1_Address;        // physical address of Timer 3 CCR1
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)LED_BYTE_Buffer;                // this is the buffer memory
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;                                                // data shifted from memory to peripheral
    DMA_InitStructure.DMA_BufferSize = 42;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;                                        // automatically increase buffer index
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;                                                        // stop DMA feed after buffer size is reached
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

    DMA_Init(DMA2_Channel5, &DMA_InitStructure);
               
                DMA_ITConfig(DMA2_Channel5, DMA_IT_TC, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel = DMA2_Channel4_5_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

   
}[/mw_shl_code]

在主函数中调用的子程序如下
[mw_shl_code=c,true]void WS2812_send(uint8_t (*color)[3], uint16_t len)
{

// 省略了逻辑处理的代码,主要是往数组中填充数据,和DMA无关

                DMA_SetCurrDataCounter(DMA2_Channel5, buffersize);         // load number of bytes to be transferred
    DMA_Cmd(DMA2_Channel5, ENABLE);                         // enable DMA channel 6
    TIM_Cmd(TIM5, ENABLE);                                                 // enable Timer 3
}[/mw_shl_code]


在DMA的传输完成中断中,关闭DMA和定时器,中断服务函数如下:
[mw_shl_code=c,true]
void DMA2_Channel4_5_IRQHandler()
{
    if(DMA_GetITStatus(DMA2_FLAG_TC5))
    {
        DMA_ClearFlag(DMA2_FLAG_GL5);
        TIM_Cmd(TIM5, DISABLE);         // disable Timer 3
        DMA_Cmd(DMA2_Channel5, DISABLE);
    }
}
[/mw_shl_code]



改完之后就不工作了!!
各位大佬帮忙看看那里出错了啊啊啊啊啊
先谢谢了
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。