driver: sensor: bmi270 Added support

Added driver support for the BMI270 IMU

Signed-off-by: Bosch Sensortec <github@bosch-sensortec.com>
This commit is contained in:
Bosch Sensortec 2021-01-25 14:09:23 +01:00 committed by Anas Nashif
commit aabbc52351
7 changed files with 990 additions and 0 deletions

View file

@ -14,6 +14,7 @@ add_subdirectory_ifdef(CONFIG_BME280 bme280)
add_subdirectory_ifdef(CONFIG_BME680 bme680)
add_subdirectory_ifdef(CONFIG_BMG160 bmg160)
add_subdirectory_ifdef(CONFIG_BMI160 bmi160)
add_subdirectory_ifdef(CONFIG_BMI270 bmi270)
add_subdirectory_ifdef(CONFIG_BMM150 bmm150)
add_subdirectory_ifdef(CONFIG_BQ274XX bq274xx)
add_subdirectory_ifdef(CONFIG_CCS811 ccs811)

View file

@ -66,6 +66,8 @@ source "drivers/sensor/bmg160/Kconfig"
source "drivers/sensor/bmi160/Kconfig"
source "drivers/sensor/bmi270/Kconfig"
source "drivers/sensor/bmm150/Kconfig"
source "drivers/sensor/bq274xx/Kconfig"

View file

@ -0,0 +1,9 @@
#
# Copyright (c) 2021 Bosch Sensortec GmbH
#
# SPDX-License-Identifier: Apache-2.0
#
zephyr_library()
zephyr_library_sources_ifdef(CONFIG_BMI270 bmi270.c)

View file

@ -0,0 +1,10 @@
# BMI270 6 Axis IMU configuration
# Copyright (c) 2021 Bosch Sensortec GmbH
# SPDX-License-Identifier: Apache-2.0
config BMI270
bool "BMI270 Inertial measurement unit"
depends on I2C
help
Enable driver for BMI270 I2C-based imu sensor

View file

