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/>.
25 #include "common/axis.h"
26 #include "common/maths.h"
27 #include "common/sensor_alignment.h"
28 #include "common/time.h"
29 #include "drivers/exti.h"
30 #include "drivers/bus.h"
31 #include "drivers/sensor.h"
32 #include "drivers/accgyro/accgyro_mpu.h"
34 #pragma GCC diagnostic push
35 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
39 #define GYRO_SCALE_2000DPS (2000.0f / (1 << 15)) // 16.384 dps/lsb scalefactor for 2000dps sensors
40 #define GYRO_SCALE_4000DPS (4000.0f / (1 << 15)) // 8.192 dps/lsb scalefactor for 4000dps sensors
66 GYRO_HARDWARE_LPF_NORMAL
,
67 #ifdef USE_GYRO_DLPF_EXPERIMENTAL
68 GYRO_HARDWARE_LPF_EXPERIMENTAL
90 typedef struct gyroDev_s
{
91 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
94 sensorGyroInitFuncPtr initFn
; // initialize function
95 sensorGyroReadFuncPtr readFn
; // read 3 axis data function
96 sensorGyroReadDataFuncPtr temperatureFn
; // read temperature if available
97 extiCallbackRec_t exti
;
99 float scale
; // scalefactor
100 float gyroZero
[XYZ_AXIS_COUNT
];
101 float gyroADC
[XYZ_AXIS_COUNT
]; // gyro data after calibration and alignment
102 int32_t gyroADCRawPrevious
[XYZ_AXIS_COUNT
];
103 int16_t gyroADCRaw
[XYZ_AXIS_COUNT
]; // raw data from sensor
105 mpuDetectionResult_t mpuDetectionResult
;
106 sensor_align_e gyroAlign
;
107 gyroRateKHz_e gyroRateKHz
;
108 gyroModeSPI_e gyroModeSPI
;
110 uint32_t detectedEXTI
;
111 uint32_t gyroLastEXTI
;
112 uint32_t gyroSyncEXTI
;
113 int32_t gyroShortPeriod
;
114 int32_t gyroDmaMaxDuration
;
115 busSegment_t segments
[2];
117 volatile bool dataReady
;
119 uint8_t hardware_lpf
;
120 uint8_t hardware_32khz_lpf
;
121 uint8_t mpuDividerDrops
;
122 ioTag_t mpuIntExtiTag
;
123 uint8_t gyroHasOverflowProtection
;
124 gyroHardware_e gyroHardware
;
125 fp_rotationMatrix_t rotationMatrix
;
126 uint16_t gyroSampleRateHz
;
127 uint16_t accSampleRateHz
;
132 typedef struct accDev_s
{
133 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
134 pthread_mutex_t lock
;
137 sensorAccInitFuncPtr initFn
; // initialize function
138 sensorAccReadFuncPtr readFn
; // read 3 axis data function
140 int16_t ADCRaw
[XYZ_AXIS_COUNT
];
141 mpuDetectionResult_t mpuDetectionResult
;
142 sensor_align_e accAlign
;
146 char revisionCode
; // a revision code for the sensor, if known
148 fp_rotationMatrix_t rotationMatrix
;
151 static inline void accDevLock(accDev_t
*acc
)
153 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
154 pthread_mutex_lock(&acc
->lock
);
160 static inline void accDevUnLock(accDev_t
*acc
)
162 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
163 pthread_mutex_unlock(&acc
->lock
);
169 static inline void gyroDevLock(gyroDev_t
*gyro
)
171 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
172 pthread_mutex_lock(&gyro
->lock
);
178 static inline void gyroDevUnLock(gyroDev_t
*gyro
)
180 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
181 pthread_mutex_unlock(&gyro
->lock
);
186 #pragma GCC diagnostic pop