Пример #1
0
int
ms5611_attach(struct i2c_dev_s *i2c)
{
	int	result = ERROR;

	dev.i2c = i2c;

	MS5611_ADDRESS = MS5611_ADDRESS_1;
	uint8_t cmd = ADDR_PROM;

	/* reset */

//	I2C_LOCK(dev.i2c, true);
	I2C_SETADDRESS(dev.i2c, MS5611_ADDRESS, 7);
	int ret = write_reg(ADDR_RESET_CMD, 1);

	/* check if the address was wrong */
	if (ret < 0)
	{
		/* try second address */
		MS5611_ADDRESS = MS5611_ADDRESS_2;
		I2C_SETADDRESS(dev.i2c, MS5611_ADDRESS, 7);
		ret = write_reg(ADDR_RESET_CMD, 1);
	}

	if (ret < 0) return EIO;

	/* wait for PROM contents to be in the device */
	usleep(10000);

	/* read PROM */
	ret = I2C_WRITEREAD(i2c, &cmd, 1, (uint8_t*)dev.prom, sizeof(dev.prom));

	/* OR PROM contents as poor-man's alive check, PROM cannot be all-zero */
	int sum = dev.prom[0] | dev.prom[1] | dev.prom[2] | dev.prom[3] | dev.prom[4] | dev.prom[5] | dev.prom[6] | dev.prom[7];

	/* verify that the device is attached and functioning */
	if ((ret >= 0) && (sum > 0)) {

		/* start first conversion */
		read_reg(ADDR_CMD_CONVERT_D1);
		/* wait */
		usleep(10000);
		read_reg(ADDR_CMD_CONVERT_D2);

		/* make ourselves available */
		register_driver("/dev/ms5611", &ms5611_fops, 0666, NULL);

		result = 0;

	} else {
		errno = EIO;
	}



	return result;
}
Пример #2
0
static int bq2425x_getreg8(FAR struct bq2425x_dev_s *priv, uint8_t regaddr,
                           FAR uint8_t *regval)
{
  uint8_t val;
  int ret;

  /* Set the I2C address and address size */

  I2C_SETADDRESS(priv->i2c, priv->addr, 7);

  /* Write the register address */

  ret = I2C_WRITE(priv->i2c, &regaddr, 1);
  if (ret < 0)
    {
      batdbg("I2C_WRITE failed: %d\n", ret);
      return ret;
    }

  /* Restart and read 8-bits from the register */

  ret = I2C_READ(priv->i2c, &val, 1);
  if (ret < 0)
    {
      batdbg("I2C_READ failed: %d\n", ret);
      return ret;
    }

  /* Copy 8-bit value to be returned */

  *regval = val;
  return OK;
}
Пример #3
0
static int max1704x_getreg16(FAR struct max1704x_dev_s *priv, uint8_t regaddr,
                             FAR uint16_t *regval)
{
  uint8_t buffer[2];
  int ret;

  /* Set the I2C address and address size */

  I2C_SETADDRESS(priv->i2c, priv->addr, 7);

  /* Write the register address */

  ret = I2C_WRITE(priv->i2c, &regaddr, 1);
  if (ret < 0)
    {
      batdbg("I2C_WRITE failed: %d\n", ret);
      return ret;
    }

  /* Restart and read 16-bits from the register */

  ret = I2C_READ(priv->i2c, buffer, 2);
  if (ret < 0)
    {
      batdbg("I2C_READ failed: %d\n", ret);
      return ret;
    }

  /* Return the 16-bit value */

  return (uint16_t)buffer[0] << 8 | (uint16_t)buffer[1];
  return OK;
}
Пример #4
0
static bool
read_values(int16_t *data)
{
	struct { /* status register and data as read back from the device */
		uint32_t		pressure;
		uint32_t		temperature;
	} __attribute__((packed))	report;

	/* exchange the report structure with the device */
//	I2C_LOCK(dev.i2c, true);
	I2C_SETADDRESS(dev.i2c, MS5611_ADDRESS_2, 7);

	uint8_t cmd = ADDR_DATA;
	int ret = 1;

	cmd = ADDR_DATA;
	ret = I2C_WRITEREAD(dev.i2c, &cmd, 1, (uint8_t*)&(report), sizeof(report));

	/* start conversion for next update */

//	I2C_LOCK(dev.i2c, false);

	/* write values and clamp them to 12 bit */
	data[0] = report.pressure;
	data[1] = report.temperature;

	/* return 1 if new data is available, 0 else */
	/* XXX Check if last read was at least 9.5 ms ago */
	return ret;
}
Пример #5
0
static int lm75_readb16(FAR struct lm75_dev_s *priv, uint8_t regaddr,
                        FAR b16_t *regvalue)
{
  uint8_t buffer[2];
  int ret;

  /* Write the register address */

  I2C_SETADDRESS(priv->i2c, priv->addr, 7);
  ret = I2C_WRITE(priv->i2c, &regaddr, 1);
  if (ret < 0)
    {
      sndbg("I2C_WRITE failed: %d\n", ret);
      return ret;
    }

  /* Restart and read 16-bits from the register (discarding 7) */

  ret = I2C_READ(priv->i2c, buffer, 2);
  if (ret < 0)
    {
      sndbg("I2C_READ failed: %d\n", ret);
      return ret;
    }

  /* Data format is:  TTTTTTTT Txxxxxxx where TTTTTTTTT is a nine-bit,
   * signed temperature value with LSB = 0.5 degrees centigrade.  So the
   * raw data is b8_t
   */

  *regvalue = b8tob16((b8_t)buffer[0] << 8 | (b8_t)buffer[1]);
  sndbg("addr: %02x value: %08x ret: %d\n", regaddr, *regvalue, ret);
  return OK;
}
Пример #6
0
static int mb7040_changeaddr(FAR struct mb7040_dev_s *priv, uint8_t addr)
{
  uint8_t buffer[3];
  int ret;

  sndbg("new addr: %02x\n", addr);

  /* Sanity check */

  DEBUGASSERT((addr & 1) == 0);
  DEBUGASSERT(addr != 0x00 && addr != 0x50 && addr != 0xa4 && addr != 0xaa);

  /* Set up a 3-byte message to send */

  buffer[0] = MB7040_ADDRUNLOCK1_REG;
  buffer[1] = MB7040_ADDRUNLOCK2_REG;
  buffer[2] = addr;

  /* Write the register address followed by the data (no RESTART) */

  I2C_SETADDRESS(priv->i2c, priv->addr, 7);
  ret = I2C_WRITE(priv->i2c, buffer, sizeof(buffer));
  if (ret < 0)
    {
      sndbg("I2C_WRITE failed: %d\n", ret);
      return ret;
    }

  priv->addr = addr;
  return ret;
}
Пример #7
0
int kxtj9_driver_init(){
    int ret = 0;

    if (g_data != NULL)
        goto init_done;
    g_data = kmm_malloc(sizeof(struct kxtj9_data));
    memset(g_data, 0, sizeof(struct kxtj9_data));

    g_data->i2c = up_i2cinitialize(CONFIG_SENSOR_KXTJ9_I2C_BUS);
    if (!g_data->i2c) {
        dbg("failed to init i2c\n");
        ret = -ENODEV;
        goto init_done;
    }

    ret = I2C_SETADDRESS(g_data->i2c, KXTJ9_I2C_ADDR, 7);
    if (ret) {
        dbg("failed to set i2c address\n");
        goto init_done;
    }

    I2C_SETFREQUENCY(g_data->i2c, CONFIG_SENSOR_KXTJ9_I2C_BUS_SPEED);

init_done:
    return ret;
}
Пример #8
0
static int
set_range(uint8_t range)
{
	I2C_SETADDRESS(dev.i2c, MS5611_ADDRESS, 7);
//	/* mask out illegal bit positions */
//	uint8_t write_range = range & REG4_RANGE_MASK;
//	/* immediately return if user supplied invalid value */
//	if (write_range != range) return EINVAL;
//	/* set remaining bits to a sane value */
//	write_range |= REG4_BDU;
//	/* write to device */
//	write_reg(ADDR_CTRL_REG4, write_range);
//	/* return 0 if register value is now written value, 1 if unchanged */
//	return !(read_reg(ADDR_CTRL_REG4) == write_range);
	return 0;
}
Пример #9
0
static int
set_rate(uint8_t rate)
{
	I2C_SETADDRESS(dev.i2c, MS5611_ADDRESS, 7);
//	/* mask out illegal bit positions */
//	uint8_t write_rate = rate & REG1_RATE_LP_MASK;
//	/* immediately return if user supplied invalid value */
//	if (write_rate != rate) return EINVAL;
//	/* set remaining bits to a sane value */
//	write_rate |= REG1_POWER_NORMAL | REG1_Z_ENABLE | REG1_Y_ENABLE | REG1_X_ENABLE;
//	/* write to device */
//	write_reg(ADDR_CTRL_REG1, write_rate);
//	/* return 0 if register value is now written value, 1 if unchanged */
//	return !(read_reg(ADDR_CTRL_REG1) == write_rate);
	return 0;
}
Пример #10
0
int tfa9890_reg_write(FAR struct tfa9890_dev_s *priv, uint8_t reg, uint16_t val)
{
    uint8_t buf[3];

    I2C_SETADDRESS(priv->i2c, priv->i2c_addr, 7);

    buf[0] = reg;
#ifdef CONFIG_ENDIAN_BIG
    buf[1] = val & 0x00ff;
    buf[2] = (val & 0xff00) >> 8;
#else
    buf[2] = val & 0x00ff;
    buf[1] = (val & 0xff00) >> 8;
#endif
    return I2C_WRITE(priv->i2c, (uint8_t *)buf, 4);
}
Пример #11
0
static int lm75_writeconf(FAR struct lm75_dev_s *priv, uint8_t conf)
{
  uint8_t buffer[2];

  sndbg("conf: %02x\n", conf);

  /* Set up a 2 byte message to send */

  buffer[0] = LM75_CONF_REG;
  buffer[1] = conf;

  /* Write the register address followed by the data (no RESTART) */

  I2C_SETADDRESS(priv->i2c, priv->addr, 7);
  return I2C_WRITE(priv->i2c, buffer, 2);
}
Пример #12
0
int tfa9890_reg_read(FAR struct tfa9890_dev_s *priv, uint8_t reg)
{
     uint8_t reg_val[2];
     int ret;

     I2C_SETADDRESS(priv->i2c, priv->i2c_addr, 7);

     ret = I2C_WRITEREAD(priv->i2c, (uint8_t *)&reg, sizeof(reg),
              reg_val, sizeof(reg_val));
     if (ret)
         return ret;

#ifdef CONFIG_ENDIAN_BIG
     ret = *((uint16_t *)reg_val);
#else
     ret = be16_to_cpu(*((uint16_t *)reg_val));
#endif
     return ret;
}
Пример #13
0
static int mb7040_measurerange(FAR struct mb7040_dev_s *priv)
{
  uint8_t regaddr;
  int ret;

  regaddr = MB7040_RANGE_REG;
  sndbg("addr: %02x\n", regaddr);

  /* Write the register address */

  I2C_SETADDRESS(priv->i2c, priv->addr, 7);
  ret = I2C_WRITE(priv->i2c, &regaddr, sizeof(regaddr));
  if (ret < 0)
    {
      sndbg("I2C_WRITE failed: %d\n", ret);
    }

  return ret;
}
Пример #14
0
static int bq2425x_putreg8(FAR struct bq2425x_dev_s *priv, uint8_t regaddr,
                           uint8_t regval)
{
  uint8_t buffer[2];

  batdbg("addr: %02x regval: %08x\n", regaddr, regval);

  /* Set up a 3 byte message to send */

  buffer[0] = regaddr;
  buffer[1] = regval;

  /* Set the I2C address and address size */

  I2C_SETADDRESS(priv->i2c, priv->addr, 7);

  /* Write the register address followed by the data (no RESTART) */

  return I2C_WRITE(priv->i2c, buffer, 2);
}
Пример #15
0
static int mb7040_readrange(FAR struct mb7040_dev_s *priv,
                            FAR uint16_t *range)
{
  uint8_t buffer[2];
  int ret;

  /* Read two bytes */

  I2C_SETADDRESS(priv->i2c, priv->addr, 7);
  ret = I2C_READ(priv->i2c, buffer, sizeof(buffer));
  if (ret < 0)
    {
      sndbg("I2C_READ failed: %d\n", ret);
      return ret;
    }

  *range = (uint16_t)buffer[0] << 8 | (uint16_t)buffer[1];
  sndbg("range: %04x ret: %d\n", *range, ret);
  return ret;
}
Пример #16
0
static int lm75_writeb16(FAR struct lm75_dev_s *priv, uint8_t regaddr,
                         b16_t regval)
{
  uint8_t buffer[3];
  b8_t regb8;

  sndbg("addr: %02x value: %08x\n", regaddr, regval);

  /* Set up a 3 byte message to send */

  buffer[0] = regaddr;

  regb8 = b16tob8(regval);
  buffer[1] = (uint8_t)(regb8 >> 8);
  buffer[2] = (uint8_t)regb8;

  /* Write the register address followed by the data (no RESTART) */

  I2C_SETADDRESS(priv->i2c, priv->addr, 7);
  return I2C_WRITE(priv->i2c, buffer, 3);
}
Пример #17
0
static int max1704x_putreg16(FAR struct max1704x_dev_s *priv, uint8_t regaddr,
                             uint16_t regval)
{
  uint8_t buffer[3];

  batdbg("addr: %02x regval: %08x\n", regaddr, regval);

  /* Set up a 3 byte message to send */

  buffer[0] = regaddr;
  buffer[1] = (uint8_t)(regval >> 8);
  buffer[2] = (uint8_t)(regval & 0xff);

  /* Set the I2C address and address size */

  I2C_SETADDRESS(priv->i2c, priv->addr, 7);

  /* Write the register address followed by the data (no RESTART) */

  return I2C_WRITE(priv->i2c, buffer, 3);
}
Пример #18
0
static int pca9635pw_i2c_write_byte(FAR struct pca9635pw_dev_s *priv,
                                    uint8_t const reg_addr,
                                    uint8_t const reg_val)
{
  dbg("pca9635pw_i2c_write_byte\n");

  /* assemble the 2 byte message comprised of reg_addr and reg_val */

  uint8_t const BUFFER_SIZE = 2;
  uint8_t buffer[BUFFER_SIZE];

  buffer[0] = reg_addr;
  buffer[1] = reg_val;

  /* Write the register address followed by the data (no RESTART) */

  uint8_t const NUMBER_OF_I2C_ADDRESS_BITS = 7;

  dbg("i2c addr: 0x%02X reg addr: 0x%02X value: 0x%02X\n", priv->i2c_addr,
      buffer[0], buffer[1]);

  int ret = I2C_SETADDRESS(priv->i2c, priv->i2c_addr,
                           NUMBER_OF_I2C_ADDRESS_BITS);
  if (ret != OK)
    {
      dbg("I2C_SETADDRESS returned error code %d\n", ret);
      return ret;
    }

  ret = I2C_WRITE(priv->i2c, buffer, BUFFER_SIZE);
  if (ret != OK)
    {
      dbg("I2C_WRITE returned error code %d\n", ret);
      return ret;
    }

  return OK;
}
Пример #19
0
static int lm75_readconf(FAR struct lm75_dev_s *priv, FAR uint8_t *conf)
{
  uint8_t buffer;
  int ret;

  /* Write the configuration register address */

  I2C_SETADDRESS(priv->i2c, priv->addr, 7);

  buffer = LM75_CONF_REG;
  ret = I2C_WRITE(priv->i2c, &buffer, 1);
  if (ret < 0)
    {
      sndbg("I2C_WRITE failed: %d\n", ret);
      return ret;
    }

  /* Restart and read 8-bits from the register */

  ret = I2C_READ(priv->i2c, conf, 1);
  sndbg("conf: %02x ret: %d\n", *conf, ret);
  return ret;
}
Пример #20
0
STMPE811_HANDLE stmpe811_instantiate(FAR struct i2c_dev_s *dev,
                                     FAR struct stmpe811_config_s *config)
