|
| 1 | +/* |
| 2 | + * This file is part of Cleanflight. |
| 3 | + * |
| 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. |
| 8 | + * |
| 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. |
| 13 | + * |
| 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/>. |
| 16 | + */ |
| 17 | + |
| 18 | + |
| 19 | +/* |
| 20 | + * Authors: |
| 21 | + * Dominic Clifton - Cleanflight implementation |
| 22 | + * John Ihlein - Initial FF32 code |
| 23 | + * Kalyn Doerr (RS2K) - Robust rewrite |
| 24 | +*/ |
| 25 | + |
| 26 | +#include <stdbool.h> |
| 27 | +#include <stdint.h> |
| 28 | + |
| 29 | +#include "platform.h" |
| 30 | +#include "light_led.h" |
| 31 | + |
| 32 | +#include "common/axis.h" |
| 33 | +#include "common/maths.h" |
| 34 | + |
| 35 | +#include "io.h" |
| 36 | + |
| 37 | +#include "system.h" |
| 38 | +#include "exti.h" |
| 39 | +#include "bus_spi.h" |
| 40 | +#include "gyro_sync.h" |
| 41 | +#include "debug.h" |
| 42 | + |
| 43 | +#include "sensor.h" |
| 44 | +#include "accgyro.h" |
| 45 | +#include "accgyro_mpu.h" |
| 46 | +#include "accgyro_spi_mpu9250.h" |
| 47 | + |
| 48 | +static void mpu9250AccAndGyroInit(uint8_t lpf); |
| 49 | + |
| 50 | +static bool mpuSpi9250InitDone = false; |
| 51 | + |
| 52 | +static IO_t mpuSpi9250CsPin = IO_NONE; |
| 53 | + |
| 54 | +#define DISABLE_MPU9250 IOHi(mpuSpi9250CsPin) |
| 55 | +#define ENABLE_MPU9250 IOLo(mpuSpi9250CsPin) |
| 56 | + |
| 57 | +void mpu9250ResetGyro(void) |
| 58 | +{ |
| 59 | + // Device Reset |
| 60 | + mpu9250WriteRegister(MPU_RA_PWR_MGMT_1, MPU9250_BIT_RESET); |
| 61 | + delay(150); |
| 62 | +} |
| 63 | + |
| 64 | +bool mpu9250WriteRegister(uint8_t reg, uint8_t data) |
| 65 | +{ |
| 66 | + ENABLE_MPU9250; |
| 67 | + delayMicroseconds(1); |
| 68 | + spiTransferByte(MPU9250_SPI_INSTANCE, reg); |
| 69 | + spiTransferByte(MPU9250_SPI_INSTANCE, data); |
| 70 | + DISABLE_MPU9250; |
| 71 | + delayMicroseconds(1); |
| 72 | + |
| 73 | + return true; |
| 74 | +} |
| 75 | + |
| 76 | +bool mpu9250ReadRegister(uint8_t reg, uint8_t length, uint8_t *data) |
| 77 | +{ |
| 78 | + ENABLE_MPU9250; |
| 79 | + spiTransferByte(MPU9250_SPI_INSTANCE, reg | 0x80); // read transaction |
| 80 | + spiTransfer(MPU9250_SPI_INSTANCE, data, NULL, length); |
| 81 | + DISABLE_MPU9250; |
| 82 | + |
| 83 | + return true; |
| 84 | +} |
| 85 | + |
| 86 | +bool mpu9250SlowReadRegister(uint8_t reg, uint8_t length, uint8_t *data) |
| 87 | +{ |
| 88 | + ENABLE_MPU9250; |
| 89 | + delayMicroseconds(1); |
| 90 | + spiTransferByte(MPU9250_SPI_INSTANCE, reg | 0x80); // read transaction |
| 91 | + spiTransfer(MPU9250_SPI_INSTANCE, data, NULL, length); |
| 92 | + DISABLE_MPU9250; |
| 93 | + delayMicroseconds(1); |
| 94 | + |
| 95 | + return true; |
| 96 | +} |
| 97 | + |
| 98 | +void mpu9250SpiGyroInit(uint8_t lpf) |
| 99 | +{ |
| 100 | + (void)(lpf); |
| 101 | + |
| 102 | + mpuIntExtiInit(); |
| 103 | + |
| 104 | + mpu9250AccAndGyroInit(lpf); |
| 105 | + |
| 106 | + spiResetErrorCounter(MPU9250_SPI_INSTANCE); |
| 107 | + |
| 108 | + spiSetDivisor(MPU9250_SPI_INSTANCE, SPI_CLOCK_FAST); //high speed now that we don't need to write to the slow registers |
| 109 | + |
| 110 | + int16_t data[3]; |
| 111 | + mpuGyroRead(data); |
| 112 | + |
| 113 | + if ((((int8_t)data[1]) == -1 && ((int8_t)data[0]) == -1) || spiGetErrorCounter(MPU9250_SPI_INSTANCE) != 0) { |
| 114 | + spiResetErrorCounter(MPU9250_SPI_INSTANCE); |
| 115 | + failureMode(FAILURE_GYRO_INIT_FAILED); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +void mpu9250SpiAccInit(acc_t *acc) |
| 120 | +{ |
| 121 | + mpuIntExtiInit(); |
| 122 | + |
| 123 | + acc->acc_1G = 512 * 8; |
| 124 | +} |
| 125 | + |
| 126 | +bool verifympu9250WriteRegister(uint8_t reg, uint8_t data) |
| 127 | +{ |
| 128 | + uint8_t in; |
| 129 | + uint8_t attemptsRemaining = 20; |
| 130 | + |
| 131 | + mpu9250WriteRegister(reg, data); |
| 132 | + delayMicroseconds(100); |
| 133 | + |
| 134 | + do { |
| 135 | + mpu9250SlowReadRegister(reg, 1, &in); |
| 136 | + if (in == data) { |
| 137 | + return true; |
| 138 | + } else { |
| 139 | + debug[3]++; |
| 140 | + mpu9250WriteRegister(reg, data); |
| 141 | + delayMicroseconds(100); |
| 142 | + } |
| 143 | + } while (attemptsRemaining--); |
| 144 | + return false; |
| 145 | +} |
| 146 | + |
| 147 | +static void mpu9250AccAndGyroInit(uint8_t lpf) { |
| 148 | + |
| 149 | + if (mpuSpi9250InitDone) { |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + spiSetDivisor(MPU9250_SPI_INSTANCE, SPI_CLOCK_INITIALIZATON); //low speed for writing to slow registers |
| 154 | + |
| 155 | + mpu9250WriteRegister(MPU_RA_PWR_MGMT_1, MPU9250_BIT_RESET); |
| 156 | + delay(50); |
| 157 | + |
| 158 | + verifympu9250WriteRegister(MPU_RA_PWR_MGMT_1, INV_CLK_PLL); |
| 159 | + |
| 160 | + verifympu9250WriteRegister(MPU_RA_GYRO_CONFIG, INV_FSR_2000DPS << 3 | FCB_DISABLED); //Fchoice_b defaults to 00 which makes fchoice 11 |
| 161 | + |
| 162 | + if (lpf == 4) { |
| 163 | + verifympu9250WriteRegister(MPU_RA_CONFIG, 1); //1KHz, 184DLPF |
| 164 | + } else if (lpf < 4) { |
| 165 | + verifympu9250WriteRegister(MPU_RA_CONFIG, 7); //8KHz, 3600DLPF |
| 166 | + } else if (lpf > 4) { |
| 167 | + verifympu9250WriteRegister(MPU_RA_CONFIG, 0); //8KHz, 250DLPF |
| 168 | + } |
| 169 | + |
| 170 | + verifympu9250WriteRegister(MPU_RA_SMPLRT_DIV, gyroMPU6xxxGetDividerDrops()); // Get Divider Drops |
| 171 | + |
| 172 | + verifympu9250WriteRegister(MPU_RA_ACCEL_CONFIG, INV_FSR_8G << 3); |
| 173 | + verifympu9250WriteRegister(MPU_RA_INT_PIN_CFG, 0 << 7 | 0 << 6 | 0 << 5 | 1 << 4 | 0 << 3 | 0 << 2 | 1 << 1 | 0 << 0); // INT_ANYRD_2CLEAR, BYPASS_EN |
| 174 | + |
| 175 | +#if defined(USE_MPU_DATA_READY_SIGNAL) |
| 176 | + verifympu9250WriteRegister(MPU_RA_INT_ENABLE, 0x01); //this resets register MPU_RA_PWR_MGMT_1 and won't read back correctly. |
| 177 | +#endif |
| 178 | + |
| 179 | + spiSetDivisor(MPU9250_SPI_INSTANCE, SPI_CLOCK_FAST); |
| 180 | + |
| 181 | + mpuSpi9250InitDone = true; //init done |
| 182 | +} |
| 183 | + |
| 184 | +bool mpu9250SpiDetect(void) |
| 185 | +{ |
| 186 | + uint8_t in; |
| 187 | + uint8_t attemptsRemaining = 20; |
| 188 | + |
| 189 | + /* not the best place for this - should really have an init method */ |
| 190 | +#ifdef MPU9250_CS_PIN |
| 191 | + mpuSpi9250CsPin = IOGetByTag(IO_TAG(MPU9250_CS_PIN)); |
| 192 | +#endif |
| 193 | + IOInit(mpuSpi9250CsPin, OWNER_MPU, RESOURCE_SPI_CS, 0); |
| 194 | + IOConfigGPIO(mpuSpi9250CsPin, SPI_IO_CS_CFG); |
| 195 | + |
| 196 | + spiSetDivisor(MPU9250_SPI_INSTANCE, SPI_CLOCK_INITIALIZATON); //low speed |
| 197 | + mpu9250WriteRegister(MPU_RA_PWR_MGMT_1, MPU9250_BIT_RESET); |
| 198 | + |
| 199 | + do { |
| 200 | + delay(150); |
| 201 | + |
| 202 | + mpu9250ReadRegister(MPU_RA_WHO_AM_I, 1, &in); |
| 203 | + if (in == MPU9250_WHO_AM_I_CONST) { |
| 204 | + break; |
| 205 | + } |
| 206 | + if (!attemptsRemaining) { |
| 207 | + return false; |
| 208 | + } |
| 209 | + } while (attemptsRemaining--); |
| 210 | + |
| 211 | + spiSetDivisor(MPU9250_SPI_INSTANCE, SPI_CLOCK_FAST); |
| 212 | + |
| 213 | + return true; |
| 214 | +} |
| 215 | + |
| 216 | +bool mpu9250SpiAccDetect(acc_t *acc) |
| 217 | +{ |
| 218 | + if (mpuDetectionResult.sensor != MPU_9250_SPI) { |
| 219 | + return false; |
| 220 | + } |
| 221 | + |
| 222 | + acc->init = mpu9250SpiAccInit; |
| 223 | + acc->read = mpuAccRead; |
| 224 | + |
| 225 | + return true; |
| 226 | +} |
| 227 | + |
| 228 | +bool mpu9250SpiGyroDetect(gyro_t *gyro) |
| 229 | +{ |
| 230 | + if (mpuDetectionResult.sensor != MPU_9250_SPI) { |
| 231 | + return false; |
| 232 | + } |
| 233 | + |
| 234 | + gyro->init = mpu9250SpiGyroInit; |
| 235 | + gyro->read = mpuGyroRead; |
| 236 | + gyro->intStatus = checkMPUDataReady; |
| 237 | + |
| 238 | + // 16.4 dps/lsb scalefactor |
| 239 | + gyro->scale = 1.0f / 16.4f; |
| 240 | + |
| 241 | + return true; |
| 242 | +} |
0 commit comments