aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32/src
diff options
context:
space:
mode:
authorJoël Schulz-Ansres <[email protected]>2024-05-15 12:54:30 +0200
committerJoël Schulz-Ansres <[email protected]>2024-05-15 12:54:30 +0200
commitdb56c4fe6fb919e89edda37fc5acb2fb05f45745 (patch)
tree63d0449b5423dd66dd6ad4fdb1a566470c1a2c11 /embassy-stm32/src
parentea70b440cd1035f28c3f332a2f72d7fa42ac995d (diff)
Add miso pullup to spi configuration, add input as field for speed
Diffstat (limited to 'embassy-stm32/src')
-rw-r--r--embassy-stm32/src/gpio.rs36
-rw-r--r--embassy-stm32/src/spi/mod.rs29
2 files changed, 63 insertions, 2 deletions
diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs
index 214813a42..d2db0a257 100644
--- a/embassy-stm32/src/gpio.rs
+++ b/embassy-stm32/src/gpio.rs
@@ -265,6 +265,7 @@ impl From<Pull> for vals::Pupdr {
265#[derive(Debug, Copy, Clone)] 265#[derive(Debug, Copy, Clone)]
266#[cfg_attr(feature = "defmt", derive(defmt::Format))] 266#[cfg_attr(feature = "defmt", derive(defmt::Format))]
267pub enum Speed { 267pub enum Speed {
268 Input,
268 Low, 269 Low,
269 Medium, 270 Medium,
270 #[cfg(not(any(syscfg_f0, gpio_v1)))] 271 #[cfg(not(any(syscfg_f0, gpio_v1)))]
@@ -278,6 +279,7 @@ impl From<Speed> for vals::Mode {
278 use Speed::*; 279 use Speed::*;
279 280
280 match speed { 281 match speed {
282 Input => vals::Mode::INPUT,
281 Low => vals::Mode::OUTPUT2MHZ, 283 Low => vals::Mode::OUTPUT2MHZ,
282 Medium => vals::Mode::OUTPUT10MHZ, 284 Medium => vals::Mode::OUTPUT10MHZ,
283 VeryHigh => vals::Mode::OUTPUT50MHZ, 285 VeryHigh => vals::Mode::OUTPUT50MHZ,
@@ -291,6 +293,7 @@ impl From<Speed> for vals::Ospeedr {
291 use Speed::*; 293 use Speed::*;
292 294
293 match speed { 295 match speed {
296 Input => vals::Ospeedr::LOWSPEED,
294 Low => vals::Ospeedr::LOWSPEED, 297 Low => vals::Ospeedr::LOWSPEED,
295 Medium => vals::Ospeedr::MEDIUMSPEED, 298 Medium => vals::Ospeedr::MEDIUMSPEED,
296 #[cfg(not(syscfg_f0))] 299 #[cfg(not(syscfg_f0))]
@@ -676,6 +679,39 @@ pub(crate) trait SealedPin {
676 #[cfg(gpio_v2)] 679 #[cfg(gpio_v2)]
677 self.block().ospeedr().modify(|w| w.set_ospeedr(pin, speed.into())); 680 self.block().ospeedr().modify(|w| w.set_ospeedr(pin, speed.into()));
678 } 681 }
682
683
684 /// Get the pull-up configuration.
685 #[inline]
686 fn pull(&self) -> Pull {
687 critical_section::with(|_| {
688 let r = self.block();
689 let n = self._pin() as usize;
690 #[cfg(gpio_v1)]
691 {
692 let crlh = if n < 8 { 0 } else { 1 };
693 match r.cr(crlh).cnf(n % 8) {
694 vals::CnfIn::FLOATING => Pull::None,
695 _ => if r.bsrr().read().bs(n % 8) {
696 Pull::Up
697 } else if r.bsrr().read().br(n % 8) {
698 Pull::Down
699 } else {
700 Pull::None
701 }
702 }
703 }
704 #[cfg(gpio_v2)]
705 {
706 match r.pupdr().read().pupdr(n % 8) {
707 vals::Pupdr::FLOATING => Pull::None,
708 vals::Pupdr::PULLDOWN => Pull::Down,
709 vals::Pupdr::PULLUP => Pull::Up,
710 vals::Pupdr::_RESERVED_3 => Pull::None,
711 }
712 }
713 })
714 }
679} 715}
680 716
681/// GPIO pin trait. 717/// GPIO pin trait.
diff --git a/embassy-stm32/src/spi/mod.rs b/embassy-stm32/src/spi/mod.rs
index c39ef1913..bcd6b0bf4 100644
--- a/embassy-stm32/src/spi/mod.rs
+++ b/embassy-stm32/src/spi/mod.rs
@@ -50,6 +50,11 @@ pub struct Config {
50 pub bit_order: BitOrder, 50 pub bit_order: BitOrder,
51 /// Clock frequency. 51 /// Clock frequency.
52 pub frequency: Hertz, 52 pub frequency: Hertz,
53 /// Enable internal pullup on MISO.
54 ///
55 /// There are some ICs that require a pull-up on the MISO pin for some applications.
56 /// If you are unsure, you probably don't need this.
57 pub miso_pullup: bool,
53} 58}
54 59
55impl Default for Config { 60impl Default for Config {
@@ -58,6 +63,7 @@ impl Default for Config {
58 mode: MODE_0, 63 mode: MODE_0,
59 bit_order: BitOrder::MsbFirst, 64 bit_order: BitOrder::MsbFirst,
60 frequency: Hertz(1_000_000), 65 frequency: Hertz(1_000_000),
66 miso_pullup: false,
61 } 67 }
62 } 68 }
63} 69}
@@ -275,6 +281,16 @@ impl<'d, T: Instance, M: PeriMode> Spi<'d, T, M> {
275 BitOrder::MsbFirst 281 BitOrder::MsbFirst
276 }; 282 };
277 283
284 let miso_pullup = match &self.miso {
285 None => false,
286 Some(pin) =>
287 if pin.pull() == Pull::Up {
288 true
289 } else {
290 false
291 }
292 };
293
278 #[cfg(any(spi_v1, spi_f1, spi_v2))] 294 #[cfg(any(spi_v1, spi_f1, spi_v2))]
279 let br = cfg.br(); 295 let br = cfg.br();
280 #[cfg(any(spi_v3, spi_v4, spi_v5))] 296 #[cfg(any(spi_v3, spi_v4, spi_v5))]
@@ -287,6 +303,7 @@ impl<'d, T: Instance, M: PeriMode> Spi<'d, T, M> {
287 mode: Mode { polarity, phase }, 303 mode: Mode { polarity, phase },
288 bit_order, 304 bit_order,
289 frequency, 305 frequency,
306 miso_pullup,
290 } 307 }
291 } 308 }
292 309
@@ -409,7 +426,11 @@ impl<'d, T: Instance> Spi<'d, T, Blocking> {
409 peri, 426 peri,
410 new_pin!(sck, AFType::OutputPushPull, Speed::VeryHigh, config.sck_pull_mode()), 427 new_pin!(sck, AFType::OutputPushPull, Speed::VeryHigh, config.sck_pull_mode()),
411 new_pin!(mosi, AFType::OutputPushPull, Speed::VeryHigh), 428 new_pin!(mosi, AFType::OutputPushPull, Speed::VeryHigh),
412 new_pin!(miso, AFType::Input, Speed::VeryHigh), 429 new_pin!(miso, AFType::Input, Speed::Input,
430 match config.miso_pullup {
431 true => Pull::Up,
432 false => Pull::None,
433 }),
413 None, 434 None,
414 None, 435 None,
415 config, 436 config,
@@ -427,7 +448,11 @@ impl<'d, T: Instance> Spi<'d, T, Blocking> {
427 peri, 448 peri,
428 new_pin!(sck, AFType::OutputPushPull, Speed::VeryHigh, config.sck_pull_mode()), 449 new_pin!(sck, AFType::OutputPushPull, Speed::VeryHigh, config.sck_pull_mode()),
429 None, 450 None,
430 new_pin!(miso, AFType::Input, Speed::VeryHigh), 451 new_pin!(miso, AFType::Input, Speed::Input,
452 match config.miso_pullup {
453 true => Pull::Up,
454 false => Pull::None,
455 }),
431 None, 456 None,
432 None, 457 None,
433 config, 458 config,