#include "includes.h"
#include "trace.h"
#include "at24cxx.h"

uint16_t EEPROM_ADDRESS;

static __IO uint32_t  I2CTimeout = I2CT_LONG_TIMEOUT;   
static uint32_t I2C_TIMEOUT_UserCallback(uint8_t errorCode);

/**
  * @brief  Basic management of the timeout situation.
  * @param  errorCode：错误代码，可以用来定义哪个环节出错
  * @retval 返回0，表示I2C读取失败
  */
static  uint32_t I2C_TIMEOUT_UserCallback(uint8_t errorCode)
{
  /* Block communication and all processes */
  EEPROM_ERROR("I2C 等待超时!errorCode = %d",errorCode);

  return 0;
}
//#define NEW_IIC_TYPE
#ifdef NEW_IIC_TYPE
/**
  * I2C I配置
  */
static void I2C_GPIO_Config(void)
{

  GPIO_InitTypeDef  GPIO_InitStructure; 

  /*!< EEPROM_I2C Periph clock enable */
  EEPROM_I2C_CLK_INIT(EEPROM_I2C_CLK, ENABLE);

  /*!< EEPROM_I2C_SCL_GPIO_CLK and EEPROM_I2C_SDA_GPIO_CLK Periph clock enable */
  RCC_APB2PeriphClockCmd(EEPROM_I2C_SCL_GPIO_CLK | EEPROM_I2C_SDA_GPIO_CLK, ENABLE);

  /*!< GPIO configuration */
  /* Connect PXx to I2C_SCL*/
  //GPIO_PinAFConfig(EEPROM_I2C_SCL_GPIO_PORT, EEPROM_I2C_SCL_SOURCE, EEPROM_I2C_SCL_AF);
  /* Connect PXx to I2C_SDA*/
  //GPIO_PinAFConfig(EEPROM_I2C_SDA_GPIO_PORT, EEPROM_I2C_SDA_SOURCE, EEPROM_I2C_SDA_AF);  

  /*!< Configure EEPROM_I2C pins: SCL */   
  GPIO_InitStructure.GPIO_Pin = EEPROM_I2C_SCL_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(EEPROM_I2C_SCL_GPIO_PORT, &GPIO_InitStructure);

  /*!< Configure EEPROM_I2C pins: SDA */
  GPIO_InitStructure.GPIO_Pin = EEPROM_I2C_SDA_PIN;
  GPIO_Init(EEPROM_I2C_SDA_GPIO_PORT, &GPIO_InitStructure);

}

/**
  * I2C 工作模式配置
  */
static void I2C_Mode_Configu(void)
{
  I2C_InitTypeDef  I2C_InitStructure; 

  /* I2C 配置 */
  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;    
  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;                            /* 高电平数据稳定，低电平数据变化，I2C时钟线的占空比 */
  I2C_InitStructure.I2C_OwnAddress1 =I2C_OWN_ADDRESS7; 
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable ;  
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; /* I2C的寻址模式 */
  I2C_InitStructure.I2C_ClockSpeed = I2C_Speed;                             /* 通信速率 */
  I2C_Init(EEPROM_I2C, &I2C_InitStructure);                                       /* I2C初始化 */
  I2C_Cmd(EEPROM_I2C, ENABLE);                                                      /* 使能I2C */

  I2C_AcknowledgeConfig(EEPROM_I2C, ENABLE);  
}

/**
  * I2C外设（EEPROM）初始化
  */
void I2C_EE_Init(void)
{
  I2C_GPIO_Config(); 

  I2C_Mode_Configu();


/* 根据头文件中的定义选择EEPROM要写入的地址 */
#ifdef EEPROM_Block0_ADDRESS
  /* 选择 EEPROM Block0 来写入 */
  EEPROM_ADDRESS = EEPROM_Block0_ADDRESS;
#endif

#ifdef EEPROM_Block1_ADDRESS  
  EEPROM_ADDRESS = EEPROM_Block1_ADDRESS;
#endif

#ifdef EEPROM_Block2_ADDRESS  
  EEPROM_ADDRESS = EEPROM_Block2_ADDRESS;
#endif

#ifdef EEPROM_Block3_ADDRESS  
  EEPROM_ADDRESS = EEPROM_Block3_ADDRESS;
#endif
}

