Skip to content

Commit

Permalink
Merge pull request #3669 from spookyvision/main
Browse files Browse the repository at this point in the history
embassy-stm32: SPI slew rate configuration
  • Loading branch information
Dirbaio authored Dec 21, 2024
2 parents e88adb6 + 7743c8f commit 6789b51
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
16 changes: 16 additions & 0 deletions embassy-stm32/src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,16 @@ fn set_as_af(pin_port: u8, af_num: u8, af_type: AfType) {
r.moder().modify(|w| w.set_moder(n, vals::Moder::ALTERNATE));
}

#[inline(never)]
#[cfg(gpio_v2)]
fn set_speed(pin_port: u8, speed: Speed) {
let pin = unsafe { AnyPin::steal(pin_port) };
let r = pin.block();
let n = pin._pin() as usize;

r.ospeedr().modify(|w| w.set_ospeedr(n, speed.to_ospeedr()));
}

#[inline(never)]
fn set_as_analog(pin_port: u8) {
let pin = unsafe { AnyPin::steal(pin_port) };
Expand Down Expand Up @@ -738,6 +748,12 @@ pub(crate) trait SealedPin {
set_as_af(self.pin_port(), af_num, af_type)
}

#[inline]
#[cfg(gpio_v2)]
fn set_speed(&self, speed: Speed) {
set_speed(self.pin_port(), speed)
}

#[inline]
fn set_as_analog(&self) {
set_as_analog(self.pin_port());
Expand Down
34 changes: 26 additions & 8 deletions embassy-stm32/src/spi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ pub struct Config {
/// There are some ICs that require a pull-up on the MISO pin for some applications.
/// If you are unsure, you probably don't need this.
pub miso_pull: Pull,
/// signal rise/fall speed (slew rate) - defaults to `Medium`.
/// Increase for high SPI speeds. Change to `Low` to reduce ringing.
pub rise_fall_speed: Speed,
}

impl Default for Config {
Expand All @@ -64,6 +67,7 @@ impl Default for Config {
bit_order: BitOrder::MsbFirst,
frequency: Hertz(1_000_000),
miso_pull: Pull::None,
rise_fall_speed: Speed::VeryHigh,
}
}
}
Expand Down Expand Up @@ -92,14 +96,14 @@ impl Config {

#[cfg(gpio_v1)]
fn sck_af(&self) -> AfType {
AfType::output(OutputType::PushPull, Speed::VeryHigh)
AfType::output(OutputType::PushPull, self.rise_fall_speed)
}

#[cfg(gpio_v2)]
fn sck_af(&self) -> AfType {
AfType::output_pull(
OutputType::PushPull,
Speed::VeryHigh,
self.rise_fall_speed,
match self.mode.polarity {
Polarity::IdleLow => Pull::Down,
Polarity::IdleHigh => Pull::Up,
Expand All @@ -118,6 +122,7 @@ pub struct Spi<'d, M: PeriMode> {
rx_dma: Option<ChannelAndRequest<'d>>,
_phantom: PhantomData<M>,
current_word_size: word_impl::Config,
rise_fall_speed: Speed,
}

impl<'d, M: PeriMode> Spi<'d, M> {
Expand All @@ -140,6 +145,7 @@ impl<'d, M: PeriMode> Spi<'d, M> {
rx_dma,
current_word_size: <u8 as SealedWord>::CONFIG,
_phantom: PhantomData,
rise_fall_speed: config.rise_fall_speed,
};
this.enable_and_init(config);
this
Expand Down Expand Up @@ -243,6 +249,17 @@ impl<'d, M: PeriMode> Spi<'d, M> {

let br = compute_baud_rate(self.kernel_clock, config.frequency);

#[cfg(gpio_v2)]
{
self.rise_fall_speed = config.rise_fall_speed;
if let Some(sck) = self.sck.as_ref() {
sck.set_speed(config.rise_fall_speed);
}
if let Some(mosi) = self.mosi.as_ref() {
mosi.set_speed(config.rise_fall_speed);
}
}

#[cfg(any(spi_v1, spi_f1, spi_v2))]
self.info.regs.cr1().modify(|w| {
w.set_cpha(cpha);
Expand Down Expand Up @@ -308,6 +325,7 @@ impl<'d, M: PeriMode> Spi<'d, M> {
bit_order,
frequency,
miso_pull,
rise_fall_speed: self.rise_fall_speed,
}
}

Expand Down Expand Up @@ -441,7 +459,7 @@ impl<'d> Spi<'d, Blocking> {
Self::new_inner(
peri,
new_pin!(sck, config.sck_af()),
new_pin!(mosi, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(mosi, AfType::output(OutputType::PushPull, config.rise_fall_speed)),
new_pin!(miso, AfType::input(config.miso_pull)),
None,
None,
Expand Down Expand Up @@ -477,7 +495,7 @@ impl<'d> Spi<'d, Blocking> {
Self::new_inner(
peri,
new_pin!(sck, config.sck_af()),
new_pin!(mosi, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(mosi, AfType::output(OutputType::PushPull, config.rise_fall_speed)),
None,
None,
None,
Expand All @@ -496,7 +514,7 @@ impl<'d> Spi<'d, Blocking> {
Self::new_inner(
peri,
None,
new_pin!(mosi, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(mosi, AfType::output(OutputType::PushPull, config.rise_fall_speed)),
None,
None,
None,
Expand All @@ -519,7 +537,7 @@ impl<'d> Spi<'d, Async> {
Self::new_inner(
peri,
new_pin!(sck, config.sck_af()),
new_pin!(mosi, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(mosi, AfType::output(OutputType::PushPull, config.rise_fall_speed)),
new_pin!(miso, AfType::input(config.miso_pull)),
new_dma!(tx_dma),
new_dma!(rx_dma),
Expand Down Expand Up @@ -561,7 +579,7 @@ impl<'d> Spi<'d, Async> {
Self::new_inner(
peri,
new_pin!(sck, config.sck_af()),
new_pin!(mosi, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(mosi, AfType::output(OutputType::PushPull, config.rise_fall_speed)),
None,
new_dma!(tx_dma),
None,
Expand All @@ -581,7 +599,7 @@ impl<'d> Spi<'d, Async> {
Self::new_inner(
peri,
None,
new_pin!(mosi, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(mosi, AfType::output(OutputType::PushPull, config.rise_fall_speed)),
None,
new_dma!(tx_dma),
None,
Expand Down

0 comments on commit 6789b51

Please sign in to comment.