Skip to content

Commit 713fc17

Browse files
tobiwan88kartben
authored andcommitted
drivers: stts22h: Add attr_lower/high_tresh
Add threshold attribute handling to set low and high temperature at runtime and validation for runtime changes Signed-off-by: Tobias Meyer <[email protected]>
1 parent b95d7e2 commit 713fc17

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

drivers/sensor/st/stts22h/stts22h.c

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
LOG_MODULE_REGISTER(STTS22H, CONFIG_SENSOR_LOG_LEVEL);
2424

25+
#define ST22H_RANGE_LOWEST_TEMP -40
26+
#define ST22H_RANGE_HIGHEST_TEMP 127
27+
2528
static inline int stts22h_set_odr_raw(const struct device *dev, stts22h_odr_temp_t odr)
2629
{
2730
const struct stts22h_config *cfg = dev->config;
@@ -105,6 +108,26 @@ static int stts22h_odr_set(const struct device *dev,
105108
return -EINVAL;
106109
}
107110
}
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+
108131

109132
static int stts22h_attr_set(const struct device *dev,
110133
enum sensor_channel chan,
@@ -115,10 +138,41 @@ static int stts22h_attr_set(const struct device *dev,
115138
LOG_ERR("Invalid channel: %d", chan);
116139
return -ENOTSUP;
117140
}
118-
119141
switch (attr) {
120142
case SENSOR_ATTR_SAMPLING_FREQUENCY:
121143
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
122176
default:
123177
LOG_ERR("Attribute %d not supported.", attr);
124178
return -ENOTSUP;

0 commit comments

Comments
 (0)