/**
  * @brief   将缓冲区的数据写入到EEPROM中
  * @param   
  *     @arg pBuffer:缓冲区指针
  *     @arg WriteAddr:写地址
  *     @arg NumByteToWrite:写的字节数
  */
void I2C_EE_BufferWrite(u8* pBuffer, u8 WriteAddr, u16 NumByteToWrite)
{
  u8 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0;

  Addr = WriteAddr % I2C_PageSize;
  count = I2C_PageSize - Addr;
  NumOfPage =  NumByteToWrite / I2C_PageSize;
  NumOfSingle = NumByteToWrite % I2C_PageSize;

  /* If WriteAddr is I2C_PageSize aligned  */
  if(Addr == 0) 
  {
    /* If NumByteToWrite < I2C_PageSize */
    if(NumOfPage == 0) 
    {
      I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
      I2C_EE_WaitEepromStandbyState();
    }
    /* If NumByteToWrite > I2C_PageSize */
    else  
    {
      while(NumOfPage--)
      {
        I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize); 
        I2C_EE_WaitEepromStandbyState();
        WriteAddr +=  I2C_PageSize;
        pBuffer += I2C_PageSize;
      }

      if(NumOfSingle!=0)
      {
        I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
        I2C_EE_WaitEepromStandbyState();
      }
    }
  }
  /* If WriteAddr is not I2C_PageSize aligned  */
  else 
  {
    /* If NumByteToWrite < I2C_PageSize */
    if(NumOfPage== 0) 
    {
      I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
      I2C_EE_WaitEepromStandbyState();
    }
    /* If NumByteToWrite > I2C_PageSize */
    else
    {
      NumByteToWrite -= count;
      NumOfPage =  NumByteToWrite / I2C_PageSize;
      NumOfSingle = NumByteToWrite % I2C_PageSize;  

      if(count != 0)
      {  
        I2C_EE_PageWrite(pBuffer, WriteAddr, count);
        I2C_EE_WaitEepromStandbyState();
        WriteAddr += count;
        pBuffer += count;
      } 

      while(NumOfPage--)
      {
        I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize);
        I2C_EE_WaitEepromStandbyState();
        WriteAddr +=  I2C_PageSize;
        pBuffer += I2C_PageSize;  
      }
      if(NumOfSingle != 0)
      {
        I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle); 
        I2C_EE_WaitEepromStandbyState();
      }
    }
  }  
}

/**
  * @brief   写一个字节到EEPROM中
  * @param   
  *     @arg pBuffer:缓冲区指针
  *     @arg WriteAddr:写地址
  */
uint32_t I2C_EE_ByteWrite(u8* pBuffer, u8 WriteAddr)
{
  /* Send STRAT condition */
  I2C_GenerateSTART(EEPROM_I2C, ENABLE);

  I2CTimeout = I2CT_FLAG_TIMEOUT;

  /* Test on EV5 and clear it */
  while(!I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_MODE_SELECT))
  {
    if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(0);
  }    

  /* Send EEPROM address for write */
  I2C_Send7bitAddress(EEPROM_I2C, EEPROM_ADDRESS, I2C_Direction_Transmitter);


  I2CTimeout = I2CT_FLAG_TIMEOUT;
  /* Test on EV6 and clear it */
  while(!I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
  {
    if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(1);
  }    

  /* Send the EEPROM's internal address to write to */
  I2C_SendData(EEPROM_I2C, WriteAddr);

  I2CTimeout = I2CT_FLAG_TIMEOUT;

  /* Test on EV8 and clear it */
  while(!I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))  
  {
    if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(2);
  } 
  /* Send the byte to be written */
  I2C_SendData(EEPROM_I2C, *pBuffer); 

  I2CTimeout = I2CT_FLAG_TIMEOUT;

  /* Test on EV8 and clear it */
  while(!I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
  {
    if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(3);
  } 

  /* Send STOP condition */
  I2C_GenerateSTOP(EEPROM_I2C, ENABLE);

  return 1;
}

