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

Changing SPI clock is unexpectedly expensive #1666

Open
ProfFan opened this issue Jun 7, 2024 · 3 comments
Open

Changing SPI clock is unexpectedly expensive #1666

ProfFan opened this issue Jun 7, 2024 · 3 comments
Labels
peripheral:spi SPI peripheral

Comments

@ProfFan
Copy link
Contributor

ProfFan commented Jun 7, 2024

Changing the SPI frequency essentially goes to

self.setup(frequency, clocks);

which is a loop-based search. However in situations like multi-slave SPI bus with different clocks, this will need to be executed many times.

Don't know what is the best way, could

  1. add forced inlining to the logic
  2. add a shortcut when the clock speed is multiples of clk_spi_mst/32 as this is the most common scenario
  3. add a static cache for computed values?

XREF: rust-embedded/embedded-hal#605

@Dominaezzz
Copy link
Collaborator

Dominaezzz commented Jun 7, 2024

3. add a static cache for computed values?

I think this the answer.
I imagine something like this.

let clock_setting = Spi::calculate_clock_settings(&clocks, frequency);

// User can store clock_setting object per device.

spi.set_frequency(clock_setting);

EDIT: This will also be very handy for setting up configurable segmented transfers.

@ProfFan
Copy link
Contributor Author

ProfFan commented Dec 28, 2024

Bumping this issue. SPI sharing is a very common use case and this can be easily supported by introducing a SpiClock struct that has From<Hz> and let the SpiBus::Config struct store this instead of the Hz value.

@bugadani
Copy link
Contributor

I don't think this is as easy as you think, the peripherals can have multiple clock sources. Since we intend to support configuring clock sources, we'll have to somehow tie these config options together, while at the same time remain backward compatible when we introduce the clock source configuration.

cc #1668

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
peripheral:spi SPI peripheral
Projects
Status: Todo
Development

No branches or pull requests

5 participants