/***********************************************************************
 * 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  "cmp.h"

uint32_t g_data_buf[512];

volatile uint8_t cmp0_callback_flag;
volatile uint8_t cmp1_callback_flag;
volatile uint8_t cmp2_callback_flag;

#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   : cmp0_callback_fun
 * description: cmp0_callback_fun 处理回调函数
 * input : none
 * return: none
 ************************************************************************/
void cmp0_callback_fun(void)
{
	cmp0_callback_flag = 1;
	printfS("cmp0 irq happended!\r\n");
}

/************************************************************************
 * function   : cmp1_callback_fun
 * description: cmp1_callback_fun 处理回调函数
 * input : none
 * return: none
 ************************************************************************/
void cmp1_callback_fun(void)
{
	cmp1_callback_flag = 1;
	printfS("cmp1 irq happended!\r\n");	
}

/************************************************************************
 * function   : cmp2_callback_fun
 * description: cmp2_callback_fun 处理回调函数
 * input : none
 * return: none
 ************************************************************************/
void cmp2_callback_fun(void)
{
	cmp2_callback_flag = 1;
	printfS("cmp2 irq happended!\r\n");	
}
/************************************************************************
 * function   : cmp0_test
 * description: cmp0_test  	//cmp0：INN PD1  INP:PD0  OUT:PA4
 * input : none
 * return: none
 ************************************************************************/
void cmp0_test(void)
{
	uint32_t event_status=0;
	
	printfS("System run at %dHz\r\n", SystemCoreClock);
	printfS("start test cmp0 function! \n");
	
	cmp_init(USED_CMP0);                                                  //初始化cmp0
	cmp_irq_init(USED_CMP0,CMP_IRDISABLE,cmp0_callback_fun);              //cmp0中断配置
	
	while(1)
	{
		event_status=get_acmp_enentstatus();
		if((event_status & 0x20) == 0x20)
		{
			printfS("cmp0 true event happended\r\n");
		}
		printfS("cmp0 don't have true event\r\n");
		delay_ms(500);
		
	}
}
/************************************************************************
 * function   : cmp1_test
 * description: cmp1_test  	//cmp1：INN PD2  INP:PD3  OUT:PA6
 * input : none
 * return: none
 ************************************************************************/
void cmp1_test(void)
{
	uint32_t event_status=0;
	
	printfS("System run at %dHz\r\n", SystemCoreClock);
	printfS("start test cmp1 function! \n");
	
	cmp_init(USED_CMP1);                                                   //初始化cmp1
	cmp_irq_init(USED_CMP1,CMP_IRDISABLE,cmp1_callback_fun);               //cmp1中断配置
	
	while(1)
	{
		event_status=get_acmp_enentstatus();
		if((event_status & 0x200) == 0x200)
		{
			printfS("cmp1 true event happended\r\n");
			cmp1_callback_flag=0;
		}
		printfS("cmp1 don't have true event\r\n");
		delay_ms(500);
	}

}
/************************************************************************
 * function   : cmp2_test
 * description: cmp2_test  	//cmp2：INN PD5  INP:PD4  OUT:PB6
 * input : none
 * return: none
 ************************************************************************/
void cmp2_test(void)
{
	uint32_t event_status=0;
	
	printfS("System run at %dHz\r\n", SystemCoreClock);
	printfS("start test cmp2 function! \n");
	
	cmp_init(USED_CMP2);                                         //初始化cmp2
	cmp_irq_init(USED_CMP2,CMP_IRDISABLE,cmp2_callback_fun);	 //cmp2中断配置
	
	while(1)
	{
		event_status=get_acmp_enentstatus();
		if((event_status & 0x2000) == 0x2000)
		{
			printfS("cmp2 true event happended\r\n");
			cmp2_callback_flag=0;
		}
		printfS("cmp2 don't have true event\r\n");
		delay_ms(500);
	}
}
/************************************************************************
 * function   : soc_test
 * description: soc_test 
 * input : none
 * return: none
 ************************************************************************/
void soc_test(void)
{

	cmp0_test(); 
//	cmp1_test(); 
//	cmp2_test(); 

}