/**
  * @brief   在EEPROM一个写循环中可以写多个字节，但一次写入的字节数不能超过EEPROM页的大小，AT24C02每页有8个字节
  * @param   
  *     @arg pBuffer:缓冲区指针
  *     @arg WriteAddr:写地址
  *     @arg NumByteToWrite:写的字节数
  */
uint32_t I2C_EE_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite)
{
  I2CTimeout = I2CT_LONG_TIMEOUT;

  while(I2C_GetFlagStatus(EEPROM_I2C, I2C_FLAG_BUSY))  
   {
    if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(4);
  } 

  /* Send START condition */
  I2C_GenerateSTART(EEPROM_I2C, ENABLE);


  I2CTimeout = I2CT_FLAG_TIMEOUT;

  /* Test on EV5 and clear it */
  while(!I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_MODE_SELECT))
  {
    if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(5);
  } 

  /* Send EEPROM address for write */
  I2C_Send7bitAddress(EEPROM_I2C, EEPROM_ADDRESS, I2C_Direction_Transmitter);

  I2CTimeout = I2CT_FLAG_TIMEOUT;

  /* Test on EV6 and clear it */
  while(!I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) 
  {
    if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(6);
  } 
  /* Send the EEPROM's internal address to write to */    
  I2C_SendData(EEPROM_I2C, WriteAddr);  

  I2CTimeout = I2CT_FLAG_TIMEOUT;

  /* Test on EV8 and clear it */
  while(! I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) 
  {
    if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(7);
  } 
  /* While there is data to be written */
  while(NumByteToWrite--)  
  {
    /* Send the current byte */
    I2C_SendData(EEPROM_I2C, *pBuffer); 

    /* Point to the next byte to be written */
    pBuffer++; 

    I2CTimeout = I2CT_FLAG_TIMEOUT;

    /* Test on EV8 and clear it */
    while (!I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
    {
    if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(8);
    } 
  }

  /* Send STOP condition */
  I2C_GenerateSTOP(EEPROM_I2C, ENABLE);

  return 1;
}

/**
  * @brief   从EEPROM里面读取一块数据
  * @param   
  *     @arg pBuffer:存放从EEPROM中读取的数据的缓冲区指针
  *     @arg WriteAddr:接收数据的EEPROM的地址
  *     @arg NumByteToWrite:要读取的字节数
  */
