/**
******************************************************************************************************************************************************************************************
*@file                       user_uart.c
*@author         
*@data                       
*@version                    v1.0
*@par Copyright(c):
*
*@par History:
*     version:author,date,desc
*     v1.0   :create this file
*
*******************************************************************************************************************************************************************************************
*/
#include "sdk_include.h"
#if IIC_MASTER_IRQ
i2c_init_type_def I2C_InitStruct;


void I2C_IRQHandler(void)
{
	uint32_t temp;
	temp = I2C->IC_INTR_STAT;
	dbg_printf("I2C Interrupt stat In %x\n",temp);
	if (i2c_get_it_status(I2C, I2C_IT_TX_ABRT))
	{
		dbg_printf("I2C_IT_TX_ABRT Interrupt In\n");
		i2c_it_config(I2C, I2C_IT_TX_ABRT, DISABLE);
		i2c_clear_it_pending_bit(I2C, I2C_IT_TX_ABRT);
	}

	if (i2c_get_it_status(I2C, I2C_IT_ACTIVITY))
	{
		dbg_printf("I2C_IT_ACTIVITY Interrupt In\n");
		i2c_it_config(I2C, I2C_IT_ACTIVITY, DISABLE);
		i2c_clear_it_pending_bit(I2C, I2C_IT_ACTIVITY);
	}

	if (i2c_get_it_status(I2C, I2C_IT_START_DET))
	{
		dbg_printf("I2C_IT_START_DET Interrupt In\n");
		i2c_it_config(I2C, I2C_IT_START_DET, DISABLE);
		i2c_clear_it_pending_bit(I2C, I2C_IT_START_DET);
	}

	if (i2c_get_it_status(I2C, I2C_IT_STOP_DET))
	{
		dbg_printf("I2C_IT_STOP_DET Interrupt In\n");
		i2c_it_config(I2C, I2C_IT_STOP_DET, DISABLE);
		i2c_clear_it_pending_bit(I2C, I2C_IT_STOP_DET);
	}

	if (i2c_get_it_status(I2C, I2C_IT_TXE))
	{
		dbg_printf("I2C_IT_TXE Interrupt In\n");
		i2c_it_config(I2C, I2C_IT_TXE, DISABLE);
		i2c_clear_it_pending_bit(I2C, I2C_IT_TXE);
	}

	if (i2c_get_it_status(I2C, I2C_IT_TXOVF))
	{
		dbg_printf("I2C_IT_TXOVF Interrupt In\n");
		i2c_it_config(I2C, I2C_IT_TXOVF, DISABLE);
		i2c_clear_it_pending_bit(I2C, I2C_IT_TXOVF);
	}

	if (i2c_get_it_status(I2C, I2C_IT_RXUDF))
	{
		dbg_printf("I2C_IT_RXUDF Interrupt In\n");
		i2c_it_config(I2C, I2C_IT_RXUDF, DISABLE);
		i2c_clear_it_pending_bit(I2C, I2C_IT_RXUDF);
	}

	if (i2c_get_it_status(I2C, I2C_IT_RXF))
	{
		dbg_printf("I2C_IT_RXF Interrupt In\n");
		i2c_it_config(I2C, I2C_IT_RXF, DISABLE);
		i2c_clear_it_pending_bit(I2C, I2C_IT_RXF);
	}

	if (i2c_get_it_status(I2C, I2C_IT_RXOVF))
	{
		dbg_printf("I2C_IT_RXOVF Interrupt In\n");
		i2c_it_config(I2C, I2C_IT_RXOVF, DISABLE);
		i2c_clear_it_pending_bit(I2C, I2C_IT_RXOVF);
	}
	NVIC_ClearPendingIRQ(I2C_IRQn);
}


