2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
26 #ifdef USE_ACCGYRO_LSM6DSO
28 #include "drivers/accgyro/accgyro.h"
29 #include "drivers/accgyro/accgyro_spi_lsm6dso.h"
30 #include "drivers/bus_spi.h"
31 #include "drivers/exti.h"
32 #include "drivers/io.h"
33 #include "drivers/io_impl.h"
34 #include "drivers/nvic.h"
35 #include "drivers/sensor.h"
36 #include "drivers/time.h"
38 // 10 MHz max SPI frequency
39 #define LSM6DSO_MAX_SPI_CLK_HZ 10000000
41 #define LSM6DSO_CHIP_ID 0x6C
43 // LSM6DSO register configuration values
45 LSM6DSO_VAL_INT1_CTRL
= 0x02, // enable gyro data ready interrupt pin 1
46 LSM6DSO_VAL_INT2_CTRL
= 0x02, // enable gyro data ready interrupt pin 2
47 LSM6DSO_VAL_CTRL1_XL_ODR833
= 0x07, // accelerometer 833hz output data rate (gyro/8)
48 LSM6DSO_VAL_CTRL1_XL_ODR1667
= 0x08, // accelerometer 1666hz output data rate (gyro/4)
49 LSM6DSO_VAL_CTRL1_XL_ODR3332
= 0x09, // accelerometer 3332hz output data rate (gyro/2)
50 LSM6DSO_VAL_CTRL1_XL_ODR3333
= 0x0A, // accelerometer 6664hz output data rate (gyro/1)
51 LSM6DSO_VAL_CTRL1_XL_8G
= 0x03, // accelerometer 8G scale
52 LSM6DSO_VAL_CTRL1_XL_16G
= 0x01, // accelerometer 16G scale
53 LSM6DSO_VAL_CTRL1_XL_LPF1
= 0x00, // accelerometer output from LPF1
54 LSM6DSO_VAL_CTRL1_XL_LPF2
= 0x01, // accelerometer output from LPF2
55 LSM6DSO_VAL_CTRL2_G_ODR6664
= 0x0A, // gyro 6664hz output data rate
56 LSM6DSO_VAL_CTRL2_G_2000DPS
= 0x03, // gyro 2000dps scale
57 LSM6DSO_VAL_CTRL3_C_BDU
= BIT(6), // (bit 6) output registers are not updated until MSB and LSB have been read (prevents MSB from being updated while burst reading LSB/MSB)
58 LSM6DSO_VAL_CTRL3_C_H_LACTIVE
= 0, // (bit 5) interrupt pins active high
59 LSM6DSO_VAL_CTRL3_C_PP_OD
= 0, // (bit 4) interrupt pins push/pull
60 LSM6DSO_VAL_CTRL3_C_SIM
= 0, // (bit 3) SPI 4-wire interface mode
61 LSM6DSO_VAL_CTRL3_C_IF_INC
= BIT(2), // (bit 2) auto-increment address for burst reads
62 LSM6DSO_VAL_CTRL4_C_I2C_DISABLE
= BIT(2), // (bit 2) disable I2C interface
63 LSM6DSO_VAL_CTRL4_C_LPF1_SEL_G
= BIT(1), // (bit 1) enable gyro LPF1
64 LSM6DSO_VAL_CTRL6_C_XL_HM_MODE
= 0, // (bit 4) enable accelerometer high performance mode
65 LSM6DSO_VAL_CTRL6_C_FTYPE_335HZ
= 0x00, // (bits 2:0) gyro LPF1 cutoff 335.5hz
66 LSM6DSO_VAL_CTRL6_C_FTYPE_232HZ
= 0x01, // (bits 2:0) gyro LPF1 cutoff 232.0hz
67 LSM6DSO_VAL_CTRL6_C_FTYPE_171HZ
= 0x02, // (bits 2:0) gyro LPF1 cutoff 171.1hz
68 LSM6DSO_VAL_CTRL6_C_FTYPE_609HZ
= 0x03, // (bits 2:0) gyro LPF1 cutoff 609.0hz
69 LSM6DSO_VAL_CTRL9_XL_I3C_DISABLE
= BIT(1),// (bit 1) disable I3C interface
70 } lsm6dsoConfigValues_e
;
72 // LSM6DSO register configuration bit masks
74 LSM6DSO_MASK_CTRL3_C
= 0x7C, // 0b01111100
75 LSM6DSO_MASK_CTRL3_C_RESET
= BIT(0), // 0b00000001
76 LSM6DSO_MASK_CTRL4_C
= 0x06, // 0b00000110
77 LSM6DSO_MASK_CTRL6_C
= 0x17, // 0b00010111
78 LSM6DSO_MASK_CTRL9_XL
= 0x02, // 0b00000010
79 } lsm6dsoConfigMasks_e
;
81 uint8_t lsm6dsoDetect(const extDevice_t
*dev
)
85 if (busReadRegisterBuffer(dev
, LSM6DSO_REG_WHO_AM_I
, &chipID
, 1)) {
86 if (chipID
== LSM6DSO_CHIP_ID
) {
94 static void lsm6dsoWriteRegister(const extDevice_t
*dev
, lsm6dsoRegister_e registerID
, uint8_t value
, unsigned delayMs
)
96 busWriteRegister(dev
, registerID
, value
);
102 static void lsm6dsoWriteRegisterBits(const extDevice_t
*dev
, lsm6dsoRegister_e registerID
, lsm6dsoConfigMasks_e mask
, uint8_t value
, unsigned delayMs
)
105 if (busReadRegisterBuffer(dev
, registerID
, &newValue
, 1)) {
106 delayMicroseconds(2);
107 newValue
= (newValue
& ~mask
) | value
;
108 lsm6dsoWriteRegister(dev
, registerID
, newValue
, delayMs
);
112 static void lsm6dsoConfig(gyroDev_t
*gyro
)
114 extDevice_t
*dev
= &gyro
->dev
;
116 // Reset the device (wait 100ms before continuing config)
117 lsm6dsoWriteRegisterBits(dev
, LSM6DSO_REG_CTRL3_C
, LSM6DSO_MASK_CTRL3_C_RESET
, BIT(0), 100);
119 // Configure interrupt pin 1 for gyro data ready only
120 lsm6dsoWriteRegister(dev
, LSM6DSO_REG_INT1_CTRL
, LSM6DSO_VAL_INT1_CTRL
, 1);
122 // Disable interrupt pin 2
123 lsm6dsoWriteRegister(dev
, LSM6DSO_REG_INT2_CTRL
, LSM6DSO_VAL_INT2_CTRL
, 1);
125 // Configure the accelerometer
126 // 833hz ODR, 16G scale, use LPF1 output
127 lsm6dsoWriteRegister(dev
, LSM6DSO_REG_CTRL1_XL
, (LSM6DSO_VAL_CTRL1_XL_ODR833
<< 4) | (LSM6DSO_VAL_CTRL1_XL_16G
<< 2) | (LSM6DSO_VAL_CTRL1_XL_LPF1
<< 1), 1);
129 // Configure the gyro
130 // 6664hz ODR, 2000dps scale
131 lsm6dsoWriteRegister(dev
, LSM6DSO_REG_CTRL2_G
, (LSM6DSO_VAL_CTRL2_G_ODR6664
<< 4) | (LSM6DSO_VAL_CTRL2_G_2000DPS
<< 2), 1);
133 // Configure control register 3
134 // latch LSB/MSB during reads; set interrupt pins active high; set interrupt pins push/pull; set 4-wire SPI; enable auto-increment burst reads
135 lsm6dsoWriteRegisterBits(dev
, LSM6DSO_REG_CTRL3_C
, LSM6DSO_MASK_CTRL3_C
, (LSM6DSO_VAL_CTRL3_C_BDU
| LSM6DSO_VAL_CTRL3_C_H_LACTIVE
| LSM6DSO_VAL_CTRL3_C_PP_OD
| LSM6DSO_VAL_CTRL3_C_SIM
| LSM6DSO_VAL_CTRL3_C_IF_INC
), 1);
137 // Configure control register 4
138 // enable accelerometer high performane mode; set gyro LPF1 cutoff to 335.5hz
139 lsm6dsoWriteRegisterBits(dev
, LSM6DSO_REG_CTRL4_C
, LSM6DSO_MASK_CTRL4_C
, (LSM6DSO_VAL_CTRL4_C_I2C_DISABLE
| LSM6DSO_VAL_CTRL4_C_LPF1_SEL_G
), 1);
141 // Configure control register 6
142 // disable I2C interface; enable gyro LPF1
143 lsm6dsoWriteRegisterBits(dev
, LSM6DSO_REG_CTRL6_C
, LSM6DSO_MASK_CTRL6_C
, (LSM6DSO_VAL_CTRL6_C_XL_HM_MODE
| LSM6DSO_VAL_CTRL6_C_FTYPE_335HZ
), 1);
145 // Configure control register 9
146 // disable I3C interface
147 lsm6dsoWriteRegisterBits(dev
, LSM6DSO_REG_CTRL9_XL
, LSM6DSO_MASK_CTRL9_XL
, LSM6DSO_VAL_CTRL9_XL_I3C_DISABLE
, 1);
150 static void lsm6dsoIntExtiInit(gyroDev_t
*gyro
)
152 if (gyro
->mpuIntExtiTag
== IO_TAG_NONE
) {
156 IO_t mpuIntIO
= IOGetByTag(gyro
->mpuIntExtiTag
);
158 IOInit(mpuIntIO
, OWNER_GYRO_EXTI
, 0);
159 EXTIHandlerInit(&gyro
->exti
, lsm6dsoExtiHandler
);
160 EXTIConfig(mpuIntIO
, &gyro
->exti
, NVIC_PRIO_MPU_INT_EXTI
, IOCFG_IN_FLOATING
, BETAFLIGHT_EXTI_TRIGGER_RISING
);
161 EXTIEnable(mpuIntIO
);
164 static void lsm6dsoSpiGyroInit(gyroDev_t
*gyro
)
166 extDevice_t
*dev
= &gyro
->dev
;
170 lsm6dsoIntExtiInit(gyro
);
172 spiSetClkDivisor(dev
, spiCalculateDivider(LSM6DSO_MAX_SPI_CLK_HZ
));
175 static void lsm6dsoSpiAccInit(accDev_t
*acc
)
177 // sensor is configured during gyro init
178 acc
->acc_1G
= 512 * 4; // 16G sensor scale
181 bool lsm6dsoSpiAccDetect(accDev_t
*acc
)
183 if (acc
->mpuDetectionResult
.sensor
!= LSM6DSO_SPI
) {
187 acc
->initFn
= lsm6dsoSpiAccInit
;
188 acc
->readFn
= lsm6dsoAccRead
;
193 bool lsm6dsoSpiGyroDetect(gyroDev_t
*gyro
)
195 if (gyro
->mpuDetectionResult
.sensor
!= LSM6DSO_SPI
) {
199 gyro
->initFn
= lsm6dsoSpiGyroInit
;
200 gyro
->readFn
= lsm6dsoGyroRead
;
201 gyro
->scale
= GYRO_SCALE_2000DPS
;
205 #endif // USE_ACCGYRO_LSM6DSO