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 | /* * Copyright (c) 2018 Linaro Ltd. * * SPDX-License-Identifier: Apache-2.0 */ #include <device.h> #include <gpio.h> #include <i2c.h> #include <kernel.h> #include <misc/byteorder.h> #include <misc/util.h> #include <sensor.h> #include <misc/__assert.h> #include <logging/log.h> #include "ccs811.h" #define LOG_LEVEL CONFIG_SENSOR_LOG_LEVEL LOG_MODULE_REGISTER(CCS811); static int ccs811_sample_fetch(struct device *dev, enum sensor_channel chan) { struct ccs811_data *drv_data = dev->driver_data; int tries = 11; u16_t buf[4]; u8_t status; /* Check data ready flag for the measurement interval of 1 seconds */ while (tries-- > 0) { if (i2c_reg_read_byte(drv_data->i2c, DT_AMS_CCS811_0_BASE_ADDRESS, CCS811_REG_STATUS, &status) < 0) { LOG_ERR("Failed to read Status register"); return -EIO; } if ((status & CCS811_STATUS_DATA_READY) || tries == 0) { break; } k_sleep(100); } if (!(status & CCS811_STATUS_DATA_READY)) { LOG_ERR("Sensor data not available"); return -EIO; } if (i2c_burst_read(drv_data->i2c, DT_AMS_CCS811_0_BASE_ADDRESS, CCS811_REG_ALG_RESULT_DATA, (u8_t *)buf, 8) < 0) { LOG_ERR("Failed to read conversion data."); return -EIO; } drv_data->co2 = sys_be16_to_cpu(buf[0]); drv_data->voc = sys_be16_to_cpu(buf[1]); drv_data->status = buf[2] & 0xff; drv_data->error = buf[2] >> 8; drv_data->resistance = sys_be16_to_cpu(buf[3]); return 0; } static int ccs811_channel_get(struct device *dev, enum sensor_channel chan, struct sensor_value *val) { struct ccs811_data *drv_data = dev->driver_data; u32_t uval; switch (chan) { case SENSOR_CHAN_CO2: val->val1 = drv_data->co2; val->val2 = 0; break; case SENSOR_CHAN_VOC: val->val1 = drv_data->voc; val->val2 = 0; break; case SENSOR_CHAN_VOLTAGE: /* * Raw ADC readings are contained in least significant 10 bits */ uval = (drv_data->resistance & CCS811_VOLTAGE_MASK) * CCS811_VOLTAGE_SCALE; val->val1 = uval / 1000000U; val->val2 = uval % 1000000; break; case SENSOR_CHAN_CURRENT: /* * Current readings are contained in most * significant 6 bits in microAmps */ uval = drv_data->resistance >> 10; val->val1 = uval / 1000000U; val->val2 = uval % 1000000; break; default: return -ENOTSUP; } return 0; } static const struct sensor_driver_api ccs811_driver_api = { .sample_fetch = ccs811_sample_fetch, .channel_get = ccs811_channel_get, }; static int switch_to_app_mode(struct device *i2c) { u8_t status, buf; LOG_DBG("Switching to Application mode..."); if (i2c_reg_read_byte(i2c, DT_AMS_CCS811_0_BASE_ADDRESS, CCS811_REG_STATUS, &status) < 0) { LOG_ERR("Failed to read Status register"); return -EIO; } /* Check for the application firmware */ if (!(status & CCS811_STATUS_APP_VALID)) { LOG_ERR("No Application firmware loaded"); return -EINVAL; } buf = CCS811_REG_APP_START; /* Set the device to application mode */ if (i2c_write(i2c, &buf, 1, DT_AMS_CCS811_0_BASE_ADDRESS) < 0) { LOG_ERR("Failed to set Application mode"); return -EIO; } if (i2c_reg_read_byte(i2c, DT_AMS_CCS811_0_BASE_ADDRESS, CCS811_REG_STATUS, &status) < 0) { LOG_ERR("Failed to read Status register"); return -EIO; } /* Check for application mode */ if (!(status & CCS811_STATUS_FW_MODE)) { LOG_ERR("Failed to start Application firmware"); return -EINVAL; } LOG_DBG("CCS811 Application firmware started!"); return 0; } int ccs811_init(struct device *dev) { struct ccs811_data *drv_data = dev->driver_data; int ret; u8_t hw_id, status; drv_data->i2c = device_get_binding(DT_AMS_CCS811_0_BUS_NAME); if (drv_data->i2c == NULL) { LOG_ERR("Failed to get pointer to %s device!", DT_AMS_CCS811_0_BUS_NAME); return -EINVAL; } #if defined(CONFIG_CCS811_GPIO_WAKEUP) || defined(CONFIG_CCS811_GPIO_RESET) drv_data->gpio = device_get_binding(CONFIG_CCS811_GPIO_DEV_NAME); if (drv_data->gpio == NULL) { LOG_ERR("Failed to get pointer to %s device!", CONFIG_CCS811_GPIO_DEV_NAME); return -EINVAL; } #endif #ifdef CONFIG_CCS811_GPIO_RESET gpio_pin_configure(drv_data->gpio, CONFIG_CCS811_GPIO_RESET_PIN_NUM, GPIO_DIR_OUT); gpio_pin_write(drv_data->gpio, CONFIG_CCS811_GPIO_RESET_PIN_NUM, 1); k_sleep(1); #endif /* * Wakeup pin should be pulled low before initiating any I2C transfer. * If it has been tied to GND by default, skip this part. */ #ifdef CONFIG_CCS811_GPIO_WAKEUP gpio_pin_configure(drv_data->gpio, CONFIG_CCS811_GPIO_WAKEUP_PIN_NUM, GPIO_DIR_OUT); gpio_pin_write(drv_data->gpio, CONFIG_CCS811_GPIO_WAKEUP_PIN_NUM, 0); k_sleep(1); #endif /* Switch device to application mode */ ret = switch_to_app_mode(drv_data->i2c); if (ret) { return ret; } /* Check Hardware ID */ if (i2c_reg_read_byte(drv_data->i2c, DT_AMS_CCS811_0_BASE_ADDRESS, CCS811_REG_HW_ID, &hw_id) < 0) { LOG_ERR("Failed to read Hardware ID register"); return -EIO; } if (hw_id != CCS881_HW_ID) { LOG_ERR("Hardware ID mismatch!"); return -EINVAL; } /* Set Measurement mode for 1 second */ if (i2c_reg_write_byte(drv_data->i2c, DT_AMS_CCS811_0_BASE_ADDRESS, CCS811_REG_MEAS_MODE, CCS811_MODE_IAQ_1SEC) < 0) { LOG_ERR("Failed to set Measurement mode"); return -EIO; } /* Check for error */ if (i2c_reg_read_byte(drv_data->i2c, DT_AMS_CCS811_0_BASE_ADDRESS, CCS811_REG_STATUS, &status) < 0) { LOG_ERR("Failed to read Status register"); return -EIO; } if (status & CCS811_STATUS_ERROR) { LOG_ERR("Error occurred during sensor configuration"); return -EINVAL; } return 0; } static struct ccs811_data ccs811_driver; DEVICE_AND_API_INIT(ccs811, DT_AMS_CCS811_0_LABEL, ccs811_init, &ccs811_driver, NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &ccs811_driver_api); |