AnsweredAssumed Answered

Timer 2 PWM and DMA not working

Question asked by Mahjoub.Youssif on May 27, 2015
Latest reply on Jun 1, 2015 by Clive One
Hello,

I am having issues with my code to drive ws2812 LEDs with my stm32f103c8t6. 
my current code uses TIM2 Channel 3, DMA 1 Channel 1, and GPIO PA2. This first code is the code i'm trying to modify to get working, but i see absolutely nothing on my scope. Just a flat line. The second code from someone else I do get something on my scope. Can anybody seen anything wrong with my code?

ws2812 datasheet: https://cdn.sparkfun.com/datasheets/Components/LED/COM-12877.pdf

Code 1 - Nothing on scope from pin PA2

#include <stm32f10x.h>
 
#define GPIO_PORT       GPIOA
#define GPIO_PIN        GPIO_Pin_2
 
#define LED_COUNT       2
 
/* Buffer that holds one complete DMA transmission.
 *
 * Ensure that this buffer is big enough to hold
 * all data bytes that need to be sent.
 *
 * The buffer size can be calculated as followas:
 * number of LEDs * 24 bytes + 42 bytes.
 *
 * This leaves us with a maximum string length of
 * (2^16 bytes per DMA stream - 42 bytes)/24 bytes per LED = 2728 LEDs.
 */
uint16_t ledBuff[2*LED_COUNT+42];
 
uint8_t rgb[3][3] = {
        {255, 0, 0},
        {0, 255, 0},
        {0, 0, 255}
};
 
 
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStruct;
TIM_OCInitTypeDef  TIM_OCInitStruct;
DMA_InitTypeDef DMA_InitStruct;
 
 
//Prototypes
void send_data(uint8_t (*color)[3], uint16_t len);
 
 
void Delay(__IO uint32_t nCount) {
  while(nCount--) {
  }
}
 
int main() {
 
    int16_t i;
    uint16_t PrescalerValue = (uint16_t) (72000000 / 24000000) - 1;
     
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);                                              // Enable clock for GPIOA   on APB2
    GPIO_InitStruct.GPIO_Pin = GPIO_PIN;                                                                                    // Set the pin we want to use in the intialization structure.
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;                       
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;                                                                 
    GPIO_Init(GPIO_PORT, &GPIO_InitStruct);
     
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);                                                    // Enable clock for TIM2        on APB1
    /* Time base configuration */
    TIM_TimeBaseStruct.TIM_Period = (30*1)-1; // 800kHz                                                     // Species the how many clock cycles = 1 period????????
    TIM_TimeBaseStruct.TIM_Prescaler = PrescalerValue;                                                      // Specifies the prescaler value used to divide the TIM clock.
    TIM_TimeBaseStruct.TIM_ClockDivision = 0;                                                                           // Specifies the clock division.
    TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;                                            //
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStruct);
 
    /* PWM1 Mode configuration: Channel1 */
    TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;                                                              // Specifies the TIM mode.
  TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;                                        //
    TIM_OCInitStruct.TIM_Pulse = 0;
  TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC3Init(TIM2, &TIM_OCInitStruct);
     
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);                                                      // Enable clock for DMA1        on AHB
     
    DMA_DeInit(DMA1_Channel1);                                                                                                      // Deinitialize DAM1 Channel 1 to their default reset values.
     
    DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&TIM2->CCR1;                              // Specifies Physical address of the peripheral in this case Timer 2 CCR1
    DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)ledBuff;                                              // Specifies the buffer memory address
    DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralDST;                                                             // Data transfered from memory to peripheral
    DMA_InitStruct.DMA_BufferSize = 42;                                                                                     // Specifies the buffer size
    DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;                                   // Do not incrament the peripheral address
    DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;                                                    // Incrament the buffer index
    DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;            // Specifies the peripheral data width
    DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;                            // Specifies the memory data width
    DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;                                                                      // Specifies the operation mode. Normal or Circular
    DMA_InitStruct.DMA_Priority = DMA_Priority_High;                                                            // Specifies the software priority
    DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;                                                                           //
     
    DMA_Init(DMA1_Channel1, &DMA_InitStruct);                                                                           // Initialize DAM1 Channel 1 to values specified in the DMA_InitStruct structure.  
     
    TIM_DMACmd(TIM2, TIM_DMA_CC1, ENABLE);                                                                              // Enables TIM2's DMA request. TIM_DMA_CC1 : TIM Capture Compare 1 DMA source
 
 
    while (1){ 
        /* first cycle through the colors on 2 LEDs chained together
         * last LED in the chain will receive first sent triplet
         * --> last LED in the chain will 'lead'
         */
        for (i = 0; i < 3; i++)
        {
            send_data(&rgb[i], 2);
            Delay(50000L);
        }
         
        /* cycle through the colors on only one LED
         * this time only the first LED that data is
         * fed into will update
         */
        for (i = 0; i < 3; i++)
        {
            send_data(&rgb[i], 2);
            Delay(50000L);
        }
    }
}
 
