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


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_init
 * Description: i2c_init 初始化
 * input : none
 * return: none
 ************************************************************************/
void i2c_init(void)
{
    i2c_master_init(I2C_SPEED_FAST);
}

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

	printfS("\n ----I2C MASTER NORMAL MODE LOOPBACK TEST----");
	printfS("\n i2c_slave_device_addr: 	0x%x\n",  I2C_SLAVE_ADDR);
    
    i2c_init();	
	
	send_buff[0] = 0xAA;    //frame head
	send_buff[1] = 0x10;
	send_buff[2] = 0x04;
	send_buff[3] = 0x44;
	send_buff[4] = 0xaa;
	send_buff[5] = 0xbb;
	send_buff[6] = 0xcc;
	send_buff[7] = 0x55;    //frame tail
	
	while(1) 
	{
        delay_ms(1000);
		
		i2c_master_send(I2C_SLAVE_ADDR, send_buff, length);
				
        delay_ms(5);
		
		if(i2c_master_recv(I2C_SLAVE_ADDR, recv_buff, length) == 1)
		{
			for(i = 0; i < length; i++)
			{
				printfS("\n----i2c master recv_buff[%d] = 0x%x",i, recv_buff[i]);
				recv_buff[i] = 0xFF; //erase for next receive
			}
		}

	}	
}

/************************************************************************
 * function   : i2c_mix_test
 * Description: for i2c mix mode example 
 * input 			: none
 * return			: none
 ************************************************************************/
void i2c_mix_test(void)
{
	uint32_t raddr = 0x20000024; //please modify the real address
	uint32_t waddr = 0x20000034; //please modify the real address
	uint8_t  addr_num = 4;       //address lenth by byte
	uint8_t  data_num = 8;       //read or write data lenth by byte
	uint8_t  i;
	
    i2c_init();	
    
	printfS("\n ----I2C MASTER MIX MODE LOOPBACK TEST----");
	printfS("\n i2c_slave_device_addr: 	0x%x",  I2C_SLAVE_ADDR);
	printfS("\n i2c_slave_write_addr : 	0x%x",	waddr);
	printfS("\n i2c_slave_read_addr  : 	0x%x",	raddr);

	send_buff[0] = 0xAA;
	send_buff[1] = 0x10;
	send_buff[2] = 0x04;
	send_buff[3] = 0x44;
	send_buff[4] = 0xaa;
	send_buff[5] = 0xbb;
	send_buff[6] = 0xcc;
	send_buff[7] = 0x55;
	
	while(1) 
	{
		delay_ms(1000);
				
		i2c_master_send_mix(I2C_SLAVE_ADDR, &waddr, addr_num, send_buff, data_num);		
				
		delay_ms(5); //5ms
		
		if(i2c_master_recv_mix(I2C_SLAVE_ADDR, &raddr, addr_num, recv_buff, data_num) == 1)
		{
			for(i = 0; i < data_num; i++)
			{
				printfS("\n----i2c master recv_buff[%d] = 0x%x",i,recv_buff[i]);
				recv_buff[i] = 0xFF; //erase for next receive
			}	
        }	
	}	
}

/************************************************************************
 * 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
    
}




