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

volatile uint8_t gpioa_it_flag = 0;
volatile uint8_t gpiob_it_flag = 0;
volatile uint8_t gpioc_it_flag = 0;
volatile uint8_t gpiod_it_flag = 0;
volatile uint8_t gpioe_it_flag = 0;
volatile uint8_t gpiof_it_flag = 0;
volatile uint8_t gpiog_it_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   : gpio_irq_pin
 * description: gpio irq pin 产生中断引脚查询 
 * input 	  : uint8_t val_ris 引脚中断值
 * return	  : none
 ************************************************************************/
uint8_t gpio_irq_pin(uint8_t val_ris)
{
	uint8_t ret;
	switch (val_ris)   							//根据寄存器，得出对应的引脚
	{
		case 0x01: ret = 0; break;
		case 0x02: ret = 1; break;
		case 0x04: ret = 2; break;
		case 0x08: ret = 3; break;
		case 0x10: ret = 4; break;
		case 0x20: ret = 5; break;
		case 0x40: ret = 6; break;
		case 0x80: ret = 7; break;
		default	 : ret = 0; break;              
	}
	return ret;
}
/************************************************************************
 * function   : gpioa_irq_func
 * description: gpioa irq func
 * input 	  : uint8_t val_ris 引脚号
 * return	  : none
 ************************************************************************/
void gpioa_irq_func(uint8_t val_ris)
{
	uint8_t temp = val_ris;
	printfS("gpioa_%d_int \r\n", gpio_irq_pin(temp));
	gpioa_it_flag = 1;
}

/************************************************************************
 * function   : gpiob_irq_func
 * description: gpiob irq func
 * input      : none
 * return     : none
 ************************************************************************/
void gpiob_irq_func(uint8_t val_ris)
{
	uint8_t temp = val_ris;
	printfS("gpiob_%d_int \r\n", gpio_irq_pin(temp));
	gpiob_it_flag = 1;
}
/************************************************************************
 * function   : gpioc_irq_func
 * description: gpioc irq func
 * input      : none
 * return     : none
 ************************************************************************/
void gpioc_irq_func(uint8_t val_ris)
{
	uint8_t temp = val_ris;
	printfS("gpioc_%d_int \r\n", gpio_irq_pin(temp));
	gpioc_it_flag = 1;
}
/************************************************************************
 * function   : gpiod_irq_func
 * description: gpiod irq func
 * input      : none
 * return     : none
 ************************************************************************/
void gpiod_irq_func(uint8_t val_ris)
{
	uint8_t temp = val_ris;
	printfS("gpiod_%d_int \r\n", gpio_irq_pin(temp));
	gpiod_it_flag = 1;
}

/************************************************************************
 * function   : gpioe_irq_func
 * description: gpioe irq func
 * input      : none
 * return     : none
 ************************************************************************/
void gpioe_irq_func(uint8_t val_ris)
{
	uint8_t temp = val_ris;
	printfS("gpioe_%d_int \r\n", gpio_irq_pin(temp));
	gpioe_it_flag = 1;
	
}
/************************************************************************
 * function   : gpiof_irq_func
 * description: gpiof irq func
 * input : none
 * return: none
 ************************************************************************/
void gpiof_irq_func(uint8_t val_ris)
{
	uint8_t temp = val_ris;
	printfS("gpiof_%d_int \r\n", gpio_irq_pin(temp));
	gpiof_it_flag = 1;
	
}
/************************************************************************
 * function   : gpiof_irq_func
 * description: gpiof irq func
 * input : none
 * return: none
 ************************************************************************/
