#include "xparameters.h"
#include "xiic.h"
#include "xil_exception.h"
#include "xil_printf.h"
#include "common.h"
#include "i2c.h"

int i2c_send_byte(u32 base_addr, u8 i2c_addr, u8 write_addr, u8 value)
{
	int ret = 0;
	u8 write_buf[2];
	write_buf[0] = write_addr;
	write_buf[1] = value;

	ret = XIic_Send(base_addr, i2c_addr, write_buf, sizeof(write_buf), XIIC_STOP);
	return ret;
}

int i2c_rcv_byte(u32 base_addr, u8 i2c_addr, u8 read_addr, u8 *value)
{
	int ret = 0;
	u8 buf[1];
	buf[0] = read_addr;
	while(XIic_Send(base_addr, i2c_addr, buf, sizeof(buf), XIIC_STOP) != sizeof(buf));

	ret = XIic_Recv(base_addr, i2c_addr, buf, sizeof(buf), XIIC_STOP);
	*value = *buf;
	return ret;
}

int i2c_rcv_bytes(u32 base_addr, u8 i2c_addr, u8 read_addr, u8 *buf, int num)
{
	int ret = 0;
	while(XIic_Send(base_addr, i2c_addr, &read_addr, sizeof(read_addr), XIIC_STOP) != sizeof(read_addr));
	ret = XIic_Recv(base_addr, i2c_addr, buf, num, XIIC_STOP);
	return ret;
}



