Skip to content

Commit 5b1a524

Browse files
hwilmersnashif
authored andcommitted
sensor: tmp116: Add support for tmp117, fix calculation issues
- Added support for TMP117 in existing driver for TMP116 The Texas Instruments TMP117 is a higher precision upgrade from TMP116. It shares most functionality, but has a differing device ID. This patch will run with the hardware IDs of both devices. - Fixed an int promote issue in tmp116_channel_get Negative temperature values in drv_data->sample were not processed correctly. The error occured during integer promotion from u16_t to s32_t in this code line: tmp = (s32_t)drv_data->sample * TMP116_RESOLUTION; By first promoting to s16_t, the correct result is obtained: tmp = (s16_t)drv_data->sample * (s32_t)TMP116_RESOLUTION; - Made temperature resolution compatible to sensor API The fractional part of the temperature was returned as a multiple of 10^-7 deg.Celsius. This differs from the resolution sugegsted by the sensor API, which is 10^-6. The driver is now returning temperature readings with a resolution of 10^-6 deg. Celsius. - The changed driver was tested using following hardware: TMP117 attached to disco_l475_iot1 via i2c1 Signed-off-by: Hans Wilmers <[email protected]>
1 parent c9d7840 commit 5b1a524

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

drivers/sensor/tmp116/tmp116.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static inline int tmp116_device_id_check(struct device *dev)
5151
return -EIO;
5252
}
5353

54-
if (value != TMP116_DEVICE_ID) {
54+
if ((value != TMP116_DEVICE_ID) && (value != TMP117_DEVICE_ID)) {
5555
LOG_ERR("%s: Failed to match the device IDs!",
5656
DT_INST_0_TI_TMP116_LABEL);
5757
return -EINVAL;
@@ -100,9 +100,9 @@ static int tmp116_channel_get(struct device *dev, enum sensor_channel chan,
100100
* See datasheet "Temperature Results and Limits" section for more
101101
* details on processing sample data.
102102
*/
103-
tmp = (s32_t)drv_data->sample * TMP116_RESOLUTION;
104-
val->val1 = tmp / 10000000; /* Tens of uCelsius */
105-
val->val2 = tmp % 10000000;
103+
tmp = ((s16_t)drv_data->sample * (s32_t)TMP116_RESOLUTION) / 10;
104+
val->val1 = tmp / 1000000; /* uCelsius */
105+
val->val2 = tmp % 1000000;
106106

107107
return 0;
108108
}

drivers/sensor/tmp116/tmp116.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#define TMP116_RESOLUTION_DIV 10000000
2323

2424
#define TMP116_DEVICE_ID 0x1116
25+
#define TMP117_DEVICE_ID 0x0117
2526

2627
struct tmp116_data {
2728
struct device *i2c;

0 commit comments

Comments
 (0)