Encapsulate LSM6DSV16X driver specific macros (#13196)
[betaflight.git] / src / main / drivers / accgyro / accgyro.h
blobb6aa4973338d2fc38276084b1fc2618f92b156b3
1 /*
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)
8 * any later version.
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/>.
21 #pragma once
23 #include "platform.h"
25 #include "common/axis.h"
26 #include "common/maths.h"
27 #include "common/sensor_alignment.h"
28 #include "common/time.h"
30 #include "drivers/accgyro/accgyro_mpu.h"
31 #include "drivers/bus.h"
32 #include "drivers/exti.h"
33 #include "drivers/sensor.h"
35 #pragma GCC diagnostic push
36 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
37 #include <pthread.h>
38 #endif
40 #define GYRO_SCALE_2000DPS (2000.0f / (1 << 15)) // 16.384 dps/lsb scalefactor for 2000dps sensors
41 #define GYRO_SCALE_4000DPS (4000.0f / (1 << 15)) // 8.192 dps/lsb scalefactor for 4000dps sensors
43 typedef enum {
44 GYRO_NONE = 0,
45 GYRO_DEFAULT,
46 GYRO_MPU6050,
47 GYRO_L3G4200D,
48 GYRO_MPU3050,
49 GYRO_L3GD20,
50 GYRO_MPU6000,
51 GYRO_MPU6500,
52 GYRO_MPU9250,
53 GYRO_ICM20601,
54 GYRO_ICM20602,
55 GYRO_ICM20608G,
56 GYRO_ICM20649,
57 GYRO_ICM20689,
58 GYRO_ICM42605,
59 GYRO_ICM42688P,
60 GYRO_BMI160,
61 GYRO_BMI270,
62 GYRO_LSM6DSO,
63 GYRO_LSM6DSV16X,
64 GYRO_VIRTUAL
65 } gyroHardware_e;
67 typedef enum {
68 GYRO_HARDWARE_LPF_NORMAL,
69 GYRO_HARDWARE_LPF_OPTION_1,
70 GYRO_HARDWARE_LPF_OPTION_2,
71 #ifdef USE_GYRO_DLPF_EXPERIMENTAL
72 GYRO_HARDWARE_LPF_EXPERIMENTAL,
73 #endif
74 GYRO_HARDWARE_LPF_COUNT
75 } gyroHardwareLpf_e;
77 typedef enum {
78 GYRO_RATE_1_kHz,
79 GYRO_RATE_1100_Hz,
80 GYRO_RATE_3200_Hz,
81 GYRO_RATE_6400_Hz,
82 GYRO_RATE_6664_Hz,
83 GYRO_RATE_8_kHz,
84 GYRO_RATE_9_kHz,
85 GYRO_RATE_32_kHz,
86 } gyroRateKHz_e;
88 typedef enum {
89 GYRO_EXTI_INIT = 0,
90 GYRO_EXTI_INT_DMA,
91 GYRO_EXTI_INT,
92 GYRO_EXTI_NO_INT
93 } gyroModeSPI_e;
95 typedef struct gyroDev_s {
96 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
97 pthread_mutex_t lock;
98 #endif
99 sensorGyroInitFuncPtr initFn; // initialize function
100 sensorGyroReadFuncPtr readFn; // read 3 axis data function
101 sensorGyroReadDataFuncPtr temperatureFn; // read temperature if available
102 extiCallbackRec_t exti;
103 extDevice_t dev;
104 float scale; // scalefactor
105 float gyroZero[XYZ_AXIS_COUNT];
106 float gyroADC[XYZ_AXIS_COUNT]; // gyro data after calibration and alignment
107 int32_t gyroADCRawPrevious[XYZ_AXIS_COUNT];
108 int16_t gyroADCRaw[XYZ_AXIS_COUNT]; // raw data from sensor
109 int16_t temperature;
110 mpuDetectionResult_t mpuDetectionResult;
111 sensor_align_e gyroAlign;
112 gyroRateKHz_e gyroRateKHz;
113 gyroModeSPI_e gyroModeSPI;
114 uint32_t detectedEXTI;
115 uint32_t gyroLastEXTI;
116 uint32_t gyroSyncEXTI;
117 int32_t gyroShortPeriod;
118 int32_t gyroDmaMaxDuration;
119 busSegment_t segments[2];
120 volatile bool dataReady;
121 bool gyro_high_fsr;
122 uint8_t hardware_lpf;
123 uint8_t hardware_32khz_lpf;
124 uint8_t mpuDividerDrops;
125 ioTag_t mpuIntExtiTag;
126 uint8_t gyroHasOverflowProtection;
127 gyroHardware_e gyroHardware;
128 fp_rotationMatrix_t rotationMatrix;
129 uint16_t gyroSampleRateHz;
130 uint16_t accSampleRateHz;
131 uint8_t accDataReg;
132 uint8_t gyroDataReg;
133 } gyroDev_t;
135 typedef struct accDev_s {
136 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
137 pthread_mutex_t lock;
138 #endif
139 float acc_1G_rec;
140 sensorAccInitFuncPtr initFn; // initialize function
141 sensorAccReadFuncPtr readFn; // read 3 axis data function
142 uint16_t acc_1G;
143 int16_t ADCRaw[XYZ_AXIS_COUNT];
144 mpuDetectionResult_t mpuDetectionResult;
145 sensor_align_e accAlign;
146 bool dataReady;
147 gyroDev_t *gyro;
148 bool acc_high_fsr;
149 char revisionCode; // a revision code for the sensor, if known
150 uint8_t filler[2];
151 fp_rotationMatrix_t rotationMatrix;
152 } accDev_t;
154 static inline void accDevLock(accDev_t *acc)
156 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
157 pthread_mutex_lock(&acc->lock);
158 #else
159 (void)acc;
160 #endif
163 static inline void accDevUnLock(accDev_t *acc)
165 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
166 pthread_mutex_unlock(&acc->lock);
167 #else
168 (void)acc;
169 #endif
172 static inline void gyroDevLock(gyroDev_t *gyro)
174 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
175 pthread_mutex_lock(&gyro->lock);
176 #else
177 (void)gyro;
178 #endif
181 static inline void gyroDevUnLock(gyroDev_t *gyro)
183 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
184 pthread_mutex_unlock(&gyro->lock);
185 #else
186 (void)gyro;
187 #endif
189 #pragma GCC diagnostic pop