This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Temperature Sensot TMP275 initailisation with TivaWare_C_Series

Other Parts Discussed in Thread: TMP275, TMP102, TMP421, TMP401, TMP100

Hi,

I am  working on TMP275 sensor with CCS version 6.0 and TivaWare_C_Series-2.1.0.12573.

I am new in sensor interface.

I found given problem while initialization.

1. is there any tmp275.c,tmp275.h and hw_tmp275.h file are available?

2. i am trying to write c file for tmp275 but my problem start for hw_tmp275, how i create hw_tmp275 file to add in my project.

3. Also need some guidance on ADXL345 sensor.

right now i am working with SMT21 and MPU9150 sensors but i want to replace this sensor with tmp275 and ADCL345.

Can you please provide any guidance to solve this? 

Regards

Mayur

 

  • Hi Mayur,

    Thanks for using TI E2E forums. I am not aware of such programs maintained by TI but there are always source code examples that you can find online, for example you can try the Kernel.org website.  I don’t think I have seen an example code for TMP275  but I it would be much different from the rest of our temp sensors(except for a few modifications). You can find a working C code of TMP102,TMP401,TMP421 drivers in the Linux Kernel. You can always copy this and modify it to suit TMP275.

    • Go to kernel.org
    • Link is as follows: Your file location\linux-3.16.1.tar\linux-3.16.1\drivers\hwmon\tmp102.c or tmp401.c or tmp421.c

    I hope this help!

    For TIVAWare question please post on the TIVA E2E forums https://e2e.ti.com/support/microcontrollers/tiva_arm/. The other questions are not for TI parts, we can't provide support in that case. 

    Regards,

  • thanks for reply,Mayrim Verdejo

    After compile the the file i found, there are differences in file. for example i used SHT21. c file from given path :ti\TivaWare_C_Series-2.1.0.12573\sensorlib

    while  compare this file with linux-3.18.17.tar\drivers\hwmon\SHT21.c it gives me lots of differences.How i proceed with TMP275 sensor?.

    I am working on windows platform.

    Please help me to solve the problem

    Regards

    Mayur

    from  TivaWare_C_Series-2.1.0.12573\sensorlib

    /* Sensirion SHT21 humidity and temperature sensor driver
     *
     * Copyright (C) 2010 Urs Fleisch <urs.fleisch@sensirion.com>
     *
     * This program is free software; you can redistribute it and/or modify
     * it under the terms of the GNU General Public License as published by
     * the Free Software Foundation; either version 2 of the License, or
     * (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program; if not, write to the Free Software
     * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA
     *
     * Data sheet available (5/2010) at
     * http://www.sensirion.com/en/pdf/product_information/Datasheet-humidity-sensor-SHT21.pdf
     */
    
    #include <linux/module.h>
    #include <linux/init.h>
    #include <linux/slab.h>
    #include <linux/i2c.h>
    #include <linux/hwmon.h>
    #include <linux/hwmon-sysfs.h>
    #include <linux/err.h>
    #include <linux/mutex.h>
    #include <linux/device.h>
    #include <linux/jiffies.h>
    
    /* I2C command bytes */
    #define SHT21_TRIG_T_MEASUREMENT_HM  0xe3
    #define SHT21_TRIG_RH_MEASUREMENT_HM 0xe5
    
    /**
     * struct sht21 - SHT21 device specific data
     * @hwmon_dev: device registered with hwmon
     * @lock: mutex to protect measurement values
     * @valid: only 0 before first measurement is taken
     * @last_update: time of last update (jiffies)
     * @temperature: cached temperature measurement value
     * @humidity: cached humidity measurement value
     */
    struct sht21 {
    	struct i2c_client *client;
    	struct mutex lock;
    	char valid;
    	unsigned long last_update;
    	int temperature;
    	int humidity;
    };
    
    /**
     * sht21_temp_ticks_to_millicelsius() - convert raw temperature ticks to
     * milli celsius
     * @ticks: temperature ticks value received from sensor
     */
    static inline int sht21_temp_ticks_to_millicelsius(int ticks)
    {
    	ticks &= ~0x0003; /* clear status bits */
    	/*
    	 * Formula T = -46.85 + 175.72 * ST / 2^16 from data sheet 6.2,
    	 * optimized for integer fixed point (3 digits) arithmetic
    	 */
    	return ((21965 * ticks) >> 13) - 46850;
    }
    
    /**
     * sht21_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to
     * one-thousandths of a percent relative humidity
     * @ticks: humidity ticks value received from sensor
     */
    static inline int sht21_rh_ticks_to_per_cent_mille(int ticks)
    {
    	ticks &= ~0x0003; /* clear status bits */
    	/*
    	 * Formula RH = -6 + 125 * SRH / 2^16 from data sheet 6.1,
    	 * optimized for integer fixed point (3 digits) arithmetic
    	 */
    	return ((15625 * ticks) >> 13) - 6000;
    }
    
    /**
     * sht21_update_measurements() - get updated measurements from device
     * @dev: device
     *
     * Returns 0 on success, else negative errno.
     */
    static int sht21_update_measurements(struct device *dev)
    {
    	int ret = 0;
    	struct sht21 *sht21 = dev_get_drvdata(dev);
    	struct i2c_client *client = sht21->client;
    
    	mutex_lock(&sht21->lock);
    	/*
    	 * Data sheet 2.4:
    	 * SHT2x should not be active for more than 10% of the time - e.g.
    	 * maximum two measurements per second at 12bit accuracy shall be made.
    	 */
    	if (time_after(jiffies, sht21->last_update + HZ / 2) || !sht21->valid) {
    		ret = i2c_smbus_read_word_swapped(client,
    						  SHT21_TRIG_T_MEASUREMENT_HM);
    		if (ret < 0)
    			goto out;
    		sht21->temperature = sht21_temp_ticks_to_millicelsius(ret);
    		ret = i2c_smbus_read_word_swapped(client,
    						  SHT21_TRIG_RH_MEASUREMENT_HM);
    		if (ret < 0)
    			goto out;
    		sht21->humidity = sht21_rh_ticks_to_per_cent_mille(ret);
    		sht21->last_update = jiffies;
    		sht21->valid = 1;
    	}
    out:
    	mutex_unlock(&sht21->lock);
    
    	return ret >= 0 ? 0 : ret;
    }
    
    /**
     * sht21_show_temperature() - show temperature measurement value in sysfs
     * @dev: device
     * @attr: device attribute
     * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
     *
     * Will be called on read access to temp1_input sysfs attribute.
     * Returns number of bytes written into buffer, negative errno on error.
     */
    static ssize_t sht21_show_temperature(struct device *dev,
    	struct device_attribute *attr,
    	char *buf)
    {
    	struct sht21 *sht21 = dev_get_drvdata(dev);
    	int ret;
    
    	ret = sht21_update_measurements(dev);
    	if (ret < 0)
    		return ret;
    	return sprintf(buf, "%d\n", sht21->temperature);
    }
    
    /**
     * sht21_show_humidity() - show humidity measurement value in sysfs
     * @dev: device
     * @attr: device attribute
     * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
     *
     * Will be called on read access to humidity1_input sysfs attribute.
     * Returns number of bytes written into buffer, negative errno on error.
     */
    static ssize_t sht21_show_humidity(struct device *dev,
    	struct device_attribute *attr,
    	char *buf)
    {
    	struct sht21 *sht21 = dev_get_drvdata(dev);
    	int ret;
    
    	ret = sht21_update_measurements(dev);
    	if (ret < 0)
    		return ret;
    	return sprintf(buf, "%d\n", sht21->humidity);
    }
    
    /* sysfs attributes */
    static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, sht21_show_temperature,
    	NULL, 0);
    static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, sht21_show_humidity,
    	NULL, 0);
    
    static struct attribute *sht21_attrs[] = {
    	&sensor_dev_attr_temp1_input.dev_attr.attr,
    	&sensor_dev_attr_humidity1_input.dev_attr.attr,
    	NULL
    };
    
    ATTRIBUTE_GROUPS(sht21);
    
    static int sht21_probe(struct i2c_client *client,
    	const struct i2c_device_id *id)
    {
    	struct device *dev = &client->dev;
    	struct device *hwmon_dev;
    	struct sht21 *sht21;
    
    	if (!i2c_check_functionality(client->adapter,
    				     I2C_FUNC_SMBUS_WORD_DATA)) {
    		dev_err(&client->dev,
    			"adapter does not support SMBus word transactions\n");
    		return -ENODEV;
    	}
    
    	sht21 = devm_kzalloc(dev, sizeof(*sht21), GFP_KERNEL);
    	if (!sht21)
    		return -ENOMEM;
    
    	sht21->client = client;
    
    	mutex_init(&sht21->lock);
    
    	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
    							   sht21, sht21_groups);
    	return PTR_ERR_OR_ZERO(hwmon_dev);
    }
    
    /* Device ID table */
    static const struct i2c_device_id sht21_id[] = {
    	{ "sht21", 0 },
    	{ }
    };
    MODULE_DEVICE_TABLE(i2c, sht21_id);
    
    static struct i2c_driver sht21_driver = {
    	.driver.name = "sht21",
    	.probe       = sht21_probe,
    	.id_table    = sht21_id,
    };
    
    module_i2c_driver(sht21_driver);
    
    MODULE_AUTHOR("Urs Fleisch <urs.fleisch@sensirion.com>");
    MODULE_DESCRIPTION("Sensirion SHT21 humidity and temperature sensor driver");
    MODULE_LICENSE("GPL");
    

    from linux-3.18.17\drivers\hwmon 

    /* Sensirion SHT21 humidity and temperature sensor driver
     *
     * Copyright (C) 2010 Urs Fleisch <urs.fleisch@sensirion.com>
     *
     * This program is free software; you can redistribute it and/or modify
     * it under the terms of the GNU General Public License as published by
     * the Free Software Foundation; either version 2 of the License, or
     * (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program; if not, write to the Free Software
     * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA
     *
     * Data sheet available (5/2010) at
     * http://www.sensirion.com/en/pdf/product_information/Datasheet-humidity-sensor-SHT21.pdf
     */
    
    #include <linux/module.h>
    #include <linux/init.h>
    #include <linux/slab.h>
    #include <linux/i2c.h>
    #include <linux/hwmon.h>
    #include <linux/hwmon-sysfs.h>
    #include <linux/err.h>
    #include <linux/mutex.h>
    #include <linux/device.h>
    #include <linux/jiffies.h>
    
    /* I2C command bytes */
    #define SHT21_TRIG_T_MEASUREMENT_HM  0xe3
    #define SHT21_TRIG_RH_MEASUREMENT_HM 0xe5
    
    /**
     * struct sht21 - SHT21 device specific data
     * @hwmon_dev: device registered with hwmon
     * @lock: mutex to protect measurement values
     * @valid: only 0 before first measurement is taken
     * @last_update: time of last update (jiffies)
     * @temperature: cached temperature measurement value
     * @humidity: cached humidity measurement value
     */
    struct sht21 {
    	struct i2c_client *client;
    	struct mutex lock;
    	char valid;
    	unsigned long last_update;
    	int temperature;
    	int humidity;
    };
    
    /**
     * sht21_temp_ticks_to_millicelsius() - convert raw temperature ticks to
     * milli celsius
     * @ticks: temperature ticks value received from sensor
     */
    static inline int sht21_temp_ticks_to_millicelsius(int ticks)
    {
    	ticks &= ~0x0003; /* clear status bits */
    	/*
    	 * Formula T = -46.85 + 175.72 * ST / 2^16 from data sheet 6.2,
    	 * optimized for integer fixed point (3 digits) arithmetic
    	 */
    	return ((21965 * ticks) >> 13) - 46850;
    }
    
    /**
     * sht21_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to
     * one-thousandths of a percent relative humidity
     * @ticks: humidity ticks value received from sensor
     */
    static inline int sht21_rh_ticks_to_per_cent_mille(int ticks)
    {
    	ticks &= ~0x0003; /* clear status bits */
    	/*
    	 * Formula RH = -6 + 125 * SRH / 2^16 from data sheet 6.1,
    	 * optimized for integer fixed point (3 digits) arithmetic
    	 */
    	return ((15625 * ticks) >> 13) - 6000;
    }
    
    /**
     * sht21_update_measurements() - get updated measurements from device
     * @dev: device
     *
     * Returns 0 on success, else negative errno.
     */
    static int sht21_update_measurements(struct device *dev)
    {
    	int ret = 0;
    	struct sht21 *sht21 = dev_get_drvdata(dev);
    	struct i2c_client *client = sht21->client;
    
    	mutex_lock(&sht21->lock);
    	/*
    	 * Data sheet 2.4:
    	 * SHT2x should not be active for more than 10% of the time - e.g.
    	 * maximum two measurements per second at 12bit accuracy shall be made.
    	 */
    	if (time_after(jiffies, sht21->last_update + HZ / 2) || !sht21->valid) {
    		ret = i2c_smbus_read_word_swapped(client,
    						  SHT21_TRIG_T_MEASUREMENT_HM);
    		if (ret < 0)
    			goto out;
    		sht21->temperature = sht21_temp_ticks_to_millicelsius(ret);
    		ret = i2c_smbus_read_word_swapped(client,
    						  SHT21_TRIG_RH_MEASUREMENT_HM);
    		if (ret < 0)
    			goto out;
    		sht21->humidity = sht21_rh_ticks_to_per_cent_mille(ret);
    		sht21->last_update = jiffies;
    		sht21->valid = 1;
    	}
    out:
    	mutex_unlock(&sht21->lock);
    
    	return ret >= 0 ? 0 : ret;
    }
    
    /**
     * sht21_show_temperature() - show temperature measurement value in sysfs
     * @dev: device
     * @attr: device attribute
     * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
     *
     * Will be called on read access to temp1_input sysfs attribute.
     * Returns number of bytes written into buffer, negative errno on error.
     */
    static ssize_t sht21_show_temperature(struct device *dev,
    	struct device_attribute *attr,
    	char *buf)
    {
    	struct sht21 *sht21 = dev_get_drvdata(dev);
    	int ret;
    
    	ret = sht21_update_measurements(dev);
    	if (ret < 0)
    		return ret;
    	return sprintf(buf, "%d\n", sht21->temperature);
    }
    
    /**
     * sht21_show_humidity() - show humidity measurement value in sysfs
     * @dev: device
     * @attr: device attribute
     * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
     *
     * Will be called on read access to humidity1_input sysfs attribute.
     * Returns number of bytes written into buffer, negative errno on error.
     */
    static ssize_t sht21_show_humidity(struct device *dev,
    	struct device_attribute *attr,
    	char *buf)
    {
    	struct sht21 *sht21 = dev_get_drvdata(dev);
    	int ret;
    
    	ret = sht21_update_measurements(dev);
    	if (ret < 0)
    		return ret;
    	return sprintf(buf, "%d\n", sht21->humidity);
    }
    
    /* sysfs attributes */
    static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, sht21_show_temperature,
    	NULL, 0);
    static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, sht21_show_humidity,
    	NULL, 0);
    
    static struct attribute *sht21_attrs[] = {
    	&sensor_dev_attr_temp1_input.dev_attr.attr,
    	&sensor_dev_attr_humidity1_input.dev_attr.attr,
    	NULL
    };
    
    ATTRIBUTE_GROUPS(sht21);
    
    static int sht21_probe(struct i2c_client *client,
    	const struct i2c_device_id *id)
    {
    	struct device *dev = &client->dev;
    	struct device *hwmon_dev;
    	struct sht21 *sht21;
    
    	if (!i2c_check_functionality(client->adapter,
    				     I2C_FUNC_SMBUS_WORD_DATA)) {
    		dev_err(&client->dev,
    			"adapter does not support SMBus word transactions\n");
    		return -ENODEV;
    	}
    
    	sht21 = devm_kzalloc(dev, sizeof(*sht21), GFP_KERNEL);
    	if (!sht21)
    		return -ENOMEM;
    
    	sht21->client = client;
    
    	mutex_init(&sht21->lock);
    
    	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
    							   sht21, sht21_groups);
    	return PTR_ERR_OR_ZERO(hwmon_dev);
    }
    
    /* Device ID table */
    static const struct i2c_device_id sht21_id[] = {
    	{ "sht21", 0 },
    	{ }
    };
    MODULE_DEVICE_TABLE(i2c, sht21_id);
    
    static struct i2c_driver sht21_driver = {
    	.driver.name = "sht21",
    	.probe       = sht21_probe,
    	.id_table    = sht21_id,
    };
    
    module_i2c_driver(sht21_driver);
    
    MODULE_AUTHOR("Urs Fleisch <urs.fleisch@sensirion.com>");
    MODULE_DESCRIPTION("Sensirion SHT21 humidity and temperature sensor driver");
    MODULE_LICENSE("GPL");
    

  • Hi Mayur,

    Unfortunately I do not support any of those libraries, neither Tiva devices. I am happy to help you if you have any question related to TMP275 functionality.

    For programming questions please use the Tiva forum.
  • Hi,

    I create library for TMP275 with TMP100 as referance.

    i found problem while reading the data :

    code is as below for tmp275 sensor


    #define TMP275_I2C_ADDRESS 0x90
    float ftemper;
    tI2CMInstance g_sI2CInst4;
    tTMP275 g_sTMP275Inst


    Initialization:

    I2CMInit(&g_sI2CInst4, I2C7_BASE, INT_I2C7, 0xff, 0xff, g_ui32SysClock);
    TMP275Init(&g_sTMP275Inst, &g_sI2CInst4, TMP275_I2C_ADDRESS,
    ISL29023AppCallback, &g_sTMP275Inst);
    ISL29023AppI2CWait(__FILE__, __LINE__);
    ROM_SysCtlDelay(g_ui32SysClock / (30 * 3));

    Reading Data:

    SysCtlDelay(g_ui32SysClock/ (30 * 3));
    TMP275DataRead(&g_sTMP275Inst, ISL29023AppCallback, &g_sTMP275Inst);
    ISL29023AppI2CWait(__FILE__, __LINE__);
    ROM_SysCtlDelay(g_ui32SysClock / (30 * 3));
    TMP275DataTemperatureGetFloat(&g_sTMP275Inst, &ftemper);

    after running the code i get ftemper value as 1.000

    Please help me to solve this...


    Regards
    Mayur
  • Mayur,

     

    There could be a number of reasons you are not reading back the expected values, it could be hardware or software or both. It is impossible to say the exact cause by staring at the code, at least for me.

     

    I would have to capture and look at the data bus with a scope and see if the wave form is as expected. And take appropriate actions from there.

     

    Regard, Guang

    Apps-Sensing Products

  • hi Guang,

    Apart from the hardware can you please tell me, initialization of TMP275 code is correct or not.. 

    Or it need to improve?

    In hardware, how I trace the communication except scope.

    Regards

    Mayur

  • Mayur,

     

    With a scope, you’ll be able to find out a lot about the system. Sometimes you may be even able to tell whether it is a HW or a SW problem. You’ll look at the I2C bus waveforms for clues. To answer your question, I don’t know of an alternative to a scope. 

     

    I’ve never used the code, therefore I can’t speak to it. Again with the help of a scope, it will be relatively easier to figure out. Meanwhile if you want to concentrate on the SW, you can do some step by step debug, make sure the right information is passed to the READ command.

     

    Hope this helps.

     

    Regard, Guang

    Apps-Sensing Products

  • Thanks for reply Guang,

    I got the solution. 

    i assigned address 0X48 instate of 0X90 and it worked..