Skip to content

Commit 224b5b7

Browse files
TilmannUntekartben
authored andcommitted
drivers: sensor: mpu6050: scale settings to DT, adds sample rate setting
Moves the MPU6050 accel/gyro scale settings from KConfig to Devicetree. Adds a new setting for the MPU6050 sample rate divider register and transmits it to the sensor upon initialization. This helps to reduce the interrupt firing rate when combined with the data ready trigger. A default division factor is provided which ensures compatibility with existing applications. The MPU6050 sample application is extended and used for hardware tests. Signed-off-by: Tilmann Unte <[email protected]>
1 parent 41d5eed commit 224b5b7

File tree

6 files changed

+55
-20
lines changed

6 files changed

+55
-20
lines changed

drivers/sensor/tdk/mpu6050/Kconfig

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,4 @@ config MPU6050_THREAD_STACK_SIZE
5353
help
5454
Stack size of thread used by the driver to handle interrupts.
5555

56-
config MPU6050_ACCEL_FS
57-
int "Accelerometer full-scale range"
58-
default 2
59-
help
60-
Magnetometer full-scale range.
61-
An X value for the config represents a range of +/- X g. Valid
62-
values are 2, 4, 8 and 16.
63-
64-
config MPU6050_GYRO_FS
65-
int "Gyroscope full-scale range"
66-
default 250
67-
help
68-
Gyroscope full-scale range.
69-
An X value for the config represents a range of +/- X degrees/second.
70-
Valid values are 250, 500, 1000, 2000.
71-
7256
endif # MPU6050

drivers/sensor/tdk/mpu6050/mpu6050.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ int mpu6050_init(const struct device *dev)
186186

187187
/* set accelerometer full-scale range */
188188
for (i = 0U; i < 4; i++) {
189-
if (BIT(i+1) == CONFIG_MPU6050_ACCEL_FS) {
189+
if (BIT(i+1) == cfg->accel_fs) {
190190
break;
191191
}
192192
}
@@ -206,7 +206,7 @@ int mpu6050_init(const struct device *dev)
206206

207207
/* set gyroscope full-scale range */
208208
for (i = 0U; i < 4; i++) {
209-
if (BIT(i) * 250 == CONFIG_MPU6050_GYRO_FS) {
209+
if (BIT(i) * 250 == cfg->gyro_fs) {
210210
break;
211211
}
212212
}
@@ -224,6 +224,12 @@ int mpu6050_init(const struct device *dev)
224224

225225
drv_data->gyro_sensitivity_x10 = mpu6050_gyro_sensitivity_x10[i];
226226

227+
if (i2c_reg_write_byte_dt(&cfg->i2c, MPU6050_REG_SMPLRT_DIV,
228+
cfg->smplrt_div) < 0) {
229+
LOG_ERR("Failed to write samplerate divider.");
230+
return -EIO;
231+
}
232+
227233
#ifdef CONFIG_MPU6050_TRIGGER
228234
if (cfg->int_gpio.port) {
229235
if (mpu6050_init_interrupt(dev) < 0) {
@@ -241,6 +247,9 @@ int mpu6050_init(const struct device *dev)
241247
\
242248
static const struct mpu6050_config mpu6050_config_##inst = { \
243249
.i2c = I2C_DT_SPEC_INST_GET(inst), \
250+
.accel_fs = DT_INST_PROP(inst, accel_fs), \
251+
.gyro_fs = DT_INST_PROP(inst, gyro_fs), \
252+
.smplrt_div = DT_INST_PROP(inst, smplrt_div), \
244253
IF_ENABLED(CONFIG_MPU6050_TRIGGER, \
245254
(.int_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int_gpios, { 0 }),)) \
246255
}; \

drivers/sensor/tdk/mpu6050/mpu6050.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#define MPU9250_CHIP_ID 0x71
2121
#define MPU6880_CHIP_ID 0x19
2222

23+
#define MPU6050_REG_SMPLRT_DIV 0x19
24+
2325
#define MPU6050_REG_GYRO_CFG 0x1B
2426
#define MPU6050_GYRO_FS_SHIFT 3
2527

@@ -80,6 +82,9 @@ struct mpu6050_data {
8082

8183
struct mpu6050_config {
8284
struct i2c_dt_spec i2c;
85+
uint8_t accel_fs;
86+
uint16_t gyro_fs;
87+
uint8_t smplrt_div;
8388
#ifdef CONFIG_MPU6050_TRIGGER
8489
struct gpio_dt_spec int_gpio;
8590
#endif /* CONFIG_MPU6050_TRIGGER */

dts/bindings/sensor/invensense,mpu6050.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,39 @@ properties:
1616
The INT signal default configuration is active-high. The
1717
property value should ensure the flags properly describe the
1818
signal that is presented to the driver.
19+
20+
accel-fs:
21+
type: int
22+
default: 2
23+
description: |
24+
Accelerometer full-scale range.
25+
An X value represents a range of +/- X g.
26+
The power-on reset state of the sensor matches the default value of 2.
27+
enum:
28+
- 2
29+
- 4
30+
- 8
31+
- 16
32+
33+
gyro-fs:
34+
type: int
35+
default: 250
36+
description: |
37+
Gyroscope full-scale range.
38+
An X value represents a range of +/- X deg/s.
39+
The power-on reset state of the sensor matches the default value of 250.
40+
enum:
41+
- 250
42+
- 500
43+
- 1000
44+
- 2000
45+
46+
smplrt-div:
47+
type: int
48+
default: 0
49+
description: |
50+
The base sample rate is 8 kHz for the gyroscope and 1 kHz for
51+
the accelerometer.
52+
A division factor of X sets the sample rate to: baserate / (1 + X).
53+
Valid values for X are 0 through 255.
54+
The power-on reset state of the sensor matches the default value of 0.

samples/sensor/mpu6050/README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ This sample application periodically (0.5 Hz) measures the sensor
1212
temperature, acceleration, and angular velocity, displaying the values
1313
on the console along with a timestamp since startup.
1414

15-
When triggered mode is enabled the measurements are displayed at the
16-
rate they are produced by the sensor.
15+
When triggered mode is enabled the measurements are displayed at a rate
16+
of 32Hz governed by the sensor's internal clock and divider.
1717

1818
Wiring
1919
*******

samples/sensor/mpu6050/boards/nrf52dk_nrf52832.overlay

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
reg = <0x68>;
1111
status = "okay";
1212
int-gpios = <&gpio0 11 GPIO_ACTIVE_HIGH>;
13+
smplrt-div = <249>;
1314
};
1415
};

0 commit comments

Comments
 (0)