diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-05-24 15:01:18 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-05-24 15:01:18 +0000 |
| commit | a5763b4df4f095108350408cf46fc13465ed5e7f (patch) | |
| tree | 4001c4a25b56db64e14dd9960848f60099217703 | |
| parent | 891ec5fa5da384cc7c8ffe48ef2df0bbfca769a3 (diff) | |
| parent | ac76a713e1dbf4b3b3de9b3017256a708b335452 (diff) | |
Merge pull request #2943 from joelsa/add-miso-pullup
Add miso pullup to spi configuration
| -rw-r--r-- | embassy-stm32/src/gpio.rs | 32 | ||||
| -rw-r--r-- | embassy-stm32/src/spi/mod.rs | 16 |
2 files changed, 46 insertions, 2 deletions
diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs index 81cb09b24..808a5bc82 100644 --- a/embassy-stm32/src/gpio.rs +++ b/embassy-stm32/src/gpio.rs | |||
| @@ -681,6 +681,38 @@ pub(crate) trait SealedPin { | |||
| 681 | #[cfg(gpio_v2)] | 681 | #[cfg(gpio_v2)] |
| 682 | self.block().ospeedr().modify(|w| w.set_ospeedr(pin, speed.into())); | 682 | self.block().ospeedr().modify(|w| w.set_ospeedr(pin, speed.into())); |
| 683 | } | 683 | } |
| 684 | |||
| 685 | /// Get the pull-up configuration. | ||
| 686 | #[inline] | ||
| 687 | fn pull(&self) -> Pull { | ||
| 688 | critical_section::with(|_| { | ||
| 689 | let r = self.block(); | ||
| 690 | let n = self._pin() as usize; | ||
| 691 | #[cfg(gpio_v1)] | ||
| 692 | { | ||
| 693 | let crlh = if n < 8 { 0 } else { 1 }; | ||
| 694 | match r.cr(crlh).read().mode(n % 8) { | ||
| 695 | vals::Mode::INPUT => match r.cr(crlh).read().cnf_in(n % 8) { | ||
| 696 | vals::CnfIn::PULL => match r.odr().read().odr(n) { | ||
| 697 | vals::Odr::LOW => Pull::Down, | ||
| 698 | vals::Odr::HIGH => Pull::Up, | ||
| 699 | }, | ||
| 700 | _ => Pull::None, | ||
| 701 | }, | ||
| 702 | _ => Pull::None, | ||
| 703 | } | ||
| 704 | } | ||
| 705 | #[cfg(gpio_v2)] | ||
| 706 | { | ||
| 707 | match r.pupdr().read().pupdr(n) { | ||
| 708 | vals::Pupdr::FLOATING => Pull::None, | ||
| 709 | vals::Pupdr::PULLDOWN => Pull::Down, | ||
| 710 | vals::Pupdr::PULLUP => Pull::Up, | ||
| 711 | vals::Pupdr::_RESERVED_3 => Pull::None, | ||
| 712 | } | ||
| 713 | } | ||
| 714 | }) | ||
| 715 | } | ||
| 684 | } | 716 | } |
| 685 | 717 | ||
| 686 | /// GPIO pin trait. | 718 | /// GPIO pin trait. |
diff --git a/embassy-stm32/src/spi/mod.rs b/embassy-stm32/src/spi/mod.rs index 0875cfe41..5fc8691ac 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_pull: Pull, | ||
| 53 | } | 58 | } |
| 54 | 59 | ||
| 55 | impl Default for Config { | 60 | impl 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_pull: Pull::None, | ||
| 61 | } | 67 | } |
| 62 | } | 68 | } |
| 63 | } | 69 | } |
| @@ -273,6 +279,11 @@ impl<'d, M: PeriMode> Spi<'d, M> { | |||
| 273 | BitOrder::MsbFirst | 279 | BitOrder::MsbFirst |
| 274 | }; | 280 | }; |
| 275 | 281 | ||
| 282 | let miso_pull = match &self.miso { | ||
| 283 | None => Pull::None, | ||
| 284 | Some(pin) => pin.pull(), | ||
| 285 | }; | ||
| 286 | |||
| 276 | #[cfg(any(spi_v1, spi_f1, spi_v2))] | 287 | #[cfg(any(spi_v1, spi_f1, spi_v2))] |
| 277 | let br = cfg.br(); | 288 | let br = cfg.br(); |
| 278 | #[cfg(any(spi_v3, spi_v4, spi_v5))] | 289 | #[cfg(any(spi_v3, spi_v4, spi_v5))] |
| @@ -284,6 +295,7 @@ impl<'d, M: PeriMode> Spi<'d, M> { | |||
| 284 | mode: Mode { polarity, phase }, | 295 | mode: Mode { polarity, phase }, |
| 285 | bit_order, | 296 | bit_order, |
| 286 | frequency, | 297 | frequency, |
| 298 | miso_pull, | ||
| 287 | } | 299 | } |
| 288 | } | 300 | } |
| 289 | 301 | ||
| @@ -406,7 +418,7 @@ impl<'d> Spi<'d, Blocking> { | |||
| 406 | peri, | 418 | peri, |
| 407 | new_pin!(sck, AFType::OutputPushPull, Speed::VeryHigh, config.sck_pull_mode()), | 419 | new_pin!(sck, AFType::OutputPushPull, Speed::VeryHigh, config.sck_pull_mode()), |
| 408 | new_pin!(mosi, AFType::OutputPushPull, Speed::VeryHigh), | 420 | new_pin!(mosi, AFType::OutputPushPull, Speed::VeryHigh), |
| 409 | new_pin!(miso, AFType::Input, Speed::VeryHigh), | 421 | new_pin!(miso, AFType::Input, Speed::VeryHigh, config.miso_pull), |
| 410 | None, | 422 | None, |
| 411 | None, | 423 | None, |
| 412 | config, | 424 | config, |
| @@ -424,7 +436,7 @@ impl<'d> Spi<'d, Blocking> { | |||
| 424 | peri, | 436 | peri, |
| 425 | new_pin!(sck, AFType::OutputPushPull, Speed::VeryHigh, config.sck_pull_mode()), | 437 | new_pin!(sck, AFType::OutputPushPull, Speed::VeryHigh, config.sck_pull_mode()), |
| 426 | None, | 438 | None, |
| 427 | new_pin!(miso, AFType::Input, Speed::VeryHigh), | 439 | new_pin!(miso, AFType::Input, Speed::VeryHigh, config.miso_pull), |
| 428 | None, | 440 | None, |
| 429 | None, | 441 | None, |
| 430 | config, | 442 | config, |
