Exemplo n.º 1
0
/* I2C receiving of bytes - does not send an offset byte */
int efx_i2c_recv_bytes(struct efx_i2c_interface *i2c, u8 device_id,
		       u8 *bytes, unsigned int len)
{
	int i;
	int rc;

	EFX_WARN_ON_PARANOID(getsda(i2c) != 1);
	EFX_WARN_ON_PARANOID(getscl(i2c) != 1);
	EFX_WARN_ON_PARANOID(len < 1);

	/* Select device */
	i2c_start(i2c);

	/* Read data from device */
	rc = i2c_send_byte(i2c, i2c_read_cmd(device_id));
	if (rc)
		goto out;

	for (i = 0; i < (len - 1); i++)
		/* Read and acknowledge all but the last byte */
		bytes[i] = i2c_recv_byte(i2c, 1);
	/* Read last byte with no acknowledgement */
	bytes[i] = i2c_recv_byte(i2c, 0);

 out:
	i2c_stop(i2c);
	i2c_release(i2c);

	return rc;
}
Exemplo n.º 2
0
int max77620_regulator_get_status(u32 id)
{
	if (id > REGULATOR_MAX)
		return 0;

	const max77620_regulator_t *reg = &_pmic_regulators[id];

	if (reg->type == REGULATOR_SD)
		return (i2c_recv_byte(I2C_5, 0x3C, MAX77620_REG_STATSD) & reg->status_mask) ? 0 : 1;
	return (i2c_recv_byte(I2C_5, 0x3C, reg->cfg_addr) & 8) ? 1 : 0;
}
bool lidar_read_register(const uint8_t reg, const uint8_t length, uint8_t *data) {
	__disable_irq();
	i2c_start();
	if(!i2c_send_byte((I2C_ADDRESS << 1) | I2C_WRITE)) {
		i2c_stop();
		__enable_irq();
		return false;
	}
	if(!i2c_send_byte(length > 1 ? (reg | CONTINOUS_RW) : reg)) {
		i2c_stop();
		__enable_irq();
		return false;
	}
	i2c_stop();
	i2c_start();
	if(!i2c_send_byte((I2C_ADDRESS << 1) | I2C_READ)) {
		i2c_stop();
		__enable_irq();
		return false;
	}

	for(uint8_t i = 0; i < length; i++) {
		data[i] = i2c_recv_byte(i != (length - 1));
	}
	i2c_stop();
	__enable_irq();

	return true;
}
Exemplo n.º 4
0
/*----------------------------------------------------------------------------*/
uint8_t i2c_receive() {
	while(rx_byte_ctr > 0) {
		// busy wait
		while(!(*I2CSR & I2C_MCF) || !(*I2CSR & I2C_MIF)) /*wait*/;

		if (rx_byte_ctr == 1) { // receiving next-to-last byte, thus turn off auto-ack for stop condition
			*I2CCR |= I2C_TXAK;
		}
		
		if (*I2CSR & I2C_MCF) {
			i2c_recv_byte(); // read new byte
		}

		if (*I2CSR & I2C_MAL) {
			*I2CSR &= ~I2C_MAL; // should be cleared in software
			printf("*** ERROR I2C: Arbitration lost\n");
			// Arbitration lost; ERROR?
		}
	}

	while(!(*I2CSR & I2C_MCF) || !(*I2CSR & I2C_MIF)) /*wait*/;
	if (*I2CSR & I2C_RXAK) {
		// NO acknoledge byte received
		printf("*** ERROR I2C: No ack received\n");
	}
	if (*I2CSR & I2C_MAL) {
		*I2CSR &= ~I2C_MAL; // should be cleared in software
		printf("*** ERROR I2C: Arbitration lost\n");
		// Arbitration lost; ERROR?
	}

	*I2CCR &= ~I2C_MSTA; // stop condition
}
Exemplo n.º 5
0
Arquivo: iic.c Projeto: Fyiyu/X4_Flyer
unsigned char CurrentRead24c512(void)
{
	unsigned char dat;
	
	i2c_start() ;
  i2c_send_byte(0xA1);
	dat = i2c_recv_byte();
	
	return dat;
}
Exemplo n.º 6
0
u32 btn_read()
{
	u32 res = 0;
	if (!gpio_read(GPIO_PORT_X, GPIO_PIN_7))
		res |= BTN_VOL_DOWN;
	if (!gpio_read(GPIO_PORT_X, GPIO_PIN_6))
		res |= BTN_VOL_UP;
	if (i2c_recv_byte(4, 0x3C, 0x15) & 0x4)
		res |= BTN_POWER;
	return res;
}
Exemplo n.º 7
0
void max77620_config_default()
{
	for (u32 i = 1; i <= REGULATOR_MAX; i++)
	{
		i2c_recv_byte(I2C_5, 0x3C, MAX77620_REG_CID4);
		max77620_regulator_config_fps(i);
		max77620_regulator_set_voltage(i, _pmic_regulators[i].mv_default);
		if (_pmic_regulators[i].fps_src != MAX77620_FPS_SRC_NONE)
			max77620_regulator_enable(i, 1);
	}
	i2c_send_byte(I2C_5, 0x3C, MAX77620_REG_SD_CFG2, 4);
}
Exemplo n.º 8
0
unsigned char mpu_read_byte(unsigned char addr){
	unsigned char dat;
	i2c_start();
	i2c_send_byte(SlaveAddress);  //Address for mpu6050
	i2c_send_byte(addr);  //Address for register
	i2c_start();
	i2c_send_byte(SlaveAddress+1);  //Address for mpu6050
	dat=i2c_recv_byte();
	i2c_ack();
	i2c_stop();
	return dat;
}
Exemplo n.º 9
0
Arquivo: iic.c Projeto: Fyiyu/X4_Flyer
unsigned char RandomRead24c512(unsigned char faddr,
											unsigned char saddr)
{
	unsigned char dat;
	
	i2c_start() ;
  i2c_send_byte(0xA0);
  i2c_send_byte(faddr);
	i2c_send_byte(saddr);
  
	i2c_start() ;
	i2c_send_byte(0xA1);
	dat = i2c_recv_byte();
  i2c_stop() ;
	
	return dat;
}
Exemplo n.º 10
0
int max77620_regulator_set_voltage(u32 id, u32 mv)
{
	if (id > REGULATOR_MAX)
		return 0;

	const max77620_regulator_t *reg = &_pmic_regulators[id];

	if (mv < reg->mv_default || mv > reg->mv_max)
		return 0;

	u32 mult = (mv + reg->mv_step - 1 - reg->mv_min) / reg->mv_step;
	u8 val = i2c_recv_byte(I2C_5, 0x3C, reg->volt_addr);
	val = (val & ~reg->volt_mask) | (mult & reg->volt_mask);
	i2c_send_byte(I2C_5, 0x3C, reg->volt_addr, val);
	sleep(1000);

	return 1;
}
Exemplo n.º 11
0
int max77620_regulator_enable(u32 id, int enable)
{
	if (id > REGULATOR_MAX)
		return 0;

	const max77620_regulator_t *reg = &_pmic_regulators[id];

	u32 addr = reg->type == REGULATOR_SD ? reg->cfg_addr : reg->volt_addr;
	u8 val = i2c_recv_byte(I2C_5, 0x3C, addr);
	if (enable)
		val = (val & ~reg->enable_mask) | ((3 << reg->enable_shift) & reg->enable_mask);
	else
		val &= ~reg->enable_mask;
	i2c_send_byte(I2C_5, 0x3C, addr, val);
	sleep(1000);

	return 1;
}
Exemplo n.º 12
0
/**********************************************************************
functionName:void pcf8563_get(uint8_t start,uint8_t num,uint8_t *buf)
description:获取数据
**********************************************************************/
void pcf8563_get(uint8_t start,uint8_t num,uint8_t *buf) 
{
	uint8_t i; 
	i2_start();
	i2c_sendbyte(0xA2);
	i2c_wait_ack();
	i2c_sendbyte(start);
	i2c_wait_ack(); 
	i2_start();
	i2c_sendbyte(0xA3);
	i2c_wait_ack(); 
	for (i=0;i<num;i++)
	{
		buf[i]=i2c_recv_byte();
		if (i!=(num-1)) i2c_send_ack();
	}
	i2c_send_notack(); 
	i2_stop();
}
Exemplo n.º 13
0
void i2c_isr (void) {
	uint8_t dummy;
	if (*I2CSR & I2C_MIF) { // interrupt is from i2c
		if (*I2CSR & I2C_MCF) { // one byte transferred/received. will be cleared automatically when I2CDR is written or I2CSR read
			if (tx_buf_ptr != 0) { // we're sending
				if (*I2CSR & I2C_RXAK) {
					// NO acknoledge byte received
					printf("*** ERROR I2C: No ack received\n");
				}

				if (tx_byte_ctr > 0) { // tx
					i2c_send_byte(); // set new byte, MCF is automatically cleared
				} else {
					*I2CCR &= ~I2C_MSTA; // generate stop condition
				}
			} else { //if (rx_buf_ptr != 0) { // receive
				if (rx_byte_ctr == 1) { // receiving next-to-last byte, thus turn off auto-ack for stop condition
					*I2CCR |= I2C_TXAK;
				}
				if (*I2CCR & I2C_MTX) { // address byte was just sent
					*I2CCR &= ~I2C_MTX; // switch to receive mode
					dummy = *I2CDR; // dummy read to throw away the address from register
					
				} else if (rx_byte_ctr > 0) {
					i2c_recv_byte(); // read new byte
				} else {
					*I2CCR &= ~I2C_MSTA; // generate stop condition
				}
				
			}
		}
		if (*I2CSR & I2C_MAL) {
			*I2CSR &= ~I2C_MAL; // should be cleared in software
			printf("*** ERROR I2C: Arbitration lost\n");
			// Arbitration lost; reset..
			rx_byte_ctr = tx_byte_ctr = 0;
			*I2CCR &= ~I2C_MSTA; // generate stop condition
		}
		
		// clear MIF
		*I2CSR &= ~I2C_MIF;
	}
}
Exemplo n.º 14
0
Arquivo: iic.c Projeto: Fyiyu/X4_Flyer
unsigned char i2c_read(unsigned char dev,
               unsigned char reg,
               unsigned char length,
               unsigned char * data)
{
    unsigned char i ;
    i2c_start() ;
    i2c_send_byte(dev) ;
    i2c_send_byte(reg) ;
    i2c_start() ;
    i2c_send_byte(dev + 1) ;
    for(i = 0; i < length; i ++)
    {
        data[i] = i2c_recv_byte() ;
        if(i == length-1)
            i2c_send_ack(_NACK) ;
        else
            i2c_send_ack(_ACK) ;
    }
    i2c_stop() ;
    return 0 ;
}
Exemplo n.º 15
0
Arquivo: iic.c Projeto: Fyiyu/X4_Flyer
void SequentialRead24c512(unsigned char faddr, 
													unsigned char saddr, 
													unsigned char *dat, 
													unsigned char num)
{
	unsigned char i;
	
	i2c_start();
	i2c_send_byte(0xA0);
  i2c_send_byte(faddr);
	i2c_send_byte(saddr);
  
	i2c_start() ;
	i2c_send_byte(0xA1);
	for(i=0;i<num;i++)
	{
		dat[i] = i2c_recv_byte();
    if(i == num-1)
      i2c_send_ack(_NACK) ;
    else
      i2c_send_ack(_ACK) ;
	}
	i2c_stop();
}
Exemplo n.º 16
0
static int saa7134_i2c_xfer(struct i2c_adapter *i2c_adap,
			    struct i2c_msg *msgs, int num)
{
	struct saa7134_dev *dev = i2c_adap->algo_data;
	enum i2c_status status;
	unsigned char data;
	int addr,rc,i,byte;

