Skip to content
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

Disable peripherals when unused #2544

Merged
merged 14 commits into from
Nov 21, 2024
Merged

Conversation

bjoernQ
Copy link
Contributor

@bjoernQ bjoernQ commented Nov 15, 2024

Thank you for your contribution!

We appreciate the time and effort you've put into this pull request.
To help us review it efficiently, please ensure you've gone through the following checklist:

Submission Checklist 📝

  • I have updated existing examples or added new ones (if applicable).
  • I have used cargo xtask fmt-packages command to ensure that all changed code is formatted correctly.
  • My changes were added to the CHANGELOG.md in the proper section.
  • I have added necessary changes to user code to the Migration Guide.
  • My changes are in accordance to the esp-rs API guidelines

Extra:

Pull Request Details 📖

Description

Closes #1382
Supercedes #2484

This disables most peripherals when their driver gets fully dropped.

There are some peripherals which don't get touched

  • UART0 (console logging would break)
  • serial-jtag (console logging would break)
  • systimer (we use it to get the time since boot)
  • TIMG (we use TIMG0-LACT to get the time on ESP32)
  • DMA (should be fine to do - but lets do it in a separate PR)
  • LEDC (that driver will see a refactoring or rewrite)

others which we currently don't explicitly enable (some of them for reasons, but not all)

Additionally, peripherals are disabled in init - gives us a ~ 5mA advantage when not using them

Testing

  • HIL-tests
  • manual testing with logging enabled

esp-hal/src/hmac.rs Outdated Show resolved Hide resolved
esp-hal/src/system.rs Outdated Show resolved Hide resolved
@bjoernQ bjoernQ marked this pull request as ready for review November 15, 2024 11:57
esp-hal/src/trace.rs Outdated Show resolved Hide resolved
esp-hal/src/lcd_cam/mod.rs Outdated Show resolved Hide resolved
esp-hal/src/system.rs Outdated Show resolved Hide resolved
esp-hal/src/system.rs Outdated Show resolved Hide resolved
esp-hal/src/i2s/master.rs Outdated Show resolved Hide resolved
@@ -220,8 +221,8 @@ where
channel.runtime_ensure_compatible(&i2s);
let channel = channel.degrade();

PeripheralClockControl::reset(i2s.peripheral());
PeripheralClockControl::enable(i2s.peripheral());
let guard = PeripheralGuard::new(i2s.peripheral());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems at one point the PeripheralGuard won over manually managing clocks. Would you consider updating AES/ADC, too?

esp-hal/src/parl_io.rs Outdated Show resolved Hide resolved
esp-hal/src/parl_io.rs Outdated Show resolved Hide resolved
esp-hal/src/rmt.rs Outdated Show resolved Hide resolved
esp-hal/src/rmt.rs Outdated Show resolved Hide resolved
@bugadani
Copy link
Contributor

In general, I like the idea of PeripheralGuard. I'd prefer it to be generic and zero-sized, but I don't see that happening with type erased peripherals. Maybe we could have two variants, one for ZST peripherals and one for erased ones.

Looking closely at the code, I can see that this PR was done mostly mechanically, there are a few places where a guard isn't necessary (RMT, and some others), or some where it looks not entirely correct to my eyes (PARL_IO).

.with_mosi(miso_mosi)
.with_miso(miso)
.with_miso(miso) // order matters
.with_mosi(miso_mosi) // order matters
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orthogonal to the PR but is this something we can fix @bugadani? Seems like would be hard to spot without knowing the internals of GPIO

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually addressed in #2557 I believe. Because with_miso in that PR only sets the input signal, this won't be a problem.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well now that PR is closed, the problem is back with us 🥲

@@ -112,7 +112,8 @@ mod tests {
)
.map_err(|e| e.0)
.unwrap();
transfer.wait();
// dropping SPI would make us see an additional edge - so let's keep SPI alive
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we reset the peripheral, we also reset CTRL.D_POL ( to 1) so MOSI is idle-high

So, it looks like this:

image

See how MOSI is set to high after resetting (near the marker)

BTW we can also see that effect on main before the initial transaction

image

There we see MOSI shows a "bump" between reset and setting D_POL to 0

esp-hal/src/trace.rs Outdated Show resolved Hide resolved
@bjoernQ bjoernQ force-pushed the disable-peripherals branch from f00a3c9 to 2cd70ec Compare November 20, 2024 11:51
@bjoernQ bjoernQ mentioned this pull request Nov 20, 2024
Copy link
Member

@jessebraham jessebraham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes LGTM, but will wait for @MabezDev to approve before merging since he had requested changes

Copy link
Member

@MabezDev MabezDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@MabezDev MabezDev added this pull request to the merge queue Nov 21, 2024
Merged via the queue into esp-rs:main with commit f9203dc Nov 21, 2024
28 checks passed
@bjoernQ bjoernQ deleted the disable-peripherals branch November 26, 2024 08:40
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.

Implement Drop for drivers to restore peripherals to their default states
5 participants