| 
 | 1 | +/*  | 
 | 2 | + * Copyright (c) 2025 Charles Dias  | 
 | 3 | + *  | 
 | 4 | + * SPDX-License-Identifier: Apache-2.0  | 
 | 5 | + */  | 
 | 6 | + | 
 | 7 | +#include <zephyr/kernel.h>  | 
 | 8 | +#include <zephyr/sys/printk.h>  | 
 | 9 | + | 
 | 10 | +#include <zephyr/drivers/sensor.h>  | 
 | 11 | + | 
 | 12 | +#include <stdio.h>  | 
 | 13 | + | 
 | 14 | +static void stts22h_config(const struct device *stts22h)  | 
 | 15 | +{  | 
 | 16 | +	struct sensor_value odr_attr;  | 
 | 17 | + | 
 | 18 | +	/* Set STTS22H ODR to 100 Hz (or as close as supported) */  | 
 | 19 | +	odr_attr.val1 = 100;  | 
 | 20 | +	odr_attr.val2 = 0;  | 
 | 21 | +	if (sensor_attr_set(stts22h, SENSOR_CHAN_AMBIENT_TEMP,  | 
 | 22 | +			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {  | 
 | 23 | +		printk("Cannot set sampling frequency for STTS22H\n");  | 
 | 24 | +		return;  | 
 | 25 | +	}  | 
 | 26 | +}  | 
 | 27 | + | 
 | 28 | +int main(void)  | 
 | 29 | +{  | 
 | 30 | +	const struct device *const stts22h = DEVICE_DT_GET_ONE(st_stts22h);  | 
 | 31 | +	int ret;  | 
 | 32 | + | 
 | 33 | +	printf("Zephyr STTS22H sensor sample. Board: %s\n", CONFIG_BOARD);  | 
 | 34 | + | 
 | 35 | +	if (!device_is_ready(stts22h)) {  | 
 | 36 | +		printk("%s: device not ready.\n", stts22h->name);  | 
 | 37 | +		return 0;  | 
 | 38 | +	}  | 
 | 39 | + | 
 | 40 | +	stts22h_config(stts22h);  | 
 | 41 | + | 
 | 42 | +	while (1) {  | 
 | 43 | +		struct sensor_value temp;  | 
 | 44 | + | 
 | 45 | +		ret = sensor_sample_fetch(stts22h);  | 
 | 46 | +		if (ret < 0) {  | 
 | 47 | +			printk("STTS22H sample fetch error (%d)\n", ret);  | 
 | 48 | +			k_msleep(1000);  | 
 | 49 | +			continue;  | 
 | 50 | +		}  | 
 | 51 | + | 
 | 52 | +		ret = sensor_channel_get(stts22h, SENSOR_CHAN_AMBIENT_TEMP, &temp);  | 
 | 53 | +		if (ret < 0) {  | 
 | 54 | +			printk("STTS22H channel read error (%d)\n", ret);  | 
 | 55 | +			k_msleep(1000);  | 
 | 56 | +			continue;  | 
 | 57 | +		}  | 
 | 58 | + | 
 | 59 | +		printf("[%6lld ms] Temperature: %.1f C\n", k_uptime_get(),  | 
 | 60 | +			sensor_value_to_double(&temp));  | 
 | 61 | + | 
 | 62 | +		k_msleep(2000);  | 
 | 63 | +	}  | 
 | 64 | +}  | 
0 commit comments