  	status = i2c_get_status(dev);
	if (!i2c_is_idle(status))
		if (!i2c_reset(dev))
			return -EIO;

	d2printk("start xfer\n");
	d1printk(KERN_DEBUG "%s: i2c xfer:",dev->name);
	for (i = 0; i < num; i++) {
		if (!(msgs[i].flags & I2C_M_NOSTART) || 0 == i) {
			/* send address */
			d2printk("send address\n");
			addr  = msgs[i].addr << 1;
			if (msgs[i].flags & I2C_M_RD)
				addr |= 1;
			if (i > 0 && msgs[i].flags & I2C_M_RD) {
				/* workaround for a saa7134 i2c bug
				 * needed to talk to the mt352 demux
				 * thanks to pinnacle for the hint */
				int quirk = 0xfd;
				d1printk(" [%02x quirk]",quirk);
				i2c_send_byte(dev,START,quirk);
				i2c_recv_byte(dev);
			}
			d1printk(" < %02x", addr);
			rc = i2c_send_byte(dev,START,addr);
			if (rc < 0)
				 goto err;
		}
		if (msgs[i].flags & I2C_M_RD) {
			/* read bytes */
			d2printk("read bytes\n");
			for (byte = 0; byte < msgs[i].len; byte++) {
				d1printk(" =");
				rc = i2c_recv_byte(dev);
				if (rc < 0)
					goto err;
				d1printk("%02x", rc);
				msgs[i].buf[byte] = rc;
			}
		} else {
			/* write bytes */
			d2printk("write bytes\n");
			for (byte = 0; byte < msgs[i].len; byte++) {
				data = msgs[i].buf[byte];
				d1printk(" %02x", data);
				rc = i2c_send_byte(dev,CONTINUE,data);
				if (rc < 0)
					goto err;
			}
		}
	}
	d2printk("xfer done\n");
	d1printk(" >");
	i2c_set_attr(dev,STOP);
	rc = -EIO;
	if (!i2c_is_busy_wait(dev))
		goto err;
  	status = i2c_get_status(dev);
	if (i2c_is_error(status))
		goto err;
	/* ensure that the bus is idle for at least one bit slot */
	msleep(1);

