SDM driver support#5610
Open
ttzytt wants to merge 3 commits into
Open
Conversation
bugadani
reviewed
May 27, 2026
Comment on lines
+840
to
+841
| clock_sources = ["apb"] | ||
| default_clock_source = "apb" |
bugadani
reviewed
May 27, 2026
| pub timing: Timing, | ||
| /// Pulse density. | ||
| /// | ||
| /// The value ranges from `-128` to `127`. |
Contributor
There was a problem hiding this comment.
That's obvious from the type
bugadani
reviewed
May 27, 2026
| /// The value ranges from `-128` to `127`. | ||
| /// | ||
| /// The channel must have been successfully connected first. | ||
| pub fn set_pulse_density(&mut self, density: i8) { |
Contributor
There was a problem hiding this comment.
There should only be apply_config, and maybe set_duty. Duplicaing the entire config API with independent methods isn't something we would like to do.
bugadani
reviewed
May 27, 2026
| /// | ||
| /// This only changes the GPIO matrix route and replaces the previous pin | ||
| /// connection. | ||
| pub fn reconnect<'d>(&mut self, pin: impl PeripheralOutput<'d>) { |
Contributor
There was a problem hiding this comment.
This should be a method on Sdm that returns the Channel
bugadani
reviewed
May 27, 2026
| | SDIO slave | ❌ | | | [❌][5169] [^1] | ❌ | [❌][5417] [^1] | | ❌ | | | | ||
| | SHA | ⚒️ | ⚒️ | ⚒️ | ⚒️ | ⚒️ | ⚒️ | ⚒️ | ❌ | ⚒️ | ⚒️ | | ||
| | SDM | [❌][2370] [^1] | | [❌][2370] [^1] | [❌][2370] [^1] | [❌][2370] [^1] | | [❌][2370] [^1] | [❌][2370] [^1] | [❌][2370] [^1] | [❌][2370] [^1] | | ||
| | SDM | ✔️ | | ✔️ | ✔️ | ✔️ | | ✔️ | ✔️ | ✔️ | ✔️ | |
Contributor
There was a problem hiding this comment.
Is every hardware feature immediately supported and tested on all chips?
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.
Add initial Sigma-Delta Modulation driver
This is inspired by the old SDM PR: #2371 and to resolve the issue #2370. I used that PR as the main reference and tried to address many of the design comments raised there. Thanks to @katyo for the original implementation and all the discussion around it.
Overview
This PR adds an initial SDM driver. The current goal is to settle the driver structure and API first, before expanding the documentation and polishing all details.
The driver exposes SDM as a peripheral-wide collection that owns
GPIO_SDand provides individual channel fields. Users can move individual channels out of the collection, for example:This follows the direction discussed in the old PR: the collection itself does not shut the peripheral down on
Drop, because the intended usage is to split channels out and potentially move them into different tasks.Resource Management
Each channel manages its own active peripheral usage through guards. When a channel is connected, it acquires an SDM clock guard. When the channel is dropped, the guard is dropped too, and the clock reference count is released.
The implementation uses
GenericPeripheralGuardfor the actual peripheral enable/disable reference counting. I used this because it is already the common pattern in other drivers. To make that possible for SDM, the metadata now also addsPeripheral::GpioSdentries for the chips that have SDM hardware.However,
GenericPeripheralGuardonly tracks whether the peripheral clock is in use. It does not store SDM-specific state, such as which source clock is currently selected. SDM has a shared source clock for all channels, so this PR also adds a small SDM-specific clock state guarded byNonReentrantMutex.That state tracks:
The public API only allows selecting the clock source at
Sdmconstruction time. Individual channels receive that source internally, but they do not expose a per-channel source selection API. This is intentional because the underlying clock source is shared.Channel Generation
Different chips expose different numbers of SDM channels. Some have 4 channels, while ESP32/ESP32-S2/ESP32-S3/ESP32-P4 have 8.
Writing all channel fields and match arms manually with
cfgattributes gets complicated quickly. To avoid that, this PR follows the style used by dedicated GPIO: the channel count comes from metadata, and metadata generates afor_each_sdm_channel!macro. The SDM driver then uses that macro to generate theSdmchannel fields and the channel-to-output-signal mapping.Clock Source Metadata
The metadata currently includes the default SDM clock source and the list of available SDM clock sources for each supported chip.
At the moment, the driver still has a handwritten
ClockSourceenum and handwritten per-chip clock source configuration code. I am not fully sure yet what the best way is to consume these metadata fields directly from driver code.This part is definitely open for improvement. Feedback on how this should fit with the existing clock-related code would be very helpful.
Example/Testing
This PR also adds an SDM example under
examples/peripheral/sdm.I tested the example on my ESP32-C5 board and the output worked there. I have not tested the other supported chips on hardware yet.
Regarding HIL testing, I'm not sure what to put there for this driver. It does not seem very practical to test the actual SDM waveform. Maybe the HIL test should instead focus on the digital side of the driver, such as connecting a channel, reading back the raw prescaler and pulse density registers, checking prescaler error handling, and verifying that dropping one channel releases the guard state so another channel can still be used. I will add some HIL testing later if that seems like a good idea.
Documentation
The documentation and comment in code is still minimal. I plan to improve it after the overall API shape and resource-management structure have been reviewed.