#endif
{
  FAR struct stmpe811_dev_s *priv;
  uint8_t regval;
  int ret;

  /* Allocate the device state structure */

#ifdef CONFIG_STMPE811_MULTIPLE
  priv = (FAR struct stmpe811_dev_s *)kmm_zalloc(sizeof(struct stmpe811_dev_s));
  if (!priv)
    {
      return NULL;
    }

  /* And save the device structure in the list of STMPE811 so that we can find it later */

  priv->flink   = g_stmpe811list;
  g_stmpe811list = priv;
#else

  /* Use the one-and-only STMPE811 driver instance */

  priv = &g_stmpe811;
#endif

  /* Initialize the device state structure */

  sem_init(&priv->exclsem, 0, 1);
  priv->config = config;

#ifdef CONFIG_STMPE811_SPI
  priv->spi = dev;
#else
  priv->i2c = dev;

  /* Set the I2C address and frequency.  REVISIT:  This logic would be
   * insufficient if we share the I2C bus with any other devices that also
   * modify the address and frequency.
   */

  I2C_SETADDRESS(dev, config->address, 7);
  I2C_SETFREQUENCY(dev, config->frequency);
#endif

  /* Read and verify the STMPE811 chip ID */

  ret = stmpe811_checkid(priv);
  if (ret < 0)
    {
#ifdef CONFIG_STMPE811_MULTIPLE
      g_stmpe811list = priv->flink;
      kmm_free(priv);
#endif
      return NULL;
    }

  /* Generate STMPE811 Software reset */

  stmpe811_reset(priv);

  /* Configure the interrupt output pin to generate interrupts on high or low level. */

  regval  = stmpe811_getreg8(priv, STMPE811_INT_CTRL);
#ifdef CONFIG_STMPE811_ACTIVELOW
  regval &= ~INT_CTRL_INT_POLARITY; /* Pin polarity: Active low / falling edge */
#else
  regval |= INT_CTRL_INT_POLARITY;  /* Pin polarity: Active high / rising edge */
#endif
#ifdef CONFIG_STMPE811_EDGE
  regval |= INT_CTRL_INT_TYPE;      /* Edge interrupt */
#else
  regval &= ~INT_CTRL_INT_TYPE;     /* Level interrupt */
#endif
  stmpe811_putreg8(priv, STMPE811_INT_CTRL, regval);

  /* Attach the STMPE811 interrupt handler. */

  config->attach(config, stmpe811_interrupt);

  /* Clear any pending interrupts */

  stmpe811_putreg8(priv, STMPE811_INT_STA, INT_ALL);
  config->clear(config);
  config->enable(config, true);

  /* Enable global interrupts */

  regval  = stmpe811_getreg8(priv, STMPE811_INT_CTRL);
  regval |= INT_CTRL_GLOBAL_INT;
  stmpe811_putreg8(priv, STMPE811_INT_CTRL, regval);

  /* Return our private data structure as an opaque handle */

  return (STMPE811_HANDLE)priv;
}
Пример #21
0
int i2ccmd_get(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv)
{
  FAR struct i2c_dev_s *dev;
  FAR char *ptr;
  uint16_t result;
  uint8_t regaddr;
  long repititions;
  int nargs;
  int argndx;
  int ret;
  int i;

  /* Parse any command line arguments */

  for (argndx = 1; argndx < argc; )
    {
      /* Break out of the look when the last option has been parsed */

      ptr = argv[argndx];
      if (*ptr != '-')
        {
          break;
        }

      /* Otherwise, check for common options */

      nargs = common_args(i2ctool, &argv[argndx]);
      if (nargs < 0)
        {
          return ERROR;
        }
      argndx += nargs;
    }

  /* There may be one more thing on the command line:  The repitition
   * count.
   */

  repititions = 1;
  if (argndx < argc)
    {
      repititions = strtol(argv[argndx], NULL, 16);
      if (repititions < 1)
        {
          i2ctool_printf(i2ctool, g_i2cargrange, argv[0]);
          return ERROR;
        }

      argndx++;
    }

  if (argndx != argc)
    {
      i2ctool_printf(i2ctool, g_i2ctoomanyargs, argv[0]);
      return ERROR;
    }

  /* Get a handle to the I2C bus */

  dev = up_i2cinitialize(i2ctool->bus);
  if (!dev)
    {
       i2ctool_printf(i2ctool, "Failed to get bus %d\n", i2ctool->bus);
       return ERROR;
    }

  /* Set the frequency and the address (NOTE:  Only 7-bit address supported now) */

  I2C_SETFREQUENCY(dev, i2ctool->freq);
  I2C_SETADDRESS(dev, i2ctool->addr, 7);

  /* Loop for the requested number of repititions */

  regaddr = i2ctool->regaddr;
  ret = OK;

  for (i = 0; i < repititions; i++)
    {
      /* Read from the I2C bus */

      ret = i2ctool_get(i2ctool, dev, regaddr, &result);

      /* Display the result */

      if (ret == OK)
        {
          i2ctool_printf(i2ctool, "READ Bus: %d Addr: %02x Subaddr: %02x Value: ",
                         i2ctool->bus, i2ctool->addr, i2ctool->regaddr);
          if (i2ctool->width == 8)
            {
              i2ctool_printf(i2ctool, "%02x\n", result);
            }
          else
            {
              i2ctool_printf(i2ctool, "%04x\n", result);
            }
        }
      else
        {
          i2ctool_printf(i2ctool, g_i2cxfrerror, argv[0], -ret);
          break;
        }

      /* Auto-increment the address if so configured */

      if (i2ctool->autoincr)
        {
          regaddr++;
        }
    }

  (void)up_i2cuninitialize(dev);
  return ret;
}
Пример #22
0
int ov2640_initialize(FAR struct i2c_dev_s *i2c)
{
  int ret;

  /* Configure I2C bus for the OV2640 */

  I2C_SETADDRESS(i2c, CONFIG_OV2640_I2CADDR, 7);
  I2C_SETFREQUENCY(i2c, CONFIG_OV2640_FREQUENCY);

  /* Reset the OVR2640 */

  ret = ov2640_reset(i2c);
  if (ret < 0)
    {
      gdbg("ERROR: ov2640_reset failed: %d\n", ret);
      goto errout;
    }

  /* Check the CHIP ID */

  ret = ovr2640_chipid(i2c);
  if (ret < 0)
    {
      gdbg("ERROR: ovr2640_chipid failed: %d\n", ret);
      goto errout;
    }

  /* Initialize the OV2640 hardware */

#ifdef CONFIG_OV2640_JPEG
  /* Initialize for JPEG output */

  ret = ov2640_putreglist(i2c, g_ov2640_jpeg_init, OV2640_JPEG_INIT_NENTRIES);
  if (ret < 0)
    {
      gdbg("ERROR: ov2640_putreglist failed: %d\n", ret);
      goto errout;
    }

  ret = ov2640_putreglist(i2c, g_ov2640_yuv422, OV2640_YUV422_NENTRIES);
  if (ret < 0)
    {
      gdbg("ERROR: ov2640_putreglist failed: %d\n", ret);
      goto errout;
    }

  ret = ov2640_putreglist(i2c, g_ov2640_jpeg, OV2640_JPEG_NENTRIES);
  if (ret < 0)
    {
      gdbg("ERROR: ov2640_putreglist failed: %d\n", ret);
      goto errout;
    }

  ret = ov2640_putreg(i2c, 0xff, 0x01);
  if (ret < 0)
    {
      gdbg("ERROR: ov2640_putreg failed: %d\n", ret);
      goto errout;
    }

  ret = ov2640_putreg(i2c, 0x15, 0x00);
  if (ret < 0)
    {
      gdbg("ERROR: ov2640_putreg failed: %d\n", ret);
      goto errout;
    }

#if defined(CONFIG_OV2640_JPEG_QCIF_RESOLUTION)
  ret = ov2640_putreglist(i2c, g_ov2640_jpeg_qcif_resolution,
                          OV2640_JPEG_QCIF_RESOUTION_NENTRIES);

#elif defined(CONFIG_OV2640_JPEG_QVGA_RESOLUTION)
  ret = ov2640_putreglist(i2c, g_ov2640_jpeg_qvga_resolution,
                          OV2640_JPEG_QVGA_RESOUTION_NENTRIES);

#elif defined(CONFIG_OV2640_JPEG_CIF_RESOLUTION)
  ret = ov2640_putreglist(i2c, g_ov2640_jpeg_cif_resolution,
                          OV2640_JPEG_CIF_RESOUTION_NENTRIES);

#elif defined(CONFIG_OV2640_JPEG_VGA_RESOLUTION)
  ret = ov2640_putreglist(i2c, g_ov2640_jpeg_vga_resolution,
                          OV2640_JPEG_VGA_RESOUTION_NENTRIES);

#elif defined(CONFIG_OV2640_JPEG_SVGA_RESOLUTION)
  ret = ov2640_putreglist(i2c, g_ov2640_jpeg_svga_resolution,
                          OV2640_JPEG_SVGA_RESOUTION_NENTRIES);

#elif defined(CONFIG_OV2640_JPEG_XVGA_RESOLUTION)
  ret = ov2640_putreglist(i2c, g_ov2640_jpeg_xvga_resolution,
                          OV2640_JPEG_XVGA_RESOUTION_NENTRIES);

#elif defined(CONFIG_OV2640_JPEG_SXVGA_RESOLUTION)
  ret = ov2640_putreglist(i2c, g_ov2640_jpeg_sxvga_resolution,
                          OV2640_JPEG_SXVGA_RESOUTION_NENTRIES);

#elif defined(CONFIG_OV2640_JPEG_UXGA_RESOLUTION)
  ret = ov2640_putreglist(i2c, g_ov2640_jpeg_uxga_resolution,
                          OV2640_JPEG_UXGA_RESOUTION_NENTRIES);

#else
#  error Unspecified JPEG resolution
#endif

  if (ret < 0)
    {
      gdbg("ERROR: ov2640_putreglist failed: %d\n", ret);
      goto errout;
    }

#else /* CONFIG_OV2640_JPEG */

  /* Setup initial register values */

  ret = ov2640_putreglist(i2c, g_ov2640_initialregs,
                          OV2640_INITIALREGS_NENTRIES);
  if (ret < 0)
    {
      gdbg("ERROR: ov2640_putreglist failed: %d\n", ret);
      goto errout;
    }

  /* Setup image resolution */

  ret = ov2640_putreglist(i2c, g_ov2640_resolution_common,
                          OV2640_RESOLUTION_COMMON_NENTRIES);
  if (ret < 0)
    {
      gdbg("ERROR: ov2640_putreglist failed: %d\n", ret);
      goto errout;
    }

#if defined(CONFIG_OV2640_QCIF_RESOLUTION)
  ret = ov2640_putreglist(i2c, g_ov2640_qcif_resolution,
                          OV2640_QCIF_RESOLUTION_NENTRIES);

#elif defined(CONFIG_OV2640_QVGA_RESOLUTION)
  ret = ov2640_putreglist(i2c, g_ov2640_qvga_resolution,
                          OV2640_QVGA_RESOLUTION_NENTRIES);

#elif defined(CONFIG_OV2640_CIF_RESOLUTION)
  ret = ov2640_putreglist(i2c, g_ov2640_cif_resolution,
                          OV2640_CIF_RESOLUTION_NENTRIES);

#elif defined(CONFIG_OV2640_VGA_RESOLUTION)
  ret = ov2640_putreglist(i2c, g_ov2640_vga_resolution,
                          OV2640_VGA_RESOLUTION_NENTRIES);

#elif defined(CONFIG_OV2640_SVGA_RESOLUTION)
  ret = ov2640_putreglist(i2c, g_ov2640_svga_resolution,
                          OV2640_SVGA_RESOLUTION_NENTRIES);

#elif defined(CONFIG_OV2640_XGA_RESOLUTION)
  ret = ov2640_putreglist(i2c, g_ov2640_xga_resolution,
                          OV2640_XGA_RESOLUTION_NENTRIES);

#elif defined(CONFIG_OV2640_SXGA_RESOLUTION)
  ret = ov2640_putreglist(i2c, g_ov2640_sxga_resolution,
                          OV2640_SXGA_RESOLUTION_NENTRIES);

#elif defined(CONFIG_OV2640_UXGA_RESOLUTION)
  ret = ov2640_putreglist(i2c, g_ov2640_uxga_resolution,
                          OV2640_UXGA_RESOLUTION_NENTRIES);

#else
#  error Unknown image resolution
#endif

  if (ret < 0)
    {
      gdbg("ERROR: ov2640_putreglist failed: %d\n", ret);
      goto errout;
    }

/* Color format register settings */

  ret = ov2640_putreglist(i2c, g_ov2640_colorfmt_common,
                    OV2640_COLORFMT_COMMON_NENTRIES);
  if (ret < 0)
    {
      gdbg("ERROR: ov2640_putreglist failed: %d\n", ret);
      goto errout;
    }

#if defined(CONFIG_OV2640_YUV422_COLORFMT)
  ret = ov2640_putreglist(i2c, g_ov2640_yuv422_colorfmt,
                    OV2640_YUV422_COLORFMT_NENTRIES);

#elif defined(CONFIG_OV2640_RGB565_COLORFMT)
  ret = ov2640_putreglist(i2c, g_ov2640_rgb565_colorfmt,
                    OV2640_RGB565_COLORFMT_NENTRIES);

#else
#  error Unknown color format
#endif

  if (ret < 0)
    {
      gdbg("ERROR: ov2640_putreglist failed: %d\n", ret);
      goto errout;
    }

#endif /* CONFIG_OV2640_JPEG */

  return OK;

errout:
  gdbg("ERROR: Failed to initialize the OV2640: %d\n", ret);
  (void)ov2640_reset(i2c);
  return ret;
}
Пример #23
0
int i2ccmd_verf(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv)
{
  FAR struct i2c_dev_s *dev;
  FAR char *ptr;
  uint16_t rdvalue;
  uint8_t regaddr;
  bool addrinaddr;
  long wrvalue;
  long repititions;
  int nargs;
  int argndx;
  int ret;
  int i;

  /* Parse any command line arguments */

  for (argndx = 1; argndx < argc; )
    {
      /* Break out of the look when the last option has been parsed */

      ptr = argv[argndx];
      if (*ptr != '-')
        {
          break;
        }

      /* Otherwise, check for common options */

      nargs = common_args(i2ctool, &argv[argndx]);
      if (nargs < 0)
        {
          return ERROR;
        }
      argndx += nargs;
    }

  /* The options may be followed by the optional wrvalue to be written.  If omitted, then
   * the register address will be used as the wrvalue, providing an address-in-address
   * test.
   */

  addrinaddr = true;
  wrvalue    = 0;

  if (argndx < argc)
    {
      wrvalue = strtol(argv[argndx], NULL, 16);
      if (i2ctool->width == 8)
        {
          if (wrvalue < 0 || wrvalue > 255)
            {
              i2ctool_printf(i2ctool, g_i2cargrange, argv[0]);
              return ERROR;
            }
        }
      else if (wrvalue < 0 || wrvalue > 65535)
        {
          i2ctool_printf(i2ctool, g_i2cargrange, argv[0]);
          return ERROR;
        }

      addrinaddr = false;
      argndx++;
    }

  /* There may be one more thing on the command line:  The repitition
   * count.
   */

  repititions = 1;
  if (argndx < argc)
    {
      repititions = strtol(argv[argndx], NULL, 16);
      if (repititions < 1)
        {
          i2ctool_printf(i2ctool, g_i2cargrange, argv[0]);
          return ERROR;
        }

      argndx++;
    }

  if (argndx != argc)
    {
      i2ctool_printf(i2ctool, g_i2ctoomanyargs, argv[0]);
      return ERROR;
    }

  /* Get a handle to the I2C bus */

  dev = up_i2cinitialize(i2ctool->bus);
  if (!dev)
    {
       i2ctool_printf(i2ctool, "Failed to get bus %d\n", i2ctool->bus);
       return ERROR;
    }

  /* Set the frequency and the address (NOTE:  Only 7-bit address supported now) */

  I2C_SETFREQUENCY(dev, i2ctool->freq);
  I2C_SETADDRESS(dev, i2ctool->addr, 7);

  /* Loop for the requested number of repititions */

  regaddr = i2ctool->regaddr;
  ret = OK;

  for (i = 0; i < repititions; i++)
    {
      /* If we are performing an address-in-address test, then use the register
       * address as the value to write.
       */

      if (addrinaddr)
        {
          wrvalue = regaddr;
        }

      /* Write to the I2C bus */

      ret = i2ctool_set(i2ctool, dev, regaddr, (uint16_t)wrvalue);
      if (ret == OK)
        {
          /* Read the value back from the I2C bus */

          ret = i2ctool_get(i2ctool, dev, regaddr, &rdvalue);
        }

      /* Display the result */

      if (ret == OK)
        {
          i2ctool_printf(i2ctool, "VERIFY Bus: %d Addr: %02x Subaddr: %02x Wrote: ",
                         i2ctool->bus, i2ctool->addr, i2ctool->regaddr);

          if (i2ctool->width == 8)
            {
              i2ctool_printf(i2ctool, "%02x Read: %02x", (int)wrvalue, (int)rdvalue);
            }
          else
            {
              i2ctool_printf(i2ctool, "%04x Read: %04x", (int)wrvalue, (int)rdvalue);
            }

          if (wrvalue != rdvalue)
            {
              i2ctool_printf(i2ctool, "  <<< FAILURE\n");
            }
          else
            {
              i2ctool_printf(i2ctool, "\n");
            }
        }
      else
        {
          i2ctool_printf(i2ctool, g_i2cxfrerror, argv[0], -ret);
          break;
        }

      /* Auto-increment the address if so configured */

      if (i2ctool->autoincr)
        {
          regaddr++;
        }
    }

  (void)up_i2cuninitialize(dev);
  return ret;
}
Пример #24
0
FAR struct lcd_dev_s *ssd1306_initialize(FAR struct i2c_dev_s *dev, unsigned int devno)
#endif
{
  FAR struct ssd1306_dev_s  *priv = &g_oleddev;

  lcdvdbg("Initializing\n");
  DEBUGASSERT(spi && devno == 0);

#ifdef CONFIG_LCD_SSD1306_SPI
  priv->spi = dev;

  /* If this SPI bus is not shared, then we can config it now.
   * If it is shared, then other device could change our config,
   * then just configure before sending data.
   */

#  ifdef CONFIG_SPI_OWNBUS
    /* Configure SPI */

    SPI_SETMODE(priv->spi, CONFIG_SSD1306_SPIMODE);
    SPI_SETBITS(priv->spi, 8);
    SPI_SETFREQUENCY(priv->spi, CONFIG_SSD1306_FREQUENCY);
#  else
    /* Configure the SPI */

    ssd1306_configspi(priv->spi);
#  endif

#else
  /* Remember the I2C configuration */

  priv->i2c  = dev;
  priv->addr = CONFIG_SSD1306_I2CADDR;

  /* Set the I2C address and frequency.  REVISIT:  This logic would be
   * insufficient if we share the I2C bus with any other devices that also
   * modify the address and frequency.
   */

  I2C_SETADDRESS(priv->i2c, CONFIG_SSD1306_I2CADDR, 7);
  I2C_SETFREQUENCY(priv->i2c, CONFIG_SSD1306_I2CFREQ);
#endif

  /* Lock and select device */

  ssd1306_select(priv, true);

  /* Select command transfer */

  ssd1306_cmddata(priv, true);

  /* Configure OLED SPI or I/O, must be delayed 1-10ms */

  up_mdelay(5);

  /* Configure the device */

  ssd1306_sendbyte(priv, SSD1306_DISPOFF);         /* Display off 0xae */
  ssd1306_sendbyte(priv, SSD1306_SETCOLL(0));      /* Set lower column address 0x00 */
  ssd1306_sendbyte(priv, SSD1306_SETCOLH(0));      /* Set higher column address 0x10 */
  ssd1306_sendbyte(priv, SSD1306_STARTLINE(0));    /* Set display start line 0x40 */
  /* ssd1306_sendbyte(priv, SSD1306_PAGEADDR(0));*//* Set page address  (Can ignore)*/
  ssd1306_sendbyte(priv, SSD1306_CONTRAST_MODE);   /* Contrast control 0x81 */
  ssd1306_sendbyte(priv,SSD1306_CONTRAST(SSD1306_DEV_CONTRAST));  /* Default contrast 0xCF */
  ssd1306_sendbyte(priv, SSD1306_REMAPPLEFT);      /* Set segment remap left 95 to 0 | 0xa1 */
  /* ssd1306_sendbyte(priv, SSD1306_EDISPOFF); */  /* Normal display off 0xa4 (Can ignore)*/
  ssd1306_sendbyte(priv, SSD1306_NORMAL);          /* Normal (un-reversed) display mode 0xa6 */
  ssd1306_sendbyte(priv, SSD1306_MRATIO_MODE);     /* Multiplex ratio 0xa8 */
  ssd1306_sendbyte(priv, SSD1306_MRATIO(SSD1306_DEV_DUTY));  /* Duty = 1/64 or 1/32 */
  /* ssd1306_sendbyte(priv, SSD1306_SCANTOCOM0);*/ /* Com scan direction: Scan from COM[n-1] to COM[0] (Can ignore)*/
  ssd1306_sendbyte(priv, SSD1306_DISPOFFS_MODE);   /* Set display offset 0xd3 */
  ssd1306_sendbyte(priv, SSD1306_DISPOFFS(0));
  ssd1306_sendbyte(priv, SSD1306_CLKDIV_SET);      /* Set clock divider 0xd5*/
  ssd1306_sendbyte(priv, SSD1306_CLKDIV(8,0));     /* 0x80*/

  ssd1306_sendbyte(priv, SSD1306_CHRGPER_SET);     /* Set pre-charge period 0xd9 */
  ssd1306_sendbyte(priv, SSD1306_CHRGPER(0x0f,1)); /* 0xf1 or 0x22 Enhanced mode */

  ssd1306_sendbyte(priv, SSD1306_CMNPAD_CONFIG);   /* Set common pads / set com pins hardware configuration 0xda */
  ssd1306_sendbyte(priv, SSD1306_CMNPAD(SSD1306_DEV_CMNPAD)); /* 0x12 or 0x02 */

  ssd1306_sendbyte(priv, SSD1306_VCOM_SET);        /* set vcomh 0xdb*/
  ssd1306_sendbyte(priv, SSD1306_VCOM(0x40));

  ssd1306_sendbyte(priv, SSD1306_CHRPUMP_SET);     /* Set Charge Pump enable/disable 0x8d ssd1306 */
  ssd1306_sendbyte(priv, SSD1306_CHRPUMP_ON);      /* 0x14 close 0x10 */

  /* ssd1306_sendbyte(priv, SSD1306_DCDC_MODE); */ /* DC/DC control mode: on (SSD1306 Not supported) */
  /* ssd1306_sendbyte(priv, SSD1306_DCDC_ON); */

  ssd1306_sendbyte(priv, SSD1306_DISPON);          /* Display ON 0xaf */

  /* De-select and unlock the device */

  ssd1306_select(priv, false);

  /* Clear the display */

  up_mdelay(100);
  ssd1306_fill(&priv->dev, SSD1306_Y1_BLACK);
  return &priv->dev;
}
Пример #25
0
/*
 * Select the destination device to communicate with.
 * I2C requires a bus address.
 */
inline void i2c_select_device(uint8_t addr)
{
    /* Write the register address followed by the data (no RESTART) */
    I2C_SETADDRESS(sw_exp_dev, addr, 7);
}