-
-
Notifications
You must be signed in to change notification settings - Fork 542
feat(radio): add gyro support for HelloRadio V16 #7232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+281
−6
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5fef8e3
feat(radio): add gyro support for HelloRadio V16
gismo2004 e523f5d
try a verified write
gismo2004 f1dbab8
new line
gismo2004 6674bd6
remove double call
gismo2004 a9d7408
remove double calls
gismo2004 4cb6f7f
clean log output
gismo2004 8d47b34
remove all the double checks. icm42607C does also not use them
gismo2004 5abaeab
remove delay_us
gismo2004 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| /* | ||
| * Copyright (C) EdgeTX | ||
| * | ||
| * Based on code named | ||
| * opentx - https://github.com/opentx/opentx | ||
| * th9x - http://code.google.com/p/th9x | ||
| * er9x - http://code.google.com/p/er9x | ||
| * gruvin9x - http://code.google.com/p/gruvin9x | ||
| * | ||
| * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License version 2 as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| */ | ||
|
|
||
| #include "hal/i2c_driver.h" | ||
|
|
||
| #include "delays_driver.h" | ||
|
|
||
| #include "icm42627.h" | ||
|
|
||
| #include "debug.h" | ||
|
|
||
| static etx_i2c_bus_t s_i2c_bus; | ||
| static uint16_t s_i2c_addr; | ||
|
|
||
| static constexpr uint32_t RESET_DELAY_MS = 150; | ||
| static constexpr uint32_t READY_TIMEOUT_MS = 200; | ||
| static constexpr uint32_t POWER_DELAY_MS = 20; | ||
| static constexpr uint32_t CONFIG_DELAY_MS = 1; | ||
| static constexpr uint8_t BURST_READ_LEN = 14; | ||
|
|
||
| static int write_cmd(uint8_t reg, uint8_t value) | ||
| { | ||
| return i2c_write(s_i2c_bus, s_i2c_addr, reg, 1, &value, 1); | ||
| } | ||
|
|
||
| static int read_cmd(uint8_t reg, uint8_t *value) | ||
| { | ||
| return i2c_read(s_i2c_bus, s_i2c_addr, reg, 1, value, 1); | ||
| } | ||
|
|
||
| static int wait_for_device_ready(uint32_t timeout_ms) | ||
| { | ||
| while (timeout_ms > 0) { | ||
| if (i2c_dev_ready(s_i2c_bus, s_i2c_addr) >= 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| delay_ms(10); | ||
| if (timeout_ms < 10) { | ||
| break; | ||
| } | ||
| timeout_ms -= 10; | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| static int gyro42627Init(etx_i2c_bus_t bus, uint16_t addr) | ||
| { | ||
| s_i2c_bus = bus; | ||
| s_i2c_addr = addr; | ||
|
|
||
| TRACE("ICM42627: probing I2C address 0x%02X", addr); | ||
|
|
||
| if (i2c_init(s_i2c_bus) < 0) { | ||
| TRACE("ICM42627: i2c_init failed"); | ||
| return -1; | ||
| } | ||
|
|
||
| if (i2c_dev_ready(s_i2c_bus, s_i2c_addr) < 0) { | ||
| TRACE("ICM42627: device did not acknowledge on I2C"); | ||
| return -1; | ||
| } | ||
|
|
||
| uint8_t who_am_i = 0; | ||
| if (read_cmd(ICM42627_WHO_AM_I_REG, &who_am_i) < 0) { | ||
| TRACE("ICM42627: failed to read WHO_AM_I"); | ||
| return -1; | ||
| } | ||
|
|
||
| if (who_am_i != ICM42627_WHO_AM_I) { | ||
| TRACE("ICM42627: unexpected WHO_AM_I 0x%02X, expected 0x%02X", who_am_i, | ||
| ICM42627_WHO_AM_I); | ||
| return -1; | ||
| } | ||
|
|
||
| if (write_cmd(ICM42627_DEVICE_CONFIG_REG, ICM42627_RESET) < 0) { | ||
| TRACE("ICM42627: failed to write reset command"); | ||
| } | ||
|
|
||
| delay_ms(RESET_DELAY_MS); | ||
|
|
||
| if (wait_for_device_ready(READY_TIMEOUT_MS) < 0) { | ||
| TRACE("ICM42627: device did not become ready after reset"); | ||
| return -1; | ||
| } | ||
|
|
||
| if (write_cmd(ICM42627_PWR_MGMT0_REG, ICM42627_PWR_STAGE1) < 0) { | ||
| TRACE("ICM42627: failed to write PWR_MGMT0 stage 1"); | ||
| return -1; | ||
| } | ||
| delay_ms(POWER_DELAY_MS); | ||
|
|
||
| if (write_cmd(ICM42627_PWR_MGMT0_REG, ICM42627_PWR_STAGE2) < 0) { | ||
| TRACE("ICM42627: failed to write PWR_MGMT0 stage 2"); | ||
| return -1; | ||
| } | ||
| delay_ms(POWER_DELAY_MS); | ||
|
|
||
| if (write_cmd(ICM42627_GYRO_CONFIG0_REG, ICM42627_GYRO_CONFIG) < 0) { | ||
| TRACE("ICM42627: failed to write GYRO_CONFIG0"); | ||
| return -1; | ||
| } | ||
| delay_ms(CONFIG_DELAY_MS); | ||
|
|
||
| if (write_cmd(ICM42627_ACCEL_CONFIG0_REG, ICM42627_ACCEL_CONFIG) < 0) { | ||
| TRACE("ICM42627: failed to write ACCEL_CONFIG0"); | ||
| return -1; | ||
| } | ||
| delay_ms(CONFIG_DELAY_MS); | ||
|
|
||
| if (write_cmd(ICM42627_GYRO_CONFIG1_REG, ICM42627_GYRO_FILTER) < 0) { | ||
| TRACE("ICM42627: failed to write GYRO_CONFIG1"); | ||
| return -1; | ||
| } | ||
| delay_ms(CONFIG_DELAY_MS); | ||
|
|
||
| if (write_cmd(ICM42627_ACCEL_GYRO_BW_REG, ICM42627_ACCEL_GYRO_BW) < 0) { | ||
| TRACE("ICM42627: failed to write ACCEL_GYRO_BW"); | ||
| return -1; | ||
| } | ||
| delay_ms(CONFIG_DELAY_MS); | ||
|
|
||
| if (write_cmd(ICM42627_ACCEL_CONFIG1_REG, ICM42627_ACCEL_FILTER) < 0) { | ||
| TRACE("ICM42627: failed to write ACCEL_CONFIG1"); | ||
| return -1; | ||
| } | ||
| delay_ms(CONFIG_DELAY_MS); | ||
|
|
||
| if (write_cmd(ICM42627_BANK_SEL_REG, ICM42627_BANK1) < 0) { | ||
| TRACE("ICM42627: failed to select register bank 1"); | ||
| return -1; | ||
| } | ||
| delay_ms(CONFIG_DELAY_MS); | ||
|
|
||
| if (write_cmd(ICM42627_GYRO_STATIC2_REG, ICM42627_GYRO_STATIC2) < 0) { | ||
| TRACE("ICM42627: failed to write GYRO_STATIC2"); | ||
| return -1; | ||
| } | ||
| delay_ms(CONFIG_DELAY_MS); | ||
|
|
||
| if (write_cmd(ICM42627_BANK_SEL_REG, ICM42627_BANK0) < 0) { | ||
| TRACE("ICM42627: failed to restore register bank 0"); | ||
| return -1; | ||
| } | ||
| delay_ms(CONFIG_DELAY_MS); | ||
|
|
||
| // Re-read WHO_AM_I after reset and banked configuration to catch a device | ||
| // that stopped responding even though the initial probe succeeded. | ||
| if (read_cmd(ICM42627_WHO_AM_I_REG, &who_am_i) < 0 || | ||
| who_am_i != ICM42627_WHO_AM_I) { | ||
| TRACE("ICM42627: final WHO_AM_I check failed (0x%02X)", who_am_i); | ||
| return -1; | ||
| } | ||
|
|
||
| TRACE("ICM42627: init complete"); | ||
| return 0; | ||
| } | ||
|
|
||
| static int gyro42627Read(etx_imu_data_t *data) | ||
| { | ||
| uint8_t buf[BURST_READ_LEN] = {0}; | ||
|
|
||
| if (i2c_read(s_i2c_bus, s_i2c_addr, ICM42627_DATA_REG, 1, buf, sizeof(buf)) < 0) { | ||
| TRACE("ICM42627: failed to read sensor sample block"); | ||
| return -1; | ||
| } | ||
|
|
||
| const int16_t accel_x = (int16_t)((buf[2] << 8) | buf[3]); | ||
| const int16_t accel_y = (int16_t)((buf[4] << 8) | buf[5]); | ||
| const int16_t accel_z = (int16_t)((buf[6] << 8) | buf[7]); | ||
| const int16_t gyro_x = (int16_t)((buf[8] << 8) | buf[9]); | ||
| const int16_t gyro_y = (int16_t)((buf[10] << 8) | buf[11]); | ||
| const int16_t gyro_z = (int16_t)((buf[12] << 8) | buf[13]); | ||
|
|
||
| data->accel_x = accel_y; | ||
| data->accel_y = accel_x; | ||
| data->accel_z = -accel_z; | ||
| data->gyro_x = gyro_y; | ||
| data->gyro_y = gyro_x; | ||
| data->gyro_z = -gyro_z; | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| const etx_imu_driver_t imu_icm42627_driver = { | ||
| gyro42627Init, | ||
| gyro42627Read, | ||
| "ICM42627", | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright (C) EdgeTX | ||
| * | ||
| * Based on code named | ||
| * opentx - https://github.com/opentx/opentx | ||
| * th9x - http://code.google.com/p/th9x | ||
| * er9x - http://code.google.com/p/er9x | ||
| * gruvin9x - http://code.google.com/p/gruvin9x | ||
| * | ||
| * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License version 2 as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "hal/imu.h" | ||
|
|
||
| #define ICM42627_WHO_AM_I_REG 0x75 | ||
| #define ICM42627_WHO_AM_I 0x20 | ||
|
|
||
| #define ICM42627_DEVICE_CONFIG_REG 0x11 | ||
| #define ICM42627_PWR_MGMT0_REG 0x4E | ||
| #define ICM42627_GYRO_CONFIG0_REG 0x4F | ||
| #define ICM42627_ACCEL_CONFIG0_REG 0x50 | ||
| #define ICM42627_GYRO_CONFIG1_REG 0x51 | ||
| #define ICM42627_ACCEL_GYRO_BW_REG 0x52 | ||
| #define ICM42627_ACCEL_CONFIG1_REG 0x53 | ||
| #define ICM42627_BANK_SEL_REG 0x76 | ||
|
|
||
| #define ICM42627_GYRO_STATIC2_REG 0x0B | ||
| #define ICM42627_DATA_REG 0x1D | ||
|
|
||
| #define ICM42627_BANK0 0x00 | ||
| #define ICM42627_BANK1 0x01 | ||
|
|
||
| #define ICM42627_RESET 0x01 | ||
| #define ICM42627_PWR_STAGE1 0x0F | ||
| #define ICM42627_PWR_STAGE2 0x2F | ||
| #define ICM42627_GYRO_CONFIG 0x07 | ||
| #define ICM42627_ACCEL_CONFIG 0x47 | ||
| #define ICM42627_GYRO_FILTER 0x0A | ||
| #define ICM42627_ACCEL_GYRO_BW 0x22 | ||
| #define ICM42627_ACCEL_FILTER 0x14 | ||
| #define ICM42627_GYRO_STATIC2 0x00 | ||
|
|
||
| extern const etx_imu_driver_t imu_icm42627_driver; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fail init when the reset command write is rejected.
At Line 95, a failed reset write is logged but init continues. This can report successful init with an undefined device state; it should fail fast like the subsequent config writes.
Suggested fix
if (write_cmd(ICM42627_DEVICE_CONFIG_REG, ICM42627_RESET) < 0) { TRACE("ICM42627: failed to write reset command"); + return -1; }📝 Committable suggestion
🤖 Prompt for AI Agents