New SPI API supporting DMA
[betaflight.git] / src / main / drivers / accgyro / accgyro_spi_mpu6500.c
blob051cdbfa218996c5090dd9a541d2254d92c649c2
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/axis.h"
27 #include "common/maths.h"
29 #include "drivers/bus_spi.h"
30 #include "drivers/exti.h"
31 #include "drivers/io.h"
32 #include "drivers/sensor.h"
33 #include "drivers/time.h"
35 #include "accgyro.h"
36 #include "accgyro_mpu.h"
37 #include "accgyro_mpu6500.h"
38 #include "accgyro_spi_mpu6500.h"
40 // 1 MHz max SPI frequency for initialisation
41 #define MPU6500_MAX_SPI_INIT_CLK_HZ 1000000
42 // 20 MHz max SPI frequency
43 #define MPU6500_MAX_SPI_CLK_HZ 20000000
45 #define BIT_SLEEP 0x40
47 static void mpu6500SpiInit(const extDevice_t *dev)
50 spiSetClkDivisor(dev, spiCalculateDivider(MPU6500_MAX_SPI_CLK_HZ));
53 uint8_t mpu6500SpiDetect(const extDevice_t *dev)
55 mpu6500SpiInit(dev);
57 const uint8_t whoAmI = spiReadRegMsk(dev, MPU_RA_WHO_AM_I);
59 uint8_t mpuDetected = MPU_NONE;
60 switch (whoAmI) {
61 case MPU6500_WHO_AM_I_CONST:
62 mpuDetected = MPU_65xx_SPI;
63 break;
64 case MPU9250_WHO_AM_I_CONST:
65 case MPU9255_WHO_AM_I_CONST:
66 mpuDetected = MPU_9250_SPI;
67 break;
68 case ICM20601_WHO_AM_I_CONST:
69 mpuDetected = ICM_20601_SPI;
70 break;
71 case ICM20602_WHO_AM_I_CONST:
72 mpuDetected = ICM_20602_SPI;
73 break;
74 case ICM20608G_WHO_AM_I_CONST:
75 mpuDetected = ICM_20608_SPI;
76 break;
77 case ICM42605_WHO_AM_I_CONST:
78 mpuDetected = ICM_42605_SPI;
79 break;
80 default:
81 mpuDetected = MPU_NONE;
83 return mpuDetected;
86 void mpu6500SpiAccInit(accDev_t *acc)
88 mpu6500AccInit(acc);
91 void mpu6500SpiGyroInit(gyroDev_t *gyro)
93 spiSetClkDivisor(&gyro->dev, spiCalculateDivider(MPU6500_MAX_SPI_INIT_CLK_HZ));
94 delayMicroseconds(1);
96 mpu6500GyroInit(gyro);
98 // Disable Primary I2C Interface
99 spiWriteReg(&gyro->dev, MPU_RA_USER_CTRL, MPU6500_BIT_I2C_IF_DIS);
100 delay(100);
102 spiSetClkDivisor(&gyro->dev, spiCalculateDivider(MPU6500_MAX_SPI_CLK_HZ));
103 delayMicroseconds(1);
106 bool mpu6500SpiAccDetect(accDev_t *acc)
108 // MPU6500 is used as a equivalent of other accelerometers by some flight controllers
109 switch (acc->mpuDetectionResult.sensor) {
110 case MPU_65xx_SPI:
111 case MPU_9250_SPI:
112 case ICM_20608_SPI:
113 case ICM_20602_SPI:
114 case ICM_20601_SPI:
115 break;
116 default:
117 return false;
120 acc->initFn = mpu6500SpiAccInit;
121 acc->readFn = mpuAccReadSPI;
123 return true;
126 bool mpu6500SpiGyroDetect(gyroDev_t *gyro)
128 // MPU6500 is used as a equivalent of other gyros by some flight controllers
129 switch (gyro->mpuDetectionResult.sensor) {
130 case MPU_65xx_SPI:
131 case MPU_9250_SPI:
132 case ICM_20608_SPI:
133 case ICM_20602_SPI:
134 gyro->scale = GYRO_SCALE_2000DPS;
135 break;
136 case ICM_20601_SPI:
137 gyro->scale = (gyro->gyro_high_fsr ? GYRO_SCALE_4000DPS : GYRO_SCALE_2000DPS);
138 break;
139 default:
140 return false;
143 gyro->initFn = mpu6500SpiGyroInit;
144 gyro->readFn = mpuGyroReadSPI;
146 return true;