void send_data(uint8_t (*color)[3], uint16_t len) {
     
    uint8_t i;
    uint8_t led;
    uint16_t memaddr;
    uint16_t buffersize;
     
    buffersize = (len*24)+42;           // number of bytes needed is #LEDs * 24 bytes + 42 trailing bytes
    memaddr = 0;                                    // reset buffer memory index
    led = 0;
     
    while (len)
    {  
        for (i = 0; i < 8; i++)                         // GREEN data
        {
            if ( (color[led][1]<<i) & 0x80 )    // data sent MSB first, j = 0 is MSB j = 7 is LSB
            {
                ledBuff[memaddr] = 17;                  // compare value for logical 1
            }
            else
            {
                ledBuff[memaddr] = 9;                       // compare value for logical 0
            }
            memaddr++;
        }
         
        for (i = 0; i < 8; i++)                         // RED data
        {  
            if ( (color[led][0]<<i) & 0x80 )    // data sent MSB first, j = 0 is MSB j = 7 is LSB
            {
                ledBuff[memaddr] = 17;                  // compare value for logical 1
            }
            else
            {
                ledBuff[memaddr] = 9;                       // compare value for logical 0
            }
            memaddr++;
        }
         
        for (i = 0; i < 8; i++)                         // BLUE data
        {
            if ( (color[led][2]<<i) & 0x80 )    // data sent MSB first, j = 0 is MSB j = 7 is LSB
            {
                ledBuff[memaddr] = 17;                  // compare value for logical 1
            }
            else
            {
                ledBuff[memaddr] = 9;                       // compare value for logical 0
            }
            memaddr++;
        }
         
        led++;
        len--;
    }
     
    // add needed delay at end of byte cycle, pulsewidth = 0
    while(memaddr < buffersize)
    {
        ledBuff[memaddr] = 0;
        memaddr++;
    }
     
    DMA_SetCurrDataCounter(DMA1_Channel1, buffersize);  // load number of bytes to be transferred
    DMA_Cmd(DMA1_Channel1, ENABLE);                                         // enable DMA channel 1
    TIM_Cmd(TIM2, ENABLE);                                                          // enable Timer 2
    while(!DMA_GetFlagStatus(DMA1_FLAG_TC1));                   // wait until transfer complete
    TIM_Cmd(TIM2, DISABLE);                                                         // disable Timer 2
    DMA_Cmd(DMA1_Channel1, DISABLE);                                        // disable DMA channel 1
    DMA_ClearFlag(DMA1_FLAG_TC1);                                           // clear DMA1 Channel 1 transfer complete flag
     
}


Code 2 - Something on scope from pin PA6 (different timer)

#include <stm32f10x.h>
 
#define NUM_LEDS 10
 
void Delay(__IO uint32_t nCount) {
  while(nCount--) {
  }
}
 
#define TIM3_CCR1_Address 0x40000434    // physical memory address of Timer 3 CCR1 register
 
#define d2r (3.14159265/180)
 
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
 
/* Buffer that holds one complete DMA transmission
 *
 * Ensure that this buffer is big enough to hold
 * all data bytes that need to be sent
 *
 * The buffer size can be calculated as follows:
 * number of LEDs * 24 bytes + 42 bytes
 *
 * This leaves us with a maximum string length of
 * (2^16 bytes per DMA stream - 42 bytes)/24 bytes per LED = 2728 LEDs
 */
uint16_t LED_BYTE_Buffer[24 * NUM_LEDS + 42];
 
/* this array holds the RGB values to represent
 * a color wheel using 256 steps on each emitter
 * 256^3 = 16777216 colors
 */
uint8_t eightbit[766][3] =
{
    {255, 0, 0},
    {254, 1, 0},
    {253, 2, 0},
    {255, 0, 0},
};
 
void Timer3_init(void)
{
    uint16_t PrescalerValue;
     
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    /* GPIOA Configuration: TIM3 Channel 1 as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
     
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    /* Compute the prescaler value */
    PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Period = 29; // 800kHz
    TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM3, &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(TIM3, &TIM_OCInitStructure);
     
    /* configure DMA */
    /* DMA clock enable */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
     
    /* DMA1 Channel6 Config */
    DMA_DeInit(DMA1_Channel6);
 
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM3_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_Channel6, &DMA_InitStructure);
     
    /* TIM3 CC1 DMA Request enable */
    TIM_DMACmd(TIM3, TIM_DMA_CC1, ENABLE);
 
    DMA_ITConfig(DMA1_Channel6, DMA_IT_TC, ENABLE);
 
    
 
    NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel6_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
 
}
 
