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

Add method to change SPI bus frequency #235

Merged
merged 1 commit into from
Oct 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions esp-hal-common/src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ where
spi
}

pub fn change_bus_frequency(&mut self, frequency: HertzU32, clocks: &Clocks) {
self.spi.ch_bus_freq(frequency, clocks);
}

/// Return the raw interface to the underlying peripheral instance
pub fn free(self) -> T {
self.spi
Expand Down Expand Up @@ -1331,6 +1335,34 @@ pub trait Instance {
self
}

fn ch_bus_freq(&mut self, frequency: HertzU32, clocks: &Clocks) {

// Disable clock source
#[cfg(not(any(feature = "esp32", feature = "esp32s2")))]
self.register_block().clk_gate.modify(|_, w| {
w.clk_en()
.clear_bit()
.mst_clk_active()
.clear_bit()
.mst_clk_sel()
.clear_bit()
});

// Change clock frequency
self.setup(frequency, clocks);

// Enable clock source
#[cfg(not(any(feature = "esp32", feature = "esp32s2")))]
self.register_block().clk_gate.modify(|_, w| {
w.clk_en()
.set_bit()
.mst_clk_active()
.set_bit()
.mst_clk_sel()
.set_bit()
});
}

fn read_byte(&mut self) -> nb::Result<u8, Error> {
let reg_block = self.register_block();

Expand Down