New SPI API supporting DMA
[betaflight.git] / src / main / pg / gyrodev.c
blob87510d8d67c4d0846670f2925a4db3b97ead65f4
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 #include <stdbool.h>
22 #include <stdint.h>
24 #include "platform.h"
26 #include "common/sensor_alignment.h"
28 #include "pg/pg.h"
29 #include "pg/pg_ids.h"
30 #include "pg/gyrodev.h"
32 #include "drivers/io.h"
33 #include "drivers/bus_spi.h"
34 #include "drivers/sensor.h"
35 #include "sensors/gyro.h"
38 ioTag_t selectMPUIntExtiConfigByHardwareRevision(void); // XXX Should be gone
40 #if defined(USE_SPI_GYRO) || defined(USE_I2C_GYRO)
41 static void gyroResetCommonDeviceConfig(gyroDeviceConfig_t *devconf, ioTag_t extiTag, uint8_t alignment, sensorAlignment_t customAlignment)
43 devconf->extiTag = extiTag;
44 devconf->alignment = alignment;
45 devconf->customAlignment = customAlignment;
47 #endif
49 #ifdef USE_SPI_GYRO
50 static void gyroResetSpiDeviceConfig(gyroDeviceConfig_t *devconf, SPI_TypeDef *instance, ioTag_t csnTag, ioTag_t extiTag, uint8_t alignment, sensorAlignment_t customAlignment)
52 devconf->busType = BUS_TYPE_SPI;
53 devconf->spiBus = SPI_DEV_TO_CFG(spiDeviceByInstance(instance));
54 devconf->csnTag = csnTag;
55 gyroResetCommonDeviceConfig(devconf, extiTag, alignment, customAlignment);
57 #endif
59 #if defined(USE_I2C_GYRO) && !defined(USE_MULTI_GYRO)
60 static void gyroResetI2cDeviceConfig(gyroDeviceConfig_t *devconf, I2CDevice i2cbus, ioTag_t extiTag, uint8_t alignment, sensorAlignment_t customAlignment)
62 devconf->busType = BUS_TYPE_I2C;
63 devconf->i2cBus = I2C_DEV_TO_CFG(i2cbus);
64 devconf->i2cAddress = GYRO_I2C_ADDRESS;
65 gyroResetCommonDeviceConfig(devconf, extiTag, alignment, customAlignment);
67 #endif
69 PG_REGISTER_ARRAY_WITH_RESET_FN(gyroDeviceConfig_t, MAX_GYRODEV_COUNT, gyroDeviceConfig, PG_GYRO_DEVICE_CONFIG, 0);
71 void pgResetFn_gyroDeviceConfig(gyroDeviceConfig_t *devconf)
73 devconf[0].index = 0;
74 sensorAlignment_t customAlignment1 = CUSTOM_ALIGN_CW0_DEG;
75 #ifdef GYRO_1_CUSTOM_ALIGN
76 customAlignment1 = GYRO_1_CUSTOM_ALIGN;
77 #else
78 buildAlignmentFromStandardAlignment(&customAlignment1, GYRO_1_ALIGN);
79 #endif // GYRO_1_CUSTOM_ALIGN
81 // All multi-gyro boards use SPI based gyros.
82 #ifdef USE_SPI_GYRO
83 gyroResetSpiDeviceConfig(&devconf[0], GYRO_1_SPI_INSTANCE, IO_TAG(GYRO_1_CS_PIN), IO_TAG(GYRO_1_EXTI_PIN), GYRO_1_ALIGN, customAlignment1);
84 #ifdef USE_MULTI_GYRO
85 devconf[1].index = 1;
86 sensorAlignment_t customAlignment2 = CUSTOM_ALIGN_CW0_DEG;
87 #ifdef GYRO_2_CUSTOM_ALIGN
88 customAlignment2 = GYRO_2_CUSTOM_ALIGN;
89 #else
90 buildAlignmentFromStandardAlignment(&customAlignment2, GYRO_2_ALIGN);
91 #endif // GYRO_2_CUSTOM_ALIGN
92 gyroResetSpiDeviceConfig(&devconf[1], GYRO_2_SPI_INSTANCE, IO_TAG(GYRO_2_CS_PIN), IO_TAG(GYRO_2_EXTI_PIN), GYRO_2_ALIGN, customAlignment2);
93 #endif // USE_MULTI_GYRO
94 #endif // USE_SPI_GYRO
96 // I2C gyros appear as a sole gyro in single gyro boards.
97 #if defined(USE_I2C_GYRO) && !defined(USE_MULTI_GYRO)
98 devconf[0].i2cBus = I2C_DEV_TO_CFG(I2CINVALID); // XXX Not required?
99 gyroResetI2cDeviceConfig(&devconf[0], I2C_DEVICE, IO_TAG(GYRO_1_EXTI_PIN), GYRO_1_ALIGN, customAlignment1);
100 #endif
102 // Special treatment for very rare F3 targets with variants having either I2C or SPI acc/gyro chip; mark it for run time detection.
103 #if defined(USE_SPI_GYRO) && defined(USE_I2C_GYRO)
104 devconf[0].busType = BUS_TYPE_GYRO_AUTO;
105 #endif