1 /* Bosch BMP388 pressure sensor 2 * 3 * Copyright (c) 2020 Facebook, Inc. and its affiliates 4 * 5 * SPDX-License-Identifier: Apache-2.0 6 * 7 * Datasheet: 8 * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp388-ds001.pdf 9 */ 10 11 #ifndef ZEPHYR_BMP388_H 12 #define ZEPHYR_BMP388_H 13 14 #include <zephyr/device.h> 15 #include <zephyr/devicetree.h> 16 #include <zephyr/drivers/spi.h> 17 #include <zephyr/drivers/i2c.h> 18 #include <zephyr/drivers/gpio.h> 19 #include <zephyr/drivers/sensor.h> 20 #include <zephyr/sys/util.h> 21 22 #define DT_DRV_COMPAT bosch_bmp388 23 #define BMP388_BUS_SPI DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) 24 #define BMP388_BUS_I2C DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) 25 #undef DT_DRV_COMPAT 26 27 #define DT_DRV_COMPAT bosch_bmp390 28 #define BMP390_BUS_SPI DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) 29 #define BMP390_BUS_I2C DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) 30 #undef DT_DRV_COMPAT 31 32 #if defined(BMP388_BUS_SPI) || defined(BMP390_BUS_SPI) 33 #define BMP3XX_USE_SPI_BUS 34 #endif 35 36 #if defined(BMP388_BUS_I2C) || defined(BMP390_BUS_I2C) 37 #define BMP3XX_USE_I2C_BUS 38 #endif 39 40 union bmp388_bus { 41 #ifdef BMP3XX_USE_SPI_BUS 42 struct spi_dt_spec spi; 43 #endif 44 #ifdef BMP3XX_USE_I2C_BUS 45 struct i2c_dt_spec i2c; 46 #endif 47 }; 48 49 typedef int (*bmp388_bus_check_fn)(const union bmp388_bus *bus); 50 typedef int (*bmp388_reg_read_fn)(const union bmp388_bus *bus, 51 uint8_t start, uint8_t *buf, int size); 52 typedef int (*bmp388_reg_write_fn)(const union bmp388_bus *bus, 53 uint8_t reg, uint8_t val); 54 55 struct bmp388_bus_io { 56 bmp388_bus_check_fn check; 57 bmp388_reg_read_fn read; 58 bmp388_reg_write_fn write; 59 }; 60 61 #ifdef BMP3XX_USE_SPI_BUS 62 #define BMP388_SPI_OPERATION (SPI_WORD_SET(8) | SPI_TRANSFER_MSB | \ 63 SPI_MODE_CPOL | SPI_MODE_CPHA) 64 extern const struct bmp388_bus_io bmp388_bus_io_spi; 65 #endif 66 67 #ifdef BMP3XX_USE_I2C_BUS 68 extern const struct bmp388_bus_io bmp388_bus_io_i2c; 69 #endif 70 71 /* registers */ 72 #define BMP388_REG_CHIPID 0x00 73 #define BMP388_REG_ERR_REG 0x02 74 #define BMP388_REG_STATUS 0x03 75 #define BMP388_REG_DATA0 0x04 76 #define BMP388_REG_DATA1 0x05 77 #define BMP388_REG_DATA2 0x06 78 #define BMP388_REG_DATA3 0x07 79 #define BMP388_REG_DATA4 0x08 80 #define BMP388_REG_DATA5 0x09 81 #define BMP388_REG_SENSORTIME0 0x0C 82 #define BMP388_REG_SENSORTIME1 0x0D 83 #define BMP388_REG_SENSORTIME2 0x0E 84 #define BMP388_REG_SENSORTIME3 0x0F 85 #define BMP388_REG_EVENT 0x10 86 #define BMP388_REG_INT_STATUS 0x11 87 #define BMP388_REG_FIFO_LENGTH0 0x12 88 #define BMP388_REG_FIFO_LENGTH1 0x13 89 #define BMP388_REG_FIFO_DATA 0x14 90 #define BMP388_REG_FIFO_WTM0 0x15 91 #define BMP388_REG_FIFO_WTM1 0x16 92 #define BMP388_REG_FIFO_CONFIG1 0x17 93 #define BMP388_REG_FIFO_CONFIG2 0x18 94 #define BMP388_REG_INT_CTRL 0x19 95 #define BMP388_REG_IF_CONF 0x1A 96 #define BMP388_REG_PWR_CTRL 0x1B 97 #define BMP388_REG_OSR 0x1C 98 #define BMP388_REG_ODR 0x1D 99 #define BMP388_REG_CONFIG 0x1F 100 #define BMP388_REG_CALIB0 0x31 101 #define BMP388_REG_CMD 0x7E 102 103 /* BMP388_REG_CHIPID */ 104 #define BMP388_CHIP_ID 0x50 105 106 /* BMP388_REG_STATUS */ 107 #define BMP388_STATUS_FATAL_ERR BIT(0) 108 #define BMP388_STATUS_CMD_ERR BIT(1) 109 #define BMP388_STATUS_CONF_ERR BIT(2) 110 #define BMP388_STATUS_CMD_RDY BIT(4) 111 #define BMP388_STATUS_DRDY_PRESS BIT(5) 112 #define BMP388_STATUS_DRDY_TEMP BIT(6) 113 114 /* BMP388_REG_INT_CTRL */ 115 #define BMP388_INT_CTRL_DRDY_EN_POS 6 116 #define BMP388_INT_CTRL_DRDY_EN_MASK BIT(6) 117 118 /* BMP388_REG_PWR_CTRL */ 119 #define BMP388_PWR_CTRL_PRESS_EN BIT(0) 120 #define BMP388_PWR_CTRL_TEMP_EN BIT(1) 121 #define BMP388_PWR_CTRL_MODE_POS 4 122 #define BMP388_PWR_CTRL_MODE_MASK (0x03 << BMP388_PWR_CTRL_MODE_POS) 123 #define BMP388_PWR_CTRL_MODE_SLEEP (0x00 << BMP388_PWR_CTRL_MODE_POS) 124 #define BMP388_PWR_CTRL_MODE_FORCED (0x01 << BMP388_PWR_CTRL_MODE_POS) 125 #define BMP388_PWR_CTRL_MODE_NORMAL (0x03 << BMP388_PWR_CTRL_MODE_POS) 126 127 /* BMP388_REG_OSR */ 128 #define BMP388_ODR_POS 0 129 #define BMP388_ODR_MASK 0x1F 130 131 /* BMP388_REG_ODR */ 132 #define BMP388_OSR_PRESSURE_POS 0 133 #define BMP388_OSR_PRESSURE_MASK (0x07 << BMP388_OSR_PRESSURE_POS) 134 #define BMP388_OSR_TEMP_POS 3 135 #define BMP388_OSR_TEMP_MASK (0x07 << BMP388_OSR_TEMP_POS) 136 137 /* BMP388_REG_CONFIG */ 138 #define BMP388_IIR_FILTER_POS 1 139 #define BMP388_IIR_FILTER_MASK (0x7 << BMP388_IIR_FILTER_POS) 140 141 /* BMP388_REG_CMD */ 142 #define BMP388_CMD_FIFO_FLUSH 0xB0 143 #define BMP388_CMD_SOFT_RESET 0xB6 144 145 /* default PWR_CTRL settings */ 146 #define BMP388_PWR_CTRL_ON \ 147 (BMP388_PWR_CTRL_PRESS_EN | \ 148 BMP388_PWR_CTRL_TEMP_EN | \ 149 BMP388_PWR_CTRL_MODE_NORMAL) 150 #define BMP388_PWR_CTRL_OFF 0 151 152 #define BMP388_SAMPLE_BUFFER_SIZE (6) 153 154 struct bmp388_cal_data { 155 uint16_t t1; 156 uint16_t t2; 157 int8_t t3; 158 int16_t p1; 159 int16_t p2; 160 int8_t p3; 161 int8_t p4; 162 uint16_t p5; 163 uint16_t p6; 164 int8_t p7; 165 int8_t p8; 166 int16_t p9; 167 int8_t p10; 168 int8_t p11; 169 } __packed; 170 171 struct bmp388_sample { 172 uint32_t press; 173 uint32_t raw_temp; 174 int64_t comp_temp; 175 }; 176 177 struct bmp388_config { 178 union bmp388_bus bus; 179 const struct bmp388_bus_io *bus_io; 180 181 #ifdef CONFIG_BMP388_TRIGGER 182 struct gpio_dt_spec gpio_int; 183 #endif 184 185 uint8_t iir_filter; 186 }; 187 188 struct bmp388_data { 189 uint8_t odr; 190 uint8_t osr_pressure; 191 uint8_t osr_temp; 192 uint8_t chip_id; 193 struct bmp388_cal_data cal; 194 195 #if defined(CONFIG_BMP388_TRIGGER) 196 struct gpio_callback gpio_cb; 197 #endif 198 199 struct bmp388_sample sample; 200 201 #ifdef CONFIG_BMP388_TRIGGER_OWN_THREAD 202 struct k_sem sem; 203 #endif 204 205 #ifdef CONFIG_BMP388_TRIGGER_GLOBAL_THREAD 206 struct k_work work; 207 #endif 208 209 #if defined(CONFIG_BMP388_TRIGGER_GLOBAL_THREAD) || \ 210 defined(CONFIG_BMP388_TRIGGER_DIRECT) 211 const struct device *dev; 212 #endif 213 214 #ifdef CONFIG_BMP388_TRIGGER 215 sensor_trigger_handler_t handler_drdy; 216 const struct sensor_trigger *trig_drdy; 217 #endif /* CONFIG_BMP388_TRIGGER */ 218 }; 219 220 int bmp388_trigger_mode_init(const struct device *dev); 221 int bmp388_trigger_set(const struct device *dev, 222 const struct sensor_trigger *trig, 223 sensor_trigger_handler_t handler); 224 int bmp388_reg_field_update(const struct device *dev, 225 uint8_t reg, 226 uint8_t mask, 227 uint8_t val); 228 229 #endif /* ZEPHYR_BMP388_H */ 230