|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Alexander Wachter. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <device.h> |
| 8 | +#include <i2c.h> |
| 9 | +#include <kernel.h> |
| 10 | +#include <misc/byteorder.h> |
| 11 | +#include <misc/util.h> |
| 12 | +#include <sensor.h> |
| 13 | +#include <misc/__assert.h> |
| 14 | +#include <logging/log.h> |
| 15 | + |
| 16 | +#include "iAQcore.h" |
| 17 | + |
| 18 | +#define LOG_LEVEL CONFIG_SENSOR_LOG_LEVEL |
| 19 | +LOG_MODULE_REGISTER(IAQ_CORE); |
| 20 | + |
| 21 | +static int iaqcore_sample_fetch(struct device *dev, enum sensor_channel chan) |
| 22 | +{ |
| 23 | + struct iaq_core_data *drv_data = dev->driver_data; |
| 24 | + struct iaq_registers buf; |
| 25 | + struct i2c_msg msg; |
| 26 | + int ret, tries; |
| 27 | + |
| 28 | + __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL); |
| 29 | + |
| 30 | + msg.buf = (u8_t *)&buf; |
| 31 | + msg.len = sizeof(struct iaq_registers); |
| 32 | + msg.flags = I2C_MSG_READ | I2C_MSG_STOP; |
| 33 | + |
| 34 | + for (tries = 0; tries < CONFIG_IAQ_CORE_MAX_READ_RETRIES; tries++) { |
| 35 | + |
| 36 | + ret = i2c_transfer(drv_data->i2c, &msg, 1, |
| 37 | + DT_AMS_IAQCORE_0_BASE_ADDRESS); |
| 38 | + if (ret < 0) { |
| 39 | + LOG_ERR("Failed to read registers data [%d].", ret); |
| 40 | + return -EIO; |
| 41 | + } |
| 42 | + |
| 43 | + drv_data->status = buf.status; |
| 44 | + |
| 45 | + if (buf.status == 0x00) { |
| 46 | + drv_data->co2 = sys_be16_to_cpu(buf.co2_pred); |
| 47 | + drv_data->voc = sys_be16_to_cpu(buf.voc); |
| 48 | + drv_data->status = buf.status; |
| 49 | + drv_data->resistance = sys_be32_to_cpu(buf.resistance); |
| 50 | + |
| 51 | + return 0; |
| 52 | + } |
| 53 | + |
| 54 | + k_sleep(100); |
| 55 | + } |
| 56 | + |
| 57 | + if (drv_data->status == 0x01) { |
| 58 | + LOG_INF("Sensor data not available"); |
| 59 | + } |
| 60 | + |
| 61 | + if (drv_data->status == 0x80) { |
| 62 | + LOG_ERR("Sensor Error"); |
| 63 | + } |
| 64 | + |
| 65 | + return -EIO; |
| 66 | +} |
| 67 | + |
| 68 | +static int iaqcore_channel_get(struct device *dev, |
| 69 | + enum sensor_channel chan, |
| 70 | + struct sensor_value *val) |
| 71 | +{ |
| 72 | + struct iaq_core_data *drv_data = dev->driver_data; |
| 73 | + |
| 74 | + switch (chan) { |
| 75 | + case SENSOR_CHAN_CO2: |
| 76 | + val->val1 = drv_data->co2; |
| 77 | + val->val2 = 0; |
| 78 | + break; |
| 79 | + case SENSOR_CHAN_VOC: |
| 80 | + val->val1 = drv_data->voc; |
| 81 | + val->val2 = 0; |
| 82 | + break; |
| 83 | + case SENSOR_CHAN_RESISTANCE: |
| 84 | + val->val1 = drv_data->resistance; |
| 85 | + val->val2 = 0; |
| 86 | + break; |
| 87 | + default: |
| 88 | + return -ENOTSUP; |
| 89 | + } |
| 90 | + |
| 91 | + return 0; |
| 92 | +} |
| 93 | + |
| 94 | +static const struct sensor_driver_api iaq_core_driver_api = { |
| 95 | + .sample_fetch = iaqcore_sample_fetch, |
| 96 | + .channel_get = iaqcore_channel_get, |
| 97 | +}; |
| 98 | + |
| 99 | +static int iaq_core_init(struct device *dev) |
| 100 | +{ |
| 101 | + struct iaq_core_data *drv_data = dev->driver_data; |
| 102 | + |
| 103 | + drv_data->i2c = device_get_binding(DT_AMS_IAQCORE_0_BUS_NAME); |
| 104 | + if (drv_data->i2c == NULL) { |
| 105 | + LOG_ERR("Failed to get pointer to %s device!", |
| 106 | + DT_AMS_IAQCORE_0_BUS_NAME); |
| 107 | + return -EINVAL; |
| 108 | + } |
| 109 | + |
| 110 | + return 0; |
| 111 | +} |
| 112 | + |
| 113 | +static struct iaq_core_data iaq_core_driver; |
| 114 | + |
| 115 | +DEVICE_AND_API_INIT(iaq_core, DT_AMS_IAQCORE_0_LABEL, iaq_core_init, |
| 116 | + &iaq_core_driver, NULL, POST_KERNEL, |
| 117 | + CONFIG_SENSOR_INIT_PRIORITY, &iaq_core_driver_api); |
0 commit comments