AnsweredAssumed Answered

Timer Output Compare DMA

Question asked by sires on Feb 19, 2014
Latest reply on Feb 19, 2014 by Clive One
Good morning, i have to drive WS2812 led wich require a square wave with this timing

WS2812_Timing.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


Outcomes