/***********************************************************************
 * Copyright (c)  2017 - 2022, Unicmicro Co.,Ltd .
 * All rights reserved.
 * Filename    : atim_dma.c
 * Description : atim_dma source file
 * Author(s)   : Hackett
 * version     : V1.0
 * Modify date : 2022-07-10
 ***********************************************************************/
#include  "atim_dma.h"
#include  "uart0.h"
#include  "uart1.h"
#include  "uart2.h"
#include  "common.h"
#include  "atimer.h"
#include  "dma.h"
#include  "gpio.h"


/************************************************************************
 * function   : dmac__tc
 * Description: DMAC外设到内存回调函数
 * input : none
 * return: none
 ************************************************************************/
void dmac_tc(void)
{
	printfS("DMAC块传输结束!!! \n");
	printfS("REG_ATIM_CCR1 : %d \r\n", atimer_get_capture(ATIM_CHANNEL1));
	printfS("SRAM : %d \r\n", *(volatile uint32_t *)(0x20001000));
	
}

/************************************************************************
 * function   : atimer_dma_test
 * Description: SRAM与TIMER0之间通过DMA进行数据传输
 * input : none
 * return: none
 ************************************************************************/
void atimer_dma_test(void)
{
	printfS("start atimer dma test! \r\n");
	
	gpio_init(GPIOF, PIN0, 7);		//PF0 ATIM_CH1	
	
	atimer_module_enable();							//使能时钟并复位定时器0模块	
	atimer_sram2atimer_init(48000, 1, ATIM_CHANNEL1);		//SRAM到比较寄存器
	atimer_irq(ATIM_CC1IE, ATIMER_IRQn, ATIM_DISABLE, NULL);//不使能中断	
	atimer_set_compare(ATIM_CHANNEL1, (48000 / 2));	//通道1的输出比较值
	//DMA配置
	dma_init(DMA_Channel_0, DMA_TR_WIDTH_32, DMA_MEM_TO_PERIP);
	dma_source_config(DMA_Channel_0, NULL, DMA_SINC_INC);
	dma_destination_config(DMA_Channel_0, DMA_DST_PER_ATIMER_CC1, DMA_DINC_NOC);	
	dma_controler_enable();	
	dma_irq_init(DMA_Channel_0, DMA_IRQ_ENABLE, dmac_tc);

	*(volatile uint32_t *)(0x20001000) = 48000 / 4;		
	delay_ms(3000);
	dma_irq_transfer(DMA_Channel_0, 0x20001000, (uint32_t)(&REG_ATIM_DMAR), 1);//开启DMA传输
}



