2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
25 #if !defined(SITL_BUILD)
26 #include "build/atomic.h"
29 #include "build/build_config.h"
31 #include "common/log.h"
32 #include "common/maths.h"
33 #include "common/utils.h"
35 #include "drivers/bus.h"
36 #include "drivers/io.h"
37 #include "drivers/nvic.h"
38 #include "drivers/sensor.h"
39 #include "drivers/system.h"
40 #include "drivers/time.h"
42 #include "drivers/sensor.h"
43 #include "drivers/accgyro/accgyro.h"
45 const gyroFilterAndRateConfig_t
* chooseGyroConfig(uint8_t desiredLpf
, uint16_t desiredRateHz
, const gyroFilterAndRateConfig_t
* configs
, int count
)
48 int8_t selectedLpf
= configs
[0].gyroLpf
;
49 const gyroFilterAndRateConfig_t
* candidate
= &configs
[0];
51 // Choose closest supported LPF value
52 for (i
= 1; i
< count
; i
++) {
53 if (ABS(desiredLpf
- configs
[i
].gyroLpf
) < ABS(desiredLpf
- selectedLpf
)) {
54 selectedLpf
= configs
[i
].gyroLpf
;
55 candidate
= &configs
[i
];
59 // Now find the closest update rate
60 for (i
= 0; i
< count
; i
++) {
61 if ((configs
[i
].gyroLpf
== selectedLpf
) && (ABS(desiredRateHz
- candidate
->gyroRateHz
) > ABS(desiredRateHz
- configs
[i
].gyroRateHz
))) {
62 candidate
= &configs
[i
];
66 LOG_VERBOSE(GYRO
, "GYRO CONFIG { %d, %d } -> { %d, %d}; regs 0x%02X, 0x%02X",
67 desiredLpf
, desiredRateHz
,
68 candidate
->gyroLpf
, candidate
->gyroRateHz
,
69 candidate
->gyroConfigValues
[0], candidate
->gyroConfigValues
[1]);
74 bool gyroCheckDataReady(gyroDev_t
* gyro
)
77 if (gyro
->dataReady
) {
79 gyro
->dataReady
= false;