uint32_t I2C_EE_BufferRead(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead)
{  
    I2CTimeout = I2CT_LONG_TIMEOUT;

  //*((u8 *)0x4001080c) |=0x80; 
    while(I2C_GetFlagStatus(EEPROM_I2C, I2C_FLAG_BUSY))   
    {
    if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(9);
    }
  /* Send START condition */
  I2C_GenerateSTART(EEPROM_I2C, ENABLE);
  //*((u8 *)0x4001080c) &=~0x80;

  I2CTimeout = I2CT_FLAG_TIMEOUT;

  /* Test on EV5 and clear it */
  while(!I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_MODE_SELECT))
  {
    if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(10);
   }

  /* Send EEPROM address for write */
  I2C_Send7bitAddress(EEPROM_I2C, EEPROM_ADDRESS+1, I2C_Direction_Transmitter);

  I2CTimeout = I2CT_FLAG_TIMEOUT;

  /* Test on EV6 and clear it */
  while(!I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) 
    {
    if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(11);
   }
  /* Clear EV6 by setting again the PE bit */
  I2C_Cmd(EEPROM_I2C, ENABLE);

  /* Send the EEPROM's internal address to write to */
  I2C_SendData(EEPROM_I2C, ReadAddr);  

     I2CTimeout = I2CT_FLAG_TIMEOUT;

  /* Test on EV8 and clear it */
  while(!I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
    {
    if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(12);
   }
  /* Send STRAT condition a second time */  
  I2C_GenerateSTART(EEPROM_I2C, ENABLE);

     I2CTimeout = I2CT_FLAG_TIMEOUT;

  /* Test on EV5 and clear it */
  while(!I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_MODE_SELECT))
    {
    if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(13);
   }
  /* Send EEPROM address for read */
  I2C_Send7bitAddress(EEPROM_I2C, EEPROM_ADDRESS, I2C_Direction_Receiver);

     I2CTimeout = I2CT_FLAG_TIMEOUT;

  /* Test on EV6 and clear it */
  while(!I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
    {
    if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(14);
   }
  /* While there is data to be read */
  while(NumByteToRead)  
  {
    if(NumByteToRead == 1)
    {
      /* Disable Acknowledgement */
      I2C_AcknowledgeConfig(EEPROM_I2C, DISABLE);

      /* Send STOP Condition */
      I2C_GenerateSTOP(EEPROM_I2C, ENABLE);
    }


        I2CTimeout = I2CT_LONG_TIMEOUT;
        while(I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED)==0)  
        {
            if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(3);
        }   
        {
          /* Read a byte from the device */
      *pBuffer = I2C_ReceiveData(EEPROM_I2C);

      /* Point to the next location where the byte read will be saved */
      pBuffer++; 

      /* Decrement the read bytes counter */
      NumByteToRead--;
        }           
  }

  /* Enable Acknowledgement to be ready for another reception */
  I2C_AcknowledgeConfig(EEPROM_I2C, ENABLE);

  return 1;
}

/**
  * @brief  Wait for EEPROM Standby state 
  */
void I2C_EE_WaitEepromStandbyState(void)      
{
  vu16 SR1_Tmp = 0;

  do
  {
    /* Send START condition */
    I2C_GenerateSTART(EEPROM_I2C, ENABLE);
    /* Read EEPROM_I2C SR1 register */
    SR1_Tmp = I2C_ReadRegister(EEPROM_I2C, I2C_Register_SR1);
    /* Send EEPROM address for write */
    I2C_Send7bitAddress(EEPROM_I2C, 0xA1, I2C_Direction_Transmitter);
  }while(!(I2C_ReadRegister(EEPROM_I2C, I2C_Register_SR1) & 0x0002));

  /* Clear AF flag */
  I2C_ClearFlag(EEPROM_I2C, I2C_FLAG_AF);
  /* STOP condition */    
  I2C_GenerateSTOP(EEPROM_I2C, ENABLE); 
}
#else
/*i2c的初始化*/
void I2C_EE_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	I2C_InitTypeDef I2C_InitStruct;
	
	EEPROM_I2C_CLK_INIT(EEPROM_I2C_CLK,ENABLE);
	RCC_APB2PeriphClockCmd(EEPROM_I2C_SCL_GPIO_CLK|EEPROM_I2C_SDA_GPIO_CLK,ENABLE);
	//GPIO的配置
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_OD;
	GPIO_InitStruct.GPIO_Pin=EEPROM_I2C_SCL_PIN|EEPROM_I2C_SDA_PIN;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStruct);
	
	//IIC的配置
	I2C_InitStruct.I2C_Ack=I2C_Ack_Enable;
	I2C_InitStruct.I2C_AcknowledgedAddress=I2C_AcknowledgedAddress_7bit;
	I2C_InitStruct.I2C_ClockSpeed=I2C_Speed;
	I2C_InitStruct.I2C_DutyCycle=I2C_DutyCycle_2;
	I2C_InitStruct.I2C_Mode=I2C_Mode_I2C;
	I2C_InitStruct.I2C_OwnAddress1=I2Cx_OWN_ADDRESS7;
	I2C_Init(EEPROM_I2C,&I2C_InitStruct);
	
	I2C_Cmd(EEPROM_I2C,ENABLE);
	
	EEPROM_ADDRESS = EEPROM_Block0_ADDRESS;
}
 
