SPRACINGF3EVO board support (#266)
[betaflight.git] / src / main / drivers / accgyro_spi_mpu6500.c
blobcbd92249d5b6dce294df191887ef7f9920f8ca73
1 /*
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/>.
18 #include <stdbool.h>
19 #include <stdint.h>
20 #include <stdlib.h>
22 #include "platform.h"
24 #include "common/axis.h"
25 #include "common/maths.h"
27 #include "system.h"
28 #include "exti.h"
29 #include "gpio.h"
30 #include "bus_spi.h"
32 #include "sensor.h"
33 #include "accgyro.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)
43 ENABLE_MPU6500;
44 spiTransferByte(MPU6500_SPI_INSTANCE, reg);
45 spiTransferByte(MPU6500_SPI_INSTANCE, data);
46 DISABLE_MPU6500;
48 return true;
51 bool mpu6500ReadRegister(uint8_t reg, uint8_t length, uint8_t *data)
53 ENABLE_MPU6500;
54 spiTransferByte(MPU6500_SPI_INSTANCE, reg | 0x80); // read transaction
55 spiTransfer(MPU6500_SPI_INSTANCE, data, NULL, length);
56 DISABLE_MPU6500;
58 return true;
61 static void mpu6500SpiInit(void)
63 static bool hardwareInitialised = false;
65 if (hardwareInitialised) {
66 return;
69 #ifdef STM32F303xC
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);
80 #endif
82 #ifdef STM32F10X
83 RCC_APB2PeriphClockCmd(MPU6500_CS_GPIO_CLK_PERIPHERAL, ENABLE);
85 gpio_config_t gpio;
86 // CS as output
87 gpio.mode = Mode_Out_PP;
88 gpio.pin = MPU6500_CS_PIN;
89 gpio.speed = Speed_50MHz;
90 gpioInit(MPU6500_CS_GPIO, &gpio);
91 #endif
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)
102 uint8_t sig;
104 mpu6500SpiInit();
106 mpu6500ReadRegister(MPU_RA_WHO_AM_I, 1, &sig);
108 if (sig == MPU6500_WHO_AM_I_CONST || sig == MPU9250_WHO_AM_I_CONST) {
109 return true;
112 return false;
115 bool mpu6500SpiAccDetect(acc_t *acc)
117 if (mpuDetectionResult.sensor != MPU_65xx_SPI) {
118 return false;
121 acc->init = mpu6500AccInit;
122 acc->read = mpuAccRead;
124 return true;
127 bool mpu6500SpiGyroDetect(gyro_t *gyro)
129 if (mpuDetectionResult.sensor != MPU_65xx_SPI) {
130 return false;
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;
140 return true;