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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | /* * Copyright (c) 2018 Alexander Wachter. * * SPDX-License-Identifier: Apache-2.0 */ #define DT_DRV_COMPAT ams_ens210 #include <zephyr/device.h> #include <zephyr/drivers/i2c.h> #include <zephyr/kernel.h> #include <zephyr/sys/byteorder.h> #include <zephyr/sys/util.h> #include <zephyr/drivers/sensor.h> #include <zephyr/sys/__assert.h> #include <zephyr/logging/log.h> #include "ens210.h" LOG_MODULE_REGISTER(ENS210, CONFIG_SENSOR_LOG_LEVEL); #ifdef CONFIG_ENS210_CRC_CHECK static uint32_t ens210_crc7(uint32_t bitstream) { uint32_t polynomial = (ENS210_CRC7_POLY << (ENS210_CRC7_DATA_WIDTH - 1)); uint32_t bit = ENS210_CRC7_DATA_MSB << ENS210_CRC7_WIDTH; uint32_t val = (bitstream << ENS210_CRC7_WIDTH) | ENS210_CRC7_IVEC; while (bit & (ENS210_CRC7_DATA_MASK << ENS210_CRC7_WIDTH)) { if (bit & val) { val ^= polynomial; } bit >>= 1; polynomial >>= 1; } return val; } #endif /* CONFIG_ENS210_CRC_CHECK */ #if defined(CONFIG_ENS210_TEMPERATURE_SINGLE) \ || defined(CONFIG_ENS210_HUMIDITY_SINGLE) static int ens210_measure(const struct device *i2c_dev, enum sensor_channel chan) { uint8_t buf; int ret; const struct ens210_sens_start sense_start = { .t_start = ENS210_T_START && (chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_AMBIENT_TEMP), .h_start = ENS210_H_START && (chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_HUMIDITY) }; /* Start measuring */ ret = i2c_reg_write_byte(i2c_dev, DT_INST_REG_ADDR(0), ENS210_REG_SENS_START, *(uint8_t *)&sense_start); if (ret < 0) { LOG_ERR("Failed to set SENS_START to 0x%x", *(uint8_t *)&sense_start); return -EIO; } /* Wait for measurement to be completed */ do { k_sleep(K_MSEC(2)); ret = i2c_reg_read_byte(i2c_dev, DT_INST_REG_ADDR(0), ENS210_REG_SENS_START, &buf); if (ret < 0) { LOG_ERR("Failed to read SENS_STAT"); } } while (buf & *(uint8_t *)&sense_start); return ret; } #endif /* Single shot mode */ static int ens210_sample_fetch(const struct device *dev, enum sensor_channel chan) { struct ens210_data *drv_data = dev->data; struct ens210_value_data data[2]; int ret, cnt; #ifdef CONFIG_ENS210_CRC_CHECK uint32_t temp_valid, humidity_valid; #endif /* CONFIG_ENS210_CRC_CHECK */ __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_AMBIENT_TEMP || chan == SENSOR_CHAN_HUMIDITY); #if defined(CONFIG_ENS210_TEMPERATURE_SINGLE) \ || defined(CONFIG_ENS210_HUMIDITY_SINGLE) ret = ens210_measure(drv_data->i2c, chan); if (ret < 0) { LOG_ERR("Failed to measure"); return ret; } #endif /* Single shot mode */ for (cnt = 0; cnt <= CONFIG_ENS210_MAX_READ_RETRIES; cnt++) { ret = i2c_burst_read(drv_data->i2c, DT_INST_REG_ADDR(0), ENS210_REG_T_VAL, (uint8_t *)&data, sizeof(data)); if (ret < 0) { LOG_ERR("Failed to read data"); continue; } /* Get temperature value */ if (chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_AMBIENT_TEMP) { if (!data[0].valid) { LOG_WRN("Temperature not valid"); continue; } #ifdef CONFIG_ENS210_CRC_CHECK temp_valid = data[0].val | (data[0].valid << (sizeof(data[0].val) * 8)); if (ens210_crc7(temp_valid) != data[0].crc7) { LOG_WRN("Temperature CRC error"); continue; } #endif /* CONFIG_ENS210_CRC_CHECK */ drv_data->temp = data[0]; } /* Get humidity value */ if (chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_HUMIDITY) { if (!data[1].valid) { LOG_WRN("Humidity not valid"); continue; } #ifdef CONFIG_ENS210_CRC_CHECK humidity_valid = data[1].val | (data[1].valid << (sizeof(data[1].val) * 8)); if (ens210_crc7(humidity_valid) != data[1].crc7) { LOG_WRN("Humidity CRC error"); continue; } #endif /* CONFIG_ENS210_CRC_CHECK */ drv_data->humidity = data[1]; } return 0; } return -EIO; } static int ens210_channel_get(const struct device *dev, enum sensor_channel chan, struct sensor_value *val) { struct ens210_data *drv_data = dev->data; int32_t temp_frac; int32_t humidity_frac; switch (chan) { case SENSOR_CHAN_AMBIENT_TEMP: /* Temperature is in 1/64 Kelvin. Subtract 273.15 for Celsius */ temp_frac = sys_le16_to_cpu(drv_data->temp.val) * (1000000 / 64); temp_frac -= 273150000; val->val1 = temp_frac / 1000000; val->val2 = temp_frac % 1000000; break; case SENSOR_CHAN_HUMIDITY: humidity_frac = sys_le16_to_cpu(drv_data->humidity.val) * (1000000 / 512); val->val1 = humidity_frac / 1000000; val->val2 = humidity_frac % 1000000; break; default: return -ENOTSUP; } return 0; } static int ens210_sys_reset(const struct device *i2c_dev) { const struct ens210_sys_ctrl sys_ctrl = { .low_power = 0, .reset = 1 }; int ret; ret = i2c_reg_write_byte(i2c_dev, DT_INST_REG_ADDR(0), ENS210_REG_SYS_CTRL, *(uint8_t *)&sys_ctrl); if (ret < 0) { LOG_ERR("Failed to set SYS_CTRL to 0x%x", *(uint8_t *)&sys_ctrl); } return ret; } static int ens210_sys_enable(const struct device *i2c_dev, uint8_t low_power) { const struct ens210_sys_ctrl sys_ctrl = { .low_power = low_power, .reset = 0 }; int ret; ret = i2c_reg_write_byte(i2c_dev, DT_INST_REG_ADDR(0), ENS210_REG_SYS_CTRL, *(uint8_t *)&sys_ctrl); if (ret < 0) { LOG_ERR("Failed to set SYS_CTRL to 0x%x", *(uint8_t *)&sys_ctrl); } return ret; } static int ens210_wait_boot(const struct device *i2c_dev) { int cnt; int ret; struct ens210_sys_stat sys_stat; for (cnt = 0; cnt <= CONFIG_ENS210_MAX_STAT_RETRIES; cnt++) { ret = i2c_reg_read_byte(i2c_dev, DT_INST_REG_ADDR(0), ENS210_REG_SYS_STAT, (uint8_t *)&sys_stat); if (ret < 0) { k_sleep(K_MSEC(1)); continue; } if (sys_stat.sys_active) { return 0; } if (cnt == 0) { ens210_sys_reset(i2c_dev); } ens210_sys_enable(i2c_dev, 0); k_sleep(K_MSEC(2)); } if (ret < 0) { LOG_ERR("Failed to read SYS_STATE"); } LOG_ERR("Sensor is not in active state"); return -EIO; } static const struct sensor_driver_api en210_driver_api = { .sample_fetch = ens210_sample_fetch, .channel_get = ens210_channel_get, }; static int ens210_init(const struct device *dev) { struct ens210_data *drv_data = dev->data; const struct ens210_sens_run sense_run = { .t_run = ENS210_T_RUN, .h_run = ENS210_H_RUN }; #if defined(CONFIG_ENS210_TEMPERATURE_CONTINUOUS) \ || defined(CONFIG_ENS210_HUMIDITY_CONTINUOUS) const struct ens210_sens_start sense_start = { .t_start = ENS210_T_RUN, .h_start = ENS210_H_RUN }; #endif int ret; uint16_t part_id; drv_data->i2c = device_get_binding(DT_INST_BUS_LABEL(0)); if (drv_data->i2c == NULL) { LOG_ERR("Failed to get pointer to %s device!", DT_INST_BUS_LABEL(0)); return -EINVAL; } /* Wait until the device is ready. */ ret = ens210_wait_boot(drv_data->i2c); if (ret < 0) { return -EIO; } /* Check Hardware ID. This is only possible after device is ready * and active */ ret = i2c_burst_read(drv_data->i2c, DT_INST_REG_ADDR(0), ENS210_REG_PART_ID, (uint8_t *)&part_id, sizeof(part_id)); if (ret < 0) { LOG_ERR("Failed to read Part ID register"); return -EIO; } if (part_id != ENS210_PART_ID) { LOG_ERR("Part ID does not match. Want 0x%x, got 0x%x", ENS210_PART_ID, part_id); return -EIO; } /* Enable low power mode */ if ((ENS210_T_RUN | ENS210_H_RUN) == 0) { ens210_sys_enable(drv_data->i2c, 1); } /* Set measurement mode*/ ret = i2c_reg_write_byte(drv_data->i2c, DT_INST_REG_ADDR(0), ENS210_REG_SENS_RUN, *(uint8_t *)&sense_run); if (ret < 0) { LOG_ERR("Failed to set SENS_RUN to 0x%x", *(uint8_t *)&sense_run); return -EIO; } #if defined(CONFIG_ENS210_TEMPERATURE_CONTINUOUS) \ || defined(CONFIG_ENS210_HUMIDITY_CONTINUOUS) /* Start measuring */ ret = i2c_reg_write_byte(drv_data->i2c, DT_INST_REG_ADDR(0), ENS210_REG_SENS_START, *(uint8_t *)&sense_start); if (ret < 0) { LOG_ERR("Failed to set SENS_START to 0x%x", *(uint8_t *)&sense_start); return -EIO; } #endif return 0; } static struct ens210_data ens210_driver; DEVICE_DT_INST_DEFINE(0, ens210_init, NULL, &ens210_driver, NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &en210_driver_api); |