AnsweredAssumed Answered

DAC waveform generation with DMA not working

Question asked by grossi.marco on Nov 27, 2014
Latest reply on Nov 28, 2014 by grossi.marco
I am using the STM32L Discovery Board (STML152RB chip on board). The IDE is Emblocks. I was trying to generate a sawtooth wave signal using DAC and DMA but it does not work. I was able to make the same program without DMA (by triggering an interrupt at regular time with a timer and updating the DAC value inside the interrup handler) but with DMA it does not work. Below is the code I use. Anyone can help to fix it?

#include "stm32l1xx_conf.h"
#include <stm32l1xx.h>
#include <stm32l1xx_rcc.h>
#include <stm32l1xx_gpio.h>
#include <stm32l1xx_tim.h>
#include <stm32l1xx_dac.h>
#include <stm32l1xx_dma.h>
#include <misc.h>
#include <math.h>

#define WAVE_FREQ 5000
#define WAVE_POINTS 100
#define TIMER_PERIOD 9

uint16_t wave_array[100];
uint8_t j;
float pi_greco = 3.14159;
int timervalue;

void GPIO_initialize(void)
     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

    GPIO_InitTypeDef gpioStructure;
    gpioStructure.GPIO_Pin = GPIO_Pin_5;
    gpioStructure.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_Init(GPIOA, &gpioStructure);


void DAC_initialize(void)
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
     RCC_APB1PeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

     DMA_InitTypeDef DMA_Structure;
    DMA_Structure.DMA_PeripheralBaseAddr = (uint32_t) &DAC->DHR12R2;
    DMA_Structure.DMA_MemoryBaseAddr = (uint32_t) &wave_array;
    DMA_Structure.DMA_DIR = DMA_DIR_PeripheralDST;
    DMA_Structure.DMA_BufferSize = WAVE_POINTS;
    DMA_Structure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_Structure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_Structure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_Structure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_Structure.DMA_Mode = DMA_Mode_Circular;
    DMA_Structure.DMA_Priority = DMA_Priority_High;
    DMA_Structure.DMA_M2M = DMA_M2M_Disable;
    DMA_Init(DMA1_Channel1, &DMA_Structure);

    DAC_InitTypeDef DAC_Structure;
     DAC_Structure.DAC_Trigger = DAC_Trigger_T6_TRGO;
     DAC_Structure.DAC_WaveGeneration = DAC_WaveGeneration_None;
     DAC_Structure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
     DAC_Init(DAC_Channel_2, &DAC_Structure);

    DMA_Cmd(DMA1_Channel1, ENABLE);
    DAC_DMACmd(DAC_Channel_2, ENABLE);
     DAC_Cmd(DAC_Channel_2, ENABLE);

void TIM6_initialize(void)
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);

     TIM_TimeBaseInitTypeDef timerInitStructure;
     timerInitStructure.TIM_Prescaler = 319;
     timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
     timerInitStructure.TIM_Period = TIMER_PERIOD;
     timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
     TIM_TimeBaseInit(TIM6, &timerInitStructure);
     TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update);

     TIM_Cmd(TIM6, ENABLE);

int main(void)
    for(j=0; j<100; j++)
        wave_array[j] = (uint16_t)(500 + 20*j);