/*写入一个字节*/
uint32_t IIC_WriteByte(u16 addr,u8 data)
{
	I2CTimeout = I2CT_FLAG_TIMEOUT;
	
	I2C_EE_WaitEepromStandbyState();
	I2C_GenerateSTART(EEPROM_I2C,ENABLE);//发送起始信号
	while(!I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_MODE_SELECT))//检测	EV5事件
	{
		if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(0);
	}	

	I2CTimeout = I2CT_FLAG_TIMEOUT;
	
	I2C_Send7bitAddress(EEPROM_I2C,EEPROM_ADDRESS,I2C_Direction_Transmitter);//发送7位EEPROM的硬件地址
	while(!I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))//检测EV6事件
	{
		if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(1);
	}	

	I2CTimeout = I2CT_FLAG_TIMEOUT;
	
	I2C_SendData(EEPROM_I2C,(u8)(addr>>8));//发送操作的内存地址
	while(!I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTED))//检测EV8事件
	{
		if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(2);
	}	

	I2CTimeout = I2CT_FLAG_TIMEOUT;

	I2C_SendData(EEPROM_I2C,(u8)(addr&0xff));//发送操作的内存地址
	while(!I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTED))//检测EV8事件
	{
		if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(2);
	}	

	I2CTimeout = I2CT_FLAG_TIMEOUT;
	
	I2C_SendData(EEPROM_I2C,data);//要写入的数据（一个字节）
	while(!I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTED))//检测EV8事件
	{
		if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(3);
	}	
	
	I2C_GenerateSTOP(EEPROM_I2C,ENABLE);//发送结束信号
	
}
 
/*读取一个字节*/
u8 IIC_ReadByte(u16 addr)
{
	u8 readtemp;

	I2CTimeout = I2CT_FLAG_TIMEOUT;
	I2C_EE_WaitEepromStandbyState();//等待EEPROM释放总线
	
	I2C_GenerateSTART(EEPROM_I2C,ENABLE);
	while(!I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_MODE_SELECT))//5
	{
		if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(4);
	}

	I2CTimeout = I2CT_FLAG_TIMEOUT;
	
	I2C_Send7bitAddress(EEPROM_I2C,EEPROM_ADDRESS,I2C_Direction_Transmitter);
	while(!I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))//6
	{
		if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(5);
	}
	
	I2CTimeout = I2CT_FLAG_TIMEOUT;
	
	I2C_SendData(EEPROM_I2C,(u8)(addr>>8));
    while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTED) != SUCCESS )
    {
 		if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(6);
	}	

	I2CTimeout = I2CT_FLAG_TIMEOUT;
	
	I2C_SendData(EEPROM_I2C,(u8)(addr&0xff));
    while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTED) != SUCCESS )
    {
 		if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(6);
	}	

	I2CTimeout = I2CT_FLAG_TIMEOUT;
	
	I2C_GenerateSTART(EEPROM_I2C,ENABLE);
	while(!I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_MODE_SELECT))
	{
		if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(7);
	}	

	I2CTimeout = I2CT_FLAG_TIMEOUT;
	
	I2C_Send7bitAddress(EEPROM_I2C,EEPROM_ADDRESS|0x1,I2C_Direction_Receiver);
	while(!I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
	{
		if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(8);
	}	

	I2CTimeout = I2CT_FLAG_TIMEOUT;
	
	I2C_AcknowledgeConfig(EEPROM_I2C,DISABLE);

	/* 停止信号 */
	I2C_GenerateSTOP(EEPROM_I2C,ENABLE);
 
	/* 检测 EV7 事件 */
	while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_BYTE_RECEIVED) != SUCCESS )
	{
		if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(9);
	}	
  
	/* 读取接收数据 */
	readtemp = I2C_ReceiveData(EEPROM_I2C);

	return readtemp;
}
 
 
/*等待eeprom释放总线*/
void I2C_EE_WaitEepromStandbyState(void)      
{
  do
  {
    I2C_GenerateSTART(EEPROM_I2C, ENABLE);
    while(I2C_GetFlagStatus(EEPROM_I2C,I2C_FLAG_SB)==ERROR);//检测EV5事件
		I2C_Send7bitAddress(EEPROM_I2C,0xA1,I2C_Direction_Transmitter);
  }while(I2C_GetFlagStatus(EEPROM_I2C,I2C_FLAG_ADDR)==ERROR);//
    I2C_ClearFlag(EEPROM_I2C, I2C_FLAG_AF);//清楚标志位
    I2C_GenerateSTOP(EEPROM_I2C, ENABLE); //结束信号
}
 
