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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | /* ST Microelectronics LIS2DW12 3-axis accelerometer driver * * Copyright (c) 2019 STMicroelectronics * * SPDX-License-Identifier: Apache-2.0 * * Datasheet: * https://www.st.com/resource/en/datasheet/lis2dw12.pdf */ #define DT_DRV_COMPAT st_lis2dw12 #include <zephyr/init.h> #include <stdlib.h> #include <zephyr/sys/__assert.h> #include <zephyr/sys/byteorder.h> #include <zephyr/logging/log.h> #include <zephyr/drivers/sensor.h> #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) #include <zephyr/drivers/spi.h> #elif DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) #include <zephyr/drivers/i2c.h> #endif #include "lis2dw12.h" LOG_MODULE_REGISTER(LIS2DW12, CONFIG_SENSOR_LOG_LEVEL); /** * lis2dw12_set_range - set full scale range for acc * @dev: Pointer to instance of struct device (I2C or SPI) * @range: Full scale range (2, 4, 8 and 16 G) */ static int lis2dw12_set_range(const struct device *dev, uint8_t fs) { int err; struct lis2dw12_data *lis2dw12 = dev->data; const struct lis2dw12_device_config *cfg = dev->config; stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx; uint8_t shift_gain = 0U; err = lis2dw12_full_scale_set(ctx, fs); if (cfg->pm == LIS2DW12_CONT_LOW_PWR_12bit) { shift_gain = LIS2DW12_SHFT_GAIN_NOLP1; } if (!err) { /* save internally gain for optimization */ lis2dw12->gain = LIS2DW12_FS_TO_GAIN(fs, shift_gain); } return err; } /** * lis2dw12_set_odr - set new sampling frequency * @dev: Pointer to instance of struct device (I2C or SPI) * @odr: Output data rate */ static int lis2dw12_set_odr(const struct device *dev, uint16_t odr) { const struct lis2dw12_device_config *cfg = dev->config; stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx; uint8_t val; /* check if power off */ if (odr == 0U) { return lis2dw12_data_rate_set(ctx, LIS2DW12_XL_ODR_OFF); } val = LIS2DW12_ODR_TO_REG(odr); if (val > LIS2DW12_XL_ODR_1k6Hz) { LOG_ERR("ODR too high"); return -ENOTSUP; } return lis2dw12_data_rate_set(ctx, val); } static inline void lis2dw12_convert(struct sensor_value *val, int raw_val, float gain) { int64_t dval; /* Gain is in ug/LSB */ /* Convert to m/s^2 */ dval = ((int64_t)raw_val * gain * SENSOR_G) / 1000000LL; val->val1 = dval / 1000000LL; val->val2 = dval % 1000000LL; } static inline void lis2dw12_channel_get_acc(const struct device *dev, enum sensor_channel chan, struct sensor_value *val) { int i; uint8_t ofs_start, ofs_stop; struct lis2dw12_data *lis2dw12 = dev->data; struct sensor_value *pval = val; switch (chan) { case SENSOR_CHAN_ACCEL_X: ofs_start = ofs_stop = 0U; break; case SENSOR_CHAN_ACCEL_Y: ofs_start = ofs_stop = 1U; break; case SENSOR_CHAN_ACCEL_Z: ofs_start = ofs_stop = 2U; break; default: ofs_start = 0U; ofs_stop = 2U; break; } for (i = ofs_start; i <= ofs_stop ; i++) { lis2dw12_convert(pval++, lis2dw12->acc[i], lis2dw12->gain); } } static int lis2dw12_channel_get(const struct device *dev, enum sensor_channel chan, struct sensor_value *val) { switch (chan) { case SENSOR_CHAN_ACCEL_X: case SENSOR_CHAN_ACCEL_Y: case SENSOR_CHAN_ACCEL_Z: case SENSOR_CHAN_ACCEL_XYZ: lis2dw12_channel_get_acc(dev, chan, val); return 0; default: LOG_DBG("Channel not supported"); break; } return -ENOTSUP; } static int lis2dw12_config(const struct device *dev, enum sensor_channel chan, enum sensor_attribute attr, const struct sensor_value *val) { switch (attr) { case SENSOR_ATTR_FULL_SCALE: return lis2dw12_set_range(dev, LIS2DW12_FS_TO_REG(sensor_ms2_to_g(val))); case SENSOR_ATTR_SAMPLING_FREQUENCY: return lis2dw12_set_odr(dev, val->val1); default: LOG_DBG("Acc attribute not supported"); break; } return -ENOTSUP; } static inline int32_t sensor_ms2_to_mg(const struct sensor_value *ms2) { int64_t nano_ms2 = (ms2->val1 * 1000000LL + ms2->val2) * 1000LL; if (nano_ms2 > 0) { return (nano_ms2 + SENSOR_G / 2) / SENSOR_G; } else { return (nano_ms2 - SENSOR_G / 2) / SENSOR_G; } } #if CONFIG_LIS2DW12_THRESHOLD /* Converts a lis2dw12_fs_t range to its value in milli-g * Range can be 2/4/8/16G */ #define FS_RANGE_TO_MG(fs_range) ((2U << fs_range) * 1000U) /* Converts a range in mg to the lsb value for the WK_THS register * For the reg value: 1 LSB = 1/64 of FS * Range can be 2/4/8/16G */ #define MG_TO_WK_THS_LSB(range_mg) (range_mg / 64) /* Calculates the WK_THS reg value * from the threshold in mg and the lsb value in mg * with correct integer rounding */ #define THRESHOLD_MG_TO_WK_THS_REG(thr_mg, lsb_mg) \ ((thr_mg + (lsb_mg / 2)) / lsb_mg) static int lis2dw12_attr_set_thresh(const struct device *dev, enum sensor_channel chan, enum sensor_attribute attr, const struct sensor_value *val) { uint8_t reg; size_t ret; int lsb_mg; const struct lis2dw12_device_config *cfg = dev->config; stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx; LOG_DBG("%s on channel %d", __func__, chan); /* can only be set for all directions at once */ if (chan != SENSOR_CHAN_ACCEL_XYZ) { return -EINVAL; } /* Configure wakeup threshold threshold. */ lis2dw12_fs_t range; int err = lis2dw12_full_scale_get(ctx, &range); if (err) { return err; } uint32_t thr_mg = abs(sensor_ms2_to_mg(val)); /* Check maximum value: depends on current FS value */ if (thr_mg >= FS_RANGE_TO_MG(range)) { return -EINVAL; } /* The threshold is applied to both positive and negative data: * for a wake-up interrupt generation at least one of the three axes must be * bigger than the threshold. */ lsb_mg = MG_TO_WK_THS_LSB(FS_RANGE_TO_MG(range)); reg = THRESHOLD_MG_TO_WK_THS_REG(thr_mg, lsb_mg); LOG_DBG("Threshold %d mg -> fs: %u mg -> reg = %d LSBs", thr_mg, FS_RANGE_TO_MG(range), reg); ret = 0; return lis2dw12_wkup_threshold_set(ctx, reg); } #endif static int lis2dw12_attr_set(const struct device *dev, enum sensor_channel chan, enum sensor_attribute attr, const struct sensor_value *val) { #if CONFIG_LIS2DW12_THRESHOLD switch (attr) { case SENSOR_ATTR_UPPER_THRESH: case SENSOR_ATTR_LOWER_THRESH: return lis2dw12_attr_set_thresh(dev, chan, attr, val); default: /* Do nothing */ break; } #endif switch (chan) { case SENSOR_CHAN_ACCEL_X: case SENSOR_CHAN_ACCEL_Y: case SENSOR_CHAN_ACCEL_Z: case SENSOR_CHAN_ACCEL_XYZ: return lis2dw12_config(dev, chan, attr, val); default: LOG_DBG("Attr not supported on %d channel", chan); break; } return -ENOTSUP; } static int lis2dw12_sample_fetch(const struct device *dev, enum sensor_channel chan) { struct lis2dw12_data *lis2dw12 = dev->data; const struct lis2dw12_device_config *cfg = dev->config; stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx; uint8_t shift; int16_t buf[3]; /* fetch raw data sample */ if (lis2dw12_acceleration_raw_get(ctx, buf) < 0) { LOG_DBG("Failed to fetch raw data sample"); return -EIO; } /* adjust to resolution */ if (cfg->pm == LIS2DW12_CONT_LOW_PWR_12bit) { shift = LIS2DW12_SHIFT_PM1; } else { shift = LIS2DW12_SHIFT_PMOTHER; } lis2dw12->acc[0] = sys_le16_to_cpu(buf[0]) >> shift; lis2dw12->acc[1] = sys_le16_to_cpu(buf[1]) >> shift; lis2dw12->acc[2] = sys_le16_to_cpu(buf[2]) >> shift; return 0; } static const struct sensor_driver_api lis2dw12_driver_api = { .attr_set = lis2dw12_attr_set, #if CONFIG_LIS2DW12_TRIGGER .trigger_set = lis2dw12_trigger_set, #endif /* CONFIG_LIS2DW12_TRIGGER */ .sample_fetch = lis2dw12_sample_fetch, .channel_get = lis2dw12_channel_get, }; static int lis2dw12_set_power_mode(const struct device *dev, lis2dw12_mode_t pm) { const struct lis2dw12_device_config *cfg = dev->config; stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx; uint8_t regval = LIS2DW12_CONT_LOW_PWR_12bit; switch (pm) { case LIS2DW12_CONT_LOW_PWR_2: case LIS2DW12_CONT_LOW_PWR_3: case LIS2DW12_CONT_LOW_PWR_4: case LIS2DW12_HIGH_PERFORMANCE: regval = pm; break; default: LOG_DBG("Apply default Power Mode"); break; } return lis2dw12_write_reg(ctx, LIS2DW12_CTRL1, ®val, 1); } static int lis2dw12_set_low_noise(const struct device *dev, bool low_noise) { const struct lis2dw12_device_config *cfg = dev->config; stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx; lis2dw12_ctrl6_t ctrl6; int ret; ret = lis2dw12_read_reg(ctx, LIS2DW12_CTRL6, (uint8_t *)&ctrl6, 1); if (ret < 0) { return ret; } ctrl6.low_noise = low_noise; return lis2dw12_write_reg(ctx, LIS2DW12_CTRL6, (uint8_t *)&ctrl6, 1); } static int lis2dw12_init(const struct device *dev) { const struct lis2dw12_device_config *cfg = dev->config; stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx; uint8_t wai; int ret; /* check chip ID */ ret = lis2dw12_device_id_get(ctx, &wai); if (ret < 0) { LOG_ERR("Not able to read dev id"); return ret; } if (wai != LIS2DW12_ID) { LOG_ERR("Invalid chip ID"); return -EINVAL; } /* reset device */ ret = lis2dw12_reset_set(ctx, PROPERTY_ENABLE); if (ret < 0) { return ret; } k_busy_wait(100); ret = lis2dw12_block_data_update_set(ctx, PROPERTY_ENABLE); if (ret < 0) { LOG_ERR("Not able to set BDU"); return ret; } /* set power mode */ LOG_DBG("power-mode is %d", cfg->pm); ret = lis2dw12_set_power_mode(dev, cfg->pm); if (ret < 0) { return ret; } LOG_DBG("low noise is %d", cfg->low_noise); ret = lis2dw12_set_low_noise(dev, cfg->low_noise); if (ret < 0) { LOG_ERR("Failed to configure low_noise"); return ret; } /* set default odr to 12.5Hz acc */ ret = lis2dw12_set_odr(dev, 12); if (ret < 0) { LOG_ERR("odr init error (12.5 Hz)"); return ret; } LOG_DBG("range is %d", cfg->range); ret = lis2dw12_set_range(dev, LIS2DW12_FS_TO_REG(cfg->range)); if (ret < 0) { LOG_ERR("range init error %d", cfg->range); return ret; } LOG_DBG("bandwidth filter is %u", (int)cfg->bw_filt); lis2dw12_filter_bandwidth_set(ctx, cfg->bw_filt); #ifdef CONFIG_LIS2DW12_TRIGGER ret = lis2dw12_init_interrupt(dev); if (ret < 0) { LOG_ERR("Failed to initialize interrupts"); return ret; } #endif /* CONFIG_LIS2DW12_TRIGGER */ LOG_DBG("high pass reference mode is %d", (int)cfg->hp_ref_mode); ret = lis2dw12_reference_mode_set(ctx, cfg->hp_ref_mode); if (ret < 0) { LOG_ERR("high pass reference mode config error %d", (int)cfg->hp_ref_mode); return ret; } LOG_DBG("high pass filter path is %d", (int)cfg->hp_filter_path); lis2dw12_fds_t fds = cfg->hp_filter_path ? LIS2DW12_HIGH_PASS_ON_OUT : LIS2DW12_LPF_ON_OUT; ret = lis2dw12_filter_path_set(ctx, fds); if (ret < 0) { LOG_ERR("filter path config error %d", (int)cfg->hp_filter_path); return ret; } return 0; } #if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0 #warning "LIS2DW12 driver enabled without any devices" #endif /* * Device creation macro, shared by LIS2DW12_DEFINE_SPI() and * LIS2DW12_DEFINE_I2C(). */ #define LIS2DW12_DEVICE_INIT(inst) \ DEVICE_DT_INST_DEFINE(inst, \ lis2dw12_init, \ NULL, \ &lis2dw12_data_##inst, \ &lis2dw12_config_##inst, \ POST_KERNEL, \ CONFIG_SENSOR_INIT_PRIORITY, \ &lis2dw12_driver_api); /* * Instantiation macros used when a device is on a SPI bus. */ #ifdef CONFIG_LIS2DW12_TAP #define LIS2DW12_CONFIG_TAP(inst) \ .tap_mode = DT_INST_PROP(inst, tap_mode), \ .tap_threshold = DT_INST_PROP(inst, tap_threshold), \ .tap_shock = DT_INST_PROP(inst, tap_shock), \ .tap_latency = DT_INST_PROP(inst, tap_latency), \ .tap_quiet = DT_INST_PROP(inst, tap_quiet), #else #define LIS2DW12_CONFIG_TAP(inst) #endif /* CONFIG_LIS2DW12_TAP */ #ifdef CONFIG_LIS2DW12_TRIGGER #define LIS2DW12_CFG_IRQ(inst) \ .gpio_int = GPIO_DT_SPEC_INST_GET(inst, irq_gpios), \ .int_pin = DT_INST_PROP(inst, int_pin), #else #define LIS2DW12_CFG_IRQ(inst) #endif /* CONFIG_LIS2DW12_TRIGGER */ #define LIS2DW12_SPI_OPERATION (SPI_WORD_SET(8) | \ SPI_OP_MODE_MASTER | \ SPI_MODE_CPOL | \ SPI_MODE_CPHA) \ #define LIS2DW12_CONFIG_SPI(inst) \ { \ .ctx = { \ .read_reg = \ (stmdev_read_ptr) stmemsc_spi_read, \ .write_reg = \ (stmdev_write_ptr) stmemsc_spi_write, \ .handle = \ (void *)&lis2dw12_config_##inst.stmemsc_cfg, \ }, \ .stmemsc_cfg = { \ .spi = SPI_DT_SPEC_INST_GET(inst, \ LIS2DW12_SPI_OPERATION, \ 0), \ }, \ .pm = DT_INST_PROP(inst, power_mode), \ .range = DT_INST_PROP(inst, range), \ .bw_filt = DT_INST_PROP(inst, bw_filt), \ .low_noise = DT_INST_PROP(inst, low_noise), \ .hp_filter_path = DT_INST_PROP(inst, hp_filter_path), \ .hp_ref_mode = DT_INST_PROP(inst, hp_ref_mode), \ .drdy_pulsed = DT_INST_PROP(inst, drdy_pulsed), \ LIS2DW12_CONFIG_TAP(inst) \ COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, irq_gpios), \ (LIS2DW12_CFG_IRQ(inst)), ()) \ } /* * Instantiation macros used when a device is on an I2C bus. */ #define LIS2DW12_CONFIG_I2C(inst) \ { \ .ctx = { \ .read_reg = \ (stmdev_read_ptr) stmemsc_i2c_read, \ .write_reg = \ (stmdev_write_ptr) stmemsc_i2c_write, \ .handle = \ (void *)&lis2dw12_config_##inst.stmemsc_cfg, \ }, \ .stmemsc_cfg = { \ .i2c = I2C_DT_SPEC_INST_GET(inst), \ }, \ .pm = DT_INST_PROP(inst, power_mode), \ .range = DT_INST_PROP(inst, range), \ .bw_filt = DT_INST_PROP(inst, bw_filt), \ .low_noise = DT_INST_PROP(inst, low_noise), \ .hp_filter_path = DT_INST_PROP(inst, hp_filter_path), \ .hp_ref_mode = DT_INST_PROP(inst, hp_ref_mode), \ .drdy_pulsed = DT_INST_PROP(inst, drdy_pulsed), \ LIS2DW12_CONFIG_TAP(inst) \ COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, irq_gpios), \ (LIS2DW12_CFG_IRQ(inst)), ()) \ } /* * Main instantiation macro. Use of COND_CODE_1() selects the right * bus-specific macro at preprocessor time. */ #define LIS2DW12_DEFINE(inst) \ static struct lis2dw12_data lis2dw12_data_##inst; \ static const struct lis2dw12_device_config lis2dw12_config_##inst = \ COND_CODE_1(DT_INST_ON_BUS(inst, spi), \ (LIS2DW12_CONFIG_SPI(inst)), \ (LIS2DW12_CONFIG_I2C(inst))); \ LIS2DW12_DEVICE_INIT(inst) DT_INST_FOREACH_STATUS_OKAY(LIS2DW12_DEFINE) |