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/>.
24 #include "common/axis.h"
25 #include "common/maths.h"
34 #include "accgyro_mpu.h"
35 #include "accgyro_mpu6500.h"
36 #include "accgyro_spi_mpu6500.h"
38 #define DISABLE_MPU6500 GPIO_SetBits(MPU6500_CS_GPIO, MPU6500_CS_PIN)
39 #define ENABLE_MPU6500 GPIO_ResetBits(MPU6500_CS_GPIO, MPU6500_CS_PIN)
41 bool mpu6500WriteRegister(uint8_t reg
, uint8_t data
)
44 spiTransferByte(MPU6500_SPI_INSTANCE
, reg
);
45 spiTransferByte(MPU6500_SPI_INSTANCE
, data
);
51 bool mpu6500ReadRegister(uint8_t reg
, uint8_t length
, uint8_t *data
)
54 spiTransferByte(MPU6500_SPI_INSTANCE
, reg
| 0x80); // read transaction
55 spiTransfer(MPU6500_SPI_INSTANCE
, data
, NULL
, length
);
61 static void mpu6500SpiInit(void)
63 static bool hardwareInitialised
= false;
65 if (hardwareInitialised
) {
70 RCC_AHBPeriphClockCmd(MPU6500_CS_GPIO_CLK_PERIPHERAL
, ENABLE
);
72 GPIO_InitTypeDef GPIO_InitStructure
;
73 GPIO_InitStructure
.GPIO_Pin
= MPU6500_CS_PIN
;
74 GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_OUT
;
75 GPIO_InitStructure
.GPIO_Speed
= GPIO_Speed_50MHz
;
76 GPIO_InitStructure
.GPIO_OType
= GPIO_OType_PP
;
77 GPIO_InitStructure
.GPIO_PuPd
= GPIO_PuPd_NOPULL
;
79 GPIO_Init(MPU6500_CS_GPIO
, &GPIO_InitStructure
);
83 RCC_APB2PeriphClockCmd(MPU6500_CS_GPIO_CLK_PERIPHERAL
, ENABLE
);
87 gpio
.mode
= Mode_Out_PP
;
88 gpio
.pin
= MPU6500_CS_PIN
;
89 gpio
.speed
= Speed_50MHz
;
90 gpioInit(MPU6500_CS_GPIO
, &gpio
);
93 GPIO_SetBits(MPU6500_CS_GPIO
, MPU6500_CS_PIN
);
95 spiSetDivisor(MPU6500_SPI_INSTANCE
, SPI_9MHZ_CLOCK_DIVIDER
);
97 hardwareInitialised
= true;
100 bool mpu6500SpiDetect(void)
106 mpu6500ReadRegister(MPU_RA_WHO_AM_I
, 1, &sig
);
108 if (sig
== MPU6500_WHO_AM_I_CONST
|| sig
== MPU9250_WHO_AM_I_CONST
) {
115 bool mpu6500SpiAccDetect(acc_t
*acc
)
117 if (mpuDetectionResult
.sensor
!= MPU_65xx_SPI
) {
121 acc
->init
= mpu6500AccInit
;
122 acc
->read
= mpuAccRead
;
127 bool mpu6500SpiGyroDetect(gyro_t
*gyro
)
129 if (mpuDetectionResult
.sensor
!= MPU_65xx_SPI
) {
133 gyro
->init
= mpu6500GyroInit
;
134 gyro
->read
= mpuGyroRead
;
135 gyro
->intStatus
= checkMPUDataReady
;
137 // 16.4 dps/lsb scalefactor
138 gyro
->scale
= 1.0f
/ 16.4f
;