示例#1
0
文件: lm75.c 项目: AlexShiLucky/NuttX
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 */

  ret = lm75_i2c_write(priv, &regaddr, 1);
  if (ret < 0)
    {
      snerr("ERROR: i2c_write failed: %d\n", ret);
      return ret;
    }

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

  ret = lm75_i2c_read(priv, buffer, 2);
  if (ret < 0)
    {
      snerr("ERROR: 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]);
  sninfo("addr: %02x value: %08x ret: %d\n", regaddr, *regvalue, ret);
  return OK;
}
示例#2
0
文件: max6675.c 项目: dagar/NuttX
int max6675_register(FAR const char *devpath, FAR struct spi_dev_s *spi)
{
  FAR struct max6675_dev_s *priv;
  int ret;

  /* Sanity check */

  DEBUGASSERT(spi != NULL);

  /* Initialize the MAX6675 device structure */

  priv = (FAR struct max6675_dev_s *)kmm_malloc(sizeof(struct max6675_dev_s));
  if (priv == NULL)
    {
      snerr("ERROR: Failed to allocate instance\n");
      return -ENOMEM;
    }

  priv->spi        = spi;
  priv->temp       = 0;

  /* Register the character driver */

  ret = register_driver(devpath, &g_max6675fops, 0666, priv);
  if (ret < 0)
    {
      snerr("ERROR: Failed to register driver: %d\n", ret);
      kmm_free(priv);
    }

  return ret;
}
示例#3
0
文件: lm75.c 项目: AlexShiLucky/NuttX
static int lm75_readtemp(FAR struct lm75_dev_s *priv, FAR b16_t *temp)
{
  b16_t temp16;
  int ret;

  /* Read the raw temperature data (b16_t) */

  ret = lm75_readb16(priv, LM75_TEMP_REG, &temp16);
  if (ret < 0)
    {
      snerr("ERROR: lm75_readb16 failed: %d\n", ret);
      return ret;
    }

  add_sensor_randomness(temp16);

  sninfo("Centigrade: %08x\n", temp16);

  /* Was fahrenheit requested? */

  if (priv->fahrenheit)
    {
      /* Centigrade to Fahrenheit conversion:  F = 9*C/5 + 32 */

      temp16 =  b16mulb16(temp16, B16_9DIV5) + B16_32;
      sninfo("Fahrenheit: %08x\n", temp16);
    }

  *temp = temp16;
  return OK;
}
示例#4
0
int stm32l4_lsm303agr_initialize(char *devpath)
{
  FAR struct i2c_master_s *i2c;
  int ret = OK;

  sninfo("INFO: Initializing LMS303AGR sensor over I2C\n");

#if defined(CONFIG_STM32L4_I2C1)
  i2c = stm32l4_i2cbus_initialize(1);
  if (i2c == NULL)
    {
      return -ENODEV;
    }

  ret = lsm303agr_sensor_register("/dev/lsm303mag0", i2c, LSM303AGRMAGNETO_ADDR);
  if (ret < 0)
    {
      snerr("ERROR: Failed to initialize LMS303AGR magneto driver %s\n", devpath);
      return -ENODEV;
    }

  sninfo("INFO: LMS303AGR sensor has been initialized successfully\n");
#endif

  return ret;
}
示例#5
0
文件: oneshot.c 项目: dagar/NuttX
int oneshot_register(FAR const char *devname,
                     FAR struct oneshot_lowerhalf_s *lower)
{
  FAR struct oneshot_dev_s *priv;
  int ret;

  sninfo("devname=%s lower=%p\n", devname, lower);
  DEBUGASSERT(devname != NULL && lower != NULL);

  /* Allocate a new oneshot timer driver instance */

  priv = (FAR struct oneshot_dev_s *)
    kmm_zalloc(sizeof(struct oneshot_dev_s));

  if (!priv)
    {
      snerr("ERROR: Failed to allocate device structure\n");
      return -ENOMEM;
    }

  /* Initialize the new oneshot timer driver instance */

  priv->od_lower = lower;
  nxsem_init(&priv->od_exclsem, 0, 1);

  /* And register the oneshot timer driver */

  ret = register_driver(devname, &g_oneshot_ops, 0666, priv);
  if (ret < 0)
    {
      snerr("ERROR: register_driver failed: %d\n", ret);
      nxsem_destroy(&priv->od_exclsem);
      kmm_free(priv);
    }

  return ret;
}
示例#6
0
文件: lm75.c 项目: AlexShiLucky/NuttX
int lm75_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, uint8_t addr)
{
  FAR struct lm75_dev_s *priv;
  int ret;

  /* Sanity check */

  DEBUGASSERT(i2c != NULL);
  DEBUGASSERT(addr == CONFIG_LM75_ADDR0 || addr == CONFIG_LM75_ADDR1 ||
              addr == CONFIG_LM75_ADDR2 || addr == CONFIG_LM75_ADDR3 ||
              addr == CONFIG_LM75_ADDR4 || addr == CONFIG_LM75_ADDR5 ||
              addr == CONFIG_LM75_ADDR6 || addr == CONFIG_LM75_ADDR7);

  /* Initialize the LM-75 device structure */

  priv = (FAR struct lm75_dev_s *)kmm_malloc(sizeof(struct lm75_dev_s));
  if (priv == NULL)
    {
      snerr("ERROR: Failed to allocate instance\n");
      return -ENOMEM;
    }

  priv->i2c        = i2c;
  priv->addr       = addr;
  priv->fahrenheit = false;

  /* Register the character driver */

  ret = register_driver(devpath, &g_lm75fops, 0666, priv);
  if (ret < 0)
    {
      snerr("ERROR: Failed to register driver: %d\n", ret);
      kmm_free(priv);
    }

  return ret;
}
示例#7
0
int stm32_qencoder_initialize(FAR const char *devpath, int timer)
{
  int ret;

  /* Initialize a quadrature encoder interface. */

  sninfo("Initializing the quadrature encoder using TIM%d\n", timer);
  ret = stm32_qeinitialize(devpath, timer);
  if (ret < 0)
    {
      snerr("ERROR: stm32_qeinitialize failed: %d\n", ret);
    }

  return ret;
}
示例#8
0
int stm32_max6675initialize(FAR const char *devpath)
{
  FAR struct spi_dev_s *spi;
  int ret;

  spi = stm32_spibus_initialize(MAX6675_SPI_PORTNO);

  if (!spi)
    {
      return -ENODEV;
    }

  /* Then register the barometer sensor */
  
  ret = max6675_register(devpath, spi);
  if (ret < 0)
    {
      snerr("ERROR: Error registering MAX6675\n");
    }

  return ret;
}
示例#9
0
文件: lm75.c 项目: AlexShiLucky/NuttX
static int lm75_readconf(FAR struct lm75_dev_s *priv, FAR uint8_t *conf)
{
  uint8_t buffer;
  int ret;

  /* Write the configuration register address */

  buffer = LM75_CONF_REG;

  ret = lm75_i2c_write(priv, &buffer, 1);
  if (ret < 0)
    {
      snerr("ERROR: i2c_write failed: %d\n", ret);
      return ret;
    }

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

  ret = lm75_i2c_read(priv, conf, 1);
  sninfo("conf: %02x ret: %d\n", *conf, ret);
  return ret;
}
示例#10
0
文件: lm75.c 项目: AlexShiLucky/NuttX
static ssize_t lm75_read(FAR struct file *filep, FAR char *buffer, size_t buflen)
{
  FAR struct inode      *inode = filep->f_inode;
  FAR struct lm75_dev_s *priv   = inode->i_private;
  FAR b16_t             *ptr;
  ssize_t                nsamples;
  int                    i;
  int                    ret;

  /* How many samples were requested to get? */

  nsamples = buflen / sizeof(b16_t);
  ptr      = (FAR b16_t *)buffer;

  sninfo("buflen: %d nsamples: %d\n", buflen, nsamples);

  /* Get the requested number of samples */

  for (i = 0; i < nsamples; i++)
    {
      b16_t temp = 0;

      /* Read the next b16_t temperature value */

      ret = lm75_readtemp(priv, &temp);
      if (ret < 0)
        {
          snerr("ERROR: lm75_readtemp failed: %d\n", ret);
          return (ssize_t)ret;
        }

      /* Save the temperature value in the user buffer */

      *ptr++ = temp;
    }

  return nsamples * sizeof(b16_t);
}
示例#11
0
文件: qencoder.c 项目: a1ien/nuttx
int qe_register(FAR const char *devpath, FAR struct qe_lowerhalf_s *lower)
{
  FAR struct qe_upperhalf_s *upper;

  /* Allocate the upper-half data structure */

  upper = (FAR struct qe_upperhalf_s *)kmm_zalloc(sizeof(struct qe_upperhalf_s));
  if (!upper)
    {
      snerr("ERROR: Allocation failed\n");
      return -ENOMEM;
    }

  /* Initialize the PWM device structure (it was already zeroed by kmm_zalloc()) */

  sem_init(&upper->exclsem, 0, 1);
  upper->lower = lower;

  /* Register the PWM device */

  sninfo("Registering %s\n", devpath);
  return register_driver(devpath, &g_qeops, 0666, upper);
}
示例#12
0
文件: max6675.c 项目: dagar/NuttX
static ssize_t max6675_read(FAR struct file *filep, FAR char *buffer, size_t buflen)
{
  FAR struct inode         *inode = filep->f_inode;
  FAR struct max6675_dev_s *priv  = inode->i_private;
  FAR uint16_t             *temp  = (FAR uint16_t *) buffer;
  int                       ret   = 2;
  int16_t                   regmsb;
  int16_t                   regval;

  /* Check for issues */

  if (!buffer)
    {
      snerr("ERROR: Buffer is null\n");
      return -EINVAL;
    }

  if (buflen != 2)
    {
      snerr("ERROR: You can't read something other than 16 bits (2 bytes)\n");
      return -EINVAL;
    }

  /* Enable MAX6675's chip select */

  max6675_lock(priv->spi);
  SPI_SELECT(priv->spi, SPIDEV_TEMPERATURE(0), true);

  /* Read temperature */

  SPI_RECVBLOCK(priv->spi, &regmsb, 2);

  /* Disable MAX6675's chip select */

  SPI_SELECT(priv->spi, SPIDEV_TEMPERATURE(0), false);
  max6675_unlock(priv->spi);

  regval  = (regmsb & 0xFF00) >> 8;
  regval |= (regmsb & 0xFF) << 8;

  sninfo("Read from MAX6675 = 0x%04X\n", regval);

  /* Verify if the device ID bit is really zero */

  if (regval & MAX6675_DEV_ID)
    {
      snerr("ERROR: The Device ID bit needs to be 0 !\n");
      ret = -EINVAL;
    }

  /* Detect if termocople input is open */

  if (regval & MAX6675_OPEN_CIRCUIT)
    {
      snerr("ERROR: The thermocouple input is not connected!\n");
      ret = -EINVAL;
    }

  /* Feed sensor data to entropy pool */

  add_sensor_randomness(regval);

  /* Get the temperature */

  *temp = (regval & MAX6675_TEMP_COUPLE) >> 3;

  /* Return two bytes, the temperature is fixed point Q10.2, then divide by 4
   * in your application in order to get real temperature in Celsius degrees.
   */

  return ret;
}
示例#13
0
/*------------------------------------------------------------------------
 * sendquery - parse the input line and send the query.  Input has
 * one of the following forms:
 * 		 [object-name]+
 * 		 next [object-name]+ 
 * 		 set [object-name type value]+
 *------------------------------------------------------------------------
 */
