Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / accgyro / accgyro_spi_icm426xx.c
blobc297a3229aeabfbe19706d4a0d4535573369da70
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/>.
22 * Author: Dominic Clifton
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <stdlib.h>
29 #include "platform.h"
31 #if defined(USE_GYRO_SPI_ICM42605) || defined(USE_GYRO_SPI_ICM42688P)
33 #include "common/axis.h"
34 #include "common/maths.h"
35 #include "build/debug.h"
37 #include "drivers/accgyro/accgyro.h"
38 #include "drivers/accgyro/accgyro_mpu.h"
39 #include "drivers/accgyro/accgyro_spi_icm426xx.h"
40 #include "drivers/bus_spi.h"
41 #include "drivers/exti.h"
42 #include "drivers/io.h"
43 #include "drivers/sensor.h"
44 #include "drivers/time.h"
46 // 24 MHz max SPI frequency
47 #define ICM426XX_MAX_SPI_CLK_HZ 24000000
49 // 10 MHz max SPI frequency for intialisation
50 #define ICM426XX_MAX_SPI_INIT_CLK_HZ 1000000
52 #define ICM426XX_RA_PWR_MGMT0 0x4E
54 #define ICM426XX_PWR_MGMT0_ACCEL_MODE_LN (3 << 0)
55 #define ICM426XX_PWR_MGMT0_GYRO_MODE_LN (3 << 2)
56 #define ICM426XX_PWR_MGMT0_TEMP_DISABLE_OFF (0 << 5)
57 #define ICM426XX_PWR_MGMT0_TEMP_DISABLE_ON (1 << 5)
59 #define ICM426XX_RA_GYRO_CONFIG0 0x4F
60 #define ICM426XX_RA_ACCEL_CONFIG0 0x50
62 #define ICM426XX_RA_GYRO_ACCEL_CONFIG0 0x52
64 #define ICM426XX_ACCEL_UI_FILT_BW_LOW_LATENCY (14 << 4)
65 #define ICM426XX_GYRO_UI_FILT_BW_LOW_LATENCY (14 << 0)
67 #define ICM426XX_RA_GYRO_DATA_X1 0x25
68 #define ICM426XX_RA_ACCEL_DATA_X1 0x1F
70 #define ICM426XX_RA_INT_CONFIG 0x14
71 #define ICM426XX_INT1_MODE_PULSED (0 << 2)
72 #define ICM426XX_INT1_MODE_LATCHED (1 << 2)
73 #define ICM426XX_INT1_DRIVE_CIRCUIT_OD (0 << 1)
74 #define ICM426XX_INT1_DRIVE_CIRCUIT_PP (1 << 1)
75 #define ICM426XX_INT1_POLARITY_ACTIVE_LOW (0 << 0)
76 #define ICM426XX_INT1_POLARITY_ACTIVE_HIGH (1 << 0)
78 #define ICM426XX_RA_INT_CONFIG0 0x63
79 #define ICM426XX_UI_DRDY_INT_CLEAR_ON_SBR ((0 << 5) || (0 << 4))
80 #define ICM426XX_UI_DRDY_INT_CLEAR_ON_SBR_DUPLICATE ((0 << 5) || (0 << 4)) // duplicate settings in datasheet, Rev 1.2.
81 #define ICM426XX_UI_DRDY_INT_CLEAR_ON_F1BR ((1 << 5) || (0 << 4))
82 #define ICM426XX_UI_DRDY_INT_CLEAR_ON_SBR_AND_F1BR ((1 << 5) || (1 << 4))
84 #define ICM426XX_RA_INT_CONFIG1 0x64
85 #define ICM426XX_INT_ASYNC_RESET_BIT 4
86 #define ICM426XX_INT_TDEASSERT_DISABLE_BIT 5
87 #define ICM426XX_INT_TDEASSERT_ENABLED (0 << ICM426XX_INT_TDEASSERT_DISABLE_BIT)
88 #define ICM426XX_INT_TDEASSERT_DISABLED (1 << ICM426XX_INT_TDEASSERT_DISABLE_BIT)
89 #define ICM426XX_INT_TPULSE_DURATION_BIT 6
90 #define ICM426XX_INT_TPULSE_DURATION_100 (0 << ICM426XX_INT_TPULSE_DURATION_BIT)
91 #define ICM426XX_INT_TPULSE_DURATION_8 (1 << ICM426XX_INT_TPULSE_DURATION_BIT)
94 #define ICM426XX_RA_INT_SOURCE0 0x65
95 #define ICM426XX_UI_DRDY_INT1_EN_DISABLED (0 << 3)
96 #define ICM426XX_UI_DRDY_INT1_EN_ENABLED (1 << 3)
98 static void icm426xxSpiInit(const extDevice_t *dev)
100 static bool hardwareInitialised = false;
102 if (hardwareInitialised) {
103 return;
107 spiSetClkDivisor(dev, spiCalculateDivider(ICM426XX_MAX_SPI_CLK_HZ));
109 hardwareInitialised = true;
112 uint8_t icm426xxSpiDetect(const extDevice_t *dev)
114 icm426xxSpiInit(dev);
116 spiSetClkDivisor(dev, spiCalculateDivider(ICM426XX_MAX_SPI_INIT_CLK_HZ));
118 spiWriteReg(dev, ICM426XX_RA_PWR_MGMT0, 0x00);
120 uint8_t icmDetected = MPU_NONE;
121 uint8_t attemptsRemaining = 20;
122 do {
123 delay(150);
124 const uint8_t whoAmI = spiReadRegMsk(dev, MPU_RA_WHO_AM_I);
125 switch (whoAmI) {
126 case ICM42605_WHO_AM_I_CONST:
127 icmDetected = ICM_42605_SPI;
128 break;
129 case ICM42688P_WHO_AM_I_CONST:
130 icmDetected = ICM_42688P_SPI;
131 break;
132 default:
133 icmDetected = MPU_NONE;
134 break;
136 if (icmDetected != MPU_NONE) {
137 break;
139 if (!attemptsRemaining) {
140 return MPU_NONE;
142 } while (attemptsRemaining--);
144 spiSetClkDivisor(dev, spiCalculateDivider(ICM426XX_MAX_SPI_CLK_HZ));
146 return icmDetected;
149 void icm426xxAccInit(accDev_t *acc)
151 acc->acc_1G = 512 * 4;
154 bool icm426xxSpiAccDetect(accDev_t *acc)
156 switch (acc->mpuDetectionResult.sensor) {
157 case ICM_42605_SPI:
158 break;
159 case ICM_42688P_SPI:
160 break;
161 default:
162 return false;
165 acc->initFn = icm426xxAccInit;
166 acc->readFn = mpuAccReadSPI;
168 return true;
171 typedef struct odrEntry_s {
172 uint8_t khz;
173 uint8_t odr; // See GYRO_ODR in datasheet.
174 } odrEntry_t;
176 static odrEntry_t icm426xxPkhzToSupportedODRMap[] = {
177 { 8, 3 },
178 { 4, 4 },
179 { 2, 5 },
180 { 1, 6 },
183 void icm426xxGyroInit(gyroDev_t *gyro)
185 mpuGyroInit(gyro);
186 gyro->accDataReg = ICM426XX_RA_ACCEL_DATA_X1;
187 gyro->gyroDataReg = ICM426XX_RA_GYRO_DATA_X1;
189 spiSetClkDivisor(&gyro->dev, spiCalculateDivider(ICM426XX_MAX_SPI_INIT_CLK_HZ));
191 spiWriteReg(&gyro->dev, ICM426XX_RA_PWR_MGMT0, ICM426XX_PWR_MGMT0_TEMP_DISABLE_OFF | ICM426XX_PWR_MGMT0_ACCEL_MODE_LN | ICM426XX_PWR_MGMT0_GYRO_MODE_LN);
192 delay(15);
194 uint8_t outputDataRate = 0;
195 bool supportedODRFound = false;
197 if (gyro->gyroRateKHz) {
198 uint8_t gyroSyncDenominator = gyro->mpuDividerDrops + 1; // rebuild it in here, see gyro_sync.c
199 uint8_t desiredODRKhz = 8 / gyroSyncDenominator;
200 for (uint32_t i = 0; i < ARRAYLEN(icm426xxPkhzToSupportedODRMap); i++) {
201 if (icm426xxPkhzToSupportedODRMap[i].khz == desiredODRKhz) {
202 outputDataRate = icm426xxPkhzToSupportedODRMap[i].odr;
203 supportedODRFound = true;
204 break;
209 if (!supportedODRFound) {
210 outputDataRate = 6;
211 gyro->gyroRateKHz = GYRO_RATE_1_kHz;
214 STATIC_ASSERT(INV_FSR_2000DPS == 3, "INV_FSR_2000DPS must be 3 to generate correct value");
215 spiWriteReg(&gyro->dev, ICM426XX_RA_GYRO_CONFIG0, (3 - INV_FSR_2000DPS) << 5 | (outputDataRate & 0x0F));
216 delay(15);
218 STATIC_ASSERT(INV_FSR_16G == 3, "INV_FSR_16G must be 3 to generate correct value");
219 spiWriteReg(&gyro->dev, ICM426XX_RA_ACCEL_CONFIG0, (3 - INV_FSR_16G) << 5 | (outputDataRate & 0x0F));
220 delay(15);
222 spiWriteReg(&gyro->dev, ICM426XX_RA_GYRO_ACCEL_CONFIG0, ICM426XX_ACCEL_UI_FILT_BW_LOW_LATENCY | ICM426XX_GYRO_UI_FILT_BW_LOW_LATENCY);
224 spiWriteReg(&gyro->dev, ICM426XX_RA_INT_CONFIG, ICM426XX_INT1_MODE_PULSED | ICM426XX_INT1_DRIVE_CIRCUIT_PP | ICM426XX_INT1_POLARITY_ACTIVE_HIGH);
225 spiWriteReg(&gyro->dev, ICM426XX_RA_INT_CONFIG0, ICM426XX_UI_DRDY_INT_CLEAR_ON_SBR);
227 #ifdef USE_MPU_DATA_READY_SIGNAL
228 spiWriteReg(&gyro->dev, ICM426XX_RA_INT_SOURCE0, ICM426XX_UI_DRDY_INT1_EN_ENABLED);
230 uint8_t intConfig1Value = spiReadRegMsk(&gyro->dev, ICM426XX_RA_INT_CONFIG1);
231 // Datasheet says: "User should change setting to 0 from default setting of 1, for proper INT1 and INT2 pin operation"
232 intConfig1Value &= ~(1 << ICM426XX_INT_ASYNC_RESET_BIT);
233 intConfig1Value |= (ICM426XX_INT_TPULSE_DURATION_8 | ICM426XX_INT_TDEASSERT_DISABLED);
235 spiWriteReg(&gyro->dev, ICM426XX_RA_INT_CONFIG1, intConfig1Value);
236 #endif
239 spiSetClkDivisor(&gyro->dev, spiCalculateDivider(ICM426XX_MAX_SPI_CLK_HZ));
242 bool icm426xxSpiGyroDetect(gyroDev_t *gyro)
244 switch (gyro->mpuDetectionResult.sensor) {
245 case ICM_42605_SPI:
246 break;
247 case ICM_42688P_SPI:
248 break;
249 default:
250 return false;
253 gyro->initFn = icm426xxGyroInit;
254 gyro->readFn = mpuGyroReadSPI;
256 gyro->scale = GYRO_SCALE_2000DPS;
258 return true;
260 #endif