1 /* Bosch BMG160 gyro driver */ 2 3 /* 4 * Copyright (c) 2016 Intel Corporation 5 * 6 * SPDX-License-Identifier: Apache-2.0 7 */ 8 9 #ifndef ZEPHYR_DRIVERS_SENSOR_BMG160_BMG160_H_ 10 #define ZEPHYR_DRIVERS_SENSOR_BMG160_BMG160_H_ 11 12 #include <zephyr/drivers/i2c.h> 13 #include <zephyr/drivers/gpio.h> 14 #include <zephyr/sys/util.h> 15 16 /* registers */ 17 #define BMG160_REG_CHIPID 0x00 18 #define BMG160_REG_RATE_X 0x02 19 #define BMG160_REG_RATE_Y 0x04 20 #define BMG160_REG_RATE_Z 0x06 21 #define BMG160_REG_TEMP 0x08 22 #define BMG160_REG_INT_STATUS0 0x09 23 #define BMG160_REG_INT_STATUS1 0x0A 24 #define BMG160_REG_INT_STATUS2 0x0B 25 #define BMG160_REG_INT_STATUS3 0x0C 26 #define BMG160_REG_FIFO_STATUS 0x0E 27 #define BMG160_REG_RANGE 0x0F 28 #define BMG160_REG_BW 0x10 29 #define BMG160_REG_LPM1 0x11 30 #define BMG160_REG_LPM2 0x12 31 #define BMG160_REG_RATE_HBW 0x13 32 #define BMG160_REG_BGW_SOFTRESET 0x14 33 #define BMG160_REG_INT_EN0 0x15 34 #define BMG160_REG_INT_EN1 0x16 35 #define BMG160_REG_INT_MAP0 0x17 36 #define BMG160_REG_INT_MAP1 0x18 37 #define BMG160_REG_INT_MAP2 0x19 38 #define BMG160_REG_FILTER 0x1A 39 #define BMG160_REG_THRES 0x1B 40 #define BMG160_REG_ANY_EN 0x1C 41 #define BMG160_REG_FIFO_WM 0x1E 42 #define BMG160_REG_INT_RST_LATCH 0x21 43 #define BMG160_REG_HIGH_TH_X 0x22 44 #define BMG160_REG_HIGH_DUR_X 0x23 45 #define BMG160_REG_HIGH_TH_Y 0x24 46 #define BMG160_REG_HIGH_DUR_Y 0x25 47 #define BMG160_REG_HIGH_TH_Z 0x26 48 #define BMG160_REG_HIGH_DUR_Z 0x27 49 #define BMG160_REG_SOC 0x31 50 #define BMG160_REG_A_FOC 0x32 51 #define BMG160_REG_TRIM_NVM_CTRL 0x33 52 #define BMG160_REG_BGW_SPI3_WDT 0x34 53 #define BMG160_REG_OFC1 0x36 54 #define BMG160_REG_OFC2 0x37 55 #define BMG160_REG_OFC3 0x38 56 #define BMG160_REG_OFC4 0x39 57 #define BMG160_REG_TRIM_GP0 0x3A 58 #define BMG160_REG_TRIM_GP1 0x3B 59 #define BMG160_REG_TRIM_BIST 0x3C 60 #define BMG160_REG_TRIM_FIFO_CONFIG0 0x3D 61 #define BMG160_REG_TRIM_FIFO_CONFIG1 0x3E 62 #define BMG160_REG_TRIM_FIFO_DATA 0x3F 63 64 /* bitfields */ 65 66 /* BMG160_REG_INT_STATUS0 */ 67 #define BMG160_HIGH_INT BIT(1) 68 #define BMG160_ANY_INT BIT(2) 69 70 /* BMG160_REG_INT_STATUS1 */ 71 #define BMG160_FIFO_INT BIT(4) 72 #define BMG160_FAST_OFFSET_INT BIT(5) 73 #define BMG160_AUTO_OFFSET_INT BIT(6) 74 #define BMG160_DATA_INT BIT(7) 75 76 /* BMG160_REG_INT_STATUS2 */ 77 #define BMG160_ANY_FIRST_X BIT(0) 78 #define BMG160_ANY_FIRST_Y BIT(1) 79 #define BMG160_ANY_FIRST_Z BIT(2) 80 #define BMG160_ANY_SIGN BIT(3) 81 82 /* BMG160_REG_INT_STATUS3 */ 83 #define BMG160_HIGH_FIRST_X BIT(0) 84 #define BMG160_HIGH_FIRST_Y BIT(1) 85 #define BMG160_HIGH_FIRST_Z BIT(2) 86 #define BMG160_HIGH_SIGN BIT(3) 87 88 /* BMG160_REG_FIFO_STATUS */ 89 #define BMG160_FIFO_FRAME_COUNTER_MASK 0x7F 90 #define BMG160_FIFO_OVERRUN BIT(7) 91 92 /* BMG160_REG_INT_EN_0 */ 93 #define BMG160_AUTO_OFFSET_EN BIT(2) 94 #define BMG160_FIFO_EN BIT(6) 95 #define BMG160_DATA_EN BIT(7) 96 97 /* BMG160_REG_INT_EN_1 */ 98 #define BMG160_INT1_LVL BIT(0) 99 #define BMG160_INT1_OD BIT(1) 100 #define BMG160_INT2_LVL BIT(2) 101 #define BMG160_INT2_OD BIT(3) 102 103 /* BMG160_REG_INT_MAP0 */ 104 #define BMG160_INT1_ANY BIT(1) 105 #define BMG160_INT1_HIGH BIT(3) 106 107 /* BMG160_REG_INT_MAP1 */ 108 #define BMG160_INT1_DATA BIT(0) 109 #define BMG160_INT1_FAST_OFFSET BIT(1) 110 #define BMG160_INT1_FIFO BIT(2) 111 #define BMG160_INT1_AUTO_OFFSET BIT(3) 112 #define BMG160_INT2_AUTO_OFFSET BIT(4) 113 #define BMG160_INT2_FIFO BIT(5) 114 #define BMG160_INT2_FAST_OFFSET BIT(6) 115 #define BMG160_INT2_DATA BIT(7) 116 117 /* BMG160_REG_ANY_EN */ 118 #define BMG160_AWAKE_DUR_POS 6 119 #define BMG160_AWAKE_DUR_MASK (0x3 << 6) 120 #define BMG160_ANY_DURSAMPLE_POS 4 121 #define BMG160_ANY_DURSAMPLE_MASK (0x3 << 4) 122 #define BMG160_ANY_EN_Z BIT(2) 123 #define BMG160_ANY_EN_Y BIT(1) 124 #define BMG160_ANY_EN_X BIT(0) 125 #define BMG160_ANY_EN_MASK 0x7 126 127 /* BMG160_REG_INT_RST_LATCH */ 128 #define BMG160_RESET_INT BIT(7) 129 #define BMG160_OFFSET_RESET BIT(6) 130 #define BMG160_LATCH_STATUS_BIT BIT(4) 131 #define BMG160_LATCH_INT_MASK 0x0F 132 133 /* BMG160_REG_THRES */ 134 #define BMG160_THRES_MASK 0x7F 135 136 /* other */ 137 #define BMG160_CHIP_ID 0x0F 138 #define BMG160_RESET 0xB6 139 140 #define BMG160_RANGE_TO_SCALE(range_dps) \ 141 ((2 * range_dps * SENSOR_PI) / 180LL / 65536LL) 142 #define BMG160_SCALE_TO_RANGE(scale) \ 143 (((scale * 90LL * 65536LL) + SENSOR_PI / 2) / SENSOR_PI) 144 145 /* default settings, based on menuconfig options */ 146 #if defined(CONFIG_BMG160_RANGE_RUNTIME) ||\ 147 defined(CONFIG_BMG160_RANGE_2000DPS) 148 # define BMG160_DEFAULT_RANGE 0 149 #elif defined(CONFIG_BMG160_RANGE_1000DPS) 150 # define BMG160_DEFAULT_RANGE 1 151 #elif defined(CONFIG_BMG160_RANGE_500DPS) 152 # define BMG160_DEFAULT_RANGE 2 153 #elif defined(CONFIG_BMG160_RANGE_250DPS) 154 # define BMG160_DEFAULT_RANGE 3 155 #else 156 # define BMG160_DEFAULT_RANGE 4 157 #endif 158 159 #if defined(CONFIG_BMG160_ODR_RUNTIME) ||\ 160 defined(CONFIG_BMG160_ODR_100) 161 # define BMG160_DEFAULT_ODR 5 162 #elif defined(CONFIG_BMG160_ODR_200) 163 # define BMG160_DEFAULT_ODR 4 164 #elif defined(CONFIG_BMG160_ODR_400) 165 # define BMG160_DEFAULT_ODR 3 166 #elif defined(CONFIG_BMG160_ODR_1000) 167 # define BMG160_DEFAULT_ODR 2 168 #else 169 # define BMG160_DEFAULT_ODR 1 170 #endif 171 172 #if defined(CONFIG_BMG160_I2C_SPEED_STANDARD) 173 #define BMG160_BUS_SPEED I2C_SPEED_STANDARD 174 #elif defined(CONFIG_BMG160_I2C_SPEED_FAST) 175 #define BMG160_BUS_SPEED I2C_SPEED_FAST 176 #endif 177 178 /* end of default settings */ 179 180 struct bmg160_device_config { 181 struct i2c_dt_spec i2c; 182 #ifdef CONFIG_BMG160_TRIGGER 183 struct gpio_dt_spec int_gpio; 184 #endif 185 }; 186 187 struct bmg160_device_data { 188 #ifdef CONFIG_BMG160_TRIGGER 189 const struct device *dev; 190 struct gpio_callback gpio_cb; 191 #endif 192 #ifdef CONFIG_BMG160_TRIGGER_OWN_THREAD 193 struct k_sem trig_sem; 194 #endif 195 struct k_sem sem; 196 #ifdef CONFIG_BMG160_TRIGGER_GLOBAL_THREAD 197 struct k_work work; 198 #endif 199 #ifdef CONFIG_BMG160_TRIGGER 200 sensor_trigger_handler_t anymotion_handler; 201 const struct sensor_trigger *anymotion_trig; 202 sensor_trigger_handler_t drdy_handler; 203 const struct sensor_trigger *drdy_trig; 204 #endif 205 int16_t raw_gyro_xyz[3]; 206 uint16_t scale; 207 uint8_t range_idx; 208 209 int8_t raw_temp; 210 }; 211 212 int bmg160_trigger_init(const struct device *dev); 213 int bmg160_trigger_set(const struct device *dev, 214 const struct sensor_trigger *trig, 215 sensor_trigger_handler_t handler); 216 int bmg160_read(const struct device *dev, uint8_t reg_addr, uint8_t *data, 217 uint8_t len); 218 int bmg160_read_byte(const struct device *dev, uint8_t reg_addr, 219 uint8_t *byte); 220 int bmg160_update_byte(const struct device *dev, uint8_t reg_addr, 221 uint8_t mask, 222 uint8_t value); 223 int bmg160_write_byte(const struct device *dev, uint8_t reg_addr, 224 uint8_t data); 225 int bmg160_slope_config(const struct device *dev, enum sensor_attribute attr, 226 const struct sensor_value *val); 227 228 #endif /* ZEPHYR_DRIVERS_SENSOR_BMG160_BMG160_H_ */ 229