AnsweredAssumed Answered

noise in dac using dma

Question asked by julienb on Dec 20, 2011
Latest reply on Dec 23, 2011 by baird.hal.001
Hello,

I can synthesize a waveform into a buffer, dma transfering it to dac. Result is ok but there's a noise that I can't remove. When the waveform is a 0 signal, the noise is still here.

The noise has a frequency (~ dma interrupt freq) it seems to be square like, and seems to be louder when CPU is more used.

Am I doing something wrong with dma or dac?
The dac pin is directly connected to the amp input, I don't know if I should add some components?
Do you know what can cause this noise?

I made a simplified source code which can reproduce the error:

#include "stm32f10x.h"
#define DAC_DHR12RD_Address      0x40007420
 
/* Init Structure definition */
DAC_InitTypeDef            DAC_InitStructure;
DMA_InitTypeDef            DMA_InitStructure;
TIM_TimeBaseInitTypeDef    TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
 
uint16_t outbuf[32] =  {
         2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048,
         2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048,
         2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048,
         2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048};
 
char compute_next_frame;
 
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void Delay(__IO uint32_t nCount);
 
 
int main(void)
{
    RCC_Configuration();
    GPIO_Configuration();
 
    /* TIM2 Configuration */
    /* Time base configuration */
    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
    TIM_TimeBaseStructure.TIM_Period = 24;
    TIM_TimeBaseStructure.TIM_Prescaler = 30;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
 
    /* TIM2 TRGO selection */
    TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
 
    /* DAC channel1 Configuration */
    DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
    DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
    DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
    DAC_Init(DAC_Channel_1, &DAC_InitStructure);
 
    DMA_DeInit(DMA1_Channel3);
    DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12RD_Address;
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&outbuf;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
    DMA_InitStructure.DMA_BufferSize = 32;
    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_Circular;
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
 
    DMA_Init(DMA1_Channel3, &DMA_InitStructure);
    DMA_Cmd(DMA1_Channel3, ENABLE);
 
    DMA_ITConfig(DMA1_Channel3, DMA_IT_TC, ENABLE);
 
    NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;   //HIGHEST_PRIORITY;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init( &NVIC_InitStructure );
 
    DAC_Cmd(DAC_Channel_1, ENABLE);
    DAC_DMACmd(DAC_Channel_1, ENABLE);
 
    /* TIM2 enable counter */
    TIM_Cmd(TIM2, ENABLE);
 
    compute_next_frame = 0;
    while (1)
    {
        while(!compute_next_frame);
 
        // compute the next frame
        // simulate cpu load
        // when this block is off there's some noise but less
        int i, j;
        for(i=1;i<2000;i++)
            j = 4984131/i;
 
        compute_next_frame=0;
 
    }
}
 
 
void RCC_Configuration(void)
{  
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}
 
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4 | GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}
 
// function called in stm32f10x_it.c by DMA1_Channel3_IRQHandler
void dma_it(){
    if(DMA_GetITStatus(DMA1_IT_TC3)) {
        int i;
 
        // simulating square signal (1048 / 3048) or 0 signal (2048 / 2048)
        if(outbuf[0]==3048)
            for(i=0; i<32; i++)
                outbuf[i] = 2048; //1048;
        else
            for(i=0; i<32; i++)
                outbuf[i] = 2048; //3048;
 
        compute_next_frame = 1;
        DMA_ClearITPendingBit(DMA1_IT_GL3);
    }
}


Julien.

Outcomes