@ -0,0 +1,709 @@
/*
* Copyright (c) 2021 Bosch Sensortec GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT bosch_bmi270
#include <drivers/i2c.h>
#include <drivers/sensor.h>
#include <init.h>
#include <sys/__assert.h>
#include <sys/byteorder.h>
#include <logging/log.h>
#include "bmi270.h"
#include "bmi270_config_file.h"
LOG_MODULE_REGISTER(bmi270, CONFIG_SENSOR_LOG_LEVEL);
#define BMI270_WR_LEN 256
#define BMI270_CONFIG_FILE_RETRIES 15
#define BMI270_CONFIG_FILE_POLL_PERIOD_US 10000
#define BMI270_INTER_WRITE_DELAY_US 450
static int reg_read(uint8_t reg, uint8_t *data, uint16_t length,
struct bmi270_data *dev)
{
return i2c_burst_read(dev->i2c, dev->i2c_addr, reg, data, length);
}
static int reg_write(uint8_t reg, const uint8_t *data, uint16_t length,
struct bmi270_data *dev)
{
return i2c_burst_write(dev->i2c, dev->i2c_addr, reg, data, length);
}
static void channel_accel_convert(struct sensor_value *val, int64_t raw_val,
uint8_t range)
{
/* 16 bit accelerometer. 2^15 bits represent the range in G */
/* Converting from G to m/s^2 */
raw_val = (raw_val * SENSOR_G * (int64_t) range) / INT16_MAX;
val->val1 = raw_val / 1000000LL;
val->val2 = raw_val % 1000000LL;
/* Normalize val to make sure val->val2 is positive */
if (val->val2 < 0) {
val->val1 -= 1LL;
val->val2 += 1000000LL;
}
}
static void channel_gyro_convert(struct sensor_value *val, int64_t raw_val,
uint16_t range)
{
/* 16 bit gyroscope. 2^15 bits represent the range in degrees/s */
/* Converting from degrees/s to radians/s */
val->val1 = ((raw_val * (int64_t) range * SENSOR_PI)
/ (180LL * INT16_MAX)) / 1000000LL;
val->val2 = ((raw_val * (int64_t) range * SENSOR_PI)
/ (180LL * INT16_MAX)) % 1000000LL;
/* Normalize val to make sure val->val2 is positive */
if (val->val2 < 0) {
val->val1 -= 1LL;
val->val2 += 1000000LL;
}
}
static uint8_t acc_odr_to_reg(const struct sensor_value *val)
{
double odr = sensor_value_to_double((struct sensor_value *) val);
uint8_t reg = 0;
if ((odr >= 0.78125f) && (odr < 1.5625f)) {
reg = BMI270_ACC_ODR_25D32_HZ;
} else if ((odr >= 1.5625f) && (odr < 3.125f)) {
reg = BMI270_ACC_ODR_25D16_HZ;
} else if ((odr >= 3.125f) && (odr < 6.25f)) {
reg = BMI270_ACC_ODR_25D8_HZ;
} else if ((odr >= 6.25f) && (odr < 12.5f)) {
reg = BMI270_ACC_ODR_25D4_HZ;
} else if ((odr >= 12.5f) && (odr < 25.0f)) {
reg = BMI270_ACC_ODR_25D2_HZ;
} else if ((odr >= 25.0f) && (odr < 50.0f)) {
reg = BMI270_ACC_ODR_25_HZ;
} else if ((odr >= 50.0f) && (odr < 100.0f)) {
reg = BMI270_ACC_ODR_50_HZ;
} else if ((odr >= 100.0f) && (odr < 200.0f)) {
reg = BMI270_ACC_ODR_100_HZ;
} else if ((odr >= 200.0f) && (odr < 400.0f)) {
reg = BMI270_ACC_ODR_200_HZ;
} else if ((odr >= 400.0f) && (odr < 800.0f)) {
reg = BMI270_ACC_ODR_400_HZ;
} else if ((odr >= 800.0f) && (odr < 1600.0f)) {
reg = BMI270_ACC_ODR_800_HZ;
} else if (odr >= 1600.0f) {
reg = BMI270_ACC_ODR_1600_HZ;
}
return reg;
}
static int set_accel_odr_osr(const struct sensor_value *odr,
const struct sensor_value *osr, struct bmi270_data *dev)
{
uint8_t acc_conf, odr_bits, pwr_ctrl, osr_bits;
int ret = 0;
if (odr || osr) {
ret = reg_read(BMI270_REG_ACC_CONF, &acc_conf, 1, dev);
if (ret != 0) {
return ret;
}
ret = reg_read(BMI270_REG_PWR_CTRL, &pwr_ctrl, 1, dev);
if (ret != 0) {
return ret;
}
}
if (odr) {
odr_bits = acc_odr_to_reg(odr);
acc_conf = BMI270_SET_BITS_POS_0(acc_conf, BMI270_ACC_ODR,
odr_bits);
/* If odr_bits is 0, implies that the sampling frequency is 0Hz
* or invalid too.
*/
if (odr_bits) {
pwr_ctrl |= BMI270_PWR_CTRL_ACC_EN;
} else {
pwr_ctrl &= ~BMI270_PWR_CTRL_ACC_EN;
}
/* If the Sampling frequency (odr) >= 100Hz, enter performance
* mode else, power optimized. This also has a consequence
* for the OSR
*/
if (odr_bits >= BMI270_ACC_ODR_100_HZ) {
acc_conf = BMI270_SET_BITS(acc_conf, BMI270_ACC_FILT,
BMI270_ACC_FILT_PERF_OPT);
} else {
acc_conf = BMI270_SET_BITS(acc_conf, BMI270_ACC_FILT,
BMI270_ACC_FILT_PWR_OPT);
}
dev->acc_odr = odr_bits;
}
if (osr) {
if (dev->acc_odr >= BMI270_ACC_ODR_100_HZ) {
/* Performance mode */
/* osr->val2 should be unused */
switch (osr->val1) {
case 4:
osr_bits = BMI270_ACC_BWP_OSR4_AVG1;
break;
case 2:
osr_bits = BMI270_ACC_BWP_OSR2_AVG2;
break;
case 1:
osr_bits = BMI270_ACC_BWP_NORM_AVG4;
break;
default:
osr_bits = BMI270_ACC_BWP_CIC_AVG8;
break;
}
} else {
/* Power optimized mode */
/* osr->val2 should be unused */
switch (osr->val1) {
case 1:
osr_bits = BMI270_ACC_BWP_OSR4_AVG1;
break;
case 2:
osr_bits = BMI270_ACC_BWP_OSR2_AVG2;
break;
case 4:
osr_bits = BMI270_ACC_BWP_NORM_AVG4;
break;
case 8:
osr_bits = BMI270_ACC_BWP_CIC_AVG8;
break;
case 16:
osr_bits = BMI270_ACC_BWP_RES_AVG16;
break;
case 32:
osr_bits = BMI270_ACC_BWP_RES_AVG32;
break;
case 64:
osr_bits = BMI270_ACC_BWP_RES_AVG64;
break;
case 128:
osr_bits = BMI270_ACC_BWP_RES_AVG128;
break;
default:
return -ENOTSUP;
}
}
acc_conf = BMI270_SET_BITS(acc_conf, BMI270_ACC_BWP,
osr_bits);
}
if (odr || osr) {
ret = reg_write(BMI270_REG_ACC_CONF, &acc_conf, 1, dev);
if (ret != 0) {
return ret;
}
/* Assuming we have advance power save enabled */
k_usleep(BMI270_TRANSC_DELAY_SUSPEND);
pwr_ctrl &= BMI270_PWR_CTRL_MSK;
ret = reg_write(BMI270_REG_PWR_CTRL, &pwr_ctrl, 1, dev);
if (ret != 0) {
return ret;
}
}
return ret;
}
static int set_accel_range(const struct sensor_value *range,
struct bmi270_data *dev)
{
int ret = 0;
uint8_t acc_range, reg;
ret = reg_read(BMI270_REG_ACC_RANGE, &acc_range, 1, dev);
if (ret != 0) {
return ret;
}
/* range->val2 is unused */
switch (range->val1) {
case 2:
reg = BMI270_ACC_RANGE_2G;
dev->acc_range = 2;
break;
case 4:
reg = BMI270_ACC_RANGE_4G;
dev->acc_range = 4;
break;
case 8:
reg = BMI270_ACC_RANGE_8G;
dev->acc_range = 8;
break;
case 16:
reg = BMI270_ACC_RANGE_16G;
dev->acc_range = 16;
break;
default:
return -ENOTSUP;
}
acc_range = BMI270_SET_BITS_POS_0(acc_range, BMI270_ACC_RANGE,
reg);
ret = reg_write(BMI270_REG_ACC_RANGE, &acc_range, 1, dev);
return ret;
}
static uint8_t gyr_odr_to_reg(const struct sensor_value *val)
{
double odr = sensor_value_to_double((struct sensor_value *) val);
uint8_t reg = 0;
if ((odr >= 25.0f) && (odr < 50.0f)) {
reg = BMI270_GYR_ODR_25_HZ;
} else if ((odr >= 50.0f) && (odr < 100.0f)) {
reg = BMI270_GYR_ODR_50_HZ;
} else if ((odr >= 100.0f) && (odr < 200.0f)) {
reg = BMI270_GYR_ODR_100_HZ;
} else if ((odr >= 200.0f) && (odr < 400.0f)) {
reg = BMI270_GYR_ODR_200_HZ;
} else if ((odr >= 400.0f) && (odr < 800.0f)) {
reg = BMI270_GYR_ODR_400_HZ;
} else if ((odr >= 800.0f) && (odr < 1600.0f)) {
reg = BMI270_GYR_ODR_800_HZ;
} else if ((odr >= 1600.0f) && (odr < 3200.0f)) {
reg = BMI270_GYR_ODR_1600_HZ;
} else if (odr >= 3200.0f) {
reg = BMI270_GYR_ODR_3200_HZ;
}
return reg;
}
static int set_gyro_odr_osr(const struct sensor_value *odr,
const struct sensor_value *osr, struct bmi270_data *dev)
{
uint8_t gyr_conf, odr_bits, pwr_ctrl, osr_bits;
int ret = 0;
if (odr || osr) {
ret = reg_read(BMI270_REG_GYR_CONF, &gyr_conf, 1, dev);
if (ret != 0) {
return ret;
}
ret = reg_read(BMI270_REG_PWR_CTRL, &pwr_ctrl, 1, dev);
if (ret != 0) {
return ret;
}
}
if (odr) {
odr_bits = gyr_odr_to_reg(odr);
gyr_conf = BMI270_SET_BITS_POS_0(gyr_conf, BMI270_GYR_ODR,
odr_bits);
/* If odr_bits is 0, implies that the sampling frequency is
* 0Hz or invalid too.
*/
if (odr_bits) {
pwr_ctrl |= BMI270_PWR_CTRL_GYR_EN;
} else {
pwr_ctrl &= ~BMI270_PWR_CTRL_GYR_EN;
}
/* If the Sampling frequency (odr) >= 100Hz, enter performance
* mode else, power optimized. This also has a consequence for
* the OSR
*/
if (odr_bits >= BMI270_GYR_ODR_100_HZ) {
gyr_conf = BMI270_SET_BITS(gyr_conf,
BMI270_GYR_FILT,
BMI270_GYR_FILT_PERF_OPT);
gyr_conf = BMI270_SET_BITS(gyr_conf,
BMI270_GYR_FILT_NOISE,
BMI270_GYR_FILT_NOISE_PERF);
} else {
gyr_conf = BMI270_SET_BITS(gyr_conf,
BMI270_GYR_FILT,
BMI270_GYR_FILT_PWR_OPT);
gyr_conf = BMI270_SET_BITS(gyr_conf,
BMI270_GYR_FILT_NOISE,
BMI270_GYR_FILT_NOISE_PWR);
}
dev->gyr_odr = odr_bits;
}
if (osr) {
/* osr->val2 should be unused */
switch (osr->val1) {
case 4:
osr_bits = BMI270_GYR_BWP_OSR4;
break;
case 2:
osr_bits = BMI270_GYR_BWP_OSR2;
break;
default:
osr_bits = BMI270_GYR_BWP_NORM;
break;
}
gyr_conf = BMI270_SET_BITS(gyr_conf, BMI270_GYR_BWP,
osr_bits);
}
if (odr || osr) {
ret = reg_write(BMI270_REG_GYR_CONF, &gyr_conf, 1, dev);
if (ret != 0) {
return ret;
}
/* Assuming we have advance power save enabled */
k_usleep(BMI270_TRANSC_DELAY_SUSPEND);
pwr_ctrl &= BMI270_PWR_CTRL_MSK;
ret = reg_write(BMI270_REG_PWR_CTRL, &pwr_ctrl, 1, dev);
if (ret != 0) {
return ret;
}
}
return ret;
}
static int set_gyro_range(const struct sensor_value *range,
struct bmi270_data *dev)
{
int ret = 0;
uint8_t gyr_range, reg;
ret = reg_read(BMI270_REG_GYR_RANGE, &gyr_range, 1, dev);
if (ret != 0) {
return ret;
}
/* range->val2 is unused */
switch (range->val1) {
case 125:
reg = BMI270_GYR_RANGE_125DPS;
dev->gyr_range = 125;
break;
case 250:
reg = BMI270_GYR_RANGE_250DPS;
dev->gyr_range = 250;
break;
case 500:
reg = BMI270_GYR_RANGE_500DPS;
dev->gyr_range = 500;
break;
case 1000:
reg = BMI270_GYR_RANGE_1000DPS;
dev->gyr_range = 1000;
break;
case 2000:
reg = BMI270_GYR_RANGE_2000DPS;
dev->gyr_range = 2000;
break;
default:
return -ENOTSUP;
}
gyr_range = BMI270_SET_BITS_POS_0(gyr_range, BMI270_GYR_RANGE, reg);
ret = reg_write(BMI270_REG_GYR_RANGE, &gyr_range, 1, dev);
return ret;
}
static int8_t write_config_file(struct bmi270_data *dev)
{
int8_t ret = 0;
uint16_t index = 0;
uint8_t addr_array[2] = { 0 };
/* Disable loading of the configuration */
for (index = 0; index < sizeof(bmi270_config_file);
index += BMI270_WR_LEN) {
/* Store 0 to 3 bits of address in first byte */
addr_array[0] = (uint8_t)((index / 2) & 0x0F);
/* Store 4 to 11 bits of address in the second byte */
addr_array[1] = (uint8_t)((index / 2) >> 4);
ret = reg_write(BMI270_REG_INIT_ADDR_0, addr_array, 2, dev);
k_usleep(BMI270_INTER_WRITE_DELAY_US);
if (ret == 0) {
ret = reg_write(BMI270_REG_INIT_DATA,
(bmi270_config_file + index),
BMI270_WR_LEN, dev);
k_usleep(BMI270_INTER_WRITE_DELAY_US);
}
}
return ret;
}
static int bmi270_sample_fetch(const struct device *dev, enum sensor_channel chan)
{
struct bmi270_data *drv_dev = dev->data;
uint8_t data[12];
int ret;
if (chan != SENSOR_CHAN_ALL) {
return -ENOTSUP;
}
ret = reg_read(BMI270_REG_ACC_X_LSB, data, 12, drv_dev);
if (ret == 0) {
drv_dev->ax = (int16_t)sys_get_le16(&data[0]);
drv_dev->ay = (int16_t)sys_get_le16(&data[2]);
drv_dev->az = (int16_t)sys_get_le16(&data[4]);
drv_dev->gx = (int16_t)sys_get_le16(&data[6]);
drv_dev->gy = (int16_t)sys_get_le16(&data[8]);
drv_dev->gz = (int16_t)sys_get_le16(&data[10]);
} else {
drv_dev->ax = 0;
drv_dev->ay = 0;
drv_dev->az = 0;
drv_dev->gx = 0;
drv_dev->gy = 0;
drv_dev->gz = 0;
}
return ret;
}
static int bmi270_channel_get(const struct device *dev, enum sensor_channel chan,
struct sensor_value *val)
{
struct bmi270_data *drv_dev = dev->data;
if (chan == SENSOR_CHAN_ACCEL_X) {
channel_accel_convert(val, drv_dev->ax, drv_dev->acc_range);
} else if (chan == SENSOR_CHAN_ACCEL_Y) {
channel_accel_convert(val, drv_dev->ay, drv_dev->acc_range);
} else if (chan == SENSOR_CHAN_ACCEL_Z) {
channel_accel_convert(val, drv_dev->az, drv_dev->acc_range);
} else if (chan == SENSOR_CHAN_ACCEL_XYZ) {
channel_accel_convert(&val[0], drv_dev->ax,
drv_dev->acc_range);
channel_accel_convert(&val[1], drv_dev->ay,
drv_dev->acc_range);
channel_accel_convert(&val[2], drv_dev->az,
drv_dev->acc_range);
} else if (chan == SENSOR_CHAN_GYRO_X) {
channel_gyro_convert(val, drv_dev->gx, drv_dev->gyr_range);
} else if (chan == SENSOR_CHAN_GYRO_Y) {
channel_gyro_convert(val, drv_dev->gy, drv_dev->gyr_range);
} else if (chan == SENSOR_CHAN_GYRO_Z) {
channel_gyro_convert(val, drv_dev->gz, drv_dev->gyr_range);
} else if (chan == SENSOR_CHAN_GYRO_XYZ) {
channel_gyro_convert(&val[0], drv_dev->gx,
drv_dev->gyr_range);
channel_gyro_convert(&val[1], drv_dev->gy,
drv_dev->gyr_range);
channel_gyro_convert(&val[2], drv_dev->gz,
drv_dev->gyr_range);
} else {
return -ENOTSUP;
}
return 0;
}
static int bmi270_attr_set(const struct device *dev, enum sensor_channel chan,
enum sensor_attribute attr, const struct sensor_value *val)
{
struct bmi270_data *drv_dev = dev->data;
int ret = -ENOTSUP;
if ((chan == SENSOR_CHAN_ACCEL_X) || (chan == SENSOR_CHAN_ACCEL_Y)
|| (chan == SENSOR_CHAN_ACCEL_Z)
|| (chan == SENSOR_CHAN_ACCEL_XYZ)) {
switch (attr) {
case SENSOR_ATTR_SAMPLING_FREQUENCY:
ret = set_accel_odr_osr(val, NULL, drv_dev);
break;
case SENSOR_ATTR_OVERSAMPLING:
ret = set_accel_odr_osr(NULL, val, drv_dev);
break;
case SENSOR_ATTR_FULL_SCALE:
ret = set_accel_range(val, drv_dev);
break;
default:
ret = -ENOTSUP;
}
} else if ((chan == SENSOR_CHAN_GYRO_X) || (chan == SENSOR_CHAN_GYRO_Y)
|| (chan == SENSOR_CHAN_GYRO_Z)
|| (chan == SENSOR_CHAN_GYRO_XYZ)) {
switch (attr) {
case SENSOR_ATTR_SAMPLING_FREQUENCY:
ret = set_gyro_odr_osr(val, NULL, drv_dev);
break;
case SENSOR_ATTR_OVERSAMPLING:
ret = set_gyro_odr_osr(NULL, val, drv_dev);
break;
case SENSOR_ATTR_FULL_SCALE:
ret = set_gyro_range(val, drv_dev);
break;
default:
ret = -ENOTSUP;
}
} else {
ret = -ENOTSUP;
}
return ret;
}
static int bmi270_init(const struct device *dev)
{
int ret;
struct bmi270_data *drv_dev = dev->data;
const struct bmi270_dev_config *cfg = dev->config;
uint8_t chip_id;
uint8_t soft_reset_cmd;
uint8_t init_ctrl;
uint8_t msg;
uint8_t tries;
uint8_t adv_pwr_save;
drv_dev->i2c = device_get_binding(cfg->i2c_master_name);
if (drv_dev->i2c == NULL) {
LOG_ERR("Could not get pointer to %s device",
cfg->i2c_master_name);
return -EINVAL;
}
drv_dev->i2c_addr = cfg->i2c_addr;
drv_dev->acc_odr = BMI270_ACC_ODR_100_HZ;
drv_dev->acc_range = 8;
drv_dev->gyr_odr = BMI270_GYR_ODR_200_HZ;
drv_dev->gyr_range = 2000;
k_usleep(BMI270_POWER_ON_TIME);
ret = reg_read(BMI270_REG_CHIP_ID, &chip_id, 1, drv_dev);
if (ret != 0) {
return ret;
}
if (chip_id != BMI270_CHIP_ID) {
LOG_ERR("Unexpected chip id (%x). Expected (%x)",
chip_id, BMI270_CHIP_ID);
return -EIO;
}
soft_reset_cmd = BMI270_CMD_SOFT_RESET;
ret = reg_write(BMI270_REG_CMD, &soft_reset_cmd, 1, drv_dev);
if (ret != 0) {
return ret;
}
k_usleep(BMI270_SOFT_RESET_TIME);
ret = reg_read(BMI270_REG_PWR_CONF, &adv_pwr_save, 1, drv_dev);
if (ret != 0) {
return ret;
}
adv_pwr_save = BMI270_SET_BITS_POS_0(adv_pwr_save,
BMI270_PWR_CONF_ADV_PWR_SAVE,
BMI270_PWR_CONF_ADV_PWR_SAVE_DIS);
ret = reg_write(BMI270_REG_PWR_CONF, &adv_pwr_save, 1, drv_dev);
if (ret != 0) {
return ret;
}
init_ctrl = BMI270_PREPARE_CONFIG_LOAD;
ret = reg_write(BMI270_REG_INIT_CTRL, &init_ctrl, 1, drv_dev);
if (ret != 0) {
return ret;
}
ret = write_config_file(drv_dev);
if (ret != 0) {
return ret;
}
init_ctrl = BMI270_COMPLETE_CONFIG_LOAD;
ret = reg_write(BMI270_REG_INIT_CTRL, &init_ctrl, 1, drv_dev);
if (ret != 0) {
return ret;
}
/* Timeout after BMI270_CONFIG_FILE_RETRIES x
* BMI270_CONFIG_FILE_POLL_PERIOD_US microseconds.
* If tries is 0 by the end of the loop,
* report an error
*/
for (tries = 0; tries <= BMI270_CONFIG_FILE_RETRIES; tries++) {
ret = reg_read(BMI270_REG_INTERNAL_STATUS, &msg, 1, drv_dev);
if (ret != 0) {
return ret;
}
msg &= BMI270_INST_MESSAGE_MSK;
if (msg == BMI270_INST_MESSAGE_INIT_OK) {
break;
}
k_usleep(BMI270_CONFIG_FILE_POLL_PERIOD_US);
}
if (tries == 0) {
return -EIO;
}
adv_pwr_save = BMI270_SET_BITS_POS_0(adv_pwr_save,
BMI270_PWR_CONF_ADV_PWR_SAVE,
BMI270_PWR_CONF_ADV_PWR_SAVE_EN);
ret = reg_write(BMI270_REG_PWR_CONF, &adv_pwr_save, 1, drv_dev);
if (ret != 0) {
return ret;
}
return ret;
}
static const struct sensor_driver_api bmi270_driver_api = {
.sample_fetch = bmi270_sample_fetch,
.channel_get = bmi270_channel_get,
.attr_set = bmi270_attr_set
};
#define BMI270_CREATE_INST(inst) \
\
static struct bmi270_data bmi270_drv_##inst; \
\
static const struct bmi270_dev_config bmi270_config_##inst = { \
.i2c_master_name = DT_INST_BUS_LABEL(inst), \
.i2c_addr = DT_INST_REG_ADDR(inst), \
}; \
\
DEVICE_DT_INST_DEFINE(inst, \
bmi270_init, \
device_pm_control_nop, \
&bmi270_drv_##inst, \
&bmi270_config_##inst, \
POST_KERNEL, \
CONFIG_SENSOR_INIT_PRIORITY, \
&bmi270_driver_api);
DT_INST_FOREACH_STATUS_OKAY(BMI270_CREATE_INST)