volatile uint8_t ws2811LedDataTransferInProgress = 0;
 
void DMA1_Channel6_IRQHandler(void)
{
    if (DMA_GetFlagStatus(DMA1_FLAG_TC6)) {
        ws2811LedDataTransferInProgress = 0;
        DMA_Cmd(DMA1_Channel6, DISABLE);            // disable DMA channel 6
        DMA_ClearFlag(DMA1_FLAG_TC6);               // clear DMA1 Channel 6 transfer complete flag
    }
}
 
/* This function sends data bytes out to a string of WS2812s
 * The first argument is a pointer to the first RGB triplet to be sent
 * The seconds argument is the number of LEDs in the chain
 *
 * This will result in the RGB triplet passed by argument 1 being sent to
 * the LED that is the furthest away from the controller (the point where
 * data is injected into the chain)
 *
 * this method is non-blocking unless an existing LED update is in progress.
 * it does not wait until all the LEDs have been updated, that happens in the background.
 */
void WS2812_send(uint8_t (*color)[3], uint16_t len)
{
    uint8_t i,j;
    uint8_t led;
    uint16_t memaddr;
    uint16_t buffersize;
 
    while(ws2811LedDataTransferInProgress);   // wait until previous transfer completes
 
    buffersize = (len*24)+42;   // number of bytes needed is #LEDs * 24 bytes + 42 trailing bytes
    memaddr = 0;                // reset buffer memory index
    led = 0;                    // reset led index
 
    // fill transmit buffer with correct compare values to achieve
    // correct pulse widths according to color values
    while (len)
    {  
        for (j = 0; j < 8; j++)                 // GREEN data
        {
            if ( (color[led][1]<<j) & 0x80 )    // data sent MSB first, j = 0 is MSB j = 7 is LSB
            {
                LED_BYTE_Buffer[memaddr] = 17;  // compare value for logical 1
            }
            else
            {
                LED_BYTE_Buffer[memaddr] = 9;   // compare value for logical 0
            }
            memaddr++;
        }
         
        for (j = 0; j < 8; j++)                 // RED data
        {
            if ( (color[led][0]<<j) & 0x80 )    // data sent MSB first, j = 0 is MSB j = 7 is LSB
            {
                LED_BYTE_Buffer[memaddr] = 17;  // compare value for logical 1
            }
            else
            {
                LED_BYTE_Buffer[memaddr] = 9;   // compare value for logical 0
            }
            memaddr++;
        }
         
        for (j = 0; j < 8; j++)                 // BLUE data
        {
            if ( (color[led][2]<<j) & 0x80 )    // data sent MSB first, j = 0 is MSB j = 7 is LSB
            {
                LED_BYTE_Buffer[memaddr] = 17;  // compare value for logical 1
            }
            else
            {
                LED_BYTE_Buffer[memaddr] = 9;   // compare value for logical 0
            }
            memaddr++;
        }
         
        led++;
        len--;
    }
     
    // add needed delay at end of byte cycle, pulsewidth = 0
    while(memaddr < buffersize)
    {
        LED_BYTE_Buffer[memaddr] = 0;
        memaddr++;
    }
 
    ws2811LedDataTransferInProgress = 1;
 
    DMA_SetCurrDataCounter(DMA1_Channel6, buffersize);  // load number of bytes to be transferred
    TIM_SetCounter(TIM3, 0);
    TIM_Cmd(TIM3, ENABLE);                      // enable Timer 3
    DMA_Cmd(DMA1_Channel6, ENABLE);             // enable DMA channel 6
}
 
int main(void) {
     
     
    int16_t i;
     
    int16_t idleCounter;
     
    Timer3_init();
 
    while (1){ 
        /* first cycle through the colors on the LEDs chained together
         * last LED in the chain will receive first sent triplet
         * --> last LED in the chain will 'lead'
         */
         
        GPIO_SetBits(GPIOC, GPIO_Pin_13);
         
        for (i = 0; i < 766 - NUM_LEDS; i += 1)
        {
            WS2812_send(&eightbit[i], NUM_LEDS);
 
            idleCounter = 0;
            while(ws2811LedDataTransferInProgress) {
                // the main loop is free to do other work while the LEDs are being updated, such as updating this idle counter
                idleCounter++;
            }
 
            Delay(5000L);
        }
         
    }
    TIM_Cmd(TIM3, DISABLE);                     // disable Timer 3
}

Outcomes