@@ -58,6 +58,29 @@ static int tmp112_update_config(const struct device *dev, uint16_t mask, uint16_
58
58
return rc ;
59
59
}
60
60
61
+ static int tmp112_set_threshold (const struct device * dev , uint8_t reg , int64_t micro_c )
62
+ {
63
+ struct tmp112_data * drv_data = dev -> data ;
64
+ int64_t v ;
65
+ uint16_t reg_value ;
66
+
67
+ v = DIV_ROUND_CLOSEST (micro_c , TMP112_TEMP_SCALE );
68
+
69
+ if (drv_data -> config_reg & TMP112_CONFIG_EM ) {
70
+ if (!IN_RANGE (v , TMP112_TEMP_MIN_EM , TMP112_TEMP_MAX_EM )) {
71
+ return - EINVAL ;
72
+ }
73
+ reg_value = (uint16_t )v << TMP112_DATA_EXTENDED_SHIFT ;
74
+ } else {
75
+ if (!IN_RANGE (v , TMP112_TEMP_MIN , TMP112_TEMP_MAX )) {
76
+ return - EINVAL ;
77
+ }
78
+ reg_value = (uint16_t )v << TMP112_DATA_NORMAL_SHIFT ;
79
+ }
80
+
81
+ return tmp112_reg_write (dev -> config , reg , reg_value );
82
+ }
83
+
61
84
static int tmp112_attr_set (const struct device * dev , enum sensor_channel chan ,
62
85
enum sensor_attribute attr , const struct sensor_value * val )
63
86
{
@@ -87,8 +110,9 @@ static int tmp112_attr_set(const struct device *dev, enum sensor_channel chan,
87
110
}
88
111
break ;
89
112
#endif
90
- case SENSOR_ATTR_SAMPLING_FREQUENCY :
113
+
91
114
#if CONFIG_TMP112_SAMPLING_FREQUENCY_RUNTIME
115
+ case SENSOR_ATTR_SAMPLING_FREQUENCY :
92
116
/* conversion rate in mHz */
93
117
cr = val -> val1 * 1000 + val -> val2 / 1000 ;
94
118
@@ -123,6 +147,12 @@ static int tmp112_attr_set(const struct device *dev, enum sensor_channel chan,
123
147
break ;
124
148
#endif
125
149
150
+ case SENSOR_ATTR_LOWER_THRESH :
151
+ return tmp112_set_threshold (dev , TMP112_REG_TLOW , sensor_value_to_micro (val ));
152
+
153
+ case SENSOR_ATTR_UPPER_THRESH :
154
+ return tmp112_set_threshold (dev , TMP112_REG_THIGH , sensor_value_to_micro (val ));
155
+
126
156
default :
127
157
return - ENOTSUP ;
128
158
}
@@ -155,17 +185,12 @@ static int tmp112_channel_get(const struct device *dev, enum sensor_channel chan
155
185
struct sensor_value * val )
156
186
{
157
187
struct tmp112_data * drv_data = dev -> data ;
158
- int32_t uval ;
159
188
160
189
if (chan != SENSOR_CHAN_AMBIENT_TEMP ) {
161
190
return - ENOTSUP ;
162
191
}
163
192
164
- uval = (int32_t )drv_data -> sample * TMP112_TEMP_SCALE ;
165
- val -> val1 = uval / 1000000 ;
166
- val -> val2 = uval % 1000000 ;
167
-
168
- return 0 ;
193
+ return sensor_value_from_micro (val , (int32_t )drv_data -> sample * TMP112_TEMP_SCALE );
169
194
}
170
195
171
196
static DEVICE_API (sensor , tmp112_driver_api ) = {
@@ -178,6 +203,7 @@ int tmp112_init(const struct device *dev)
178
203
{
179
204
const struct tmp112_config * cfg = dev -> config ;
180
205
struct tmp112_data * data = dev -> data ;
206
+ int ret ;
181
207
182
208
if (!device_is_ready (cfg -> bus .bus )) {
183
209
LOG_ERR ("I2C dev %s not ready" , cfg -> bus .bus -> name );
@@ -187,14 +213,34 @@ int tmp112_init(const struct device *dev)
187
213
data -> config_reg = TMP112_CONV_RATE (cfg -> cr ) | TMP112_CONV_RES_MASK |
188
214
(cfg -> extended_mode ? TMP112_CONFIG_EM : 0 );
189
215
190
- return tmp112_update_config (dev , 0 , 0 );
216
+ ret = tmp112_update_config (dev , 0 , 0 );
217
+ if (ret ) {
218
+ LOG_ERR ("Failed to set configuration (%d)" , ret );
219
+ return ret ;
220
+ }
221
+
222
+ ret = tmp112_set_threshold (dev , TMP112_REG_TLOW , cfg -> t_low_micro_c );
223
+ if (ret ) {
224
+ LOG_ERR ("Failed to set tLow threshold (%d)" , ret );
225
+ return ret ;
226
+ }
227
+
228
+ ret = tmp112_set_threshold (dev , TMP112_REG_THIGH , cfg -> t_high_micro_c );
229
+ if (ret ) {
230
+ LOG_ERR ("Failed to set tHigh threshold (%d)" , ret );
231
+ return ret ;
232
+ }
233
+
234
+ return 0 ;
191
235
}
192
236
193
237
#define TMP112_INST (inst ) \
194
238
static struct tmp112_data tmp112_data_##inst; \
195
239
static const struct tmp112_config tmp112_config_##inst = { \
196
240
.bus = I2C_DT_SPEC_INST_GET(inst), \
197
241
.cr = DT_INST_ENUM_IDX(inst, conversion_rate), \
242
+ .t_low_micro_c = DT_INST_PROP(inst, t_low_micro_c), \
243
+ .t_high_micro_c = DT_INST_PROP(inst, t_high_micro_c), \
198
244
.extended_mode = DT_INST_PROP(inst, extended_mode), \
199
245
}; \
200
246
\
0 commit comments