cancel
Showing results for 
Search instead for 
Did you mean: 

Timer Output Compare DMA

andiob
Associate II
Posted on February 19, 2014 at 13:50

Good morning, i have to drive WS2812 led wich require a square wave with this timing

0690X0000060563QAA.png

T0H = 0.35 us, T1H = 0.70 us and TH+TL = 1.25 us.

I set up timer4 to generate square wave on channel 2 and i'im planning to use dma to copy to the TIM4->CCR2 the time value for te current bit from a prepoulated array. Here is my current code

GPIO_InitTypeDef GPIO_InitStructure;

    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

    TIM_OCInitTypeDef  TIM_OCInitStructure;

    DMA_InitTypeDef DMA_InitStructure;

    

    //Enable TIM4,GPIOD,DMA2 Clock

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);

    

    

  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_13;

    GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);

    GPIO_Init(GPIOD, &GPIO_InitStructure);

    

    TIM_OCStructInit(&TIM_OCInitStructure);

    

    TIM_TimeBaseStructure.TIM_Prescaler = 0;

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

    TIM_TimeBaseStructure.TIM_Period = RGB_PULSE_PERIOD;    

    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

    

    /* PWM1 Mode configuration: Channel2 */

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = 1;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

    TIM_OC2Init(TIM4, &TIM_OCInitStructure);

    

    /* DMA2 Channel 0 Config */

    DMA_DeInit(DMA2_Stream0);

    

    DMA_InitStructure.DMA_Channel = DMA_Channel_0;  

  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&TIM4->CCR2;//(uint32_t)0x40000838;

    DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Ch1Buffer;

  DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;

  DMA_InitStructure.DMA_BufferSize = RGB_BUFFER_SIZE;

  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_Normal;

  DMA_InitStructure.DMA_Priority = DMA_Priority_High;

  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         

  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;

  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;

  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;

    

    DMA_Init(DMA2_Stream0,&DMA_InitStructure);

    

    TIM_DMACmd(TIM4, TIM_DMA_CC2, ENABLE);

followed by the call of this function

void RgbSendFrame(uint8_t Channel, uint8_t* Buffer, uint16_t Size)

{

     uint8_t i,j,bit;

     bit = 0x80;

     j = 0;

     if(Channel & 0x01)

     {        

        while(Size)

        {

            for (i = 0; i < 8; i++)

            {

                if(*Buffer & bit)

                {

                    Ch1Buffer[j] = RGB_PULSE_1;

                }else

                {

                    Ch1Buffer[j] = RGB_PULSE_0;

                }

                j++;

                bit >>= 1;

            }

            

            bit = 0x80;

            Buffer++;

            Size--;

        }

        

        while(j < RGB_BUFFER_SIZE)

        {

            Ch1Buffer[j] = 0;

            j++;

        }

        

        TIM4->CCR2 = 1;

    

    

        DMA_SetCurrDataCounter(DMA2_Stream0, RGB_BUFFER_SIZE);

        DMA_Cmd(DMA2_Stream0,ENABLE);

        TIM_Cmd(TIM4, ENABLE);

        LedOn(2);

        while(DMA_GetFlagStatus(DMA2_Stream0,DMA_FLAG_TCIF0) == RESET);

        LedOff(2);

        TIM_Cmd(TIM4, DISABLE);

        DMA_Cmd(DMA2_Stream0, DISABLE);

        DMA_ClearFlag(DMA2_Stream0,DMA_FLAG_TCIF0);

        

     }//Fine Canale1

}

Nothing works, the code get stuck while waiting for the dma to complete transfer. Some help would be great

#rtfm
3 REPLIES 3
chen
Associate II
Posted on February 19, 2014 at 14:03

Have you tried writing to TIM4-CCR2 without DMA?

andiob
Associate II
Posted on February 19, 2014 at 14:09

I'm able to write manually to the TIM4->CCR2, i also tryed with captur compare interrupt but i wasen't able to mantain the time schedule

Posted on February 19, 2014 at 16:27

Going to assume an STM32F4 as you haven't been specific.

Not sure where you pulled the DMA2 Channel 0 Stream 0 from, but you really should review the reference manual instead of picking random associations out of the air.

TIM4_UP DMA1 Channel 2 Stream 6 would be my preference

TIM4_CH1 - DMA1 Channel 2 Stream 0

TIM4_CH2 - DMA1 Channel 2 Stream 3

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..