diff --git a/drivers/sensor/ism330dhcx/Kconfig b/drivers/sensor/ism330dhcx/Kconfig index d22ac3c9703..25480e59c8c 100644 --- a/drivers/sensor/ism330dhcx/Kconfig +++ b/drivers/sensor/ism330dhcx/Kconfig @@ -102,20 +102,6 @@ endif # ISM330DHCX_SENSORHUB menu "Attributes" -config ISM330DHCX_GYRO_FS - int "Gyroscope full-scale range" - default 0 - help - Specify the default gyroscope full-scale range. - An X value for the config represents a range of +/- X degree per - second. Valid values are: - 0: Full Scale selected at runtime - 125: +/- 125dps - 250: +/- 250dps - 500: +/- 500dps - 1000: +/- 1000dps - 2000: +/- 2000dps - config ISM330DHCX_GYRO_ODR int "Gyroscope Output data rate frequency" range 0 10 @@ -135,19 +121,6 @@ config ISM330DHCX_GYRO_ODR 9: 3330Hz 10: 6660Hz -config ISM330DHCX_ACCEL_FS - int "Accelerometer full-scale range" - default 0 - help - Specify the default accelerometer full-scale range. - An X value for the config represents a range of +/- X G. Valid values - are: - 0: Full Scale selected at runtime - 2: +/- 2g - 4: +/- 4g - 8: +/- 8g - 16: +/- 16g - config ISM330DHCX_ACCEL_ODR int "Accelerometer Output data rate frequency" range 0 10 diff --git a/drivers/sensor/ism330dhcx/ism330dhcx.c b/drivers/sensor/ism330dhcx/ism330dhcx.c index 65743bf88c7..85dba6fce8c 100644 --- a/drivers/sensor/ism330dhcx/ism330dhcx.c +++ b/drivers/sensor/ism330dhcx/ism330dhcx.c @@ -52,7 +52,6 @@ static int ism330dhcx_odr_to_freq_val(uint16_t odr) return ism330dhcx_odr_map[ARRAY_SIZE(ism330dhcx_odr_map) - 1]; } -#ifdef ISM330DHCX_ACCEL_FS_RUNTIME static const uint16_t ism330dhcx_accel_fs_map[] = {2, 16, 4, 8}; static const uint16_t ism330dhcx_accel_fs_sens[] = {1, 8, 2, 4}; @@ -68,9 +67,7 @@ static int ism330dhcx_accel_range_to_fs_val(int32_t range) return -EINVAL; } -#endif -#ifdef ISM330DHCX_GYRO_FS_RUNTIME static const uint16_t ism330dhcx_gyro_fs_map[] = {250, 500, 1000, 2000, 125}; static const uint16_t ism330dhcx_gyro_fs_sens[] = {2, 4, 8, 16, 1}; @@ -86,7 +83,6 @@ static int ism330dhcx_gyro_range_to_fs_val(int32_t range) return -EINVAL; } -#endif static inline int ism330dhcx_reboot(const struct device *dev) { @@ -169,7 +165,6 @@ static int ism330dhcx_accel_odr_set(const struct device *dev, uint16_t freq) } #endif -#ifdef ISM330DHCX_ACCEL_FS_RUNTIME static int ism330dhcx_accel_range_set(const struct device *dev, int32_t range) { int fs; @@ -188,7 +183,6 @@ static int ism330dhcx_accel_range_set(const struct device *dev, int32_t range) data->acc_gain = (ism330dhcx_accel_fs_sens[fs] * GAIN_UNIT_XL); return 0; } -#endif static int ism330dhcx_accel_config(const struct device *dev, enum sensor_channel chan, @@ -196,10 +190,8 @@ static int ism330dhcx_accel_config(const struct device *dev, const struct sensor_value *val) { switch (attr) { -#ifdef ISM330DHCX_ACCEL_FS_RUNTIME case SENSOR_ATTR_FULL_SCALE: return ism330dhcx_accel_range_set(dev, sensor_ms2_to_g(val)); -#endif #ifdef ISM330DHCX_ACCEL_ODR_RUNTIME case SENSOR_ATTR_SAMPLING_FREQUENCY: return ism330dhcx_accel_odr_set(dev, val->val1); @@ -231,7 +223,6 @@ static int ism330dhcx_gyro_odr_set(const struct device *dev, uint16_t freq) } #endif -#ifdef ISM330DHCX_GYRO_FS_RUNTIME static int ism330dhcx_gyro_range_set(const struct device *dev, int32_t range) { int fs; @@ -250,7 +241,6 @@ static int ism330dhcx_gyro_range_set(const struct device *dev, int32_t range) data->gyro_gain = (ism330dhcx_gyro_fs_sens[fs] * GAIN_UNIT_G); return 0; } -#endif static int ism330dhcx_gyro_config(const struct device *dev, enum sensor_channel chan, @@ -258,10 +248,8 @@ static int ism330dhcx_gyro_config(const struct device *dev, const struct sensor_value *val) { switch (attr) { -#ifdef ISM330DHCX_GYRO_FS_RUNTIME case SENSOR_ATTR_FULL_SCALE: return ism330dhcx_gyro_range_set(dev, sensor_rad_to_degrees(val)); -#endif #ifdef ISM330DHCX_GYRO_ODR_RUNTIME case SENSOR_ATTR_SAMPLING_FREQUENCY: return ism330dhcx_gyro_odr_set(dev, val->val1); @@ -490,8 +478,7 @@ static int ism330dhcx_gyro_channel_get(enum sensor_channel chan, struct sensor_value *val, struct ism330dhcx_data *data) { - return ism330dhcx_gyro_get_channel(chan, val, data, - ISM330DHCX_DEFAULT_GYRO_SENSITIVITY); + return ism330dhcx_gyro_get_channel(chan, val, data, data->gyro_gain); } #if defined(CONFIG_ISM330DHCX_ENABLE_TEMP) @@ -691,6 +678,7 @@ static const struct sensor_driver_api ism330dhcx_api_funcs = { static int ism330dhcx_init_chip(const struct device *dev) { + const struct ism330dhcx_config * const cfg = dev->config; struct ism330dhcx_data *ism330dhcx = dev->data; uint8_t chip_id; @@ -715,12 +703,11 @@ static int ism330dhcx_init_chip(const struct device *dev) k_busy_wait(100); - if (ism330dhcx_accel_set_fs_raw(dev, - ISM330DHCX_DEFAULT_ACCEL_FULLSCALE) < 0) { + LOG_DBG("accel range is %d", cfg->accel_range); + if (ism330dhcx_accel_range_set(dev, cfg->accel_range) < 0) { LOG_DBG("failed to set accelerometer full-scale"); return -EIO; } - ism330dhcx->acc_gain = ISM330DHCX_DEFAULT_ACCEL_SENSITIVITY; ism330dhcx->accel_freq = ism330dhcx_odr_to_freq_val(CONFIG_ISM330DHCX_ACCEL_ODR); if (ism330dhcx_accel_set_odr_raw(dev, CONFIG_ISM330DHCX_ACCEL_ODR) < 0) { @@ -728,11 +715,11 @@ static int ism330dhcx_init_chip(const struct device *dev) return -EIO; } - if (ism330dhcx_gyro_set_fs_raw(dev, ISM330DHCX_DEFAULT_GYRO_FULLSCALE) < 0) { + LOG_DBG("gyro range is %d", cfg->gyro_range); + if (ism330dhcx_gyro_range_set(dev, cfg->gyro_range) < 0) { LOG_DBG("failed to set gyroscope full-scale"); return -EIO; } - ism330dhcx->gyro_gain = ISM330DHCX_DEFAULT_GYRO_SENSITIVITY; ism330dhcx->gyro_freq = ism330dhcx_odr_to_freq_val(CONFIG_ISM330DHCX_GYRO_ODR); if (ism330dhcx_gyro_set_odr_raw(dev, CONFIG_ISM330DHCX_GYRO_ODR) < 0) { @@ -758,6 +745,8 @@ static struct ism330dhcx_data ism330dhcx_data; static const struct ism330dhcx_config ism330dhcx_config = { .bus_name = DT_INST_BUS_LABEL(0), + .accel_range = DT_INST_PROP(0, accel_range), + .gyro_range = DT_INST_PROP(0, gyro_range), #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) .bus_init = ism330dhcx_spi_init, .spi_conf.frequency = DT_INST_PROP(0, spi_max_frequency), diff --git a/drivers/sensor/ism330dhcx/ism330dhcx.h b/drivers/sensor/ism330dhcx/ism330dhcx.h index 5087affa4b1..c21a59ca339 100644 --- a/drivers/sensor/ism330dhcx/ism330dhcx.h +++ b/drivers/sensor/ism330dhcx/ism330dhcx.h @@ -41,52 +41,10 @@ union axis1bit16_t { #define SENSOR_DEG2RAD_DOUBLE (SENSOR_PI_DOUBLE / 180) #define SENSOR_G_DOUBLE (SENSOR_G / 1000000.0) -#if CONFIG_ISM330DHCX_ACCEL_FS == 0 - #define ISM330DHCX_ACCEL_FS_RUNTIME 1 - #define ISM330DHCX_DEFAULT_ACCEL_FULLSCALE 0 - #define ISM330DHCX_DEFAULT_ACCEL_SENSITIVITY GAIN_UNIT_XL -#elif CONFIG_ISM330DHCX_ACCEL_FS == 2 - #define ISM330DHCX_DEFAULT_ACCEL_FULLSCALE 0 - #define ISM330DHCX_DEFAULT_ACCEL_SENSITIVITY GAIN_UNIT_XL -#elif CONFIG_ISM330DHCX_ACCEL_FS == 4 - #define ISM330DHCX_DEFAULT_ACCEL_FULLSCALE 2 - #define ISM330DHCX_DEFAULT_ACCEL_SENSITIVITY (2.0 * GAIN_UNIT_XL) -#elif CONFIG_ISM330DHCX_ACCEL_FS == 8 - #define ISM330DHCX_DEFAULT_ACCEL_FULLSCALE 3 - #define ISM330DHCX_DEFAULT_ACCEL_SENSITIVITY (4.0 * GAIN_UNIT_XL) -#elif CONFIG_ISM330DHCX_ACCEL_FS == 16 - #define ISM330DHCX_DEFAULT_ACCEL_FULLSCALE 1 - #define ISM330DHCX_DEFAULT_ACCEL_SENSITIVITY (8.0 * GAIN_UNIT_XL) -#endif - #if (CONFIG_ISM330DHCX_ACCEL_ODR == 0) #define ISM330DHCX_ACCEL_ODR_RUNTIME 1 #endif -#define GYRO_FULLSCALE_125 4 - -#if CONFIG_ISM330DHCX_GYRO_FS == 0 - #define ISM330DHCX_GYRO_FS_RUNTIME 1 - #define ISM330DHCX_DEFAULT_GYRO_FULLSCALE 4 - #define ISM330DHCX_DEFAULT_GYRO_SENSITIVITY GAIN_UNIT_G -#elif CONFIG_ISM330DHCX_GYRO_FS == 125 - #define ISM330DHCX_DEFAULT_GYRO_FULLSCALE 4 - #define ISM330DHCX_DEFAULT_GYRO_SENSITIVITY GAIN_UNIT_G -#elif CONFIG_ISM330DHCX_GYRO_FS == 250 - #define ISM330DHCX_DEFAULT_GYRO_FULLSCALE 0 - #define ISM330DHCX_DEFAULT_GYRO_SENSITIVITY (2.0 * GAIN_UNIT_G) -#elif CONFIG_ISM330DHCX_GYRO_FS == 500 - #define ISM330DHCX_DEFAULT_GYRO_FULLSCALE 1 - #define ISM330DHCX_DEFAULT_GYRO_SENSITIVITY (4.0 * GAIN_UNIT_G) -#elif CONFIG_ISM330DHCX_GYRO_FS == 1000 - #define ISM330DHCX_DEFAULT_GYRO_FULLSCALE 2 - #define ISM330DHCX_DEFAULT_GYRO_SENSITIVITY (8.0 * GAIN_UNIT_G) -#elif CONFIG_ISM330DHCX_GYRO_FS == 2000 - #define ISM330DHCX_DEFAULT_GYRO_FULLSCALE 3 - #define ISM330DHCX_DEFAULT_GYRO_SENSITIVITY (16.0 * GAIN_UNIT_G) -#endif - - #if (CONFIG_ISM330DHCX_GYRO_ODR == 0) #define ISM330DHCX_GYRO_ODR_RUNTIME 1 #endif @@ -94,6 +52,8 @@ union axis1bit16_t { struct ism330dhcx_config { char *bus_name; int (*bus_init)(const struct device *dev); + uint8_t accel_range; + uint16_t gyro_range; #ifdef CONFIG_ISM330DHCX_TRIGGER const char *int_gpio_port; uint8_t int_gpio_pin; diff --git a/dts/bindings/sensor/st,ism330dhcx-common.yaml b/dts/bindings/sensor/st,ism330dhcx-common.yaml index effcef93b97..9add7e247f2 100644 --- a/dts/bindings/sensor/st,ism330dhcx-common.yaml +++ b/dts/bindings/sensor/st,ism330dhcx-common.yaml @@ -11,3 +11,41 @@ properties: This pin defaults to active high when produced by the sensor. The property value should ensure the flags properly describe the signal that is presented to the driver. + + accel-range: + type: int + required: false + default: 2 + description: | + Range in g. Default is power-up configuration. + + Selection + 16 16g (0.488 mg/LSB) + 8 8g (0.244 mg/LSB) + 4 4g (0.122 mg/LSB) + 2 2g (0.061 mg/LSB) + enum: + - 16 + - 8 + - 4 + - 2 + + gyro-range: + type: int + required: false + default: 125 + description: | + Range in dps. Default is power-up configuration. + + Selection + 125 +/- 125dps + 250 +/- 250dps + 500 +/- 500dps + 1000 +/- 1000dps + 2000 +/- 2000dps + enum: + - 125 + - 250 + - 500 + - 1000 + - 2000