void iic_test_demo(void)
{
	int i;
	//config an error TargetAddress to Trigger abrt interrupt
	i2c_it_config(I2C, I2C_IT_TX_ABRT, ENABLE);
	i2c_cmd(I2C, ENABLE);
	
	i2c_write_data_to_dr(I2C, i2c_extend_data(0, I2C_DataCMD_Write, I2C_DataEndCondition_Stop));
	
	i = 0xffff;
	while(i--);

	//config an useful TargetAddress
	I2C_InitStruct.I2C_TargetAddress = 0x50;
	i2c_init(I2C, &I2C_InitStruct);
	i2c_cmd(I2C, ENABLE);

	//I2C_IT_ACTIVITY Interrupt Test
	i2c_it_config(I2C, I2C_IT_ACTIVITY, ENABLE);
	i2c_write_data_to_dr(I2C, i2c_extend_data(0, I2C_DataCMD_Write, I2C_DataEndCondition_Stop));
	i = 0xffff;
	while(i--);
	
	//I2C_IT_START_DET Interrupt Test
	i2c_it_config(I2C, I2C_IT_START_DET, ENABLE);
	i2c_write_data_to_dr(I2C, i2c_extend_data(0, I2C_DataCMD_Write, I2C_DataEndCondition_Stop));
	i = 0xffff;
	while(i--);

	//I2C_IT_STOP_DET Interrupt Test
	i2c_it_config(I2C, I2C_IT_STOP_DET, ENABLE);
	i2c_write_data_to_dr(I2C, i2c_extend_data(0, I2C_DataCMD_Write, I2C_DataEndCondition_Stop));
	i = 0xffff;
	while(i--);

	//I2C_IT_TXE Interrupt Test
	i2c_it_config(I2C, I2C_IT_TXE, ENABLE);
	i2c_write_data_to_dr(I2C, i2c_extend_data(0, I2C_DataCMD_Write, I2C_DataEndCondition_Stop));
	i = 0xffff;
	while(i--);

	//I2C_IT_TXOVF Interrupt Test	
	i2c_it_config(I2C, I2C_IT_TXOVF, ENABLE);
	I2C->IC_ENABLE |= 0x1;
	I2C->IC_TX_TL = 0x01;
	for (i = 0; i < 100; i++)
	{
		i2c_write_data_to_dr(I2C,0);
	}
	i = 0xffff;
	while(i--);	

	//I2C_IT_RXUDF Interrupt Test
	i2c_it_config(I2C, I2C_IT_RXUDF, ENABLE);
	i2c_write_data_to_dr(I2C, i2c_extend_data(0, I2C_DataCMD_Write, I2C_DataEndCondition_Stop));
	i2c_write_data_to_dr(I2C, i2c_extend_data(0, I2C_DataCMD_Read, I2C_DataEndCondition_None));
	i2c_write_data_to_dr(I2C, i2c_extend_data(0, I2C_DataCMD_Read, I2C_DataEndCondition_Stop));

	i2c_read_data_from_dr(I2C);
	i2c_read_data_from_dr(I2C);
	i2c_read_data_from_dr(I2C);
	i2c_read_data_from_dr(I2C);
	i2c_read_data_from_dr(I2C);
	i2c_read_data_from_dr(I2C);
	
	i = 0xffff;
	while(i--);

	//I2C_IT_RXF Interrupt Test
	i2c_it_config(I2C, I2C_IT_RXF, ENABLE);
	I2C->IC_RX_TL = 0x7;
	i2c_write_data_to_dr(I2C, i2c_extend_data(0, I2C_DataCMD_Write, I2C_DataEndCondition_Stop));
	i2c_write_data_to_dr(I2C, i2c_extend_data(0, I2C_DataCMD_Read, I2C_DataEndCondition_None));
	while (!(i2c_get_raw_it_status(I2C, I2C_IT_TXE)));
	for (i = 0; i < 7; i++)
	{
		i2c_write_data_to_dr(I2C, i2c_extend_data(0, I2C_DataCMD_Read, I2C_DataEndCondition_None));
	}
	i2c_write_data_to_dr(I2C, i2c_extend_data(0, I2C_DataCMD_Read, I2C_DataEndCondition_Stop));
	
	i = 0xffff;
	while(i--);
	while (i2c_get_flag_status(I2C, I2C_FLAG_RXNE))
	{
		i2c_read_data_from_dr(I2C);
	}

	//I2C_IT_RXOVF Interrupt Test
	i2c_it_config(I2C, I2C_IT_RXOVF, ENABLE);
	i2c_write_data_to_dr(I2C, i2c_extend_data(0, I2C_DataCMD_Write, I2C_DataEndCondition_Stop));
	i2c_write_data_to_dr(I2C, i2c_extend_data(0, I2C_DataCMD_Read, I2C_DataEndCondition_None));
	while (!(i2c_get_raw_it_status(I2C, I2C_IT_TXE)));
	for (i = 0; i < 8; i++)
	{
		while (!(i2c_get_raw_it_status(I2C, I2C_IT_TXE)));
		i2c_write_data_to_dr(I2C, i2c_extend_data(0, I2C_DataCMD_Read, I2C_DataEndCondition_None));
	}
	i2c_write_data_to_dr(I2C, i2c_extend_data(0, I2C_DataCMD_Read, I2C_DataEndCondition_Stop));
	i = 0xffff;
	while(i--);
	while (i2c_get_flag_status(I2C, I2C_FLAG_RXNE))
	{
		i2c_read_data_from_dr(I2C);
	}

	dbg_printf("\n");
	dbg_printf("Test End\n");
}
#endif