/*	向eeprom中写入n个数据*/
void I2C_EE_BufferWrite(u8* pBuffer, u8 WriteAddr, u16 NumByteToWrite)
{
	if((WriteAddr+NumByteToWrite) > 255)//避免写入的数据超过总的内存
	{
		EEPROM_INFO("refuse to write\r\n");
		EEPROM_INFO("please enter less than %d char\r\n",(255-WriteAddr));
	}
	else
	{
		EEPROM_INFO("allow to write\r\n");
		while(NumByteToWrite)
		{
			IIC_WriteByte(WriteAddr,*pBuffer);
			WriteAddr++;
			pBuffer++;
			NumByteToWrite--;
		}
		EEPROM_INFO("write success\r\n");
	}
}
 
uint32_t I2C_EE_BufferRead(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead)
{
	while(NumByteToRead)
	{
		*pBuffer=IIC_ReadByte(ReadAddr);
		ReadAddr++;
		pBuffer++;
		NumByteToRead--;
	}
	
}
#endif
u8 I2c_Buf_Write[256], I2c_Buf_Read[256];
/*********************************************END OF FILE**********************/

/**
* @brief I2C(AT24C02)读写测试
* @param 无
* @retval 正常返回 1 ，不正常返回 0
*/
uint8_t I2C_Test(void)
{
	u16 i;

	I2C_EE_Init();

	EEPROM_INFO("\r\n写入的数据\r\n");

	for ( i=0; i<=255; i++ ) //填充缓冲
	{
		I2c_Buf_Write[i] = i;

		EEPROM_INFO("0x%02X ", I2c_Buf_Write[i]);
		if (i%16 == 15)
			EEPROM_INFO("\r\n");
	}

	//将 I2c_Buf_Write 中顺序递增的数据写入 EERPOM 中
	//页写入方式
	// I2C_EE_BufferWrite( I2c_Buf_Write, EEP_Firstpage, 256);
	//字节写入方式
	I2C_EE_BufferWrite( I2c_Buf_Write, 0, 128);

	EEPROM_INFO("\r\n写结束\r\n");

	EEPROM_INFO("读出的数据\r\n");
	//将 EEPROM 读出数据顺序保持到 I2c_Buf_Read 中
	I2C_EE_BufferRead(I2c_Buf_Read, 0, 128);

	//将 I2c_Buf_Read 中的数据通过串口打印
	for (i=0; i<256; i++)
	{
	#if 0
		if (I2c_Buf_Read[i] != I2c_Buf_Write[i])
		{
			EEPROM_ERROR("0x%02X ", I2c_Buf_Read[i]);
			EEPROM_ERROR("错误:I2C EEPROM 写入与读出的数据不一致\r\n");
			return 0;
		}
	#endif
		EEPROM_INFO("0x%02X ", I2c_Buf_Read[i]);
		if (i%16 == 15)
			EEPROM_INFO("\r\n");
	}
	EEPROM_INFO("\r\nI2C(AT24C02)读写测试成功\r\n");

	return 1;
}
