//! \file spi.h
//! \author tkl
//! \date Apr 26, 2012
//! \brief Header file of the architecture independent i2c driver.
#ifndef I2C_H_
#define I2C_H_

//-----------------------------------------------------------------------------
//! \brief Function pointer to the open function.
typedef int (*i2c_fp_open_t)(const void *);

//-----------------------------------------------------------------------------
//! \brief Function pointer to the close function.
typedef int (*i2c_fp_close_t)(const void *);

//-----------------------------------------------------------------------------
//! \brief Function pointer to the write function.
typedef int (*i2c_fp_write_t)(const void *, char addr, const char *buffer,
		unsigned int len);

//-----------------------------------------------------------------------------
//! \brief Function pointer to the read function.
typedef int (*i2c_fp_read_t)(const void *, char addr, char *buffer,
		unsigned int len);

//-----------------------------------------------------------------------------
//! \brief Contains the function pointer to access the i2c driver.
struct i2c_fp {
	const i2c_fp_open_t		open;	//!< Function pointer to the open function.
	const i2c_fp_close_t	close;	//!< Function pointer to the close function.
	const i2c_fp_write_t	write;	//!< Function pointer to the write function.
	const i2c_fp_read_t		read;	//!< Function pointer to the read function.
};

//-----------------------------------------------------------------------------
//! \brief Contains the architecture depended device and the access functions to the i2c driver.
struct i2c {
	const void			*arch_dep_device;	//!< Architecture depended i2c device (i.e. stm32f10x_i2c_t).
	const struct i2c_fp	*fp;				//!< Function pointer for the i2c driver access.
	int					cnt_subscriber;		//!< Number of opened slaves
};

//-----------------------------------------------------------------------------
//! \brief Open i2c device.
//! \param device The device to open.
//! \retval -1 in error case.
int i2c_open(struct i2c *device);

//-----------------------------------------------------------------------------
//! \brief Close i2c device.
//! \param device The device to close.
//! \retval -1 in error case.
int i2c_close(struct i2c *device);

//-----------------------------------------------------------------------------
//! \brief Write to an i2c device.
//! \param device The device to write to.
//! \param addr The i2c address of the i2c sink.
//! \param buffer The buffer to write.
//! \param len The length of the buffer.
//! \retval -1 in error case, otherwise the number of written bytes.
int i2c_write(const struct i2c *device, char addr, const char *buffer,
		unsigned int len);

//-----------------------------------------------------------------------------
//! \brief Read from an i2c device.
//! \param device The device to read from.
//! \param addr The i2c address of the i2c source
//! \param buffer The buffer to read to.
//! \param len The maximum length of the buffer.
//! \retval -1 in error case, otherwise the number of read bytes.
int i2c_read(const struct i2c *device, char addr, char *buffer,
		unsigned int len);

#endif /* I2C_H_ */
