#ifndef __IICSETUP_H__
#define __IICSETUP_H__
/*--------------------------------------------------------------------------------------
*  @file     IICSetup.h
*  @author   ZhangJing
*  @version  base on stm32f0x   
*  @date     2015.09.11
*  @brief    模拟IIC驱动
---------------------------------------------------------------------------------------*/
#include "stdint.h"

#define TWI_SCL_0 GPIOA->BRR=GPIO_Pin_5//SCL低 PINA05
#define TWI_SCL_1 GPIOA->BSRR=GPIO_Pin_5//SCL高 PINA05
#define TWI_SDA_0 GPIOA->BRR=GPIO_Pin_4//SDA低 PINA04
#define TWI_SDA_1 GPIOA->BSRR=GPIO_Pin_4//SDA高 PINA04
#define TWI_SDA_STATE (GPIOA->IDR&GPIO_Pin_4)//SDA状态

#define RTC_SCL_0 GPIOE->BRR=GPIO_Pin_2//SCL低 PINE02
#define RTC_SCL_1 GPIOE->BSRR=GPIO_Pin_2//SCL高 PINE02
#define RTC_SDA_0 GPIOE->BRR=GPIO_Pin_3//SDA低 PINE03
#define RTC_SDA_1 GPIOE->BSRR=GPIO_Pin_3//SDA高 PINE03
#define RTC_SDA_STATE (GPIOE->IDR&GPIO_Pin_3)//SDA状态

#define TWI_RETRY_COUNT 3 //重试次数

#define TWI_NOP TWI_Delay()
#define RTC_NOP RTC_Delay()

enum ENUM_TWI_REPLY  //应答状态
{
	TWI_NACK=0,
	TWI_ACK=1,
};

enum ENUM_RTC_REPLY//应答状态
{
	RTC_NACK=0,
	RTC_ACK=1,
};

enum ENUM_TWI_BUS_STATE//总线工作状态
{
	TWI_READY=0,
	TWI_BUS_BUSY=1,
	TWI_BUS_ERROR=2,
};

enum ENUM_RTC_BUS_STATE//总线工作状态
{
	RTC_READY=0,
	RTC_BUS_BUSY=1,
	RTC_BUS_ERROR=2,
};

void TWI_Delay(void);
void RTC_Delay(void);
void IICInit( void );
uint8_t TWI_START(void);
void TWI_STOP(void);
void TWI_SendACK(void);
void TWI_SendNACK(void);
uint8_t TWI_WaitAck(void);
uint8_t TWI_SendByte(uint8_t Data);
uint8_t TWI_ReceiveByte(void);

uint8_t RTC_START(void);
void RTC_STOP(void);
void RTC_SendACK(void);
void RTC_SendNACK(void);
uint8_t RTC_WaitAck(void);
uint8_t RTC_SendByte(uint8_t Data);
uint8_t RTC_ReceiveByte(void);

#endif
