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, ®addr, 1); if (ret < 0) { sndbg("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) { 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; }
static int mb7040_ioctl(FAR struct file *filep, int cmd, unsigned long arg) { FAR struct inode *inode = filep->f_inode; FAR struct mb7040_dev_s *priv = inode->i_private; int ret = OK; /* Handle ioctl commands */ switch (cmd) { /* Command the device to measure the range. Arg: None. */ case SNIOC_MEASURE: DEBUGASSERT(arg == 0); ret = mb7040_measurerange(priv); break; /* Read the range last measured by the device. Arg: int32_t* pointer. */ case SNIOC_RANGE: { FAR int32_t *ptr = (FAR int32_t *)((uintptr_t)arg); uint16_t range = 0; DEBUGASSERT(ptr != NULL); ret = mb7040_readrange(priv, &range); if (ret == OK) { *ptr = (int32_t)range; } sndbg("range: %04x ret: %d\n", *ptr, ret); } break; /* Change the device's I2C address. Arg: uint8_t value. */ case SNIOC_CHANGEADDR: ret = mb7040_changeaddr(priv, (uint8_t)arg); sndbg("new addr: %02x ret: %d\n", *(uint8_t *)arg, ret); break; /* Unrecognized commands */ default: sndbg("Unrecognized cmd: %d arg: %ld\n", cmd, arg); ret = -ENOTTY; break; } return ret; }
static ssize_t max31855_read(FAR struct file *filep, FAR char *buffer, size_t buflen) { FAR struct inode *inode = filep->f_inode; FAR struct max31855_dev_s *priv = inode->i_private; FAR uint16_t *temp = (FAR uint16_t *) buffer; int ret = 2; int32_t regmsb; int32_t regval; /* Check for issues */ if (!buffer) { sndbg("Buffer is null\n"); return -EINVAL; } if (buflen != 2) { sndbg("You can't read something other than 16 bits (2 bytes)\n"); return -EINVAL; } /* Enable MAX31855's chip select */ SPI_SELECT(priv->spi, SPIDEV_TEMPERATURE, true); /* Read temperature */ SPI_RECVBLOCK(priv->spi, ®msb, 4); /* Disable MAX31855's chip select */ SPI_SELECT(priv->spi, SPIDEV_TEMPERATURE, false); regval = (regmsb & 0xFF000000) >> 24; regval |= (regmsb & 0xFF0000) >> 8; regval |= (regmsb & 0xFF00) << 8; regval |= (regmsb & 0xFF) << 24; sndbg("Read from MAX31855 = 0x%08X\n", regval); /* If negative, fix signal bits */ if (regval & 0x80000000) { *temp = 0xc000 | (regval >> 18); }
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, ®addr, sizeof(regaddr)); if (ret < 0) { sndbg("I2C_WRITE failed: %d\n", ret); } return ret; }
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; }
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) { sndbg("i2c_write failed: %d\n", ret); return ret; } /* Restart and read 8-bits from the register */ ret = lm75_i2c_read(priv, conf, 1); sndbg("conf: %02x ret: %d\n", *conf, ret); return ret; }
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; sndbg("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) { sndbg("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); }
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; }
int lm75_register(FAR const char *devpath, FAR struct i2c_dev_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) { sndbg("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) { sndbg("Failed to register driver: %d\n", ret); kmm_free(priv); } return ret; }
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) */ return lm75_i2c_write(priv, buffer, 2); }
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); }
uint16_t adxl345_getreg16(FAR struct adxl345_dev_s *priv, uint8_t regaddr) { /* 16-bit data read sequence: * * Start - I2C_Write_Address - ADXL345_Reg_Address - * Repeated_Start - I2C_Read_Address - ADXL345_Read_Data_1 - * ADXL345_Read_Data_2 - STOP */ struct i2c_msg_s msg[2]; uint8_t rxbuffer[2]; int ret; /* Setup 8-bit ADXL345 address write message */ msg[0].addr = priv->config->address; /* 7-bit address */ msg[0].flags = 0; /* Write transaction, beginning with START */ msg[0].buffer = ®addr; /* Transfer from this address */ msg[0].length = 1; /* Send one byte following the address * (no STOP) */ /* Set up the 8-bit ADXL345 data read message */ msg[1].addr = priv->config->address; /* 7-bit address */ msg[1].flags = I2C_M_READ; /* Read transaction, beginning with Re-START */ msg[1].buffer = rxbuffer; /* Transfer to this address */ msg[1].length = 2; /* Receive two bytes following the address * (then STOP) */ /* Perform the transfer */ ret = I2C_TRANSFER(priv->i2c, msg, 2); if (ret < 0) { sndbg("I2C_TRANSFER failed: %d\n", ret); return 0; } #ifdef CONFIG_ADXL345_REGDEBUG dbg("%02x->%02x%02x\n", regaddr, rxbuffer[0], rxbuffer[1]); #endif return (uint16_t)rxbuffer[0] << 8 | (uint16_t)rxbuffer[1]; }
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) */ return lm75_i2c_write(priv, buffer, 3); }
int stm32_max31855initialize(FAR const char *devpath) { FAR struct spi_dev_s *spi; int ret; spi = up_spiinitialize(MAX31855_SPI_PORTNO); if (!spi) { return -ENODEV; } /* Then register the barometer sensor */ ret = max31855_register(devpath, spi); if (ret < 0) { sndbg("Error registering MAX31855\n"); } return ret; }
void adxl345_putreg8(FAR struct adxl345_dev_s *priv, uint8_t regaddr, uint8_t regval) { /* 8-bit data read sequence: * * Start - I2C_Write_Address - ADXL345_Reg_Address - ADXL345_Write_Data - STOP */ struct i2c_msg_s msg; uint8_t txbuffer[2]; int ret; #ifdef CONFIG_ADXL345_REGDEBUG dbg("%02x<-%02x\n", regaddr, regval); #endif /* Setup to the data to be transferred. Two bytes: The ADXL345 register * address followed by one byte of data. */ txbuffer[0] = regaddr; txbuffer[1] = regval; /* Setup 8-bit ADXL345 address write message */ msg.addr = priv->config->address; /* 7-bit address */ msg.flags = 0; /* Write transaction, beginning with START */ msg.buffer = txbuffer; /* Transfer from this address */ msg.length = 2; /* Send two byte following the address * (then STOP) */ /* Perform the transfer */ ret = I2C_TRANSFER(priv->i2c, &msg, 1); if (ret < 0) { sndbg("I2C_TRANSFER failed: %d\n", ret); } }
int qe_devinit(void) { static bool initialized = false; int ret; /* Check if we are already initialized */ if (!initialized) { /* Initialize a quadrature encoder interface. */ snvdbg("Initializing the quadrature encoder using TIM%d\n", TIMID); ret = stm32_qeinitialize("/dev/qe0", TIMID); if (ret < 0) { sndbg("stm32_qeinitialize failed: %d\n", ret); return ret; } initialized = true; } return OK; }
static int lm75_ioctl(FAR struct file *filep, int cmd, unsigned long arg) { FAR struct inode *inode = filep->f_inode; FAR struct lm75_dev_s *priv = inode->i_private; int ret = OK; switch (cmd) { /* Read from the configuration register. Arg: uint8_t* pointer */ case SNIOC_READCONF: { FAR uint8_t *ptr = (FAR uint8_t *)((uintptr_t)arg); DEBUGASSERT(ptr != NULL); ret = lm75_readconf(priv, ptr); sndbg("conf: %02x ret: %d\n", *ptr, ret); } break; /* Wrtie to the configuration register. Arg: uint8_t value */ case SNIOC_WRITECONF: ret = lm75_writeconf(priv, (uint8_t)arg); sndbg("conf: %02x ret: %d\n", *(FAR uint8_t *)arg, ret); break; /* Shutdown the LM75, Arg: None */ case SNIOC_SHUTDOWN: { uint8_t conf; ret = lm75_readconf(priv, &conf); if (ret == OK) { ret = lm75_writeconf(priv, conf | LM75_CONF_SHUTDOWN); } sndbg("conf: %02x ret: %d\n", conf | LM75_CONF_SHUTDOWN, ret); } break; /* Powerup the LM75, Arg: None */ case SNIOC_POWERUP: { uint8_t conf; ret = lm75_readconf(priv, &conf); if (ret == OK) { ret = lm75_writeconf(priv, conf & ~LM75_CONF_SHUTDOWN); } sndbg("conf: %02x ret: %d\n", conf & ~LM75_CONF_SHUTDOWN, ret); } break; /* Report samples in Fahrenheit */ case SNIOC_FAHRENHEIT: priv->fahrenheit = true; sndbg("Fahrenheit\n"); break; /* Report Samples in Centigrade */ case SNIOC_CENTIGRADE: priv->fahrenheit = false; sndbg("Centigrade\n"); break; /* Read THYS temperature register. Arg: b16_t* pointer */ case SNIOC_READTHYS: { FAR b16_t *ptr = (FAR b16_t *)((uintptr_t)arg); DEBUGASSERT(ptr != NULL); ret = lm75_readb16(priv, LM75_THYS_REG, ptr); sndbg("THYS: %08x ret: %d\n", *ptr, ret); } break; /* Write THYS temperature register. Arg: b16_t value */ case SNIOC_WRITETHYS: ret = lm75_writeb16(priv, LM75_THYS_REG, (b16_t)arg); sndbg("THYS: %08x ret: %d\n", (b16_t)arg, ret); break; /* Read TOS (Over-temp Shutdown Threshold) Register. Arg: b16_t* pointer */ case SNIOC_READTOS: { FAR b16_t *ptr = (FAR b16_t *)((uintptr_t)arg); DEBUGASSERT(ptr != NULL); ret = lm75_readb16(priv, LM75_TOS_REG, ptr); sndbg("TOS: %08x ret: %d\n", *ptr, ret); } break; /* Write TOS (Over-temp Shutdown Threshold) Register. Arg: b16_t value */ case SNIOC_WRITETOS: ret = lm75_writeb16(priv, LM75_TOS_REG, (b16_t)arg); sndbg("TOS: %08x ret: %d\n", (b16_t)arg, ret); break; default: sndbg("Unrecognized cmd: %d\n", cmd); ret = -ENOTTY; break; } return ret; }
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) { sndbg("Buffer is null\n"); return -EINVAL; } if (buflen != 2) { sndbg("You can't read something other than 16 bits (2 bytes)\n"); return -EINVAL; } /* Enable MAX6675's chip select */ SPI_SELECT(priv->spi, SPIDEV_TEMPERATURE, true); /* Read temperature */ SPI_RECVBLOCK(priv->spi, ®msb, 2); /* Disable MAX6675's chip select */ SPI_SELECT(priv->spi, SPIDEV_TEMPERATURE, false); regval = (regmsb & 0xFF00) >> 8; regval |= (regmsb & 0xFF) << 8; sndbg("Read from MAX6675 = 0x%04X\n", regval); /* Verify if the device ID bit is really zero */ if (regval & MAX6675_DEV_ID) { sndbg("ERROR: The Device ID bit needs to be 0 !\n"); ret = -EINVAL; } /* Detect if termocople input is open */ if (regval & MAX6675_OPEN_CIRCUIT) { sndbg("The thermocouple input is not connected!\n"); ret = -EINVAL; } /* 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; }