/********************************** (C) COPYRIGHT *******************************
 * File Name          : main.c
 * Author             : WCH
 * Version            : V1.0.0
 * Date               : 2019/10/15
 * Description        : Main program body.
 *********************************************************************************
 * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
 * Attention: This software (modified or not) and binary are used for 
 * microcontroller manufactured by Nanjing Qinheng Microelectronics.
 *******************************************************************************/

/*
 *@Note
 *DAC DMA conversion routines:
 *DAC channel 0 (PA4) output
 *DMA is configured in cyclic mode, a DAC conversion is triggered by a TIM3_TRGO 
 *event, and PA4 outputs the corresponding voltage.
 *
*/

#include "debug.h"

/* Global define */


/* Global Variable */
u16 dacbuff16bit[8] = {0x123, 0x234, 0x345, 0x456, 0x567, 0x678, 0x789, 0x89a};

/*********************************************************************
 * @fn      Dac_Init
 *
 * @brief   Initializes DAC collection.
 *
 * @return  none
 */
void Dac_Init( void )
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};
    DAC_InitTypeDef DAC_InitType = {0};

    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE );
    RCC_APB1PeriphClockCmd( RCC_APB1Periph_DAC, ENABLE );

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init( GPIOA, &GPIO_InitStructure );
    GPIO_SetBits( GPIOA, GPIO_Pin_4 );

    DAC_InitType.DAC_Trigger = DAC_Trigger_T3_TRGO;
    DAC_InitType.DAC_WaveGeneration = DAC_WaveGeneration_None;
    DAC_InitType.DAC_OutputBuffer = DAC_OutputBuffer_Disable ;
    DAC_Init( DAC_Channel_1, &DAC_InitType );

    DAC_Cmd( DAC_Channel_1, ENABLE );
    DAC_DMACmd( DAC_Channel_1, ENABLE );
}


/*********************************************************************
 * @fn      DAC1_DMA_INIT
 *
 * @brief   Initializes DMA of DAC1 collection.
 *
 * @return  none
 */
void DAC1_DMA_Init( void )
{
    DMA_InitTypeDef DMA_InitStructure = {0};
    RCC_AHBPeriphClockCmd( RCC_AHBPeriph_DMA1, ENABLE );

    DMA_StructInit( &DMA_InitStructure );
    /* Note:DAC1--->DMA2.CH3   DAC2--->DMA2.CH4 */
    DMA_InitStructure.DMA_PeripheralBaseAddr = ( u32 ) & ( DAC->R12BDHR1 );
    DMA_InitStructure.DMA_MemoryBaseAddr = ( u32 )&dacbuff16bit;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
    DMA_InitStructure.DMA_BufferSize = 8;
    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_VeryHigh;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

    DMA_Init( DMA1_Channel3, &DMA_InitStructure );
    DMA_Cmd( DMA1_Channel3, ENABLE );
}

/*********************************************************************
 * @fn      TIM3_Init
 *
 * @brief   Initializes TIM3 collection.
 *
 * @param   arr - TIM_Period
 *          psc - TIM_Prescaler
 *
 * @return  none
 */
void TIM3_Init( u16 arr, u16 psc )
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure = {0};
    RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE );

    TIM_TimeBaseStructure.TIM_Period = arr;
    TIM_TimeBaseStructure.TIM_Prescaler = psc;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure );

    TIM_SelectOutputTrigger( TIM3, TIM_TRGOSource_Update );

    TIM_Cmd( TIM3, ENABLE );
}

/*********************************************************************
 * @fn      main
 *
 * @brief   Main program.
 *
 * @return  none
 */
int main( void )
{
    NVIC_PriorityGroupConfig( NVIC_PriorityGroup_2 );
    SystemCoreClockUpdate();
    Delay_Init();
    USART_Printf_Init( 115200 );
    printf( "SystemClk:%d\r\n", SystemCoreClock );
    printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
    printf( "DAC DMA TEST\r\n" );
    Dac_Init();
    DAC1_DMA_Init();
    TIM3_Init( 4, 7199 );

    while( 1 )
    {
        Delay_Ms( 1000 );
        printf( "run\r\n" );
        printf( "DAC->R12BDHR1:0x%04x\r\n", DAC->R12BDHR1 );
        printf( "DAC->DOR1:0x%04x\r\n", DAC->DOR1 );
        printf( "TIM3->CNT:%d\r\n", TIM3->CNT );
    }
}



