New SPI API supporting DMA
[betaflight.git] / src / main / drivers / accgyro / accgyro.h
blob91936505269ed3fe30cff1c8867c921a81d165e6
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 "drivers/exti.h"
29 #include "drivers/bus.h"
30 #include "drivers/sensor.h"
31 #include "drivers/accgyro/accgyro_mpu.h"
33 #pragma GCC diagnostic push
34 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
35 #include <pthread.h>
36 #endif
38 #define GYRO_SCALE_2000DPS (2000.0f / (1 << 15)) // 16.384 dps/lsb scalefactor for 2000dps sensors
39 #define GYRO_SCALE_4000DPS (4000.0f / (1 << 15)) // 8.192 dps/lsb scalefactor for 4000dps sensors
41 typedef enum {
42 GYRO_NONE = 0,
43 GYRO_DEFAULT,
44 GYRO_MPU6050,
45 GYRO_L3G4200D,
46 GYRO_MPU3050,
47 GYRO_L3GD20,
48 GYRO_MPU6000,
49 GYRO_MPU6500,
50 GYRO_MPU9250,
51 GYRO_ICM20601,
52 GYRO_ICM20602,
53 GYRO_ICM20608G,
54 GYRO_ICM20649,
55 GYRO_ICM20689,
56 GYRO_ICM42605,
57 GYRO_BMI160,
58 GYRO_BMI270,
59 GYRO_LSM6DSO,
60 GYRO_FAKE
61 } gyroHardware_e;
63 typedef enum {
64 GYRO_HARDWARE_LPF_NORMAL,
65 #ifdef USE_GYRO_DLPF_EXPERIMENTAL
66 GYRO_HARDWARE_LPF_EXPERIMENTAL
67 #endif
68 } gyroHardwareLpf_e;
70 typedef enum {
71 GYRO_RATE_1_kHz,
72 GYRO_RATE_1100_Hz,
73 GYRO_RATE_3200_Hz,
74 GYRO_RATE_6400_Hz,
75 GYRO_RATE_6664_Hz,
76 GYRO_RATE_8_kHz,
77 GYRO_RATE_9_kHz,
78 GYRO_RATE_32_kHz,
79 } gyroRateKHz_e;
81 typedef struct gyroDev_s {
82 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
83 pthread_mutex_t lock;
84 #endif
85 sensorGyroInitFuncPtr initFn; // initialize function
86 sensorGyroReadFuncPtr readFn; // read 3 axis data function
87 sensorGyroReadDataFuncPtr temperatureFn; // read temperature if available
88 extiCallbackRec_t exti;
89 extDevice_t dev;
90 float scale; // scalefactor
91 float gyroZero[XYZ_AXIS_COUNT];
92 float gyroADC[XYZ_AXIS_COUNT]; // gyro data after calibration and alignment
93 int32_t gyroADCRawPrevious[XYZ_AXIS_COUNT];
94 int16_t gyroADCRaw[XYZ_AXIS_COUNT]; // raw data from sensor
95 int16_t temperature;
96 mpuDetectionResult_t mpuDetectionResult;
97 sensor_align_e gyroAlign;
98 gyroRateKHz_e gyroRateKHz;
99 bool dataReady;
100 bool gyro_high_fsr;
101 uint8_t hardware_lpf;
102 uint8_t hardware_32khz_lpf;
103 uint8_t mpuDividerDrops;
104 ioTag_t mpuIntExtiTag;
105 uint8_t gyroHasOverflowProtection;
106 gyroHardware_e gyroHardware;
107 fp_rotationMatrix_t rotationMatrix;
108 uint16_t gyroSampleRateHz;
109 uint16_t accSampleRateHz;
110 } gyroDev_t;
112 typedef struct accDev_s {
113 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
114 pthread_mutex_t lock;
115 #endif
116 float acc_1G_rec;
117 sensorAccInitFuncPtr initFn; // initialize function
118 sensorAccReadFuncPtr readFn; // read 3 axis data function
119 uint16_t acc_1G;
120 int16_t ADCRaw[XYZ_AXIS_COUNT];
121 mpuDetectionResult_t mpuDetectionResult;
122 sensor_align_e accAlign;
123 bool dataReady;
124 gyroDev_t *gyro;
125 bool acc_high_fsr;
126 char revisionCode; // a revision code for the sensor, if known
127 uint8_t filler[2];
128 fp_rotationMatrix_t rotationMatrix;
129 } accDev_t;
131 static inline void accDevLock(accDev_t *acc)
133 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
134 pthread_mutex_lock(&acc->lock);
135 #else
136 (void)acc;
137 #endif
140 static inline void accDevUnLock(accDev_t *acc)
142 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
143 pthread_mutex_unlock(&acc->lock);
144 #else
145 (void)acc;
146 #endif
149 static inline void gyroDevLock(gyroDev_t *gyro)
151 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
152 pthread_mutex_lock(&gyro->lock);
153 #else
154 (void)gyro;
155 #endif
158 static inline void gyroDevUnLock(gyroDev_t *gyro)
160 #if defined(SIMULATOR_BUILD) && defined(SIMULATOR_MULTITHREAD)
161 pthread_mutex_unlock(&gyro->lock);
162 #else
163 (void)gyro;
164 #endif
166 #pragma GCC diagnostic pop