void gpiog_irq_func(uint8_t val_ris)
{
	uint8_t temp = val_ris;
	printfS("gpiog_%d_int \r\n", gpio_irq_pin(temp));
	gpiog_it_flag = 1;
	
}
/************************************************************************
 * function   : gpio_in_test
 * description: gpio_in_test
 * input 	  : uint8_t port ：gpio端口选择 GPIOA\GPIOB\GPIOC...
 * 			    uint8_t pin  ：gpio引脚选择 PIN0\PIN1\PIN2....
 * return     : none
 ************************************************************************/
 void gpio_in_test(uint8_t port, uint8_t pin)
 {
 	uint8_t io_state;
	
	printfS("GPIO in test.\n\r");

	gpio_init(port, pin,0);										//时钟初始化，模块正常工作,GPIO功能选择为普通gpio
	gpio_dir(port,pin, GPIO_DIR_IN);							//配置为输入 
	gpio_in_enable(port, pin, ENABLE);							//输入使能
	gpio_config_pu(port, pin, ENABLE);							//上拉使能

	while(1)
	{	
		io_state = gpio_read_io(port, pin);	 	
		printfS("P%d level is %d\n\r",pin, io_state);
		delay_ms(500);
	} 
 }
 /************************************************************************
 * function   : gpio_out_test
 * description: gpio_out_test
 * input 	  : uint8_t port ：gpio端口选择 GPIOA\GPIOB\GPIOC...
 *				uint8_t pin  ：gpio引脚选择 PIN0\PIN1\PIN2....
 * return	  : none
 ************************************************************************/
 void gpio_out_test(uint8_t port, uint8_t pin)
{	
	printfS("GPIO out test.\n\r");
	
	gpio_init(port, pin,0);										//时钟初始化，模块正常工作
	gpio_dir(port,pin, GPIO_DIR_OUT);							//配置为输出
	gpio_config_od(port, pin, DISABLE);							//禁止OD输出

	while(1)
	{	 	
		gpio_setio(port, pin, HIGH);
		delay_ms(500);

		gpio_setio(port, pin, LOW);
		delay_ms(500);		
	} 
}

/************************************************************************
 * function   : gpio_irq_test
 * description: gpio_irq_test
 * input 	  : uint8_t port ：gpio端口选择 GPIOA\GPIOB\GPIOC...
 *              uint8_t pin  ：gpio引脚选择 PIN0\PIN1\PIN2....
 *              void (*pfunc)(uint8_t val_ris) : 引脚中断回调函数
 * return     : none
 ************************************************************************/
 void gpio_irq_test(uint8_t port, uint8_t pin, void (*pfunc)(uint8_t val_ris))
 {
	printfS("GPIO interrupt test.\n\r");

	gpio_init(port, pin,0);										//时钟初始化，模块正常工作
	
	gpio_dir(port, pin, GPIO_DIR_IN);							//配置为输入
	 
	gpio_in_enable(port, pin, ENABLE);							//输入使能

	gpio_config_pu(port, pin, ENABLE);							//上拉使能	
	
	gpio_set_is(port, pin, GPIO_IS_EDGE);						//EDGE触发中断

	gpio_set_ibe(port, pin, GPIO_IBE_SINGLE);					//单边触发
	
	gpio_set_iev(port, pin, GPIO_IEV_FALLEDGEORLOWLEVEL);		//下降沿触发
	
	gpio_irq_init(port, pin, ENABLE, pfunc);					//使能GPIO模块中断,并注册回调函数
	
     while(1)
	{
		if(gpioa_it_flag)
		{
			gpioa_it_flag = 0;
			printfS("gpioa_%d_int is happened!\r\n", pin);
		}
		else if(gpiob_it_flag)
		{
			gpiob_it_flag = 0;
			printfS("gpiob_%d_int is happened!\r\n", pin);
		}
		else if(gpioc_it_flag)
		{
			gpioc_it_flag = 0;
			printfS("gpioc_%d_int is happened!\r\n", pin);
		}
		else if(gpiod_it_flag)
		{
			gpiod_it_flag = 0;
			printfS("gpiod_%d_int is happened!\r\n", pin);
	    }
		else if(gpioe_it_flag)
		{
			gpioe_it_flag = 0;
			printfS("gpioe_%d_int is happened!\r\n", pin);
	    }
		else if(gpiof_it_flag)
		{
			gpiof_it_flag = 0;
			printfS("gpiof_%d_int is happened!\r\n", pin);
	    }
		else if(gpiog_it_flag)
		{
			gpiog_it_flag = 0;
			printfS("gpiog_%d_int is happened!\r\n", pin);
	    }
	}
 }
 
/************************************************************************
 * 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 gpio! \n");
  
	gpio_in_test(GPIOE,PIN0);                    //PE0引脚设置为输入

//	gpio_out_test(GPIOB,PIN6);                   //PB6引脚设置为输出
	
//  gpio_irq_test(GPIOE, PIN5, gpioa_irq_func);  //PE5引脚设置输入，下降沿触发中断
 
    
}