LOCAL int
sendquery(
    int		stdout,
    char	*server)
{
	struct req_desc	rqd;
	struct snbentry	*bl;
	char		*word;
	int 		repl;

	initgetword(buf);
	rqd.reqtype = PDU_GET;		/* by default */
	rqd.bindle = rqd.bindlf = NULLCH;

	getword(&word);
	if (*word == '\0')
		return OK;

	if (strequ(word, "next")) {
		rqd.reqtype = PDU_GETN;
		getword(&word);
		if (parseoidlist(&rqd, &word) == SYSERR) {
			fprintf(stdout, "unknown variable\n");
			return SYSERR;
		}
		if ((bl = rqd.bindlf) == (void *)NULLPTR) {
			if (lastoidset) { /* no oids so use last one */
				rqd.bindlf = rqd.bindle = bl = getnewbl();
				bl->sb_oid.len = lastobjid.len;
				memcpy(bl->sb_oid.id, lastobjid.id,
					lastobjid.len*2);
				SVTYPE(bl) = ASN1_NULL;
			} else {
				fprintf(stdout, "bad syntax\n");
				return SYSERR;
			}
		}
	} else if (strequ(word, "set")) {
		rqd.reqtype = PDU_SET;
		if (parseset(&rqd, stdout) == SYSERR)
			return SYSERR;
	} else if (parseoidlist(&rqd, &word) == SYSERR) {
		fprintf(stdout, "unknown variable\n");
		return SYSERR;
	}
	repl = snclient(&rqd, server, stdout);
	switch (repl) {
	case SCL_OK:
		if (rqd.err_stat == SNMP_OK)
		    snmpprint(stdout, rqd.bindlf);
		else
		    snerr(stdout, &rqd);
		break;
	case SCL_OPENF:
		fprintf(stdout, "snmp: open failed\n");
		break;
	case SCL_WRITEF:
		fprintf(stdout, "snmp: write failed\n");
		break;
	case SCL_NORESP:
		fprintf(stdout, "snmp: No response from server %s\n", server);
		break;
	case SCL_READF:
		fprintf(stdout, "snmp: read failed\n");
		break;
	case SCL_BADRESP:
		fprintf(stdout, "snmp: received bad response\n");
		break;
	}
	
	/* save this object for use with the "next" operation */
	lastobjid.len = rqd.bindlf->sb_oid.len;
	memcpy(lastobjid.id, rqd.bindlf->sb_oid.id,
		rqd.bindlf->sb_oid.len*2);
	lastoidset = TRUE;

	snfreebl(&rqd.bindlf);
	return OK;
}