sparkfun / SparkFun_ICM-20948_ArduinoLibrary

Arduino support for ICM_20948 w/ portable C backbone
Other
160 stars 69 forks source link

Bank change to 0 after reset is omitted #71

Closed peterleif closed 2 years ago

peterleif commented 3 years ago

Subject of the issue

This library implements a mechanism to reduce the amount of I2C traffic by caching the previously used register bank and skip the bank change operation when it is not required.

Currently, no initialization method for ICM_20948_Device_t is provided and therefore all members including _last_bank is initialized as 0 (if declared as a global or static variable).

It means that if the first register access uses register bank 0 no bank change operation is performed.

This behavior can be problematic if the MCU is reset while the power to ICM-20948 is kept on. Suppose the MCU is performing register access using bank 2, for example, and then the MCU resets, and the first register access after the reset is intended to use register bank 0. Since the register bank change operation is skipped, the register access is recognized as access to the register bank 2.

To prevent this problem, I would suggest implementing an initialization method for ICM_20948_Device_t that sets an invalid value (e.g. 4) to _last_bank so the first register bank change operation is performed 100%.

Your workbench

Steps to reproduce

Expected behavior

The MCU should access bank 0 after the reset.

Actual behavior

The MCU accesses bank 2 since no bank change operation is performed after the reset.

PaulZC commented 3 years ago

Hi Etsushi (@peterleif ), Great suggestion - thank you. Would you like to send a Pull Request with the changes? Best wishes, Paul

peterleif commented 3 years ago

Hi Paul (@PaulZC ), I would like to send a PR. I don't have an Arduino test environment, so please forgive me for not being able to do adequate testing.

Best wishes, Etsushi

PaulZC commented 2 years ago

Merged in v1.2.8. Closing...