#ifndef _I2C_H_
#define _I2C_H_

#ifndef _XTAL_FREQ		/* postavi frekvenciju */
#define _XTAL_FREQ 4000000
#endif

#define I2C_MODULE 1

#ifdef I2C_MODULE
#define SCL RB6		/* clock on port B bit 6 */
#define SCL_DIR	TRISB6		/* define direction */
#define SDA RB4		/* data on port B pin 4 */
#define SDA_DIR TRISB4		/* define direction */
#define I2CTRIS TRISB
#define MASTER_MODE 0B1011
#define SSPMode(val) SSPCON &=0xF0; SSPCON|=(val & 0xf)

#else
#define SCL RB6		/* clock on port B bit 7 */
#define SCL_DIR	TRISB6		/* define direction */
#define SDA RB4		/* data on port B pin 5 */
#define SDA_DIR TRISB4		/* define direction */
#define I2CTRIS TRISB

#endif

#define M_SDA_INP 0x10
#define M_SDA_OUT 0xEF
#define M_SCL_INP 0x40
#define M_SCL_OUT 0xBF

#define I2C_INPUT 1		/* data direction input */
#define I2C_OUTPUT 0	/* data direction output */

#define I2C_READ 0x01	/* read bit used with address */
#define I2C_WRITE 0x00	/* write bit used with address */

#define FALSE 0
#define TRUE !FALSE

#define I2C_ERROR (-1)
#define I2C_LAST FALSE	/* SendAck: no more bytes to send */
#define I2C_MORE TRUE	/* SendAck: more bytes to send */

#define i2c_start()		i2c_restart()
#define i2c_write_to(address)	i2c_open((address), I2C_WRITE)
#define i2c_read_from(address)	i2c_open((address), I2C_READ)

#ifdef I2C_MODULE
#define SCL_HIGH() SCL_DIR = I2C_INPUT
#define SCL_LOW() SCL_DIR = I2C_OUTPUT
#define SDA_HIGH() SDA_DIR = I2C_INPUT
#define SDA_LOW() SDA_DIR = I2C_OUTPUT
#else
#define SCL_HIGH() SCL = 1; SCL_DIR = I2C_OUTPUT
#define SCL_LOW() SCL = 0; SCL_DIR = I2C_OUTPUT
#define SDA_HIGH() SDA = 1; SDA_DIR = I2C_OUTPUT
#define SDA_LOW() SDA = 0; SDA_DIR = I2C_OUTPUT
#endif

#define I2C_TM_BUS_FREE		5
#define	I2C_TM_START_SU		5
#define I2C_TM_START_HD		4
#define I2C_TM_SCL_LOW		5
#define	I2C_TM_SCL_HIGH		4
#define I2C_TM_DATA_SU		1
#define I2C_TM_DATA_HD          0
#define I2C_TM_SCL_TO_DATA	4	/* SCL low to data valid */
#define	I2C_TM_STOP_SU		4
#define I2C_TM_SCL_TMO		10	/* clock time out */


extern signed char	i2c_read_ack(void);
extern unsigned char	i2c_send_address(unsigned char, unsigned char);
extern unsigned char	i2c_send_byte(unsigned char);
extern unsigned char		i2c_read_byte(void);
extern void		i2c_restart(void);
extern void		i2c_stop(void);
extern void		i2c_send_ack(unsigned char);
extern signed char	i2c_put_byte(unsigned char);
extern int		i2c_get_byte(unsigned char);
extern unsigned char	i2c_open(unsigned char, unsigned char);
extern unsigned char	i2c_get_string(unsigned char *, unsigned char);
extern int		i2c_put_string(const unsigned char *, unsigned char);
extern unsigned char	i2c_wait_for_SCL(void);
extern void 		i2c_free(void);
extern unsigned char	i2c_read(unsigned char);
#endif	