	d1printk("\n");
	return num;
 err:
	if (1 == i2c_debug) {
		status = i2c_get_status(dev);
		printk(" ERROR: %s\n",str_i2c_status[status]);
	}
	return rc;
}
static int saa7134_i2c_xfer(struct i2c_adapter *i2c_adap,
			    struct i2c_msg *msgs, int num)
{
	struct saa7134_dev *dev = i2c_adap->algo_data;
	enum i2c_status status;
	unsigned char data;
	int addr,rc,i,byte;

	status = i2c_get_status(dev);
	if (!i2c_is_idle(status))
		if (!i2c_reset(dev))
			return -EIO;

	d2printk("start xfer\n");
	d1printk(KERN_DEBUG "%s: i2c xfer:",dev->name);
	for (i = 0; i < num; i++) {
		if (!(msgs[i].flags & I2C_M_NOSTART) || 0 == i) {
			
			d2printk("send address\n");
			addr  = msgs[i].addr << 1;
			if (msgs[i].flags & I2C_M_RD)
				addr |= 1;
			if (i > 0 && msgs[i].flags &
			    I2C_M_RD && msgs[i].addr != 0x40 &&
			    msgs[i].addr != 0x19) {
				int quirk = 0xfe;
				d1printk(" [%02x quirk]",quirk);
				i2c_send_byte(dev,START,quirk);
				i2c_recv_byte(dev);
			}
			d1printk(" < %02x", addr);
			rc = i2c_send_byte(dev,START,addr);
			if (rc < 0)
				 goto err;
		}
		if (msgs[i].flags & I2C_M_RD) {
			
			d2printk("read bytes\n");
			for (byte = 0; byte < msgs[i].len; byte++) {
				d1printk(" =");
				rc = i2c_recv_byte(dev);
				if (rc < 0)
					goto err;
				d1printk("%02x", rc);
				msgs[i].buf[byte] = rc;
			}
			if (0x19 == msgs[i].addr) {
				d1printk(" ?");
				rc = i2c_recv_byte(dev);
				if (rc < 0)
					goto err;
				d1printk("%02x", rc);
			}
		} else {
			
			d2printk("write bytes\n");
			for (byte = 0; byte < msgs[i].len; byte++) {
				data = msgs[i].buf[byte];
				d1printk(" %02x", data);
				rc = i2c_send_byte(dev,CONTINUE,data);
				if (rc < 0)
					goto err;
			}
		}
	}
	d2printk("xfer done\n");
	d1printk(" >");
	i2c_set_attr(dev,STOP);
	rc = -EIO;
	if (!i2c_is_busy_wait(dev))
		goto err;
	status = i2c_get_status(dev);
	if (i2c_is_error(status))
		goto err;
	
