1 /*
2 * Copyright (c) 2024 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/drivers/sensor.h>
8
9 #define TEMP_CHANNEL {SENSOR_CHAN_AMBIENT_TEMP, 0}
10
11 const struct device *const temp0 = DEVICE_DT_GET(DT_ALIAS(temp0));
12
13 SENSOR_DT_READ_IODEV(temp_iodev, DT_ALIAS(temp0), {TEMP_CHANNEL});
14 RTIO_DEFINE(temp_ctx, 1, 1);
15
main(void)16 int main(void)
17 {
18 int rc;
19 uint8_t buf[8];
20 uint32_t temp_frame_iter = 0;
21 struct sensor_q31_data temp_data = {0};
22 struct sensor_decode_context temp_decoder = SENSOR_DECODE_CONTEXT_INIT(
23 SENSOR_DECODER_DT_GET(DT_ALIAS(temp0)), buf, SENSOR_CHAN_AMBIENT_TEMP, 0);
24
25 while (1) {
26 /* Blocking read */
27 rc = sensor_read(temp_iodev, &temp_ctx, buf, sizeof(buf));
28
29 if (rc != 0) {
30 printk("sensor_read() failed %d\n", rc);
31 }
32
33 /* Decode the data into a single q31 */
34 sensor_decode(&temp_decoder, &temp_data, 1);
35
36 printk("Temperature " PRIsensor_q31_data "\n",
37 PRIsensor_q31_data_arg(temp_data, 0));
38
39 k_msleep(1);
40 }
41 }
42