New SPI API supporting DMA
[betaflight.git] / src / main / drivers / accgyro / accgyro_spi_icm42605.c
blob31a7a9a04565c4a0116a13d1c8376b1ccee0eb5d
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 #ifdef USE_GYRO_SPI_ICM42605
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_icm42605.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 ICM42605_MAX_SPI_CLK_HZ 24000000
49 // 10 MHz max SPI frequency for intialisation
50 #define ICM42605_MAX_SPI_INIT_CLK_HZ 1000000
52 #define ICM42605_RA_PWR_MGMT0 0x4E
54 #define ICM42605_PWR_MGMT0_ACCEL_MODE_LN (3 << 0)
55 #define ICM42605_PWR_MGMT0_GYRO_MODE_LN (3 << 2)
56 #define ICM42605_PWR_MGMT0_TEMP_DISABLE_OFF (0 << 5)
57 #define ICM42605_PWR_MGMT0_TEMP_DISABLE_ON (1 << 5)
59 #define ICM42605_RA_GYRO_CONFIG0 0x4F
60 #define ICM42605_RA_ACCEL_CONFIG0 0x50
62 #define ICM42605_RA_GYRO_ACCEL_CONFIG0 0x52
64 #define ICM42605_ACCEL_UI_FILT_BW_LOW_LATENCY (14 << 4)
65 #define ICM42605_GYRO_UI_FILT_BW_LOW_LATENCY (14 << 0)
67 #define ICM42605_RA_GYRO_DATA_X1 0x25
68 #define ICM42605_RA_ACCEL_DATA_X1 0x1F
70 #define ICM42605_RA_INT_CONFIG 0x14
71 #define ICM42605_INT1_MODE_PULSED (0 << 2)
72 #define ICM42605_INT1_MODE_LATCHED (1 << 2)
73 #define ICM42605_INT1_DRIVE_CIRCUIT_OD (0 << 1)
74 #define ICM42605_INT1_DRIVE_CIRCUIT_PP (1 << 1)
75 #define ICM42605_INT1_POLARITY_ACTIVE_LOW (0 << 0)
76 #define ICM42605_INT1_POLARITY_ACTIVE_HIGH (1 << 0)
78 #define ICM42605_RA_INT_CONFIG0 0x63
79 #define ICM42605_UI_DRDY_INT_CLEAR_ON_SBR ((0 << 5) || (0 << 4))
80 #define ICM42605_UI_DRDY_INT_CLEAR_ON_SBR_DUPLICATE ((0 << 5) || (0 << 4)) // duplicate settings in datasheet, Rev 1.2.
81 #define ICM42605_UI_DRDY_INT_CLEAR_ON_F1BR ((1 << 5) || (0 << 4))
82 #define ICM42605_UI_DRDY_INT_CLEAR_ON_SBR_AND_F1BR ((1 << 5) || (1 << 4))
84 #define ICM42605_RA_INT_CONFIG1 0x64
85 #define ICM42605_INT_ASYNC_RESET_BIT 4
86 #define ICM42605_INT_TDEASSERT_DISABLE_BIT 5
87 #define ICM42605_INT_TDEASSERT_ENABLED (0 << ICM42605_INT_TDEASSERT_DISABLE_BIT)
88 #define ICM42605_INT_TDEASSERT_DISABLED (1 << ICM42605_INT_TDEASSERT_DISABLE_BIT)
89 #define ICM42605_INT_TPULSE_DURATION_BIT 6
90 #define ICM42605_INT_TPULSE_DURATION_100 (0 << ICM42605_INT_TPULSE_DURATION_BIT)
91 #define ICM42605_INT_TPULSE_DURATION_8 (1 << ICM42605_INT_TPULSE_DURATION_BIT)
94 #define ICM42605_RA_INT_SOURCE0 0x65
95 #define ICM42605_UI_DRDY_INT1_EN_DISABLED (0 << 3)
96 #define ICM42605_UI_DRDY_INT1_EN_ENABLED (1 << 3)
98 static void icm42605SpiInit(const extDevice_t *dev)
100 static bool hardwareInitialised = false;
102 if (hardwareInitialised) {
103 return;
107 spiSetClkDivisor(dev, spiCalculateDivider(ICM42605_MAX_SPI_CLK_HZ));
109 hardwareInitialised = true;
112 uint8_t icm42605SpiDetect(const extDevice_t *dev)
114 icm42605SpiInit(dev);
116 spiSetClkDivisor(dev, spiCalculateDivider(ICM42605_MAX_SPI_INIT_CLK_HZ));
118 spiWriteReg(dev, ICM42605_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 default:
130 icmDetected = MPU_NONE;
131 break;
133 if (icmDetected != MPU_NONE) {
134 break;
136 if (!attemptsRemaining) {
137 return MPU_NONE;
139 } while (attemptsRemaining--);
141 spiSetClkDivisor(dev, spiCalculateDivider(ICM42605_MAX_SPI_CLK_HZ));
143 return icmDetected;
146 void icm42605AccInit(accDev_t *acc)
148 acc->acc_1G = 512 * 4;
151 bool icm42605AccRead(accDev_t *acc)
153 uint8_t data[6];
155 const bool ack = busReadRegisterBuffer(&acc->gyro->dev, ICM42605_RA_ACCEL_DATA_X1, data, 6);
156 if (!ack) {
157 return false;
160 acc->ADCRaw[X] = (int16_t)((data[0] << 8) | data[1]);
161 acc->ADCRaw[Y] = (int16_t)((data[2] << 8) | data[3]);
162 acc->ADCRaw[Z] = (int16_t)((data[4] << 8) | data[5]);
164 return true;
166 bool icm42605SpiAccDetect(accDev_t *acc)
168 switch (acc->mpuDetectionResult.sensor) {
169 case ICM_42605_SPI:
170 break;
171 default:
172 return false;
175 acc->initFn = icm42605AccInit;
176 acc->readFn = icm42605AccRead;
178 return true;
181 typedef struct odrEntry_s {
182 uint8_t khz;
183 uint8_t odr; // See GYRO_ODR in datasheet.
184 } odrEntry_t;
186 static odrEntry_t icm42605PkhzToSupportedODRMap[] = {
187 { 8, 3 },
188 { 4, 4 },
189 { 2, 5 },
190 { 1, 6 },
193 void icm42605GyroInit(gyroDev_t *gyro)
195 mpuGyroInit(gyro);
197 spiSetClkDivisor(&gyro->dev, spiCalculateDivider(ICM42605_MAX_SPI_INIT_CLK_HZ));
199 spiWriteReg(&gyro->dev, ICM42605_RA_PWR_MGMT0, ICM42605_PWR_MGMT0_TEMP_DISABLE_OFF | ICM42605_PWR_MGMT0_ACCEL_MODE_LN | ICM42605_PWR_MGMT0_GYRO_MODE_LN);
200 delay(15);
202 uint8_t outputDataRate = 0;
203 bool supportedODRFound = false;
205 if (gyro->gyroRateKHz) {
206 uint8_t gyroSyncDenominator = gyro->mpuDividerDrops + 1; // rebuild it in here, see gyro_sync.c
207 uint8_t desiredODRKhz = 8 / gyroSyncDenominator;
208 for (uint32_t i = 0; i < ARRAYLEN(icm42605PkhzToSupportedODRMap); i++) {
209 if (icm42605PkhzToSupportedODRMap[i].khz == desiredODRKhz) {
210 outputDataRate = icm42605PkhzToSupportedODRMap[i].odr;
211 supportedODRFound = true;
212 break;
217 if (!supportedODRFound) {
218 outputDataRate = 6;
219 gyro->gyroRateKHz = GYRO_RATE_1_kHz;
222 STATIC_ASSERT(INV_FSR_2000DPS == 3, "INV_FSR_2000DPS must be 3 to generate correct value");
223 spiWriteReg(&gyro->dev, ICM42605_RA_GYRO_CONFIG0, (3 - INV_FSR_2000DPS) << 5 | (outputDataRate & 0x0F));
224 delay(15);
226 STATIC_ASSERT(INV_FSR_16G == 3, "INV_FSR_16G must be 3 to generate correct value");
227 spiWriteReg(&gyro->dev, ICM42605_RA_ACCEL_CONFIG0, (3 - INV_FSR_16G) << 5 | (outputDataRate & 0x0F));
228 delay(15);
230 spiWriteReg(&gyro->dev, ICM42605_RA_GYRO_ACCEL_CONFIG0, ICM42605_ACCEL_UI_FILT_BW_LOW_LATENCY | ICM42605_GYRO_UI_FILT_BW_LOW_LATENCY);
232 spiWriteReg(&gyro->dev, ICM42605_RA_INT_CONFIG, ICM42605_INT1_MODE_PULSED | ICM42605_INT1_DRIVE_CIRCUIT_PP | ICM42605_INT1_POLARITY_ACTIVE_HIGH);
233 spiWriteReg(&gyro->dev, ICM42605_RA_INT_CONFIG0, ICM42605_UI_DRDY_INT_CLEAR_ON_SBR);
235 #ifdef USE_MPU_DATA_READY_SIGNAL
236 spiWriteReg(&gyro->dev, ICM42605_RA_INT_SOURCE0, ICM42605_UI_DRDY_INT1_EN_ENABLED);
238 uint8_t intConfig1Value = spiReadRegMsk(&gyro->dev, ICM42605_RA_INT_CONFIG1);
239 // Datasheet says: "User should change setting to 0 from default setting of 1, for proper INT1 and INT2 pin operation"
240 intConfig1Value &= ~(1 << ICM42605_INT_ASYNC_RESET_BIT);
241 intConfig1Value |= (ICM42605_INT_TPULSE_DURATION_8 | ICM42605_INT_TDEASSERT_DISABLED);
243 spiWriteReg(&gyro->dev, ICM42605_RA_INT_CONFIG1, intConfig1Value);
244 #endif
247 spiSetClkDivisor(&gyro->dev, spiCalculateDivider(ICM42605_MAX_SPI_CLK_HZ));
250 bool icm42605GyroReadSPI(gyroDev_t *gyro)
252 STATIC_DMA_DATA_AUTO uint8_t dataToSend[7] = {ICM42605_RA_GYRO_DATA_X1 | 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
253 STATIC_DMA_DATA_AUTO uint8_t data[7];
255 const bool ack = spiReadWriteBufRB(&gyro->dev, dataToSend, data, 7);
256 if (!ack) {
257 return false;
260 gyro->gyroADCRaw[X] = (int16_t)((data[1] << 8) | data[2]);
261 gyro->gyroADCRaw[Y] = (int16_t)((data[3] << 8) | data[4]);
262 gyro->gyroADCRaw[Z] = (int16_t)((data[5] << 8) | data[6]);
264 return true;
267 bool icm42605SpiGyroDetect(gyroDev_t *gyro)
269 switch (gyro->mpuDetectionResult.sensor) {
270 case ICM_42605_SPI:
271 break;
272 default:
273 return false;
276 gyro->initFn = icm42605GyroInit;
277 gyro->readFn = icm42605GyroReadSPI;
279 gyro->scale = GYRO_SCALE_2000DPS;
281 return true;
283 #endif