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

volatile uint32_t  	flag_rx = 0;
volatile uint32_t 	rx_count = 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   : i2c_rec_pro
 * Description: i2c 接收处理回调函数
 * input : none
 * return: none
 ************************************************************************/
void i2c_recv_pro(void)
{		
	recv_buff[rx_count++] = i2c_read_byte();
	flag_rx = 0x89;	//状态表示：数据接收中
}
/************************************************************************
 * function   : polling_cmd_frame
 * Description: polling_cmd_frame int mode
 * input : none
 * return: FALSE:receive false, TRUE: receive successful
 ************************************************************************/
uint8_t rx_data_temp[100];
uint8_t polling_cmd_frame(void)
{
	if(flag_rx)	//接收中 
	{
		if(recv_buff[0]!= FRAME_HEAD) 	
		{	
			flag_rx = 0;   
			rx_count = 0;	//回到初始状态
			return FALSE;				
		}	
		else
		{
			if(rx_count > 2)
			{
				if(rx_count == (recv_buff[2]+4))	//完整有效包接收完成
				{
					delay(1000); // >10us wait stop frame
					
                    for(uint8_t i=0;i<rx_count;i++)
                    {
                        rx_data_temp[i] = recv_buff[i];
                    }
                    
					flag_rx = 0;
					rx_count = 0;
                    
					return TRUE;	 					
				}				
			}				
		}
	}
	
	return FALSE;	
}
/************************************************************************
 * function   : i2c_init
 * Description: i2c_init 初始化
 * input : none
 * return: none
 ************************************************************************/
void i2c_init(void)
{
	i2c_slave_init();
	
#ifdef I2C_Rx_INT_MODE
	i2c_irq_init(I2C_IRQ_ENABLE, i2c_recv_pro, NULL);
    
#endif		
}

/************************************************************************
 * function   : i2c_test
 * Description: for i2c normal mode example 
 * input 			: none
 * return			: none
 ************************************************************************/
void i2c_test(void)
{
	uint8_t length = 8;

    i2c_init();
    
	printfS("\n ----I2C SLAVE NORMAL MODE LOOPBACK TEST----");
	printfS("\n i2c_slave_device_addr0: 	0x%x",  REG_I2C_SAD0);
	printfS("\n i2c_slave_device_addr1: 	0x%x",  REG_I2C_SAD1);
	printfS("\n i2c_slave_device_addr2: 	0x%x",  REG_I2C_SAD2);
	printfS("\n i2c_slave_device_addr3: 	0x%x",  REG_I2C_SAD3);
	printfS("\n i2c_slave_device_xaddr: 	0x%x",  REG_I2C_XSAD0);

	while(1)
	{		
#ifdef I2C_Rx_INT_MODE		
		if(polling_cmd_frame() == TRUE)			
#else //查询模式			
		if(i2c_slave_recv_data(recv_buff,length) == TRUE)			
#endif
		{		
			printfS("\n i2c slave(normal) receive   ok!");
		
			//receive ok, loop back
			if(i2c_slave_send_data(recv_buff, length) == TRUE)
				printfS("\n i2c slave(normal) send back ok!");
		}
   }		
}

/************************************************************************
 * function   : i2c_mix_test
 * Description: for i2c mix mode example 
 * input 			: none
 * return			: none
 ************************************************************************/
void i2c_mix_test(void)
{
	uint8_t addr_num = 4;
	uint8_t data_num = 8;

    i2c_init();
    
	printfS("\n ----I2C SLAVE MIX MODE LOOPBACK TEST----");
	printfS("\n i2c_slave_device_addr0: 	0x%x",  REG_I2C_SAD0);
	printfS("\n i2c_slave_device_addr1: 	0x%x",  REG_I2C_SAD1);
	printfS("\n i2c_slave_device_addr2: 	0x%x",  REG_I2C_SAD2);
	printfS("\n i2c_slave_device_addr3: 	0x%x",  REG_I2C_SAD3);
	printfS("\n i2c_slave_device_xaddr: 	0x%x",  REG_I2C_XSAD0);

	while(1)
	{		
#ifdef I2C_Rx_INT_MODE
		if(polling_cmd_frame() == TRUE)	
#else //查询模式			
		if(i2c_slave_mixrecv_data(addr_num, recv_buff, data_num) == TRUE)			
#endif		
		{		
			printfS("\n i2c slave(mix) receive   ok!");
		
			//receive ok, loop back
			if(i2c_slave_mixsend_data(addr_num, data_num) == TRUE)
				printfS("\n i2c slave(mix) send back ok!");
		}
	}		
}

/************************************************************************
 * function  : soc_test
 * Description: for i2c example 
                void i2c_test(void) for normal mode
                void i2c_mix_test(void) for mix mode
 * input 	: none
 * return	: none
 ************************************************************************/
void soc_test(void)
{
    
#if TEST_MODULE == TEST_NORMAL_MODULE
    i2c_test();
    
#elif TEST_MODULE == TEST_MIX_MODULE
    i2c_mix_test();
    
#endif
    
}

