cancel
Showing results for 
Search instead for 
Did you mean: 

NUCLEO-F746, DAC circular DMA bug!

kylongmu
Associate II
Posted on September 30, 2016 at 18:28

&sharpdefine SAMPLESDAC (1024*2)

static TIM_HandleTypeDef  Timer4Handle;

void Timer4_init()

{

    TIM_ClockConfigTypeDef TimerClockConfig;

    TIM_MasterConfigTypeDef sMasterConfig;

    __TIM4_CLK_ENABLE();

    //Timer init

    Timer4Handle.Instance = (TIM_TypeDef *)TIM4;

    Timer4Handle.Init.Prescaler = 0;

    Timer4Handle.Init.CounterMode = TIM_COUNTERMODE_UP;

    Timer4Handle.Init.Period = 108;

    Timer4Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

    //Timer4Handle.Init.RepetitionCounter = 0;

    //Timer4Handle.Channel = HAL_TIM_ACTIVE_CHANNEL_1;

    //Timer4Handle.hdma[0] = &DMA_ADC_Handle;

    HAL_TIM_Base_Init(&Timer4Handle);

    TimerClockConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

    HAL_TIM_ConfigClockSource(&Timer4Handle,&TimerClockConfig);

    sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;

    sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

    HAL_TIMEx_MasterConfigSynchronization(&Timer4Handle, &sMasterConfig);

    //HAL_TIM_Base_Start(&Timer4Handle);

    HAL_TIM_Base_Start_IT(&Timer4Handle);

}

static DMA_HandleTypeDef DMA_DAC_Handle;

static DAC_HandleTypeDef    DacHandle;

void dac_init()

{

    // we assume AnalogIn has configureed GPIO, we need ADC channel ?

    __DAC_CLK_ENABLE();  // Enable ADC clock

    // Configure ADC

    DacHandle.Instance = (DAC_TypeDef *)DAC;

    //DacHandle.DMA_Handle1 =&DMA_DAC_Handle;

    HAL_DAC_Init(&DacHandle);

}

void dma_dac_init()

{

    //  DMA init  ADC1 is DMA2 channel0 stream 0 or 4 use DMA2_Stream0  thd

    __DMA1_CLK_ENABLE();

    DMA_DAC_Handle.Instance = DMA1_Stream6;

    //DMA_DAC_Handle.State = HAL_DMA_STATE_READY;

    //HAL_DMA_DeInit(&DMA_DAC_Handle);

    DMA_DAC_Handle.Init.Channel = DMA_CHANNEL_7;

    DMA_DAC_Handle.Init.Direction = DMA_MEMORY_TO_PERIPH;

    DMA_DAC_Handle.Init.PeriphInc = DMA_PINC_DISABLE;

    DMA_DAC_Handle.Init.MemInc = DMA_MINC_ENABLE;

    DMA_DAC_Handle.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;

    DMA_DAC_Handle.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;

    DMA_DAC_Handle.Init.Mode =  DMA_CIRCULAR;

    DMA_DAC_Handle.Init.Priority = DMA_PRIORITY_VERY_HIGH;

    DMA_DAC_Handle.Init.FIFOMode = DMA_FIFOMODE_DISABLE;

    DMA_DAC_Handle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;

    DMA_DAC_Handle.Init.MemBurst = DMA_MBURST_SINGLE;

    DMA_DAC_Handle.Init.PeriphBurst = DMA_PBURST_SINGLE;

    HAL_DMA_Init(&DMA_DAC_Handle);

    __HAL_LINKDMA(&DacHandle, DMA_Handle2, DMA_DAC_Handle);

}

void dac_readn( uint16_t * data, uint32_t nelems)

{

    DAC_ChannelConfTypeDef sConfig;

    sConfig.DAC_Trigger = DAC_TRIGGER_T4_TRGO;  // Timer6 as triger

    sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;

    HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_2);

    HAL_DAC_Start_DMA(&DacHandle, DAC_CHANNEL_2, (uint32_t *)data, nelems, DAC_ALIGN_12B_R);

    //while (DMA_DAC_Handle.Instance->CR & DMA_SxCR_EN);   // spin

    //HAL_DAC_Stop_DMA(&DacHandle,DAC_CHANNEL_1);

}

void SawToothWave()

{

    int i = 0;

    

    for(; i <= SAMPLESDAC; i ++) {

        DAC_Val[i]=1024+i;

        //DAC_Val[i]=2048;

    }

}

#dac-bug
5 REPLIES 5
kylongmu
Associate II
Posted on September 30, 2016 at 18:36

int main()

{

    Timer4_init();

    SawToothWave();

    Timer6_init();

    dac_init();

    dma_dac_init();

    dac_readn(DAC_Val,SAMPLESDAC);//Followed by large noise 

    while (true) {

        led1 = !led1;

        Thread::wait(500);

    }

}

/************************************************************/

If i call as the above code , it optput SAW UP signal followed by large noise,

When i do a strange code bellow, it output SAW UP signal correctly.

int main()

{

    Timer4_init();

    SawToothWave();

    Timer6_init();

    dac_init();

    dma_dac_init();

    dac_readn(DAC_Val,SAMPLESDAC-512);//correct SAW UP signal 

    while (true) {

        led1 = !led1;

        Thread::wait(500);

    }

}

kylongmu
Associate II
Posted on September 30, 2016 at 18:38

Why I must leave 512 point in DMA to get a correct waveform?

kylongmu
Associate II
Posted on September 30, 2016 at 18:42

correct waveform with 512 point off.

________________

Attachments :

graphs_ok.jpg : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hzwz&d=%2Fa%2F0X0000000bRP%2F_FRFm6o6yxa8qo9krz9r3423EtBIfTVludvykMfgZuc&asPdf=false
kylongmu
Associate II
Posted on September 30, 2016 at 18:44

Wrong waveform , but code is understandable.

________________

Attachments :

graphs.jpg : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hzwu&d=%2Fa%2F0X0000000bRO%2Fq4oARtUmBjunBj96AN8CoEQr9jRM6o4zHJohvJEqN98&asPdf=false
Posted on September 30, 2016 at 20:17

You can edit/amend your own posts...

Code still incomplete, when trying to demonstrate a failure provide a complete example that will compile.

Is the pattern buffer placed in non-cached memory?

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