Skip to content

begin() was split up - #19

Open
mahboud wants to merge 2 commits into
adafruit:masterfrom
mahboud:new-init-method
Open

mahboud wants to merge 2 commits into
adafruit:masterfrom
mahboud:new-init-method

Conversation

@mahboud

@mahboud mahboud commented Mar 7, 2022

Copy link
Copy Markdown
Contributor

The ESP32 has a deep sleep mode, which returns, not to where deep sleep was initiated, but to setup() all over again. A difference between that and a regular reset or power-up is that the RTC variables are not zeroed out.

An MPU6050 can be connected to the ESP32 and be allowed to continue looking for motion, while the ESP32 is asleep. When the MPU6050 wakes the ESP32 back, it is good to be able to access the MPU6050's interrupt and other registers. However, if the only way to regain communication with the MPU6050 through I2C, is by calling a mpu.begin(), then begin() will reset the MPU and erase the interesting data.

This PR attempts to show one workaround. begin() is split into a private function initI2C() and there is also an init() call added. Normally, users will use begin() as before. The behavior should be unchanged.

Users who wish to preserve the contents of the MPU can call init(), which only does the bare minimum to restore communication with the MPU via i2c.

There are two ways (at least) to know whether the MPU has already been set up

  • a RTC_DATA_ATTR int bootCount = 0; that is incremented by setup(). This will always be 0 if the ESP32 has been reset or is powered up, or non-zero if it is waking up. You can use begin() when it is zero and init() when it is non-zero.
  • do an init() call, check the MPU6050's registers for known values you had set before and if you don't find those, then call begin().

Mahboud Zabetian and others added 2 commits March 7, 2022 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant