Exemple #1
0
Fichier : i2c.c Projet : cpizano/lk
void i2c_init_early(void)
{
	LTRACE_ENTRY;

	/* enable clocks on i2c 0-2 */
	RMWREG32(CM_FCLKEN1_CORE, 15, 3, 0x7),
	RMWREG32(CM_ICLKEN1_CORE, 15, 3, 0x7),

	i2c_reset_bus(0);
	i2c_reset_bus(1);
	i2c_reset_bus(2);

#if 0
	// write something into a reg
	char buf[2];
	i2c_write_reg(0, 0x4b, 0x14, 0x99);
	i2c_write_reg(0, 0x4b, 0x15, 0x98);

	i2c_read_reg(0, 0x4b, 0x15, buf);
	printf("0x%hhx\n", buf[0]);
	i2c_read_reg(0, 0x4b, 0x14, buf);
	printf("0x%hhx\n", buf[0]);

	int i;
	for (i=0; i < 255; i++) {
		char buf[1];
		buf[0] = i;
		i2c_transmit(0, 0x4b, buf, 1);
		i2c_receive(0, 0x4b, buf, sizeof(buf));
		printf("0x%hhx\n", buf[0]);
	}
#endif

	LTRACE_EXIT;
}
Exemple #2
0
//PS enable function
int pa12201001_enable_ps(struct i2c_client *client, int enable)
{
  int res;
  u8 regdata=0;
  u8 sendvalue=0;
	
  if(enable == 1) //PS ON
  {
     printk("pa12201001 enable ps sensor\n");
     res=i2c_read_reg(client,REG_CFG0,&regdata); //Read Status
     if(res<0){
		APS_ERR("i2c_read function err\n");
		return res;
     }else{

     	//sendvalue=regdata & 0xFD; //clear bit
     	//sendvalue=sendvalue | 0x02; //0x02 PS Flag
     	sendvalue=regdata & 0xFC; //clear bit-0 & bit-1
     	sendvalue=sendvalue | 0x02; //0x02 PS On

     	res=i2c_write_reg(client,REG_CFG0,sendvalue); //Write PS enable 
     
    	 if(res<0){
		     APS_ERR("i2c_write function err\n");
		     return res;
          }	  		 	
         res=i2c_read_reg(client,REG_CFG0,&regdata); //Read Status
     	APS_LOG("CFG0 Status: %d\n",regdata);
      }
    }else{       //PS OFF
			
       printk("pa12201001 disable ps sensor\n");
       res=i2c_read_reg(client,REG_CFG0,&regdata); //Read Status
       if(res<0){
		  APS_ERR("i2c_read function err\n");
		  return res;
       }else{
          APS_LOG("CFG0 Status: %d\n",regdata);
		
          //sendvalue=regdata & 0xFD; //clear bit
          sendvalue=regdata & 0xFC; //clear bit-0 & bit-1
	res=i2c_write_reg(client,REG_CFG0,sendvalue); //Write PS disable 
		
          if(res<0){
		      APS_ERR("i2c_write function err\n");
			 return res;
		 }	  	
       }
     }
	
     return 0;
} 
Exemple #3
0
static int
i2c_read(device_t dev, char *buf, int len, int *read, int last, int delay)
{
	struct i2c_softc *sc;
	int error, reg;

	sc = device_get_softc(dev);
	*read = 0;

	mtx_lock(&sc->mutex);

	if (len) {
		if (len == 1)
			i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
			    I2CCR_MSTA | I2CCR_TXAK);

		else
			i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
			    I2CCR_MSTA);

		/* dummy read */
		i2c_read_reg(sc, I2C_DATA_REG);
		DELAY(1000);
	}

	while (*read < len) {
		error = wait_for_icf(sc);
		if (error) {
			mtx_unlock(&sc->mutex);
			return (error);
		}
		i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
		if ((*read == len - 2) && last) {
			/* NO ACK on last byte */
			i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
			    I2CCR_MSTA | I2CCR_TXAK);
		}

		if ((*read == len - 1) && last) {
			/* Transfer done, remove master bit */
			i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
			    I2CCR_TXAK);
		}

		reg = i2c_read_reg(sc, I2C_DATA_REG);
		*buf++ = reg;
		(*read)++;
	}
	mtx_unlock(&sc->mutex);

	return (IIC_NOERR);
}
Exemple #4
0
int tsl2561_init(tsl2561_t *dev,
                 i2c_t i2c, uint8_t addr, uint8_t gain, uint8_t integration)
{
    dev->i2c_dev = i2c;
    dev->addr = addr;
    dev->gain = gain;
    dev->integration = integration;
    _print_init_info(dev);

    /* Initialize I2C interface */
    if (i2c_init_master(dev->i2c_dev, I2C_SPEED_NORMAL)) {
        DEBUG("[Error] I2C device not enabled\n");
        return TSL2561_NOI2C;
    }

    DEBUG("[Info] I2C device initialized with success!\n");

    /* Acquire exclusive access */
    i2c_acquire(dev->i2c_dev);

    DEBUG("[Info] Access acquired !\n");

    /* Verify sensor ID */
    uint8_t id;
    i2c_read_reg(dev->i2c_dev, dev->addr,
                 TSL2561_COMMAND_MODE | TSL2561_REGISTER_ID, &id);
    DEBUG("[Info] ID ? %d\n", id);
    if (id != TSL2561_ID ) {
        DEBUG("[Error] not a TSL2561 sensor\n");
        return TSL2561_BADDEV;
    }

    _enable(dev);

    /* configuring gain and integration time */
    i2c_write_reg(dev->i2c_dev, dev->addr,
                  TSL2561_COMMAND_MODE | TSL2561_REGISTER_TIMING,
                  dev->integration | dev->gain);

#if ENABLE_DEBUG
    uint8_t timing;
    i2c_read_reg(dev->i2c_dev, dev->addr,
                 TSL2561_COMMAND_MODE | TSL2561_REGISTER_TIMING, &timing);
    DEBUG("[Info] Timing ? %d (expected: %d)\n",
          timing, dev->integration | dev->gain);
#endif

    _disable(dev);

    return TSL2561_OK;
}
Exemple #5
0
static int setup_pmic_voltages(void)
{
	unsigned char value, rev_id = 0 ;
	struct i2c_adapter *adapter = NULL;
	struct i2c_client client;
	int addr = -1, bus = 0;

	/* I2C2 bus (2-1 = 1 in barebox numbering) */
	bus = 1;

	/* PFUZE100 device address is 0x08 */
	addr = 0x08;

	adapter = i2c_get_adapter(bus);
	if (!adapter) {
		pr_err("i2c bus %d not found\n", bus);
		return -ENODEV;
	}

	client.adapter = adapter;
	client.addr = addr;

	/* Attempt to locate the PFUZE100 chip. */
	if (i2c_read_reg(&client, 0x00, &value, 1) != 1) {
		pr_err("Read device ID error!\n");
		return -1;
	}
	if (i2c_read_reg(&client, 0x03, &rev_id, 1) != 1) {
		pr_err("Read Rev ID error!\n");
		return -1;
	}

	pr_info("Found PFUZE100! deviceid=%x,revid=%x\n", value, rev_id);

	/* Set Gigabit Ethernet voltage (SOM v1.1/1.0)*/
        value = 0x60;
	if (i2c_write_reg(&client, 0x4a, &value, 1) != 1) {
		pr_err("Set ETH error!\n");
		return -EIO;
	}

	/* set VGEN3 to 2.5V */
	value = 0x77;
	if (i2c_write_reg(&client, 0x6e, &value, 1) != 1) {
		pr_err("Set VGEN3 error!\n");
		return -EIO;
	}

	return 0;
}
Exemple #6
0
static int
i2c_read(device_t dev, char *buf, int len, int *read, int last, int delay)
{
	struct i2c_softc *sc;
	int error;

	sc = device_get_softc(dev);
	*read = 0;

	mtx_lock(&sc->mutex);
	if (len) {
		if (len == 1)
			i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
			    I2CCR_MSTA | I2CCR_TXAK);

		else
			i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
			    I2CCR_MSTA);

		/* dummy read */
		i2c_read_reg(sc, I2C_DATA_REG);
		DELAY(1000);
	}

	while (*read < len) {
		DELAY(1000);
		error = i2c_do_wait(dev, sc, 0, 0);
		if (error) {
			mtx_unlock(&sc->mutex);
			return (error);
		}
		if ((*read == len - 2) && last) {
			i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
			    I2CCR_MSTA | I2CCR_TXAK);
		}

		if ((*read == len - 1) && last) {
			i2c_write_reg(sc, I2C_CONTROL_REG,  I2CCR_MEN |
			    I2CCR_TXAK);
		}

		*buf++ = i2c_read_reg(sc, I2C_DATA_REG);
		(*read)++;
		DELAY(1250);
	}
	mtx_unlock(&sc->mutex);

	return (IIC_NOERR);
}
Exemple #7
0
/* Wait for transfer to complete, optionally check RXAK. */
static int
wait_for_xfer(struct i2c_softc *sc, int checkack)
{
	int retry, sr;

	/*
	 * Sleep for about the time it takes to transfer a byte (with precision
	 * set to tolerate 5% oversleep).  We calculate the approximate byte
	 * transfer time when we set the bus speed divisor.  Slaves are allowed
	 * to do clock-stretching so the actual transfer time can be larger, but
	 * this gets the bulk of the waiting out of the way without tying up the
	 * processor the whole time.
	 */
	pause_sbt("imxi2c", sc->byte_time_sbt, sc->byte_time_sbt / 20, 0);

	retry = 10000;
	while (retry --) {
		sr = i2c_read_reg(sc, I2C_STATUS_REG);
		if (sr & I2CSR_MIF) {
                        if (sr & I2CSR_MAL) 
				return (IIC_EBUSERR);
			else if (checkack && (sr & I2CSR_RXAK))
				return (IIC_ENOACK);
			else
				return (IIC_NOERR);
		}
		DELAY(1);
	}
	return (IIC_ETIMEOUT);
}
Exemple #8
0
static int
i2c_do_wait(device_t dev, struct i2c_softc *sc, int write, int start)
{
	int err;
	uint8_t status;

	status = i2c_read_reg(sc, I2C_STATUS_REG);
	if (status & I2CSR_MIF) {
		if (write && start && (status & I2CSR_RXAK)) {
			debugf("no ack %s", start ?
			    "after sending slave address" : "");
			err = IIC_ENOACK;
			goto error;
		}
		if (status & I2CSR_MAL) {
			debugf("arbitration lost");
			err = IIC_EBUSERR;
			goto error;
		}
		if (!write && !(status & I2CSR_MCF)) {
			debugf("transfer unfinished");
			err = IIC_EBUSERR;
			goto error;
		}
	}

	return (IIC_NOERR);

error:
	i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
	i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN | I2CCR_TXAK);
	return (err);
}
Exemple #9
0
Fichier : i2c.c Projet : cpizano/lk
static int cmd_i2c(int argc, const cmd_args *argv)
{
	int err;

	if (argc < 5) {
		printf("not enough arguments\n");
usage:
		printf("%s read_reg <bus> <i2c address> <register>\n", argv[0].str);
		printf("%s write_reg <bus> <i2c address> <register> <val>\n", argv[0].str);
		return -1;
	}

	int bus = argv[2].u;
	uint8_t i2c_address = argv[3].u;

	if (!strcmp(argv[1].str, "read_reg")) {
		uint8_t reg = argv[4].u;
		uint8_t val;

		err = i2c_read_reg(bus, i2c_address, reg, &val);
		printf("i2c_read_reg err %d, val 0x%hhx\n", err, val);
	} else if (!strcmp(argv[1].str, "write_reg")) {
		uint8_t reg = argv[4].u;
		uint8_t val = argv[5].u;
		err = i2c_write_reg(bus, i2c_address, reg, val);
		printf("i2c_write_reg err %d\n", err);
	} else {
		printf("unrecognized subcommand\n");
		goto usage;
	}

	return 0;
}
Exemple #10
0
static int
i2c_start(device_t dev, u_char slave, int timeout)
{
	struct i2c_softc *sc;
	int error;

	sc = device_get_softc(dev);

	mtx_lock(&sc->mutex);
	i2c_write_reg(sc, I2C_ADDR_REG, slave);
	if (i2c_read_reg(sc, I2C_STATUS_REG) & I2CSR_MBB) {
		mtx_unlock(&sc->mutex);
		return (IIC_EBUSBSY);
	}

	/* Set start condition */
	i2c_write_reg(sc, I2C_CONTROL_REG,
	    I2CCR_MEN | I2CCR_MSTA | I2CCR_TXAK);
	DELAY(100);
	i2c_write_reg(sc, I2C_CONTROL_REG,
	    I2CCR_MEN | I2CCR_MSTA | I2CCR_MTX | I2CCR_TXAK);
	/* Clear status */
	i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
	/* Write target address - LSB is R/W bit */
	i2c_write_reg(sc, I2C_DATA_REG, slave);

	error = wait_for_iif(sc);

	mtx_unlock(&sc->mutex);
	if (error)
		return (error);

	return (IIC_NOERR);
}
Exemple #11
0
static int
i2c_start(device_t dev, u_char slave, int timeout)
{
	struct i2c_softc *sc;
	uint8_t status;
	int error;

	sc = device_get_softc(dev);
	DELAY(1000);

	mtx_lock(&sc->mutex);
	status = i2c_read_reg(sc, I2C_STATUS_REG);
	/* Check if bus is idle or busy */
	if (status & I2CSR_MBB) {
		debugf("bus busy");
		mtx_unlock(&sc->mutex);
		i2c_stop(dev);
		return (IIC_EBUSBSY);
	}

	/* Set start condition */
	i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN | I2CCR_MSTA | I2CCR_MTX);
	/* Write target address - LSB is R/W bit */
	i2c_write_reg(sc, I2C_DATA_REG, slave);
	DELAY(1250);

	error = i2c_do_wait(dev, sc, 1, 1);

	mtx_unlock(&sc->mutex);
	if (error)
		return (error);

	return (IIC_NOERR);
}
Exemple #12
0
int srf02_init(srf02_t *dev, i2c_t i2c, uint8_t addr)
{
    dev->i2c = i2c;
    dev->addr = (addr >> 1);    /* internally we right align the 7-bit addr */
    uint8_t rev;

    /* Acquire exclusive access to the bus. */
    i2c_acquire(dev->i2c);
    /* initialize i2c interface */
    if (i2c_init_master(dev->i2c, BUS_SPEED) < 0) {
        DEBUG("[srf02] error initializing I2C bus\n");
        return -1;
    }
    /* try to read the software revision (read the CMD reg) from the device */
    i2c_read_reg(i2c, dev->addr, REG_CMD, &rev);
    if (rev == 0 || rev == 255) {
        i2c_release(dev->i2c);
        DEBUG("[srf02] error reading the devices software revision\n");
        return -1;
    } else {
        DEBUG("[srf02] software revision: 0x%02x\n", rev);
    }
    /* Release the bus for other threads. */
    i2c_release(dev->i2c);

    DEBUG("[srf02] initialization successful\n");
    return 0;
}
Exemple #13
0
BAREBOX_CMD_END

static int do_i2c_read(int argc, char *argv[])
{
	struct i2c_adapter *adapter = NULL;
	struct i2c_client client;
	u8 *buf;
	int count = -1, addr = -1, reg = -1, verbose = 0, ret, opt, bus = 0, wide = 0;

	while ((opt = getopt(argc, argv, "a:b:c:r:v:w")) > 0) {
		switch (opt) {
		case 'a':
			addr = simple_strtol(optarg, NULL, 0);
			break;
		case 'c':
			count = simple_strtoul(optarg, NULL, 0);
			break;
		case 'b':
			bus = simple_strtoul(optarg, NULL, 0);
			break;
		case 'r':
			reg = simple_strtol(optarg, NULL, 0);
			break;
		case 'v':
			verbose = 1;
			break;
		case 'w':
			wide = 1;
			break;
		}
	}

	if ((addr < 0) || (reg < 0) || (count < 1) || (addr > 0x7F))
		return COMMAND_ERROR_USAGE;

	adapter = i2c_get_adapter(bus);
	if (!adapter) {
		printf("i2c bus %d not found\n", bus);
		return -ENODEV;
	}

	client.adapter = adapter;
	client.addr = addr;

	buf = xmalloc(count);
	ret = i2c_read_reg(&client, reg | (wide ? I2C_ADDR_16_BIT : 0), buf, count);
	if (ret == count) {
		int i;
		if (verbose)
			printf("read %i bytes starting at reg 0x%04x from i2cdev 0x%02x on bus %i\n",
				count, reg, addr, adapter->nr);
		for (i = 0; i < count; i++)
			printf("0x%02x ", *(buf + i));
		printf("\n");
		ret = 0;
	}

	free(buf);
	return ret;
}
Exemple #14
0
static ssize_t txc_als_data_show(struct device *dev, struct device_attribute *attr,
	char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	int ret;
	u8 msb, lsb;
	u16 als_data;
	    
	ret = i2c_read_reg(client, REG_ALS_DATA_LSB, &lsb); 
	ret = i2c_read_reg(client, REG_ALS_DATA_MSB, &msb);

	als_data = (msb << 8) | lsb;
	printk("als data is %d \n", als_data);

	return sprintf(buf, "%d\n", als_data);
}
Exemple #15
0
int mc9sdz60_reg_read(struct mc9sdz60 *mc9sdz60, enum mc9sdz60_reg reg, u8 *val)
{
	int ret;

	ret = i2c_read_reg(mc9sdz60->client, reg, val, 1);

	return ret == 1 ? 0 : ret;
}
Exemple #16
0
static void set_ps_work_mode(struct i2c_client *client)
{
    int ret = 0;
    u8 data = 0;

    ret = i2c_read_reg(client, REG_CFG2, &data);
    ret = i2c_write_reg(client, REG_CFG2, data | PA12_INT_PS);
}
Exemple #17
0
int mc34704_reg_read(struct mc34704 *mc34704, u8 reg, u8 *val)
{
	int ret;

	ret = i2c_read_reg(mc34704->client, reg, val, 1);

	return ret == 1 ? 0 : ret;
}
Exemple #18
0
static int
i2c_read(device_t dev, char *buf, int len, int *read, int last, int delay)
{
	struct i2c_softc *sc;
	int error, reg;

	sc = device_get_softc(dev);
	*read = 0;

	if (len) {
		if (len == 1)
			i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
			    I2CCR_MSTA | I2CCR_TXAK);
		else
			i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
			    I2CCR_MSTA);
                /* Dummy read to prime the receiver. */
		i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
		i2c_read_reg(sc, I2C_DATA_REG);
	}

	error = 0;
	*read = 0;
	while (*read < len) {
		if ((error = wait_for_xfer(sc, false)) != IIC_NOERR)
			break;
		i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
		if (last) {
			if (*read == len - 2) {
				/* NO ACK on last byte */
				i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
				    I2CCR_MSTA | I2CCR_TXAK);
			} else if (*read == len - 1) {
				/* Transfer done, signal stop. */
				i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
				    I2CCR_TXAK);
				wait_for_busbusy(sc, false);
			}
		}
		reg = i2c_read_reg(sc, I2C_DATA_REG);
		*buf++ = reg;
		(*read)++;
	}

	return (i2c_error_handler(sc, error));
}
int16 ad_ave1(int16 N) //均值滤波
{
    int32 tmp = 0;
    int16 lsb,msb;
    int16 temp;
    int16  i;
    for(i = 0; i < N; i++)
    {
        msb=i2c_read_reg(I2C0,0x68,0x43);
        lsb=i2c_read_reg(I2C0,0x68,0x44);
        temp=msb<<8|lsb;
        tmp+=temp;
        lptmr_delay_ms(5);
    }
    tmp = tmp / N;
    return (int16)tmp;
}
Exemple #20
0
int stmpe_reg_read(struct stmpe *stmpe, u32 reg, u8 *val)
{
	int ret;

	ret = i2c_read_reg(stmpe->client, reg, val, 1);

	return ret == 1 ? 0 : ret;
}
Exemple #21
0
//Read ALS Count : 16 bit
int pa12201001_read_als(struct i2c_client *client, u16 *data)
{
   int res;
   u8 LSB = 0;
   u8 MSB = 0;
	
  // APS_FUN(f);
    res = i2c_read_reg(client, REG_ALS_DATA_LSB, &LSB); //Read PS Data
    res = i2c_read_reg(client, REG_ALS_DATA_MSB, &MSB); //Read PS Data
    *data = (MSB << 8) | LSB; 
    //psdata = i2c_smbus_read_byte_data(client, REG_PS_DATA); 
   if(res < 0){
        APS_ERR("i2c_send function err\n");
   }

   return res;
}
Exemple #22
0
int act8846_reg_read(struct act8846 *act8846, enum act8846_reg reg, u8 *val)
{
	int ret;

	ret = i2c_read_reg(act8846->client, reg, val, 1);

	return ret == 1 ? 0 : ret;
}
Exemple #23
0
static __inline void
i2c_flag_set(struct i2c_softc *sc, bus_size_t off, uint8_t mask)
{
	uint8_t status;

	status = i2c_read_reg(sc, off);
	status |= mask;
	i2c_write_reg(sc, off, status);
}
Exemple #24
0
int isl29125_init(isl29125_t *dev, i2c_t i2c, gpio_t gpio,
                  isl29125_mode_t mode, isl29125_range_t range,
                  isl29125_resolution_t resolution)
{
    DEBUG("isl29125_init\n");

    /* initialize device descriptor */
    dev->i2c = i2c;
    dev->res = resolution;
    dev->range = range;
    dev->gpio = gpio;

    /* configuration 1: operation mode, range, resolution */
    uint8_t conf1 = 0x00;
    conf1 |= mode;
    conf1 |= range;
    conf1 |= resolution;
    conf1 |= ISL29125_CON1_SYNCOFF; /* TODO: implement SYNC mode configuration */

    /* TODO: implement configuration 2: infrared compensation configuration */

    /* acquire exclusive access to the bus */
    DEBUG("isl29125_init: i2c_acquire\n");
    (void) i2c_acquire(dev->i2c);

    /* initialize the I2C bus */
    DEBUG("isl29125_init: i2c_init_master\n");
    (void) i2c_init_master(i2c, I2C_SPEED_NORMAL);

    /* verify the device ID */
    DEBUG("isl29125_init: i2c_read_reg\n");
    uint8_t reg_id;
    int ret = i2c_read_reg(dev->i2c, ISL29125_I2C_ADDRESS, ISL29125_REG_ID, &reg_id);
    if ((reg_id == ISL29125_ID) && (ret == 1)) {
        DEBUG("isl29125_init: ID successfully verified\n");
    }
    else {
        DEBUG("isl29125_init: ID could not be verified, ret: %i\n", ret);
        (void) i2c_release(dev->i2c);
        return -1;
    }

    /* configure and enable the sensor */
    DEBUG("isl29125_init: i2c_write_reg(ISL29125_REG_RESET)\n");
    (void) i2c_write_reg(dev->i2c, ISL29125_I2C_ADDRESS, ISL29125_REG_RESET, ISL29125_CMD_RESET);

    DEBUG("isl29125_init: i2c_write_reg(ISL29125_REG_CONF1)\n");
    (void) i2c_write_reg(dev->i2c, ISL29125_I2C_ADDRESS, ISL29125_REG_CONF1, conf1);

    /* release the I2C bus */
    DEBUG("isl29125_init: i2c_release\n");
    (void) i2c_release(dev->i2c);

    DEBUG("isl29125_init: success\n");
    return 0;
}
Exemple #25
0
static int twl4030_usb_read(uint8_t address)
{
	uint8_t data;

	int err = i2c_read_reg(TWL_I2C_BUS, TWL_USB_ADDR, address, &data);
	if (err < 0)
		return err;

	return data;
}
Exemple #26
0
int lps331ap_read_temp(const lps331ap_t *dev)
{
    uint8_t tmp;
    int16_t val = 0;
    float res = TEMP_BASE;      /* reference value -> see datasheet */

    i2c_acquire(dev->i2c);
    i2c_read_reg(dev->i2c, dev->address, LPS331AP_REG_TEMP_OUT_L, &tmp);
    val |= tmp;
    i2c_read_reg(dev->i2c, dev->address, LPS331AP_REG_TEMP_OUT_H, &tmp);
    i2c_release(dev->i2c);
    val |= ((uint16_t)tmp << 8);

    /* compute actual temperature value in °C */
    res += ((float)val) / TEMP_DIVIDER;

    /* return temperature in m°C */
    return (int)(res * 1000);
}
Exemple #27
0
int isl29020_read(const isl29020_t *dev)
{
    uint8_t low, high;
    uint16_t res;
    int ret;

    i2c_acquire(DEV_I2C);
    /* read lighting value */
    ret = i2c_read_reg(DEV_I2C, DEV_ADDR, ISL29020_REG_LDATA, &low, 0);
    ret += i2c_read_reg(DEV_I2C, DEV_ADDR, ISL29020_REG_HDATA, &high, 0);
    i2c_release(DEV_I2C);
    if (ret < 0) {
        return -1;
    }
    res = (high << 8) | low;
    DEBUG("ISL29020: Raw value: %i - high: %i, low: %i\n", res, high, low);
    /* calculate and return the actual lux value */
    return (int)(dev->lux_fac * res);
}
Exemple #28
0
static int mc13892_i2c_reg_read(struct mc13892 *mc13892, enum mc13892_reg reg, u32 *val)
{
	u8 buf[3];
	int ret;

	ret = i2c_read_reg(mc13892->client, reg, buf, 3);
	*val = buf[0] << 16 | buf[1] << 8 | buf[2] << 0;

	return ret == 3 ? 0 : ret;
}
Exemple #29
0
int mpu9150_init(mpu9150_t *dev, i2c_t i2c, mpu9150_hw_addr_t hw_addr,
        mpu9150_comp_addr_t comp_addr)
{
    char temp;

    dev->i2c_dev = i2c;
    dev->hw_addr = hw_addr;
    dev->comp_addr = comp_addr;
    dev->conf = DEFAULT_STATUS;

    /* Initialize I2C interface */
    if (i2c_init_master(dev->i2c_dev, I2C_SPEED_FAST)) {
        DEBUG("[Error] I2C device not enabled\n");
        return -1;
    }

    /* Acquire exclusive access */
    i2c_acquire(dev->i2c_dev);

    /* Reset MPU9150 registers and afterwards wake up the chip */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_1_REG, MPU9150_PWR_RESET);
    hwtimer_wait(HWTIMER_TICKS(MPU9150_RESET_SLEEP_US));
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_1_REG, MPU9150_PWR_WAKEUP);

    /* Release the bus, it is acquired again inside each function */
    i2c_release(dev->i2c_dev);

    /* Set default full scale ranges and sample rate */
    mpu9150_set_gyro_fsr(dev, MPU9150_GYRO_FSR_2000DPS);
    mpu9150_set_accel_fsr(dev, MPU9150_ACCEL_FSR_2G);
    mpu9150_set_sample_rate(dev, MPU9150_DEFAULT_SAMPLE_RATE);

    /* Disable interrupt generation */
    i2c_acquire(dev->i2c_dev);
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_INT_ENABLE_REG, REG_RESET);

    /* Initialize magnetometer */
    if (compass_init(dev)) {
        i2c_release(dev->i2c_dev);
        return -2;
    }
    /* Release the bus, it is acquired again inside each function */
    i2c_release(dev->i2c_dev);
    mpu9150_set_compass_sample_rate(dev, 10);
    /* Enable all sensors */
    i2c_acquire(dev->i2c_dev);
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_1_REG, MPU9150_PWR_PLL);
    i2c_read_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_2_REG, &temp);
    temp &= ~(MPU9150_PWR_ACCEL | MPU9150_PWR_GYRO);
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_2_REG, temp);
    i2c_release(dev->i2c_dev);
    hwtimer_wait(HWTIMER_TICKS(MPU9150_PWR_CHANGE_SLEEP_US));

    return 0;
}
Exemple #30
0
/*******************************************************************************
* Function Name  : Acc_read()
* Description    : read acceletate data
* Input          : buf to save acc data
* Output         : None
* Return         : None
* Editor         :wenhai
***************************************************************************/
void Acc_read()
{
  static unsigned int temp[6];
  temp[0] =  i2c_read_reg(I2C0, SlaveAddress8700, OUT_X_MSB_REG);
  i2c_delay();
  temp[1] =  i2c_read_reg(I2C0, SlaveAddress8700, OUT_X_LSB_REG);
  i2c_delay();
//temp[2] =  i2c_read_reg(I2C0,SlaveAddress8700,OUT_Y_MSB_REG);
  // i2c_delay();
  // temp[3] =  i2c_read_reg(I2C0,SlaveAddress8700,OUT_Y_LSB_REG);
  // i2c_delay();
  temp[4] =  i2c_read_reg(I2C0, SlaveAddress8700, OUT_Z_MSB_REG);
  i2c_delay();
  temp[5] =  i2c_read_reg(I2C0, SlaveAddress8700, OUT_Z_LSB_REG);
  i2c_delay();
  Acc_data.x = ((int16)((0x0000 | (temp[0] << 8)) + temp[1])) - 140;
  // Acc_datas.y = ((int16)((0x0000 | (temp[2]<<8)) + temp[3]))-65;
  Acc_data.z = ((int16)((0x0000 | (temp[4] << 8)) + temp[5]));

}