#ifndef AT24C128_H
#define AT24C128_H

#define IIC_WP_PORT     GPIOE
#define IIC_WP_PIN			GPIO_Pin_15

#define IIC_SDA_PORT    GPIOB
#define IIC_SDA_PIN			GPIO_Pin_11

#define IIC_SCL_PORT		GPIOB
#define IIC_SCL_PIN     GPIO_Pin_10

#define HIGH    1
#define LOW     0
#define PinInput   1
#define PinOutput  0

//https://blog.csdn.net/fireboyorhotboy/article/details/92645607
#define SDA_IN()  {GPIOB->CRH&=0XFFFF0FFF;GPIOB->CRH|=(u32)8<<12;}	//PB11输入模式,(11-8)*4=12
#define SDA_OUT() {GPIOB->CRH&=0XFFFF0FFF;GPIOB->CRH|=(u32)7<<12;}  //PB11输出模式,(11-8)*4=12



#define IIC_SCL    PBout(10) //SCL
#define IIC_SDA    PBout(11) //SDA	
#define READ_SDA   PBin(11)  //输入SDA 


//写保护,低电平关闭，高电平打开
#define IIC_WRITE_PROTECT_ON()   {GPIO_SetBits(IIC_WP_PORT, IIC_WP_PIN);} // 禁止写入
#define IIC_WRITE_PROTECT_OFF()  {GPIO_ResetBits(IIC_WP_PORT, IIC_WP_PIN);} // 可以写入

#define E_DELAY	2		// 两个EEPROM命令之间的间隔

extern u8 iic_cmd_delay;

extern OS_SEM i2c_sem;

void I2C_init(void);
extern u8 AT24CXX_ReadOneByte(u32 ReadAddr);
extern void AT24CXX_WriteOneByte(u32 WriteAddr,u8 DataToWrite);
extern void AT24CXX_Read(u32 ReadAddr,u8 *pBuffer,u16 NumToRead);
extern void AT24CXX_Write(u32 WriteAddr,u8 *pBuffer,u16 NumToWrite);
extern void AT24CXX_clr(u32 WriteAddr,u32 NumToWrite);

extern void IIC_Start1(void);
void IIC_Stop(void);
u8 IIC_Wait_Ack(void);
void IIC_Ack(void);
void IIC_NAck(void);
void IIC_Send_Byte(u8 txd);

void delay_nop(void);
#define EEPROM_INFO(fmt,arg...)           OTP_TRACE(fmt,##arg)//trace_otp_trace1(uart_msg_send, 4, fmt, ##arg);
//printf("<<-EEPROM-INFO->> "fmt"\n",##arg)
#define EEPROM_ERROR(fmt,arg...)          //printf("<<-EEPROM-ERROR->> "fmt"\n",##arg)
#define EEPROM_DEBUG(fmt,arg...)          /*do{\
	                                          if(EEPROM_DEBUG_ON)\
	                                          printf("<<-EEPROM-DEBUG->> [%d]"fmt"\n",__LINE__, ##arg);\
                                          }while(0)*/

#endif