	msleep(1);

	d1printk("\n");
	return num;
 err:
	if (1 == i2c_debug) {
		status = i2c_get_status(dev);
		printk(" ERROR: %s\n",str_i2c_status[status]);
	}
	return rc;
}
Exemplo n.º 18
0
static int saa7134_i2c_xfer(struct i2c_adapter *i2c_adap,
			    struct i2c_msg *msgs, int num)
{
	struct saa7134_dev *dev = i2c_adap->algo_data;
	enum i2c_status status;
	unsigned char data;
	int addr,rc,i,byte;

	status = i2c_get_status(dev);
	if (!i2c_is_idle(status))
		if (!i2c_reset(dev))
			return -EIO;

	i2c_dbg(2, "start xfer\n");
	i2c_dbg(1, "i2c xfer:");
	for (i = 0; i < num; i++) {
		if (!(msgs[i].flags & I2C_M_NOSTART) || 0 == i) {
			/* send address */
			i2c_dbg(2, "send address\n");
			addr  = msgs[i].addr << 1;
			if (msgs[i].flags & I2C_M_RD)
				addr |= 1;
			if (i > 0 && msgs[i].flags &
			    I2C_M_RD && msgs[i].addr != 0x40 &&
			    msgs[i].addr != 0x41 &&
			    msgs[i].addr != 0x19) {
				/* workaround for a saa7134 i2c bug
				 * needed to talk to the mt352 demux
				 * thanks to pinnacle for the hint */
				int quirk = 0xfe;
				i2c_cont(1, " [%02x quirk]", quirk);
				i2c_send_byte(dev,START,quirk);
				i2c_recv_byte(dev);
			}
			i2c_cont(1, " < %02x", addr);
			rc = i2c_send_byte(dev,START,addr);
			if (rc < 0)
				 goto err;
		}
		if (msgs[i].flags & I2C_M_RD) {
			/* read bytes */
			i2c_dbg(2, "read bytes\n");
			for (byte = 0; byte < msgs[i].len; byte++) {
				i2c_cont(1, " =");
				rc = i2c_recv_byte(dev);
				if (rc < 0)
					goto err;
				i2c_cont(1, "%02x", rc);
				msgs[i].buf[byte] = rc;
			}
			/* discard mysterious extra byte when reading
			   from Samsung S5H1411.  i2c bus gets error
			   if we do not. */
			if (0x19 == msgs[i].addr) {
				i2c_cont(1, " ?");
				rc = i2c_recv_byte(dev);
				if (rc < 0)
					goto err;
				i2c_cont(1, "%02x", rc);
			}
		} else {
			/* write bytes */
			i2c_dbg(2, "write bytes\n");
			for (byte = 0; byte < msgs[i].len; byte++) {
				data = msgs[i].buf[byte];
				i2c_cont(1, " %02x", data);
				rc = i2c_send_byte(dev,CONTINUE,data);
				if (rc < 0)
					goto err;
			}
		}
	}
	i2c_dbg(2, "xfer done\n");
	i2c_cont(1, " >");
	i2c_set_attr(dev,STOP);
	rc = -EIO;
	if (!i2c_is_busy_wait(dev))
		goto err;
	status = i2c_get_status(dev);
	if (i2c_is_error(status))
		goto err;
	/* ensure that the bus is idle for at least one bit slot */
	msleep(1);

	i2c_cont(1, "\n");
	return num;
 err:
	if (1 == i2c_debug) {
		status = i2c_get_status(dev);
		i2c_cont(1, " ERROR: %s\n", str_i2c_status[status]);
	}
	return rc;
}