/*****************************************************************************
 函 数 名  : iic_configuration
 功能描述  : iic 配置
 输入参数  : void  
 输出参数  : 无
 返 回 值  : 
*****************************************************************************/
void iic_configuration(void)
{
	i2c_init_type_def i2c_init_struct;
	i2c_struct_init(&i2c_init_struct);
	i2c_init_struct.i2c_clock_speed 		= I2C_ClockSpeed_300KHz;		//速率配置
	i2c_init_struct.i2c_mode 				= I2C_Mode_Master;				//模式选择
	i2c_init_struct.i2c_duty_cycle 			= I2C_DutyCycle_1;				//时钟控制：I2C standard mode Tlow/Thigh = 1
	i2c_init_struct.i2c_target_address 		= 0x24;							//目标地址
	i2c_init_struct.i2c_target_address_mode = I2C_TargetAddressMode_7bit;	//目标地址模式：7bit
	
//I2C GPIO管脚映射
	gpio_remap_config(GPIOD_Pin4,GPIO_Remap_I2C_SCL);
	gpio_remap_config(GPIOD_Pin5,GPIO_Remap_I2C_SDA);

	i2c_init(I2C,&i2c_init_struct);//初始化
	i2c_it_config(I2C,I2C_IT_RXF | I2C_IT_TXE | I2C_IT_RD_REQ | I2C_IT_RX_DONE,ENABLE);//中断使能
	i2c_cmd(I2C,ENABLE);//I2C使能
}



/*****************************************************************************
 函 数 名  : user_iic_example
 功能描述  : iic 用户举例
 输入参数  : void  
 输出参数  : 无
*****************************************************************************/
void user_iic_example(void)
{

	uint8_t buf[10];

//【1】 开启I2C 时钟	
	system_clock_config(ws_i2c,ENABLE);

//【2】 开启I2C 中断
	nvic_init_type_def nvic_init_structure;
	NVIC_SetPriorityGrouping(NVIC_PriorityGroup_2);
	nvic_init_structure.nvic_irq_channel_preemption_priority	= 1;
	nvic_init_structure.nvic_irq_channel_subpriority			= 0;
	nvic_init_structure.nvic_irq_channel_cmd					= ENABLE; 
	nvic_init_structure.nvic_irq_channel						= I2C_IRQn;
	nvic_init(&nvic_init_structure);

//【3】 初始化IIC
	iic_configuration();

//【4】 从0x01寄存器开始读取数据
	i2c_send_data(I2C, 0x01, I2C_DataEndCondition_Stop);

//【5】 读取数据
	i2c_receive_bytes(I2C, buf, 5, I2C_DataEndCondition_Stop);

//【4】 向0x01寄存器开始写入数据
	i2c_send_data(I2C, 0x01, I2C_DataEndCondition_Stop);

//【7】 发送数据
	i2c_send_bytes(I2C, buf, 5, I2C_DataEndCondition_Stop);
}



