|
| 1 | +/* |
| 2 | + * Copyright (C) EdgeTX |
| 3 | + * |
| 4 | + * Based on code named |
| 5 | + * opentx - https://github.com/opentx/opentx |
| 6 | + * th9x - http://code.google.com/p/th9x |
| 7 | + * er9x - http://code.google.com/p/er9x |
| 8 | + * gruvin9x - http://code.google.com/p/gruvin9x |
| 9 | + * |
| 10 | + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html |
| 11 | + * |
| 12 | + * This program is free software; you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU General Public License version 2 as |
| 14 | + * published by the Free Software Foundation. |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU General Public License for more details. |
| 20 | + */ |
| 21 | + |
| 22 | +#include "hal/i2c_driver.h" |
| 23 | + |
| 24 | +#include "delays_driver.h" |
| 25 | + |
| 26 | +#include "icm42627.h" |
| 27 | + |
| 28 | +#include "debug.h" |
| 29 | + |
| 30 | +static etx_i2c_bus_t s_i2c_bus; |
| 31 | +static uint16_t s_i2c_addr; |
| 32 | + |
| 33 | +static constexpr uint32_t RESET_DELAY_MS = 150; |
| 34 | +static constexpr uint32_t READY_TIMEOUT_MS = 200; |
| 35 | +static constexpr uint32_t POWER_DELAY_MS = 20; |
| 36 | +static constexpr uint32_t CONFIG_DELAY_MS = 1; |
| 37 | +static constexpr uint8_t BURST_READ_LEN = 14; |
| 38 | + |
| 39 | +static int write_cmd(uint8_t reg, uint8_t value) |
| 40 | +{ |
| 41 | + return i2c_write(s_i2c_bus, s_i2c_addr, reg, 1, &value, 1); |
| 42 | +} |
| 43 | + |
| 44 | +static int read_cmd(uint8_t reg, uint8_t *value) |
| 45 | +{ |
| 46 | + return i2c_read(s_i2c_bus, s_i2c_addr, reg, 1, value, 1); |
| 47 | +} |
| 48 | + |
| 49 | +static int wait_for_device_ready(uint32_t timeout_ms) |
| 50 | +{ |
| 51 | + while (timeout_ms > 0) { |
| 52 | + if (i2c_dev_ready(s_i2c_bus, s_i2c_addr) >= 0) { |
| 53 | + return 0; |
| 54 | + } |
| 55 | + |
| 56 | + delay_ms(10); |
| 57 | + if (timeout_ms < 10) { |
| 58 | + break; |
| 59 | + } |
| 60 | + timeout_ms -= 10; |
| 61 | + } |
| 62 | + |
| 63 | + return -1; |
| 64 | +} |
| 65 | + |
| 66 | +static int gyro42627Init(etx_i2c_bus_t bus, uint16_t addr) |
| 67 | +{ |
| 68 | + s_i2c_bus = bus; |
| 69 | + s_i2c_addr = addr; |
| 70 | + |
| 71 | + TRACE("ICM42627: probing I2C address 0x%02X", addr); |
| 72 | + |
| 73 | + if (i2c_init(s_i2c_bus) < 0) { |
| 74 | + TRACE("ICM42627: i2c_init failed"); |
| 75 | + return -1; |
| 76 | + } |
| 77 | + |
| 78 | + if (i2c_dev_ready(s_i2c_bus, s_i2c_addr) < 0) { |
| 79 | + TRACE("ICM42627: device did not acknowledge on I2C"); |
| 80 | + return -1; |
| 81 | + } |
| 82 | + |
| 83 | + uint8_t who_am_i = 0; |
| 84 | + if (read_cmd(ICM42627_WHO_AM_I_REG, &who_am_i) < 0) { |
| 85 | + TRACE("ICM42627: failed to read WHO_AM_I"); |
| 86 | + return -1; |
| 87 | + } |
| 88 | + |
| 89 | + if (who_am_i != ICM42627_WHO_AM_I) { |
| 90 | + TRACE("ICM42627: unexpected WHO_AM_I 0x%02X, expected 0x%02X", who_am_i, |
| 91 | + ICM42627_WHO_AM_I); |
| 92 | + return -1; |
| 93 | + } |
| 94 | + |
| 95 | + if (write_cmd(ICM42627_DEVICE_CONFIG_REG, ICM42627_RESET) < 0) { |
| 96 | + TRACE("ICM42627: failed to write reset command"); |
| 97 | + } |
| 98 | + |
| 99 | + delay_ms(RESET_DELAY_MS); |
| 100 | + |
| 101 | + if (wait_for_device_ready(READY_TIMEOUT_MS) < 0) { |
| 102 | + TRACE("ICM42627: device did not become ready after reset"); |
| 103 | + return -1; |
| 104 | + } |
| 105 | + |
| 106 | + if (write_cmd(ICM42627_PWR_MGMT0_REG, ICM42627_PWR_STAGE1) < 0) { |
| 107 | + TRACE("ICM42627: failed to write PWR_MGMT0 stage 1"); |
| 108 | + return -1; |
| 109 | + } |
| 110 | + delay_ms(POWER_DELAY_MS); |
| 111 | + |
| 112 | + if (write_cmd(ICM42627_PWR_MGMT0_REG, ICM42627_PWR_STAGE2) < 0) { |
| 113 | + TRACE("ICM42627: failed to write PWR_MGMT0 stage 2"); |
| 114 | + return -1; |
| 115 | + } |
| 116 | + delay_ms(POWER_DELAY_MS); |
| 117 | + |
| 118 | + if (write_cmd(ICM42627_GYRO_CONFIG0_REG, ICM42627_GYRO_CONFIG) < 0) { |
| 119 | + TRACE("ICM42627: failed to write GYRO_CONFIG0"); |
| 120 | + return -1; |
| 121 | + } |
| 122 | + delay_ms(CONFIG_DELAY_MS); |
| 123 | + |
| 124 | + if (write_cmd(ICM42627_ACCEL_CONFIG0_REG, ICM42627_ACCEL_CONFIG) < 0) { |
| 125 | + TRACE("ICM42627: failed to write ACCEL_CONFIG0"); |
| 126 | + return -1; |
| 127 | + } |
| 128 | + delay_ms(CONFIG_DELAY_MS); |
| 129 | + |
| 130 | + if (write_cmd(ICM42627_GYRO_CONFIG1_REG, ICM42627_GYRO_FILTER) < 0) { |
| 131 | + TRACE("ICM42627: failed to write GYRO_CONFIG1"); |
| 132 | + return -1; |
| 133 | + } |
| 134 | + delay_ms(CONFIG_DELAY_MS); |
| 135 | + |
| 136 | + if (write_cmd(ICM42627_ACCEL_GYRO_BW_REG, ICM42627_ACCEL_GYRO_BW) < 0) { |
| 137 | + TRACE("ICM42627: failed to write ACCEL_GYRO_BW"); |
| 138 | + return -1; |
| 139 | + } |
| 140 | + delay_ms(CONFIG_DELAY_MS); |
| 141 | + |
| 142 | + if (write_cmd(ICM42627_ACCEL_CONFIG1_REG, ICM42627_ACCEL_FILTER) < 0) { |
| 143 | + TRACE("ICM42627: failed to write ACCEL_CONFIG1"); |
| 144 | + return -1; |
| 145 | + } |
| 146 | + delay_ms(CONFIG_DELAY_MS); |
| 147 | + |
| 148 | + if (write_cmd(ICM42627_BANK_SEL_REG, ICM42627_BANK1) < 0) { |
| 149 | + TRACE("ICM42627: failed to select register bank 1"); |
| 150 | + return -1; |
| 151 | + } |
| 152 | + delay_ms(CONFIG_DELAY_MS); |
| 153 | + |
| 154 | + if (write_cmd(ICM42627_GYRO_STATIC2_REG, ICM42627_GYRO_STATIC2) < 0) { |
| 155 | + TRACE("ICM42627: failed to write GYRO_STATIC2"); |
| 156 | + return -1; |
| 157 | + } |
| 158 | + delay_ms(CONFIG_DELAY_MS); |
| 159 | + |
| 160 | + if (write_cmd(ICM42627_BANK_SEL_REG, ICM42627_BANK0) < 0) { |
| 161 | + TRACE("ICM42627: failed to restore register bank 0"); |
| 162 | + return -1; |
| 163 | + } |
| 164 | + delay_ms(CONFIG_DELAY_MS); |
| 165 | + |
| 166 | + // Re-read WHO_AM_I after reset and banked configuration to catch a device |
| 167 | + // that stopped responding even though the initial probe succeeded. |
| 168 | + if (read_cmd(ICM42627_WHO_AM_I_REG, &who_am_i) < 0 || |
| 169 | + who_am_i != ICM42627_WHO_AM_I) { |
| 170 | + TRACE("ICM42627: final WHO_AM_I check failed (0x%02X)", who_am_i); |
| 171 | + return -1; |
| 172 | + } |
| 173 | + |
| 174 | + TRACE("ICM42627: init complete"); |
| 175 | + return 0; |
| 176 | +} |
| 177 | + |
| 178 | +static int gyro42627Read(etx_imu_data_t *data) |
| 179 | +{ |
| 180 | + uint8_t buf[BURST_READ_LEN] = {0}; |
| 181 | + |
| 182 | + if (i2c_read(s_i2c_bus, s_i2c_addr, ICM42627_DATA_REG, 1, buf, sizeof(buf)) < 0) { |
| 183 | + TRACE("ICM42627: failed to read sensor sample block"); |
| 184 | + return -1; |
| 185 | + } |
| 186 | + |
| 187 | + const int16_t accel_x = (int16_t)((buf[2] << 8) | buf[3]); |
| 188 | + const int16_t accel_y = (int16_t)((buf[4] << 8) | buf[5]); |
| 189 | + const int16_t accel_z = (int16_t)((buf[6] << 8) | buf[7]); |
| 190 | + const int16_t gyro_x = (int16_t)((buf[8] << 8) | buf[9]); |
| 191 | + const int16_t gyro_y = (int16_t)((buf[10] << 8) | buf[11]); |
| 192 | + const int16_t gyro_z = (int16_t)((buf[12] << 8) | buf[13]); |
| 193 | + |
| 194 | + data->accel_x = accel_y; |
| 195 | + data->accel_y = accel_x; |
| 196 | + data->accel_z = -accel_z; |
| 197 | + data->gyro_x = gyro_y; |
| 198 | + data->gyro_y = gyro_x; |
| 199 | + data->gyro_z = -gyro_z; |
| 200 | + |
| 201 | + return 0; |
| 202 | +} |
| 203 | + |
| 204 | +const etx_imu_driver_t imu_icm42627_driver = { |
| 205 | + gyro42627Init, |
| 206 | + gyro42627Read, |
| 207 | + "ICM42627", |
| 208 | +}; |
0 commit comments