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 extern uint16_t acc_1G
;
43 bool mpu6500WriteRegister(uint8_t reg
, uint8_t data
)
46 spiTransferByte(MPU6500_SPI_INSTANCE
, reg
);
47 spiTransferByte(MPU6500_SPI_INSTANCE
, data
);
53 bool mpu6500ReadRegister(uint8_t reg
, uint8_t length
, uint8_t *data
)
56 spiTransferByte(MPU6500_SPI_INSTANCE
, reg
| 0x80); // read transaction
57 spiTransfer(MPU6500_SPI_INSTANCE
, data
, NULL
, length
);
63 static void mpu6500SpiInit(void)
65 static bool hardwareInitialised
= false;
67 if (hardwareInitialised
) {
72 RCC_AHBPeriphClockCmd(MPU6500_CS_GPIO_CLK_PERIPHERAL
, ENABLE
);
74 GPIO_InitTypeDef GPIO_InitStructure
;
75 GPIO_InitStructure
.GPIO_Pin
= MPU6500_CS_PIN
;
76 GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_OUT
;
77 GPIO_InitStructure
.GPIO_Speed
= GPIO_Speed_50MHz
;
78 GPIO_InitStructure
.GPIO_OType
= GPIO_OType_PP
;
79 GPIO_InitStructure
.GPIO_PuPd
= GPIO_PuPd_NOPULL
;
81 GPIO_Init(MPU6500_CS_GPIO
, &GPIO_InitStructure
);
85 RCC_APB2PeriphClockCmd(MPU6500_CS_GPIO_CLK_PERIPHERAL
, ENABLE
);
89 gpio
.mode
= Mode_Out_PP
;
90 gpio
.pin
= MPU6500_CS_PIN
;
91 gpio
.speed
= Speed_50MHz
;
92 gpioInit(MPU6500_CS_GPIO
, &gpio
);
95 GPIO_SetBits(MPU6500_CS_GPIO
, MPU6500_CS_PIN
);
97 spiSetDivisor(MPU6500_SPI_INSTANCE
, SPI_9MHZ_CLOCK_DIVIDER
);
99 hardwareInitialised
= true;
102 bool mpu6500SpiDetect(void)
108 mpu6500ReadRegister(MPU_RA_WHO_AM_I
, 1, &tmp
);
110 if (tmp
!= MPU6500_WHO_AM_I_CONST
)
116 bool mpu6500SpiAccDetect(acc_t
*acc
)
118 if (mpuDetectionResult
.sensor
!= MPU_65xx_SPI
) {
122 acc
->init
= mpu6500AccInit
;
123 acc
->read
= mpuAccRead
;
128 bool mpu6500SpiGyroDetect(gyro_t
*gyro
)
130 if (mpuDetectionResult
.sensor
!= MPU_65xx_SPI
) {
134 gyro
->init
= mpu6500GyroInit
;
135 gyro
->read
= mpuGyroRead
;
137 // 16.4 dps/lsb scalefactor
138 gyro
->scale
= 1.0f
/ 16.4f
;