Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers: sensors: bmi270: Add CRT feature support #63015

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions drivers/sensor/bosch/bmi270/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ zephyr_library_sources(bmi270.c)
zephyr_library_sources_ifdef(CONFIG_BMI270_BUS_I2C bmi270_i2c.c)
zephyr_library_sources_ifdef(CONFIG_BMI270_BUS_SPI bmi270_spi.c)
zephyr_library_sources_ifdef(CONFIG_BMI270_TRIGGER bmi270_trigger.c)
zephyr_library_sources_ifdef(CONFIG_BMI270_CRT bmi270_crt.c)
5 changes: 5 additions & 0 deletions drivers/sensor/bosch/bmi270/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ config BMI270_THREAD_STACK_SIZE
help
Stack size of thread used by the driver to handle interrupts.

config BMI270_CRT
bool "CRT feature support for bmi270"
help
BMI270 Gyro Component Re-Trimming(CRT) calibration feature support.

endif # BMI270
156 changes: 108 additions & 48 deletions drivers/sensor/bosch/bmi270/bmi270.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,19 @@ static int bmi270_attr_set(const struct device *dev, enum sensor_channel chan,
case SENSOR_ATTR_FULL_SCALE:
ret = set_gyro_range(dev, val);
break;
#ifdef CONFIG_BMI270_CRT
case SENSOR_ATTR_CALIBRATION:
ARG_UNUSED(val);
ret = bmi270_gyro_crt(dev);
break;
case BMI270_SENSOR_ATTR_NVM_PROG:
ARG_UNUSED(val);
ret = bmi270_nvm_prog(dev);
break;
case BMI270_SENSOR_ATTR_GAIN_COMP:
ret = bmi270_set_gyro_gain(dev, val);
break;
#endif /* CONFIG_BMI270_CRT */
default:
ret = -ENOTSUP;
}
Expand All @@ -641,51 +654,47 @@ static int bmi270_attr_set(const struct device *dev, enum sensor_channel chan,
return ret;
}

static int bmi270_init(const struct device *dev)
static int bmi270_attr_get(const struct device *dev, enum sensor_channel chan,
enum sensor_attribute attr, struct sensor_value *val)
{
int ret;
struct bmi270_data *data = dev->data;
uint8_t chip_id;
uint8_t soft_reset_cmd;
uint8_t init_ctrl;
uint8_t msg;
uint8_t tries;
uint8_t adv_pwr_save;

ret = bmi270_bus_check(dev);
if (ret < 0) {
LOG_ERR("Could not initialize bus");
return ret;
}

#if CONFIG_BMI270_TRIGGER
data->dev = dev;
k_mutex_init(&data->trigger_mutex);
#endif

data->acc_odr = BMI270_ACC_ODR_100_HZ;
data->acc_range = 8;
data->gyr_odr = BMI270_GYR_ODR_200_HZ;
data->gyr_range = 2000;

k_usleep(BMI270_POWER_ON_TIME);
int ret = -ENOTSUP;

ret = bmi270_bus_init(dev);
if (ret != 0) {
LOG_ERR("Could not initiate bus communication");
return ret;
if ((chan == SENSOR_CHAN_ACCEL_X) || (chan == SENSOR_CHAN_ACCEL_Y)
|| (chan == SENSOR_CHAN_ACCEL_Z)
|| (chan == SENSOR_CHAN_ACCEL_XYZ)) {
switch (attr) {
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) {
#ifdef CONFIG_BMI270_CRT
case SENSOR_ATTR_CALIBRATION:
ret = bmi270_get_gyro_user_gain(dev, val);
break;
#endif /* CONFIG_BMI270_CRT */
default:
ret = -ENOTSUP;
}
}

ret = bmi270_reg_read(dev, BMI270_REG_CHIP_ID, &chip_id, 1);
if (ret != 0) {
return ret;
}
return ret;
}

if (chip_id != BMI270_CHIP_ID) {
LOG_ERR("Unexpected chip id (%x). Expected (%x)",
chip_id, BMI270_CHIP_ID);
return -EIO;
}
/**
* @brief This function resets bmi2 sensor.
* All registers are overwritten with their default values.
*/
int bmi270_soft_reset(const struct device *dev)
{
int ret;
uint8_t tries;
uint8_t soft_reset_cmd;
uint8_t init_ctrl;
uint8_t adv_pwr_save;
uint8_t msg;

soft_reset_cmd = BMI270_CMD_SOFT_RESET;
ret = bmi270_reg_write(dev, BMI270_REG_CMD, &soft_reset_cmd, 1);
Expand Down Expand Up @@ -751,28 +760,74 @@ static int bmi270_init(const struct device *dev)
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 = bmi270_reg_write_with_delay(dev, BMI270_REG_PWR_CONF, &adv_pwr_save, 1,
BMI270_INTER_WRITE_DELAY_US);
return ret;
}

static int bmi270_init(const struct device *dev)
{
int ret;
struct bmi270_data *data = dev->data;
uint8_t chip_id;

ret = bmi270_bus_check(dev);
if (ret < 0) {
LOG_ERR("Could not initialize bus");
return ret;
}

#if CONFIG_BMI270_TRIGGER
data->dev = dev;
k_mutex_init(&data->trigger_mutex);
#endif

data->acc_odr = BMI270_ACC_ODR_100_HZ;
data->acc_range = 8;
data->gyr_odr = BMI270_GYR_ODR_200_HZ;
data->gyr_range = 2000;

k_usleep(BMI270_POWER_ON_TIME);

ret = bmi270_bus_init(dev);
if (ret != 0) {
LOG_ERR("Could not initiate bus communication");
return ret;
}

ret = bmi270_reg_read(dev, BMI270_REG_CHIP_ID, &chip_id, 1);
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;
}

ret = bmi270_soft_reset(dev);
if (ret != 0) {
LOG_ERR("Soft reset failed, err: %d", ret);
return ret;
}

#if CONFIG_BMI270_TRIGGER
ret = bmi270_init_interrupts(dev);
if (ret) {
LOG_ERR("bmi270_init_interrupts returned %d", ret);
return ret;
}
#endif

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 = bmi270_reg_write_with_delay(dev, BMI270_REG_PWR_CONF,
&adv_pwr_save, 1,
BMI270_INTER_WRITE_DELAY_US);

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,
.attr_get = bmi270_attr_get,
#if defined(CONFIG_BMI270_TRIGGER)
.trigger_set = bmi270_trigger_set,
#endif
Expand All @@ -790,6 +845,11 @@ static const struct bmi270_feature_config bmi270_feature_base = {
.config_file_len = sizeof(bmi270_config_file_base),
.anymo_1 = &(struct bmi270_feature_reg){ .page = 1, .addr = 0x3C },
.anymo_2 = &(struct bmi270_feature_reg){ .page = 1, .addr = 0x3E },
#if defined(CONFIG_BMI270_CRT)
.g_trig_1 = &(struct bmi270_feature_reg){.page = 1, .addr = 0x32},
.gyr_gain_status = &(struct bmi270_feature_reg){.page = 0, .addr = 0x38},
.gen_set_1 = &(struct bmi270_feature_reg){.page = 1, .addr = 0x34},
#endif
};

#define BMI270_FEATURE(inst) ( \
Expand Down
Loading
Loading