AnsweredAssumed Answered

NUCLEO-F746, DAC circular DMA bug!

Question asked by qiyong.mu.001 on Sep 30, 2016
Latest reply on Sep 30, 2016 by Clive One
#define 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;
    }
}

Outcomes