Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | /* * Copyright (c) 2016 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr/device.h> #include <zephyr/sys/util.h> #include <zephyr/kernel.h> #include <zephyr/drivers/sensor.h> #include "sht3xd.h" #include <zephyr/logging/log.h> LOG_MODULE_DECLARE(SHT3XD, CONFIG_SENSOR_LOG_LEVEL); static uint16_t sht3xd_temp_processed_to_raw(const struct sensor_value *val) { uint64_t uval; /* ret = (val + 45) * (2^16 - 1) / 175 */ uval = (uint64_t)(val->val1 + 45) * 1000000U + val->val2; return ((uval * 0xFFFF) / 175) / 1000000; } static int sht3xd_rh_processed_to_raw(const struct sensor_value *val) { uint64_t uval; /* ret = val * (2^16 -1) / 100 */ uval = (uint64_t)val->val1 * 1000000U + val->val2; return ((uval * 0xFFFF) / 100) / 1000000; } int sht3xd_attr_set(const struct device *dev, enum sensor_channel chan, enum sensor_attribute attr, const struct sensor_value *val) { struct sht3xd_data *data = dev->data; uint16_t set_cmd, clear_cmd, reg_val, temp, rh; if (attr == SENSOR_ATTR_LOWER_THRESH) { if (chan == SENSOR_CHAN_AMBIENT_TEMP) { data->t_low = sht3xd_temp_processed_to_raw(val); } else if (chan == SENSOR_CHAN_HUMIDITY) { data->rh_low = sht3xd_rh_processed_to_raw(val); } else { return -ENOTSUP; } set_cmd = SHT3XD_CMD_WRITE_TH_LOW_SET; clear_cmd = SHT3XD_CMD_WRITE_TH_LOW_CLEAR; temp = data->t_low; rh = data->rh_low; } else if (attr == SENSOR_ATTR_UPPER_THRESH) { if (chan == SENSOR_CHAN_AMBIENT_TEMP) { data->t_high = sht3xd_temp_processed_to_raw(val); } else if (chan == SENSOR_CHAN_HUMIDITY) { data->rh_high = sht3xd_rh_processed_to_raw(val); } else { return -ENOTSUP; } set_cmd = SHT3XD_CMD_WRITE_TH_HIGH_SET; clear_cmd = SHT3XD_CMD_WRITE_TH_HIGH_CLEAR; temp = data->t_high; rh = data->rh_high; } else { return -ENOTSUP; } reg_val = (rh & 0xFE00) | ((temp & 0xFF80) >> 7); if (sht3xd_write_reg(dev, set_cmd, reg_val) < 0 || sht3xd_write_reg(dev, clear_cmd, reg_val) < 0) { LOG_DBG("Failed to write threshold value!"); return -EIO; } return 0; } static inline void setup_alert(const struct device *dev, bool enable) { struct sht3xd_data *data = dev->data; const struct sht3xd_config *cfg = (const struct sht3xd_config *)dev->config; unsigned int flags = enable ? GPIO_INT_EDGE_TO_ACTIVE : GPIO_INT_DISABLE; gpio_pin_interrupt_configure(data->alert_gpio, cfg->alert_pin, flags); } static inline void handle_alert(const struct device *dev) { setup_alert(dev, false); #if defined(CONFIG_SHT3XD_TRIGGER_OWN_THREAD) struct sht3xd_data *data = dev->data; k_sem_give(&data->gpio_sem); #elif defined(CONFIG_SHT3XD_TRIGGER_GLOBAL_THREAD) struct sht3xd_data *data = dev->data; k_work_submit(&data->work); #endif } int sht3xd_trigger_set(const struct device *dev, const struct sensor_trigger *trig, sensor_trigger_handler_t handler) { struct sht3xd_data *data = dev->data; const struct sht3xd_config *cfg = (const struct sht3xd_config *)dev->config; setup_alert(dev, false); if (trig->type != SENSOR_TRIG_THRESHOLD) { return -ENOTSUP; } data->handler = handler; if (handler == NULL) { return 0; } data->trigger = *trig; setup_alert(dev, true); /* If ALERT is active we probably won't get the rising edge, * so invoke the callback manually. */ if (gpio_pin_get(data->alert_gpio, cfg->alert_pin)) { handle_alert(dev); } return 0; } static void sht3xd_gpio_callback(const struct device *dev, struct gpio_callback *cb, uint32_t pins) { struct sht3xd_data *data = CONTAINER_OF(cb, struct sht3xd_data, alert_cb); handle_alert(data->dev); } static void sht3xd_thread_cb(const struct device *dev) { struct sht3xd_data *data = dev->data; if (data->handler != NULL) { data->handler(dev, &data->trigger); } setup_alert(dev, true); } #ifdef CONFIG_SHT3XD_TRIGGER_OWN_THREAD static void sht3xd_thread(struct sht3xd_data *data) { while (1) { k_sem_take(&data->gpio_sem, K_FOREVER); sht3xd_thread_cb(data->dev); } } #endif #ifdef CONFIG_SHT3XD_TRIGGER_GLOBAL_THREAD static void sht3xd_work_cb(struct k_work *work) { struct sht3xd_data *data = CONTAINER_OF(work, struct sht3xd_data, work); sht3xd_thread_cb(data->dev); } #endif int sht3xd_init_interrupt(const struct device *dev) { struct sht3xd_data *data = dev->data; const struct sht3xd_config *cfg = dev->config; const struct device *gpio = device_get_binding(cfg->alert_gpio_name); int rc; /* setup gpio interrupt */ if (gpio == NULL) { LOG_DBG("Failed to get pointer to %s device!", cfg->alert_gpio_name); return -EINVAL; } data->alert_gpio = gpio; rc = gpio_pin_configure(gpio, cfg->alert_pin, GPIO_INPUT | cfg->alert_flags); if (rc != 0) { LOG_DBG("Failed to configure alert pin %u!", cfg->alert_pin); return -EIO; } gpio_init_callback(&data->alert_cb, sht3xd_gpio_callback, BIT(cfg->alert_pin)); rc = gpio_add_callback(gpio, &data->alert_cb); if (rc < 0) { LOG_DBG("Failed to set gpio callback!"); return -EIO; } /* set alert thresholds to match measurement ranges */ data->t_low = 0U; data->rh_low = 0U; data->t_high = 0xFFFF; data->rh_high = 0xFFFF; if (sht3xd_write_reg(dev, SHT3XD_CMD_WRITE_TH_HIGH_SET, 0xFFFF) < 0) { LOG_DBG("Failed to write threshold high set value!"); return -EIO; } if (sht3xd_write_reg(dev, SHT3XD_CMD_WRITE_TH_HIGH_CLEAR, 0xFFFF) < 0) { LOG_DBG("Failed to write threshold high clear value!"); return -EIO; } if (sht3xd_write_reg(dev, SHT3XD_CMD_WRITE_TH_LOW_SET, 0) < 0) { LOG_DBG("Failed to write threshold low set value!"); return -EIO; } if (sht3xd_write_reg(dev, SHT3XD_CMD_WRITE_TH_LOW_SET, 0) < 0) { LOG_DBG("Failed to write threshold low clear value!"); return -EIO; } #if defined(CONFIG_SHT3XD_TRIGGER_OWN_THREAD) k_sem_init(&data->gpio_sem, 0, K_SEM_MAX_LIMIT); k_thread_create(&data->thread, data->thread_stack, CONFIG_SHT3XD_THREAD_STACK_SIZE, (k_thread_entry_t)sht3xd_thread, data, NULL, NULL, K_PRIO_COOP(CONFIG_SHT3XD_THREAD_PRIORITY), 0, K_NO_WAIT); #elif defined(CONFIG_SHT3XD_TRIGGER_GLOBAL_THREAD) data->work.handler = sht3xd_work_cb; #endif return 0; } |