View file

@ -0,0 +1,215 @@
/*
* Copyright (c) 2021 Bosch Sensortec GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_DRIVERS_SENSOR_BMI270_BMI270_H_
#define ZEPHYR_DRIVERS_SENSOR_BMI270_BMI270_H_
#include <device.h>
#include <sys/util.h>
#include <zephyr/types.h>
#include <drivers/sensor.h>
#define BMI270_REG_CHIP_ID 0x00
#define BMI270_REG_ERROR 0x02
#define BMI270_REG_STATUS 0x03
#define BMI270_REG_AUX_X_LSB 0x04
#define BMI270_REG_ACC_X_LSB 0x0C
#define BMI270_REG_GYR_X_LSB 0x12
#define BMI270_REG_SENSORTIME_0 0x18
#define BMI270_REG_EVENT 0x1B
#define BMI270_REG_INT_STATUS_0 0x1C
#define BMI270_REG_SC_OUT_0 0x1E
#define BMI270_REG_WR_GEST_ACT 0x20
#define BMI270_REG_INTERNAL_STATUS 0x21
#define BMI270_REG_TEMPERATURE_0 0x22
#define BMI270_REG_FIFO_LENGTH_0 0x24
#define BMI270_REG_FIFO_DATA 0x26
#define BMI270_REG_FEAT_PAGE 0x2F
#define BMI270_REG_FEATURES_0 0x30
#define BMI270_REG_ACC_CONF 0x40
#define BMI270_REG_ACC_RANGE 0x41
#define BMI270_REG_GYR_CONF 0x42
#define BMI270_REG_GYR_RANGE 0x43
#define BMI270_REG_AUX_CONF 0x44
#define BMI270_REG_FIFO_DOWNS 0x45
#define BMI270_REG_FIFO_WTM_0 0x46
#define BMI270_REG_FIFO_CONFIG_0 0x48
#define BMI270_REG_SATURATION 0x4A
#define BMI270_REG_AUX_DEV_ID 0x4B
#define BMI270_REG_AUX_IF_CONF 0x4C
#define BMI270_REG_AUX_RD_ADDR 0x4D
#define BMI270_REG_AUX_WR_ADDR 0x4E
#define BMI270_REG_AUX_WR_DATA 0x4F
#define BMI270_REG_ERR_REG_MSK 0x52
#define BMI270_REG_INT1_IO_CTRL 0x53
#define BMI270_REG_INT2_IO_CTRL 0x54
#define BMI270_REG_INT_LATCH 0x55
#define BMI270_REG_INT1_MAP_FEAT 0x56
#define BMI270_REG_INT2_MAP_FEAT 0x57
#define BMI270_REG_INT_MAP_DATA 0x58
#define BMI270_REG_INIT_CTRL 0x59
#define BMI270_REG_INIT_ADDR_0 0x5B
#define BMI270_REG_INIT_DATA 0x5E
#define BMI270_REG_INTERNAL_ERROR 0x5F
#define BMI270_REG_AUX_IF_TRIM 0x68
#define BMI270_REG_GYR_CRT_CONF 0x69
#define BMI270_REG_NVM_CONF 0x6A
#define BMI270_REG_IF_CONF 0x6B
#define BMI270_REG_DRV 0x6C
#define BMI270_REG_ACC_SELF_TEST 0x6D
#define BMI270_REG_GYR_SELF_TEST 0x6E
#define BMI270_REG_NV_CONF 0x70
#define BMI270_REG_OFFSET_0 0x71
#define BMI270_REG_PWR_CONF 0x7C
#define BMI270_REG_PWR_CTRL 0x7D
#define BMI270_REG_CMD 0x7E
#define BMI270_CHIP_ID 0x24
#define BMI270_CMD_G_TRIGGER 0x02
#define BMI270_CMD_USR_GAIN 0x03
#define BMI270_CMD_NVM_PROG 0xA0
#define BMI270_CMD_FIFO_FLUSH OxB0
#define BMI270_CMD_SOFT_RESET 0xB6
#define BMI270_POWER_ON_TIME 500
#define BMI270_SOFT_RESET_TIME 2000
#define BMI270_ACC_SUS_TO_NOR_START_UP_TIME 2000
#define BMI270_GYR_SUS_TO_NOR_START_UP_TIME 45000
#define BMI270_GYR_FAST_START_UP_TIME 2000
#define BMI270_TRANSC_DELAY_SUSPEND 450
#define BMI270_TRANSC_DELAY_NORMAL 2
#define BMI270_PREPARE_CONFIG_LOAD 0x00
#define BMI270_COMPLETE_CONFIG_LOAD 0x01
#define BMI270_INST_MESSAGE_MSK 0x0F
#define BMI270_INST_MESSAGE_NOT_INIT 0x00
#define BMI270_INST_MESSAGE_INIT_OK 0x01
#define BMI270_INST_MESSAGE_INIT_ERR 0x02
#define BMI270_INST_MESSAGE_DRV_ERR 0x03
#define BMI270_INST_MESSAGE_SNS_STOP 0x04
#define BMI270_INST_MESSAGE_NVM_ERR 0x05
#define BMI270_INST_MESSAGE_STRTUP_ERR 0x06
#define BMI270_INST_MESSAGE_COMPAT_ERR 0x07
#define BMI270_INST_AXES_REMAP_ERROR 0x20
#define BMI270_INST_ODR_50HZ_ERROR 0x40
#define BMI270_PWR_CONF_ADV_PWR_SAVE_MSK 0x01
#define BMI270_PWR_CONF_ADV_PWR_SAVE_EN 0x01
#define BMI270_PWR_CONF_ADV_PWR_SAVE_DIS 0x00
#define BMI270_PWR_CONF_FIFO_SELF_WKUP_MSK 0x02
#define BMI270_PWR_CONF_FIFO_SELF_WKUP_POS 0x01
#define BMI270_PWR_CONF_FIFO_SELF_WKUP_EN 0x01
#define BMI270_PWR_CONF_FIFO_SELF_WKUP_DIS 0x00
#define BMI270_PWR_CONF_FUP_EN_MSK 0x04
#define BMI270_PWR_CONF_FUP_EN_POS 0x02
#define BMI270_PWR_CONF_FUP_EN 0x01
#define BMI270_PWR_CONF_FUP_DIS 0x00
#define BMI270_PWR_CTRL_MSK 0x0F
#define BMI270_PWR_CTRL_AUX_EN 0x01
#define BMI270_PWR_CTRL_GYR_EN 0x02
#define BMI270_PWR_CTRL_ACC_EN 0x04
#define BMI270_PWR_CTRL_TEMP_EN 0x08
#define BMI270_ACC_ODR_MSK 0x0F
#define BMI270_ACC_ODR_25D32_HZ 0x01
#define BMI270_ACC_ODR_25D16_HZ 0x02
#define BMI270_ACC_ODR_25D8_HZ 0x03
#define BMI270_ACC_ODR_25D4_HZ 0x04
#define BMI270_ACC_ODR_25D2_HZ 0x05
#define BMI270_ACC_ODR_25_HZ 0x06
#define BMI270_ACC_ODR_50_HZ 0x07
#define BMI270_ACC_ODR_100_HZ 0x08
#define BMI270_ACC_ODR_200_HZ 0x09
#define BMI270_ACC_ODR_400_HZ 0x0A
#define BMI270_ACC_ODR_800_HZ 0x0B
#define BMI270_ACC_ODR_1600_HZ 0x0C
#define BMI270_ACC_BWP_MSK 0x30
#define BMI270_ACC_BWP_POS 4
#define BMI270_ACC_BWP_OSR4_AVG1 0x00
#define BMI270_ACC_BWP_OSR2_AVG2 0x01
#define BMI270_ACC_BWP_NORM_AVG4 0x02
#define BMI270_ACC_BWP_CIC_AVG8 0x03
#define BMI270_ACC_BWP_RES_AVG16 0x04
#define BMI270_ACC_BWP_RES_AVG32 0x05
#define BMI270_ACC_BWP_RES_AVG64 0x06
#define BMI270_ACC_BWP_RES_AVG128 0x07
#define BMI270_ACC_FILT_MSK 0x80
#define BMI270_ACC_FILT_POS 7
#define BMI270_ACC_FILT_PWR_OPT 0x00
#define BMI270_ACC_FILT_PERF_OPT 0x01
#define BMI270_ACC_RANGE_MSK 0x03
#define BMI270_ACC_RANGE_2G 0x00
#define BMI270_ACC_RANGE_4G 0x01
#define BMI270_ACC_RANGE_8G 0x02
#define BMI270_ACC_RANGE_16G 0x03
#define BMI270_GYR_ODR_MSK 0x0F
#define BMI270_GYR_ODR_25_HZ 0x06
#define BMI270_GYR_ODR_50_HZ 0x07
#define BMI270_GYR_ODR_100_HZ 0x08
#define BMI270_GYR_ODR_200_HZ 0x09
#define BMI270_GYR_ODR_400_HZ 0x0A
#define BMI270_GYR_ODR_800_HZ 0x0B
#define BMI270_GYR_ODR_1600_HZ 0x0C
#define BMI270_GYR_ODR_3200_HZ 0x0D
#define BMI270_GYR_BWP_MSK 0x30
#define BMI270_GYR_BWP_POS 4
#define BMI270_GYR_BWP_OSR4 0x00
#define BMI270_GYR_BWP_OSR2 0x01
#define BMI270_GYR_BWP_NORM 0x02
#define BMI270_GYR_FILT_NOISE_MSK 0x40
#define BMI270_GYR_FILT_NOISE_POS 6
#define BMI270_GYR_FILT_NOISE_PWR 0x00
#define BMI270_GYR_FILT_NOISE_PERF 0x01
#define BMI270_GYR_FILT_MSK 0x80
#define BMI270_GYR_FILT_POS 7
#define BMI270_GYR_FILT_PWR_OPT 0x00
#define BMI270_GYR_FILT_PERF_OPT 0x01
#define BMI270_GYR_RANGE_MSK 0x07
#define BMI270_GYR_RANGE_2000DPS 0x00
#define BMI270_GYR_RANGE_1000DPS 0x01
#define BMI270_GYR_RANGE_500DPS 0x02
#define BMI270_GYR_RANGE_250DPS 0x03
#define BMI270_GYR_RANGE_125DPS 0x04
#define BMI270_GYR_OIS_RANGE_MSK 0x80
#define BMI270_GYR_OIS_RANGE_POS 3
#define BMI270_GYR_OIS_RANGE_250DPS 0x00
#define BMI270_GYR_OIS_RANGE_2000DPS 0x01
#define BMI270_SET_BITS(reg_data, bitname, data) \
((reg_data & ~(bitname##_MSK)) | ((data << bitname##_POS) \
& bitname##_MSK))
#define BMI270_SET_BITS_POS_0(reg_data, bitname, data) \
((reg_data & ~(bitname##_MSK)) | (data & bitname##_MSK))
struct bmi270_data {
const struct device *i2c;
uint8_t i2c_addr;
int16_t ax, ay, az, gx, gy, gz;
uint8_t acc_range, acc_odr, gyr_odr;
uint16_t gyr_range;
};
struct bmi270_dev_config {
const char *i2c_master_name;
uint16_t i2c_addr;
};
#endif /* ZEPHYR_DRIVERS_SENSOR_BMI270_BMI270_H_ */

