1 /*
2 * Copyright (c) 2021 Jimmy Johnson <catch22@fastmail.net>
3 * Copyright (c) 2025 Byteflies NV
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <stdio.h>
9
10 #include <zephyr/kernel.h>
11 #include <zephyr/drivers/sensor.h>
12 #include <zephyr/drivers/sensor/tmp108.h>
13
get_tmp_sensor_dev(void)14 const struct device *get_tmp_sensor_dev(void)
15 {
16
17 const struct device *temp_sensor = DEVICE_DT_GET_ANY(ti_tmp108);
18
19 if (!temp_sensor) {
20 printf("warning: tmp108 device not found checking for compatible ams device\n");
21
22 temp_sensor = DEVICE_DT_GET_ANY(ams_as6212);
23 }
24
25 if (!temp_sensor) {
26 temp_sensor = DEVICE_DT_GET_ANY(ams_as6221);
27
28 if (!temp_sensor) {
29 printf("error: tmp108 compatible devices not found\n");
30 return 0;
31 }
32 }
33
34 return temp_sensor;
35 }
36
temperature_one_shot(const struct device * dev,const struct sensor_trigger * trigger)37 void temperature_one_shot(const struct device *dev,
38 const struct sensor_trigger *trigger)
39 {
40
41 struct sensor_value temp_value;
42 int result;
43
44 result = sensor_channel_get(dev,
45 SENSOR_CHAN_AMBIENT_TEMP,
46 &temp_value);
47
48 if (result) {
49 printf("error: sensor_channel_get failed: %d\n", result);
50 return;
51 }
52
53 printf("One shot power saving mode enabled, temperature is %gC\n",
54 sensor_value_to_double(&temp_value));
55 }
56
temperature_alert(const struct device * dev,const struct sensor_trigger * trigger)57 void temperature_alert(const struct device *dev,
58 const struct sensor_trigger *trigger)
59 {
60
61 struct sensor_value temp_flags = { 0 };
62
63 sensor_attr_get(dev,
64 SENSOR_CHAN_AMBIENT_TEMP,
65 SENSOR_ATTR_CONFIGURATION,
66 &temp_flags);
67
68 /* use a mask to pull your specific chip set bits out */
69
70 printf("Temperature alert config register = %x!\n", temp_flags.val1);
71 }
72
enable_temp_alerts(const struct device * tmp108)73 void enable_temp_alerts(const struct device *tmp108)
74 {
75
76 struct sensor_trigger sensor_trigger_type_temp_alert = {
77 .chan = SENSOR_CHAN_AMBIENT_TEMP,
78 .type = SENSOR_TRIG_THRESHOLD
79 };
80
81 struct sensor_value alert_upper_thresh = {
82 CONFIG_APP_TEMP_ALERT_HIGH_THRESH,
83 0
84 };
85
86 struct sensor_value alert_lower_thresh = {
87 CONFIG_APP_TEMP_ALERT_LOW_THRESH,
88 0
89 };
90
91 struct sensor_value alert_hysterisis = { 1, 0 };
92 struct sensor_value thermostat_mode = { 0, 0 };
93
94 sensor_attr_set(tmp108,
95 SENSOR_CHAN_AMBIENT_TEMP,
96 SENSOR_ATTR_ALERT,
97 &thermostat_mode);
98
99 sensor_attr_set(tmp108,
100 SENSOR_CHAN_AMBIENT_TEMP,
101 SENSOR_ATTR_HYSTERESIS,
102 &alert_hysterisis);
103
104 sensor_attr_set(tmp108,
105 SENSOR_CHAN_AMBIENT_TEMP,
106 SENSOR_ATTR_UPPER_THRESH,
107 &alert_upper_thresh);
108
109 sensor_attr_set(tmp108,
110 SENSOR_CHAN_AMBIENT_TEMP,
111 SENSOR_ATTR_LOWER_THRESH,
112 &alert_lower_thresh);
113
114 sensor_trigger_set(tmp108,
115 &sensor_trigger_type_temp_alert,
116 temperature_alert);
117 }
118
enable_one_shot(const struct device * tmp108)119 void enable_one_shot(const struct device *tmp108)
120 {
121
122 struct sensor_trigger sensor_trigger_type_temp_one_shot = {
123 .chan = SENSOR_CHAN_AMBIENT_TEMP,
124 .type = SENSOR_TRIG_DATA_READY
125 };
126
127 sensor_attr_set(tmp108,
128 SENSOR_CHAN_AMBIENT_TEMP,
129 SENSOR_ATTR_TMP108_ONE_SHOT_MODE,
130 NULL);
131
132 sensor_trigger_set(tmp108,
133 &sensor_trigger_type_temp_one_shot,
134 temperature_one_shot);
135 }
136
get_temperature_continuous(const struct device * tmp108)137 void get_temperature_continuous(const struct device *tmp108)
138 {
139
140 struct sensor_value temp_value;
141 int result;
142
143 result = sensor_channel_get(tmp108,
144 SENSOR_CHAN_AMBIENT_TEMP,
145 &temp_value);
146
147 if (result) {
148 printf("error: sensor_channel_get failed: %d\n", result);
149 return;
150 }
151
152 printf("temperature is %gC\n", sensor_value_to_double(&temp_value));
153 }
154
main(void)155 int main(void)
156 {
157 const struct device *temp_sensor;
158 int result;
159
160 printf("TI TMP108 Example, %s\n", CONFIG_ARCH);
161
162 temp_sensor = get_tmp_sensor_dev();
163
164 if (!device_is_ready(temp_sensor)) {
165 printf("error: tmp108 device not ready\n");
166 return 0;
167 }
168
169 sensor_attr_set(temp_sensor,
170 SENSOR_CHAN_AMBIENT_TEMP,
171 SENSOR_ATTR_TMP108_CONTINUOUS_CONVERSION_MODE,
172 NULL);
173
174 #if CONFIG_APP_ENABLE_ONE_SHOT
175 enable_one_shot(temp_sensor);
176 #endif
177
178 #if CONFIG_APP_REPORT_TEMP_ALERTS
179 enable_temp_alerts(temp_sensor);
180 #endif
181
182 while (1) {
183
184 result = sensor_sample_fetch(temp_sensor);
185
186 if (result) {
187 printf("error: sensor_sample_fetch failed: %d\n", result);
188 break;
189 }
190
191 #if !CONFIG_APP_ENABLE_ONE_SHOT
192 get_temperature_continuous(temp_sensor);
193 #endif
194 k_sleep(K_MSEC(3000));
195 }
196 return 0;
197 }
198