/*
 * iic_exec:
 *
 *	Simplified I2C client interface engine.
 *
 *	This and the SMBus routines are the preferred interface for
 *	client access to I2C/SMBus, since many automated controllers
 *	do not provide access to the low-level primitives of the I2C
 *	bus protocol.
 */
int
iic_exec(i2c_tag_t tag, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
    size_t cmdlen, void *vbuf, size_t buflen, int flags)
{
	const uint8_t *cmd = vcmd;
	uint8_t *buf = vbuf;
	int error;
	size_t len;

	/*
	 * Defer to the controller if it provides an exec function.  Use
	 * it if it does.
	 */
	if (tag->ic_exec != NULL)
		return ((*tag->ic_exec)(tag->ic_cookie, op, addr, cmd,
					cmdlen, buf, buflen, flags));

	if ((len = cmdlen) != 0) {
		if ((error = iic_initiate_xfer(tag, addr, flags)) != 0)
			goto bad;
		while (len--) {
			if ((error = iic_write_byte(tag, *cmd++, flags)) != 0)
				goto bad;
		}
	}

	if (I2C_OP_READ_P(op))
		flags |= I2C_F_READ;

	len = buflen;
	while (len--) {
		if (len == 0 && I2C_OP_STOP_P(op))
			flags |= I2C_F_STOP;
		if (I2C_OP_READ_P(op)) {
			/* Send REPEATED START. */
			if ((len + 1) == buflen &&
			    (error = iic_initiate_xfer(tag, addr, flags)) != 0)
				goto bad;
			/* NACK on last byte. */
			if (len == 0)
				flags |= I2C_F_LAST;
			if ((error = iic_read_byte(tag, buf++, flags)) != 0)
				goto bad;
		} else  {
			/* Maybe send START. */
			if ((len + 1) == buflen && cmdlen == 0 &&
			    (error = iic_initiate_xfer(tag, addr, flags)) != 0)
				goto bad;
			if ((error = iic_write_byte(tag, *buf++, flags)) != 0)
				goto bad;
		}
	}

	return (0);
 bad:
	iic_send_stop(tag, flags);
	return (error);
}
Example #2
0
/**************************实现函数********************************************
*函数原型:void iic_addr_test(uint8_t reg, uint8_t *data)
*功  能:尝试读写地址i上的reg
*输    入:reg    目标设备地址
*          addr    寄存器地址
*          addr_num   将要写的数据
*返    回:返回是否成功
*******************************************************************************/ 
void iic_addr_test(uint8_t reg, uint8_t *addr, uint8_t addr_num)
{
	uint8_t i;
	uint8_t temp;
	
	for(i=0;i!=255;i++)
	{
		if(iic_write_byte(i, reg, temp) == IIC_SUCCESS)
			addr[(addr_num--)-2] = i;
		else if(iic_read_byte(i, reg, &temp) == IIC_SUCCESS)
			addr[(addr_num--)-2] = i;
		if(addr_num == 0)
			break;
	}
}
Example #3
0
bool_t pmu_write(u8_t reg, u8_t val)
{
	if(iic_write_byte(0x34, reg, val) == 0)
		return FALSE;
	return TRUE;
}