/***********************************************************************
 * Copyright (c)  2017 - 2020, Unicmicro Co.,Ltd .
 * All rights reserved.
 * Filename    : app.c
 * Description : app driver source file
 * Author(s)   : wanyi
 * version     : V1.0
 * Modify date : 2020-07-27
 ***********************************************************************/
#include  "system_um800x.h"
#include  "app.h"
#include  "config.h"
#include  "common.h"
#include  "uart0.h"
#include  "i2c.h"

volatile uint8_t rx_flag ;
volatile uint8_t uart0_rx_buf[32];
volatile uint8_t uart0_tx_buf[32];
volatile uint16_t rx_count = 0;
volatile uint16_t tx_count = 0;
/************************************************************************
 * function   : uart0_rec_pro
 * Description: uart0_rec_pro 串口接收处理回调函数
 * input : none
 * return: none
 ************************************************************************/
void uart0_rec_pro(void)
{
	uart0_rx_buf[rx_count++]= uart0_recv_byte();
	rx_flag = 1;	
}

/************************************************************************
 * function   : uart_init
 * Description: uart_init 串口初始化
 * input : none
 * return: none
 ************************************************************************/
void uart_init(void)
{
	rx_flag = 0;
	uart0_init(UART0_BAUD_RATE);
    uart0_irq_init(UART0_IRQ_ENABLE,uart0_rec_pro);
}

void i2c_test(void)
{
	i2c_master_test();

}
	

void i2c_master_test(void)
{	
	i2c_master_send_test();
//	i2c_master_recv_test();
}

void i2c_master_send_test(void)
{
	uint8_t xdata status;
	uint8_t xdata i;
	uint8_t xdata i2c_send_data[10];
	for(i=0 ; i<8; i++)
	{
		i2c_send_data[i] = i+0x10;
	
	}
	printfS("i2c master send start\r\n");
	
	i2c_master_init(MASTER_I2C_SPEED_100K);		

	while(1)
	{
		status = i2c_master_write_data(I2C_SLAVE_ADDR,i2c_send_data,8);
	
		printfS("status = 0x%bx\r\n",status);
		printfS("i2c master send end\r\n");
		delay1ms(1000);
	}

}

void i2c_master_recv_test(void)
{
	uint8_t xdata status;
	uint8_t xdata i2c_recv_data[10];
	printfS("i2c master recv start\r\n");
	
	i2c_master_init(MASTER_I2C_SPEED_100K);

	while(1)
	{
		status = i2c_master_read_data(I2C_SLAVE_ADDR,i2c_recv_data,8);
			
		printfS("status = 0x%bx\r\n",status);
		printfS("i2c master recv end\r\n");
		printfB8(i2c_recv_data,8);
		delay1ms(1000);
	}

}
	