/***********************************************************************
 * 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  "adc.h"
#include  "dma.h"


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

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
}

/************************************************************************
 * function   : adc_dma_test
 * Description: adc_dma_test
 * input : none
 * return: none
 ************************************************************************/
void adc_dma_test(void)
{
	uint8_t i;
	
	uint16_t dma_buf[8] = {0,0,0,0,0,0,0,0};
	
	adc_init(ADC_REF_VDDH,ADC_SAMPL_RATE_50K_SPS,ADC_SINGLE_MODE);		//ADC初始化，选择ADC参考电压源、ADCCLK分频和采样模式
	adc_rxfifo_config(ADC_RXFIFO_LEAST_1DATA, ADC_DMA_ACCESS_RXFIFO, ENABLE);
	adc_channel_gpio_config(ADC_CH1_EN|ADC_CH2_EN|ADC_CH3_EN, ADC_PF0|ADC_PA6|ADC_PB0);		//配置ADC转换通道
	printfS("adc single mode --- single channel start!!!\r\n");	
	
	dma_init(DMA_Channel_0, DMA_TR_WIDTH_16, DMA_PERIP_TO_MEM);
	dma_source_config(DMA_Channel_0, DMA_SRC_PER_ADC_RX, DMA_SINC_NOC);
	dma_destination_config(DMA_Channel_0, NULL, DMA_DINC_INC);
	dma_controler_enable();	
	
	adc_convert_start();delay_ms(10);
	dma_poll_transfer(DMA_Channel_0, (uint32_t)&REG_ADC_RXREG, (uint32_t)(&dma_buf), 3); 

	for(i = 0; i<3; i++)
		printfS("dma_recv_buf[%d] = %d, adc_vol = %.3fV\r\n", i, dma_buf[i], (float)dma_buf[i]/4095*(float)3.3);
}

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

	printfS("System run at %dHz \r\n", SystemCoreClock);
	printfS("start test adc! \r\n");
	
	adc_dma_test();

}
