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