View file

@ -0,0 +1,44 @@
/*
* Copyright (c) 2021 Bosch Sensortec GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef BMI270_BMI270_CONFIG_FILE_H_
#define BMI270_BMI270_CONFIG_FILE_H_
/* Source : https://github.com/BoschSensortec/BMI270-Sensor-API/blob/v2.63.1/bmi270_maximum_fifo.c#L51 */
static const uint8_t bmi270_config_file[] = {
0xc8, 0x2e, 0x00, 0x2e, 0x80, 0x2e, 0x1a, 0x00, 0xc8, 0x2e, 0x00,
0x2e, 0xc8, 0x2e, 0x00, 0x2e, 0xc8, 0x2e, 0x00, 0x2e, 0xc8, 0x2e,
0x00, 0x2e, 0xc8, 0x2e, 0x00, 0x2e, 0xc8, 0x2e, 0x00, 0x2e, 0x90,
0x32, 0x21, 0x2e, 0x59, 0xf5, 0x10, 0x30, 0x21, 0x2e, 0x6a, 0xf5,
0x1a, 0x24, 0x22, 0x00, 0x80, 0x2e, 0x3b, 0x00, 0xc8, 0x2e, 0x44,
0x47, 0x22, 0x00, 0x37, 0x00, 0xa4, 0x00, 0xff, 0x0f, 0xd1, 0x00,
0x07, 0xad, 0x80, 0x2e, 0x00, 0xc1, 0x80, 0x2e, 0x00, 0xc1, 0x80,
0x2e, 0x00, 0xc1, 0x80, 0x2e, 0x00, 0xc1, 0x80, 0x2e, 0x00, 0xc1,
0x80, 0x2e, 0x00, 0xc1, 0x80, 0x2e, 0x00, 0xc1, 0x80, 0x2e, 0x00,
0xc1, 0x80, 0x2e, 0x00, 0xc1, 0x80, 0x2e, 0x00, 0xc1, 0x80, 0x2e,
0x00, 0xc1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x24, 0xfc,
0xf5, 0x80, 0x30, 0x40, 0x42, 0x50, 0x50, 0x00, 0x30, 0x12, 0x24,
0xeb, 0x00, 0x03, 0x30, 0x00, 0x2e, 0xc1, 0x86, 0x5a, 0x0e, 0xfb,
0x2f, 0x21, 0x2e, 0xfc, 0xf5, 0x13, 0x24, 0x63, 0xf5, 0xe0, 0x3c,
0x48, 0x00, 0x22, 0x30, 0xf7, 0x80, 0xc2, 0x42, 0xe1, 0x7f, 0x3a,
0x25, 0xfc, 0x86, 0xf0, 0x7f, 0x41, 0x33, 0x98, 0x2e, 0xc2, 0xc4,
0xd6, 0x6f, 0xf1, 0x30, 0xf1, 0x08, 0xc4, 0x6f, 0x11, 0x24, 0xff,
0x03, 0x12, 0x24, 0x00, 0xfc, 0x61, 0x09, 0xa2, 0x08, 0x36, 0xbe,
0x2a, 0xb9, 0x13, 0x24, 0x38, 0x00, 0x64, 0xbb, 0xd1, 0xbe, 0x94,
0x0a, 0x71, 0x08, 0xd5, 0x42, 0x21, 0xbd, 0x91, 0xbc, 0xd2, 0x42,
0xc1, 0x42, 0x00, 0xb2, 0xfe, 0x82, 0x05, 0x2f, 0x50, 0x30, 0x21,
0x2e, 0x21, 0xf2, 0x00, 0x2e, 0x00, 0x2e, 0xd0, 0x2e, 0xf0, 0x6f,
0x02, 0x30, 0x02, 0x42, 0x20, 0x26, 0xe0, 0x6f, 0x02, 0x31, 0x03,
0x40, 0x9a, 0x0a, 0x02, 0x42, 0xf0, 0x37, 0x05, 0x2e, 0x5e, 0xf7,
0x10, 0x08, 0x12, 0x24, 0x1e, 0xf2, 0x80, 0x42, 0x83, 0x84, 0xf1,
0x7f, 0x0a, 0x25, 0x13, 0x30, 0x83, 0x42, 0x3b, 0x82, 0xf0, 0x6f,
0x00, 0x2e, 0x00, 0x2e, 0xd0, 0x2e, 0x12, 0x40, 0x52, 0x42, 0x00,
0x2e, 0x12, 0x40, 0x52, 0x42, 0x3e, 0x84, 0x00, 0x40, 0x40, 0x42,
0x7e, 0x82, 0xe1, 0x7f, 0xf2, 0x7f, 0x98, 0x2e, 0x6a, 0xd6, 0x21,
0x30, 0x23, 0x2e, 0x61, 0xf5, 0xeb, 0x2c, 0xe1, 0x6f
};
#endif /* BMI270_BMI270_CONFIG_FILE_H_ */