fix(icm20948): Fix endianness and scaling of magnetometer data#597
Merged
fix(icm20948): Fix endianness and scaling of magnetometer data#597
Conversation
|
✅Static analysis result - no issues found! ✅ |
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| Icm20948<I>::RawValue Icm20948<I>::get_magnetometer_raw(std::error_code &ec) { | ||
| return get_raw(RegisterBank0::MAG_DATA, ec); | ||
| // the magnetometer data is read through the I2C master, but it has a | ||
| // different endianess than the accelerometer and gyroscope data. |
There was a problem hiding this comment.
Typo in comment: “endianess” should be “endianness”.
Suggested change
| // different endianess than the accelerometer and gyroscope data. | |
| // different endianness than the accelerometer and gyroscope data. |
Comment on lines
+1030
to
+1034
| return { | ||
| static_cast<int16_t>(((raw_mag.x & 0xFF00) >> 8) | ((raw_mag.x & 0xFF) << 8)), | ||
| static_cast<int16_t>(((raw_mag.y & 0xFF00) >> 8) | ((raw_mag.y & 0xFF) << 8)), | ||
| static_cast<int16_t>(((raw_mag.z & 0xFF00) >> 8) | ((raw_mag.z & 0xFF) << 8)), | ||
| }; |
There was a problem hiding this comment.
The manual byte-swap logic is duplicated for x/y/z and is a bit hard to read/maintain. Consider extracting a small helper (e.g., a local lambda) that swaps a 16-bit value (ideally operating on uint16_t) and reuse it for each axis to reduce repetition and avoid subtle signed/bitshift pitfalls.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Motivation and Context
Closes #596
How has this been tested?
Build and run
icm20948/exampleand verify that magnetometer data is being read correctly and is scaled appropriately.Screenshots (if appropriate, e.g. schematic, board, console logs, lab pictures):
Types of changes
Checklist:
Software
.github/workflows/build.ymlfile to add my new test to the automated cloud build github action.