22
22
23
23
LOG_MODULE_REGISTER (STTS22H , CONFIG_SENSOR_LOG_LEVEL );
24
24
25
+ #define ST22H_RANGE_LOWEST_TEMP -40
26
+ #define ST22H_RANGE_HIGHEST_TEMP 127
27
+
25
28
static inline int stts22h_set_odr_raw (const struct device * dev , stts22h_odr_temp_t odr )
26
29
{
27
30
const struct stts22h_config * cfg = dev -> config ;
@@ -105,6 +108,26 @@ static int stts22h_odr_set(const struct device *dev,
105
108
return - EINVAL ;
106
109
}
107
110
}
111
+ #if CONFIG_STTS22H_TRIGGER
112
+ static inline uint32_t stts22h_val_to_threshold (const struct sensor_value * val )
113
+ {
114
+ if (val -> val1 < ST22H_RANGE_LOWEST_TEMP || val -> val1 > ST22H_RANGE_HIGHEST_TEMP ) {
115
+ LOG_ERR ("Invalid value: %d" , val -> val1 );
116
+ return UINT32_MAX ;
117
+ }
118
+ /* The conversion formula is: (degC / 0.64) + 63, we multiply by 100*/
119
+ int32_t temperature_degree_times100 = val -> val1 * 100 + val -> val2 / 10000 ;
120
+ uint32_t raw_value = (temperature_degree_times100 / 64 ) + 63 ;
121
+
122
+ if (raw_value > 0xFF ) {
123
+ LOG_ERR ("Invalid value: %d" , raw_value );
124
+ return UINT32_MAX ;
125
+ }
126
+
127
+ return raw_value ;
128
+ }
129
+ #endif
130
+
108
131
109
132
static int stts22h_attr_set (const struct device * dev ,
110
133
enum sensor_channel chan ,
@@ -115,10 +138,41 @@ static int stts22h_attr_set(const struct device *dev,
115
138
LOG_ERR ("Invalid channel: %d" , chan );
116
139
return - ENOTSUP ;
117
140
}
118
-
119
141
switch (attr ) {
120
142
case SENSOR_ATTR_SAMPLING_FREQUENCY :
121
143
return stts22h_odr_set (dev , val );
144
+ #if CONFIG_STTS22H_TRIGGER
145
+ case SENSOR_ATTR_UPPER_THRESH : {
146
+ const struct stts22h_config * cfg = dev -> config ;
147
+ stmdev_ctx_t * ctx = (stmdev_ctx_t * )& cfg -> ctx ;
148
+
149
+ uint32_t raw_value = stts22h_val_to_threshold (val );
150
+
151
+ if (raw_value == UINT32_MAX ) {
152
+ return - EINVAL ;
153
+ }
154
+ if (stts22h_temp_trshld_high_set (ctx , (uint8_t )raw_value ) < 0 ) {
155
+ LOG_DBG ("Could not set high threshold" );
156
+ return - EIO ;
157
+ }
158
+ return 0 ;
159
+ }
160
+ case SENSOR_ATTR_LOWER_THRESH : {
161
+ const struct stts22h_config * cfg = dev -> config ;
162
+ stmdev_ctx_t * ctx = (stmdev_ctx_t * )& cfg -> ctx ;
163
+
164
+ uint32_t raw_value = stts22h_val_to_threshold (val );
165
+
166
+ if (raw_value == UINT32_MAX ) {
167
+ return - EINVAL ;
168
+ }
169
+ if (stts22h_temp_trshld_low_set (ctx , (uint8_t )raw_value ) < 0 ) {
170
+ LOG_DBG ("Could not set low threshold " );
171
+ return - EIO ;
172
+ }
173
+ return 0 ;
174
+ }
175
+ #endif
122
176
default :
123
177
LOG_ERR ("Attribute %d not supported." , attr );
124
178
return - ENOTSUP ;
0 commit comments