1 /*
2 * Copyright (c) 2019 Centaur Analytics, Inc
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/device.h>
9 #include <zephyr/drivers/sensor.h>
10 #include <zephyr/drivers/eeprom.h>
11 #include <zephyr/drivers/sensor/tmp116.h>
12 #include <zephyr/sys/printk.h>
13 #include <zephyr/sys/__assert.h>
14
15 #define TMP116_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(ti_tmp116)
16 #define TMP116_EEPROM_NODE DT_CHILD(TMP116_NODE, ti_tmp116_eeprom_0)
17
18 static uint8_t eeprom_content[EEPROM_TMP116_SIZE];
19
main(void)20 int main(void)
21 {
22 const struct device *const dev = DEVICE_DT_GET(TMP116_NODE);
23 const struct device *const eeprom = DEVICE_DT_GET(TMP116_EEPROM_NODE);
24 struct sensor_value temp_value;
25
26 /* offset to be added to the temperature
27 * only supported by TMP117
28 */
29 struct sensor_value offset_value;
30 int ret;
31
32 __ASSERT(device_is_ready(dev), "TMP116 device not ready");
33 __ASSERT(device_is_ready(eeprom), "TMP116 eeprom device not ready");
34
35 printk("Device %s - %p is ready\n", dev->name, dev);
36
37 ret = eeprom_read(eeprom, 0, eeprom_content, sizeof(eeprom_content));
38 if (ret == 0) {
39 printk("eeprom content %02x%02x%02x%02x%02x%02x%02x%02x\n",
40 eeprom_content[0], eeprom_content[1],
41 eeprom_content[2], eeprom_content[3],
42 eeprom_content[4], eeprom_content[5],
43 eeprom_content[6], eeprom_content[7]);
44 } else {
45 printk("Failed to get eeprom content\n");
46 }
47
48 /*
49 * if an offset of 2.5 oC is to be added,
50 * set val1 = 2 and val2 = 500000.
51 * See struct sensor_value documentation for more details.
52 */
53 offset_value.val1 = 0;
54 offset_value.val2 = 0;
55 ret = sensor_attr_set(dev, SENSOR_CHAN_AMBIENT_TEMP,
56 SENSOR_ATTR_OFFSET, &offset_value);
57 if (ret) {
58 printk("sensor_attr_set failed ret = %d\n", ret);
59 printk("SENSOR_ATTR_OFFSET is only supported by TMP117\n");
60 }
61 while (1) {
62 ret = sensor_sample_fetch(dev);
63 if (ret) {
64 printk("Failed to fetch measurements (%d)\n", ret);
65 return 0;
66 }
67
68 ret = sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP,
69 &temp_value);
70 if (ret) {
71 printk("Failed to get measurements (%d)\n", ret);
72 return 0;
73 }
74
75 printk("temp is %d.%d oC\n", temp_value.val1, temp_value.val2);
76
77 k_sleep(K_MSEC(1000));
78 }
79 return 0;
80 }
81