1 /* ST Microelectronics LIS2DW12 3-axis accelerometer driver 2 * 3 * Copyright (c) 2019 STMicroelectronics 4 * 5 * SPDX-License-Identifier: Apache-2.0 6 * 7 * Datasheet: 8 * https://www.st.com/resource/en/datasheet/lis2dw12.pdf 9 */ 10 11 #ifndef ZEPHYR_DRIVERS_SENSOR_LIS2DW12_LIS2DW12_H_ 12 #define ZEPHYR_DRIVERS_SENSOR_LIS2DW12_LIS2DW12_H_ 13 14 #include <zephyr/drivers/spi.h> 15 #include <zephyr/drivers/gpio.h> 16 #include <zephyr/sys/util.h> 17 #include <zephyr/drivers/sensor.h> 18 #include <stmemsc.h> 19 #include "lis2dw12_reg.h" 20 21 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) 22 #include <zephyr/drivers/spi.h> 23 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */ 24 25 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) 26 #include <zephyr/drivers/i2c.h> 27 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */ 28 29 /* Return ODR reg value based on data rate set */ 30 #define LIS2DW12_ODR_TO_REG(_odr) \ 31 ((_odr <= 1) ? LIS2DW12_XL_ODR_1Hz6_LP_ONLY : \ 32 (_odr <= 12) ? LIS2DW12_XL_ODR_12Hz5 : \ 33 ((31 - __builtin_clz(_odr / 25))) + 3) 34 35 /* Return data rate in Hz for given register value */ 36 #define LIS2DW12_REG_TO_ODR(_reg) \ 37 ((_reg == 0) ? 0 : \ 38 (_reg == 1) ? 1 : \ 39 (_reg == 2) ? 12 : \ 40 (_reg > 9) ? 1600 : \ 41 (1 << (_reg - 3)) * 25) 42 43 /* FS reg value from Full Scale */ 44 #define LIS2DW12_FS_TO_REG(_fs) (30 - __builtin_clz(_fs)) 45 46 /* Acc Gain value in ug/LSB in High Perf mode */ 47 #define LIS2DW12_FS_2G_GAIN 244 48 #define LIS2DW12_FS_4G_GAIN 488 49 #define LIS2DW12_FS_8G_GAIN 976 50 #define LIS2DW12_FS_16G_GAIN 1952 51 52 #define LIS2DW12_SHFT_GAIN_NOLP1 2 53 #define LIS2DW12_ACCEL_GAIN_DEFAULT_VAL LIS2DW12_FS_2G_GAIN 54 #define LIS2DW12_FS_TO_GAIN(_fs, _lp1) \ 55 (LIS2DW12_FS_2G_GAIN << ((_fs) + (_lp1))) 56 57 /* shift value for power mode */ 58 #define LIS2DW12_SHIFT_PM1 4 59 #define LIS2DW12_SHIFT_PMOTHER 2 60 61 /** 62 * struct lis2dw12_device_config - lis2dw12 hw configuration 63 * @bus_name: Pointer to bus master identifier. 64 * @pm: Power mode (lis2dh_powermode). 65 * @int_pin: Sensor int pin (int1/int2). 66 */ 67 struct lis2dw12_device_config { 68 stmdev_ctx_t ctx; 69 union { 70 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) 71 const struct i2c_dt_spec i2c; 72 #endif 73 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) 74 const struct spi_dt_spec spi; 75 #endif 76 } stmemsc_cfg; 77 lis2dw12_mode_t pm; 78 uint16_t odr; 79 uint8_t range; 80 uint8_t bw_filt; 81 bool low_noise; 82 bool hp_filter_path; 83 bool hp_ref_mode; 84 bool drdy_pulsed; 85 #ifdef CONFIG_LIS2DW12_TRIGGER 86 struct gpio_dt_spec gpio_int; 87 uint8_t int_pin; 88 #ifdef CONFIG_LIS2DW12_TAP 89 uint8_t tap_mode; 90 uint8_t tap_threshold[3]; 91 uint8_t tap_shock; 92 uint8_t tap_latency; 93 uint8_t tap_quiet; 94 #endif /* CONFIG_LIS2DW12_TAP */ 95 #ifdef CONFIG_LIS2DW12_FREEFALL 96 uint8_t freefall_duration; 97 uint8_t freefall_threshold; 98 #endif /* CONFIG_LIS2DW12_FREEFALL */ 99 #ifdef CONFIG_LIS2DW12_THRESHOLD 100 uint8_t wakeup_duration; 101 #endif /* CONFIG_LIS2DW12_THRESHOLD */ 102 #endif /* CONFIG_LIS2DW12_TRIGGER */ 103 }; 104 105 /* sensor data */ 106 struct lis2dw12_data { 107 int16_t acc[3]; 108 109 /* save sensitivity */ 110 uint16_t gain; 111 /* output data rate */ 112 uint16_t odr; 113 114 #ifdef CONFIG_LIS2DW12_TRIGGER 115 const struct device *dev; 116 117 struct gpio_callback gpio_cb; 118 sensor_trigger_handler_t drdy_handler; 119 const struct sensor_trigger *drdy_trig; 120 #ifdef CONFIG_LIS2DW12_TAP 121 sensor_trigger_handler_t tap_handler; 122 const struct sensor_trigger *tap_trig; 123 sensor_trigger_handler_t double_tap_handler; 124 const struct sensor_trigger *double_tap_trig; 125 #endif /* CONFIG_LIS2DW12_TAP */ 126 #ifdef CONFIG_LIS2DW12_THRESHOLD 127 sensor_trigger_handler_t threshold_handler; 128 const struct sensor_trigger *threshold_trig; 129 #endif /* CONFIG_LIS2DW12_THRESHOLD */ 130 #ifdef CONFIG_LIS2DW12_FREEFALL 131 sensor_trigger_handler_t freefall_handler; 132 const struct sensor_trigger *freefall_trig; 133 #endif /* CONFIG_LIS2DW12_FREEFALL */ 134 #if defined(CONFIG_LIS2DW12_TRIGGER_OWN_THREAD) 135 K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_LIS2DW12_THREAD_STACK_SIZE); 136 struct k_thread thread; 137 struct k_sem gpio_sem; 138 #elif defined(CONFIG_LIS2DW12_TRIGGER_GLOBAL_THREAD) 139 struct k_work work; 140 #endif /* CONFIG_LIS2DW12_TRIGGER_GLOBAL_THREAD */ 141 #endif /* CONFIG_LIS2DW12_TRIGGER */ 142 }; 143 144 #ifdef CONFIG_LIS2DW12_TRIGGER 145 int lis2dw12_init_interrupt(const struct device *dev); 146 int lis2dw12_trigger_set(const struct device *dev, 147 const struct sensor_trigger *trig, 148 sensor_trigger_handler_t handler); 149 #endif /* CONFIG_LIS2DW12_TRIGGER */ 150 151 #endif /* ZEPHYR_DRIVERS_SENSOR_LIS2DW12_LIS2DW12_H_ */ 152