Skip to content

Commit 5379f4a

Browse files
WangHanChikartben
authored andcommitted
drivers: sensor: tmp1075: add support for fractional threshold values
Previously, the TMP1075 driver only used the integer part (val1) of the sensor_value when setting TLOW and THIGH thresholds. This limited the precision of temperature threshold configuration and could be insufficient in applications requiring fine-grained control. This patch adds proper handling for the fractional part (val2) by encoding it into bits [7:4] of the 12-bit temperature register according to the TMP1075 datasheet. The decoding logic in get_threshold_attribute() is also updated to recover the fractional value accurately. Signed-off-by: Hank Wang <wanghanchi2000@gmail.com>
1 parent 6636d65 commit 5379f4a

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

drivers/sensor/ti/tmp1075/tmp1075.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,22 @@ static int tmp1075_attr_set(const struct device *dev, enum sensor_channel chan,
6767

6868
switch (attr) {
6969
#if CONFIG_TMP1075_ALERT_INTERRUPTS
70+
int integer, frac;
71+
7072
case SENSOR_ATTR_LOWER_THRESH:
71-
return set_threshold_attribute(dev, TMP1075_REG_TLOW, val->val1 << 8,
73+
/* Extract integer and fractional parts from TMP1075 12-bit register value */
74+
integer = (val->val1 << TMP1075_DATA_INTE_SHIFT) & TMP1075_DATA_INTE_MASK;
75+
frac = ((val->val2 / TMP1075_TEMP_SCALE) << TMP1075_DATA_FRAC_SHIFT) &
76+
TMP1075_DATA_FRAC_MASK;
77+
return set_threshold_attribute(dev, TMP1075_REG_TLOW, integer | frac,
7278
"SENSOR_ATTR_LOWER_THRESH");
7379

7480
case SENSOR_ATTR_UPPER_THRESH:
75-
return set_threshold_attribute(dev, TMP1075_REG_THIGH, val->val1 << 8,
81+
/* Extract integer and fractional parts from TMP1075 12-bit register value */
82+
integer = (val->val1 << TMP1075_DATA_INTE_SHIFT) & TMP1075_DATA_INTE_MASK;
83+
frac = ((val->val2 / TMP1075_TEMP_SCALE) << TMP1075_DATA_FRAC_SHIFT) &
84+
TMP1075_DATA_FRAC_MASK;
85+
return set_threshold_attribute(dev, TMP1075_REG_THIGH, integer | frac,
7686
"SENSOR_ATTR_UPPER_THRESH");
7787
#endif
7888

@@ -91,7 +101,9 @@ static int get_threshold_attribute(const struct device *dev, uint8_t reg, struct
91101
LOG_ERR("Failed to get %s attribute!", error_msg);
92102
return -EIO;
93103
}
94-
val->val1 = value >> 8;
104+
val->val1 = (value & TMP1075_DATA_INTE_MASK) >> TMP1075_DATA_INTE_SHIFT;
105+
val->val2 =
106+
((value & TMP1075_DATA_FRAC_MASK) >> TMP1075_DATA_FRAC_SHIFT) * TMP1075_TEMP_SCALE;
95107
return 0;
96108
}
97109
#endif

drivers/sensor/ti/tmp1075/tmp1075.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
/* Extended resolution is not supported on TMP1075 */
1616
#define TMP1075_DATA_NORMAL_SHIFT 4
17+
#define TMP1075_DATA_FRAC_SHIFT 4
18+
#define TMP1075_DATA_FRAC_MASK 0x00F0
19+
#define TMP1075_DATA_INTE_SHIFT 8
20+
#define TMP1075_DATA_INTE_MASK 0xFF00
1721
#define uCELSIUS_IN_CELSIUS 1000000
1822

1923
#define TMP1075_REG_TEMPERATURE 0x00

0 commit comments

Comments
 (0)