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

uint8_t dma_send_buf[BUF_SIZE]; //发送数据缓冲区
uint8_t dma_recv_buf[BUF_SIZE]; //接收数据缓冲区

volatile uint8_t dma_complete_flag = 0;


uint32_t g_data_buf[512];

#ifdef UART0_COMM
uint8_t uart0_rx_flag = 0;
uint8_t uart0_rx_buf[64];
uint8_t uart0_rx_count = 0;
#endif

#ifdef UART1_COMM
uint8_t uart1_rx_flag = 0;
uint8_t uart1_rx_buf[64];
uint8_t uart1_rx_count = 0;
#endif

#ifdef UART2_COMM
uint8_t uart2_rx_flag = 0;
uint8_t uart2_rx_buf[64];
uint8_t uart2_rx_count = 0;
#endif

#ifdef UART0_COMM
/************************************************************************
 * function   : uart0_rec_pro
 * Description: uart0 rec pro 串口接收处理回调函数
 * input : none
 * return: none
 ************************************************************************/
void uart0_rec_pro(void)
{
	uart0_rx_buf[uart0_rx_count] = uart0_recv_byte();
	uart0_rx_count++;
	uart0_rx_flag = 1;	
}
#endif

#ifdef UART1_COMM
/************************************************************************
 * function   : uart1_rec_pro
 * Description: uart1 rec pro 串口接收处理回调函数
 * input : none
 * return: none
 ************************************************************************/
void uart1_rec_pro(void)
{
	uart1_rx_buf[uart1_rx_count] = uart1_recv_byte();
	uart1_rx_count++;
	uart1_rx_flag = 1;	
}
#endif

#ifdef UART2_COMM
/************************************************************************
 * function   : uart1_rec_pro
 * Description: uart1 rec pro 串口接收处理回调函数
 * input : none
 * return: none
 ************************************************************************/
void uart2_rec_pro(void)
{
	uart2_rx_buf[uart2_rx_count] = uart2_recv_byte();
	uart2_rx_count++;
	uart2_rx_flag = 1;	
}
#endif
/************************************************************************
 * function   : uart_init
 * Description: uart_init 串口初始化
 * input : none
 * return: none
 ************************************************************************/
void uart_init(void)
{
#ifdef  UART0_COMM     
	uart0_rx_flag = 0;
	uart0_init(PCLK, UART0_BAUD_RATE);
	uart0_irq_init(UART0_IRQ_ENABLE, uart0_rec_pro);
#endif

#ifdef  UART1_COMM	
	uart1_rx_flag = 0;	
	uart1_init(PCLK, UART1_BAUD_RATE);
	uart1_irq_init(UART1_IRQ_ENABLE, uart1_rec_pro);
#endif

#ifdef  UART2_COMM    
    uart2_rx_flag = 0;	
	uart2_init(PCLK, UART2_BAUD_RATE);
	uart2_irq_init(UART2_IRQ_ENABLE, uart2_rec_pro);
#endif
}


void dmac_tc(void)
{
	dma_complete_flag = 1;
	printfS("DMA块传输结束!!! \r\n");
}


/************************************************************************
 * function   : dma_test
 * Description: dma_test
 * input : none
 * return: none
 ************************************************************************/
void dma_test(void)
{
	uint16_t i; 
	uint8_t result;
	uint8_t ch = DMA_Channel_7;

	
	for(i=0;i<BUF_SIZE;i++)
	{
		dma_send_buf[i] = i;
	}
	
	printfS("System run at %dHz\r\n", SystemCoreClock);
	printfS("start test dma! \r\n");
	
	
	dma_init(ch, DMA_TR_WIDTH_8, DMA_MEM_TO_MEM);
	dma_source_config(ch, NULL, DMA_SINC_INC);
	dma_destination_config(ch, NULL, DMA_DINC_INC);
	dma_controler_enable();
	
#if DMA_IRQ															//app.h中开关
	dma_irq_init(ch,DMA_IRQ_ENABLE,dmac_tc);
	dma_irq_transfer(ch, (uint32_t)(&dma_send_buf), (uint32_t)(&dma_recv_buf), BUF_SIZE); 
	while(!(dma_complete_flag==1));
	dma_complete_flag = 0;
#else
	dma_poll_transfer(ch, (uint32_t)(&dma_send_buf), (uint32_t)(&dma_recv_buf), BUF_SIZE); 
	
#endif	
	result = memcmp(dma_send_buf,dma_recv_buf,BUF_SIZE);
	if(!result)
	{
		printfS("DMA传输数据正确\r\n");
	}
	else
	{
		printfS("DMA传输数据错误\r\n");
	}	
}

/************************************************************************
 * function   : soc_test
 * Description: soc_test
 * input : none
 * return: none
 ************************************************************************/
void soc_test(void)
{

	dma_test();

}
