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 350 351 352 353 354 355 356 357 358 359 360 361 | /* * Copyright (c) 2019 Thomas Schmid <tom@lfence.de> * * SPDX-License-Identifier: Apache-2.0 */ #define DT_DRV_COMPAT meas_ms5607 #include <zephyr/init.h> #include <zephyr/kernel.h> #include <zephyr/sys/byteorder.h> #include <zephyr/drivers/sensor.h> #include <zephyr/sys/__assert.h> #include "ms5607.h" #define LOG_LEVEL CONFIG_SENSOR_LOG_LEVEL #include <zephyr/logging/log.h> LOG_MODULE_REGISTER(ms5607); static void ms5607_compensate(struct ms5607_data *data, const int32_t adc_temperature, const int32_t adc_pressure) { int64_t dT; int64_t OFF; int64_t SENS; int64_t temp_sq; int64_t Ti; int64_t OFFi; int64_t SENSi; /* first order compensation as per datasheet * (https://www.te.com/usa-en/product-CAT-BLPS0035.html) section * PRESSURE AND TEMPERATURE CALCULATION */ dT = adc_temperature - ((int32_t)(data->t_ref) << 8); data->temperature = 2000 + (dT * data->tempsens) / (1ll << 23); OFF = ((int64_t)(data->off_t1) << 17) + (dT * data->tco) / (1ll << 6); SENS = ((int64_t)(data->sens_t1) << 16) + (dT * data->tcs) / (1ll << 7); /* Second order compensation as per datasheet * (https://www.te.com/usa-en/product-CAT-BLPS0035.html) section * SECOND ORDER TEMPERATURE COMPENSATION */ temp_sq = (int64_t)(data->temperature - 2000) * (int64_t)(data->temperature - 2000); if (data->temperature < 2000) { Ti = (dT * dT) / (1ll << 31); OFFi = (61ll * temp_sq) / (1ll << 4); SENSi = 2ll * temp_sq; if (data->temperature < -1500) { temp_sq = (int64_t)(data->temperature + 1500) * (int64_t)(data->temperature + 1500); OFFi += 15ll * temp_sq; SENSi += 8ll * temp_sq; } } else { SENSi = 0; OFFi = 0; Ti = 0; } OFF -= OFFi; SENS -= SENSi; data->temperature -= Ti; data->pressure = (SENS * (int64_t)adc_pressure / (1ll << 21) - OFF) / (1ll << 15); } static int ms5607_read_prom(const struct ms5607_config *config, uint8_t cmd, uint16_t *val) { int err; err = config->tf->read_prom(config, cmd, val); if (err < 0) { LOG_ERR("Error reading prom"); return err; } return 0; } static int ms5607_get_measurement(const struct ms5607_config *config, uint32_t *val, uint8_t cmd, uint8_t delay) { int err; *val = 0U; err = config->tf->start_conversion(config, cmd); if (err < 0) { return err; } k_msleep(delay); err = config->tf->read_adc(config, val); if (err < 0) { return err; } return 0; } static int ms5607_sample_fetch(const struct device *dev, enum sensor_channel channel) { const struct ms5607_config *config = dev->config; struct ms5607_data *data = dev->data; int err; uint32_t adc_pressure, adc_temperature; __ASSERT_NO_MSG(channel == SENSOR_CHAN_ALL); err = ms5607_get_measurement(config, &adc_pressure, data->pressure_conv_cmd, data->pressure_conv_delay); if (err < 0) { return err; } err = ms5607_get_measurement(config, &adc_temperature, data->temperature_conv_cmd, data->temperature_conv_delay); if (err < 0) { return err; } ms5607_compensate(data, adc_temperature, adc_pressure); return 0; } static int ms5607_channel_get(const struct device *dev, enum sensor_channel chan, struct sensor_value *val) { const struct ms5607_data *data = dev->data; switch (chan) { case SENSOR_CHAN_AMBIENT_TEMP: val->val1 = data->temperature / 100; val->val2 = data->temperature % 100 * 10000; break; case SENSOR_CHAN_PRESS: val->val1 = data->pressure / 100; val->val2 = data->pressure % 100 * 10000; break; default: return -EINVAL; } return 0; } static int ms5607_attr_set(const struct device *dev, enum sensor_channel chan, enum sensor_attribute attr, const struct sensor_value *val) { struct ms5607_data *data = dev->data; uint8_t p_conv_cmd, t_conv_cmd, conv_delay; if (attr != SENSOR_ATTR_OVERSAMPLING) { return -ENOTSUP; } switch (val->val1) { case 4096: p_conv_cmd = MS5607_CMD_CONV_P_4096; t_conv_cmd = MS5607_CMD_CONV_T_4096; conv_delay = 9U; break; case 2048: p_conv_cmd = MS5607_CMD_CONV_P_2048; t_conv_cmd = MS5607_CMD_CONV_T_2048; conv_delay = 5U; break; case 1024: p_conv_cmd = MS5607_CMD_CONV_P_1024; t_conv_cmd = MS5607_CMD_CONV_T_1024; conv_delay = 3U; break; case 512: p_conv_cmd = MS5607_CMD_CONV_P_512; t_conv_cmd = MS5607_CMD_CONV_T_512; conv_delay = 2U; break; case 256: p_conv_cmd = MS5607_CMD_CONV_P_256; t_conv_cmd = MS5607_CMD_CONV_T_256; conv_delay = 1U; break; default: LOG_ERR("invalid oversampling rate %d", val->val1); return -EINVAL; } switch (chan) { case SENSOR_CHAN_ALL: data->pressure_conv_cmd = p_conv_cmd; data->temperature_conv_cmd = t_conv_cmd; data->temperature_conv_delay = conv_delay; data->pressure_conv_delay = conv_delay; break; case SENSOR_CHAN_PRESS: data->pressure_conv_cmd = p_conv_cmd; data->pressure_conv_delay = conv_delay; break; case SENSOR_CHAN_AMBIENT_TEMP: data->temperature_conv_cmd = t_conv_cmd; data->temperature_conv_delay = conv_delay; break; default: return -ENOTSUP; } return 0; } static int ms5607_init(const struct device *dev) { const struct ms5607_config *const config = dev->config; struct ms5607_data *data = dev->data; struct sensor_value val; int err; err = config->tf->bus_check(config); if (err < 0) { return err; } data->pressure = 0; data->temperature = 0; val.val1 = MS5607_PRES_OVER_DEFAULT; err = ms5607_attr_set(dev, SENSOR_CHAN_PRESS, SENSOR_ATTR_OVERSAMPLING, &val); if (err < 0) { return err; } val.val1 = MS5607_TEMP_OVER_DEFAULT; err = ms5607_attr_set(dev, SENSOR_CHAN_AMBIENT_TEMP, SENSOR_ATTR_OVERSAMPLING, &val); if (err < 0) { return err; } err = config->tf->reset(config); if (err < 0) { return err; } k_sleep(K_MSEC(2)); err = ms5607_read_prom(config, MS5607_CMD_CONV_READ_OFF_T1, &data->off_t1); if (err < 0) { return err; } LOG_DBG("OFF_T1: %d", data->off_t1); err = ms5607_read_prom(config, MS5607_CMD_CONV_READ_SENSE_T1, &data->sens_t1); if (err < 0) { return err; } LOG_DBG("SENSE_T1: %d", data->sens_t1); err = ms5607_read_prom(config, MS5607_CMD_CONV_READ_T_REF, &data->t_ref); if (err < 0) { return err; } LOG_DBG("T_REF: %d", data->t_ref); err = ms5607_read_prom(config, MS5607_CMD_CONV_READ_TCO, &data->tco); if (err < 0) { return err; } LOG_DBG("TCO: %d", data->tco); err = ms5607_read_prom(config, MS5607_CMD_CONV_READ_TCS, &data->tcs); if (err < 0) { return err; } LOG_DBG("TCS: %d", data->tcs); err = ms5607_read_prom(config, MS5607_CMD_CONV_READ_TEMPSENS, &data->tempsens); if (err < 0) { return err; } LOG_DBG("TEMPSENS: %d", data->tempsens); return 0; } static const struct sensor_driver_api ms5607_api_funcs = { .attr_set = ms5607_attr_set, .sample_fetch = ms5607_sample_fetch, .channel_get = ms5607_channel_get, }; #define MS5607_SPI_OPERATION (SPI_OP_MODE_MASTER | SPI_WORD_SET(8) | \ SPI_MODE_CPOL | SPI_MODE_CPHA | SPI_TRANSFER_MSB) /* Initializes a struct ms5607_config for an instance on a SPI bus. */ #define MS5607_CONFIG_SPI(inst) \ { \ .bus = DEVICE_DT_GET(DT_INST_BUS(inst)), \ .tf = &ms5607_spi_transfer_function, \ .bus_cfg.spi_bus = \ SPI_DT_SPEC_INST_GET(inst, \ MS5607_SPI_OPERATION, \ 0), \ } /* Initializes a struct ms5607_config for an instance on a I2C bus. */ #define MS5607_CONFIG_I2C(inst) \ { \ .bus = DEVICE_DT_GET(DT_INST_BUS(inst)), \ .tf = &ms5607_i2c_transfer_function, \ .bus_cfg.i2c_addr = DT_INST_REG_ADDR(inst) \ } /* * Main instantiation macro, which selects the correct bus-specific * instantiation macros for the instance. */ #define MS5607_DEFINE(inst) \ static struct ms5607_data ms5607_data_##inst; \ static const struct ms5607_config ms5607_config_##inst = \ COND_CODE_1(DT_INST_ON_BUS(inst, spi), \ (MS5607_CONFIG_SPI(inst)), \ (MS5607_CONFIG_I2C(inst))); \ DEVICE_DT_INST_DEFINE(inst, \ ms5607_init, \ NULL, \ &ms5607_data_##inst, \ &ms5607_config_##inst, \ POST_KERNEL, \ CONFIG_SENSOR_INIT_PRIORITY, \ &ms5607_api_funcs); /* Create the struct device for every status "okay" node in the devicetree. */ DT_INST_FOREACH_STATUS_OKAY(MS5607_DEFINE) |