diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-09-24 20:20:45 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-09-24 20:20:45 +0200 |
| commit | f8d833e0c507112dcf52f621ae9730c9cacf9476 (patch) | |
| tree | 7843499303ae01686d6c4b4fc400cb091efab8f4 | |
| parent | c8394ea7b9bd3ccdc7df60210d430c99e6054b12 (diff) | |
| parent | e4b37c40c9216e805daf6de13435fb09aa876004 (diff) | |
Merge pull request #403 from mryndzionek/af_type
Small adjustment to 'set_as_af' interface
| -rw-r--r-- | embassy-stm32/src/can/bxcan.rs | 9 | ||||
| -rw-r--r-- | embassy-stm32/src/eth/v2/mod.rs | 4 | ||||
| -rw-r--r-- | embassy-stm32/src/gpio.rs | 21 | ||||
| -rw-r--r-- | embassy-stm32/src/i2c/v1.rs | 14 | ||||
| -rw-r--r-- | embassy-stm32/src/spi/v1.rs | 14 | ||||
| -rw-r--r-- | embassy-stm32/src/usart/v1.rs | 5 | ||||
| -rw-r--r-- | embassy-stm32/src/usart/v2.rs | 5 |
7 files changed, 47 insertions, 25 deletions
diff --git a/embassy-stm32/src/can/bxcan.rs b/embassy-stm32/src/can/bxcan.rs index 906978e8b..9e10a8b02 100644 --- a/embassy-stm32/src/can/bxcan.rs +++ b/embassy-stm32/src/can/bxcan.rs | |||
| @@ -4,7 +4,10 @@ use core::ops::{Deref, DerefMut}; | |||
| 4 | use embassy::util::Unborrow; | 4 | use embassy::util::Unborrow; |
| 5 | use embassy_hal_common::unborrow; | 5 | use embassy_hal_common::unborrow; |
| 6 | 6 | ||
| 7 | use crate::gpio::Pin; | 7 | use crate::gpio::{ |
| 8 | sealed::OutputType::{OpenDrain, PushPull}, | ||
| 9 | Pin, | ||
| 10 | }; | ||
| 8 | use crate::{peripherals, rcc::RccPeripheral}; | 11 | use crate::{peripherals, rcc::RccPeripheral}; |
| 9 | 12 | ||
| 10 | pub use bxcan::*; | 13 | pub use bxcan::*; |
| @@ -23,8 +26,8 @@ impl<'d, T: Instance + bxcan::Instance> Can<'d, T> { | |||
| 23 | unborrow!(peri, rx, tx); | 26 | unborrow!(peri, rx, tx); |
| 24 | 27 | ||
| 25 | unsafe { | 28 | unsafe { |
| 26 | rx.set_as_af(rx.af_num()); | 29 | rx.set_as_af(rx.af_num(), OpenDrain); |
| 27 | tx.set_as_af(tx.af_num()); | 30 | tx.set_as_af(tx.af_num(), PushPull); |
| 28 | } | 31 | } |
| 29 | 32 | ||
| 30 | T::enable(); | 33 | T::enable(); |
diff --git a/embassy-stm32/src/eth/v2/mod.rs b/embassy-stm32/src/eth/v2/mod.rs index ff734f78c..8eb7c3393 100644 --- a/embassy-stm32/src/eth/v2/mod.rs +++ b/embassy-stm32/src/eth/v2/mod.rs | |||
| @@ -9,8 +9,8 @@ use embassy_hal_common::unborrow; | |||
| 9 | use embassy_net::{Device, DeviceCapabilities, LinkState, PacketBuf, MTU}; | 9 | use embassy_net::{Device, DeviceCapabilities, LinkState, PacketBuf, MTU}; |
| 10 | 10 | ||
| 11 | use crate::gpio::sealed::Pin as __GpioPin; | 11 | use crate::gpio::sealed::Pin as __GpioPin; |
| 12 | use crate::gpio::AnyPin; | ||
| 13 | use crate::gpio::Pin as GpioPin; | 12 | use crate::gpio::Pin as GpioPin; |
| 13 | use crate::gpio::{sealed::OutputType::PushPull, AnyPin}; | ||
| 14 | use crate::pac::gpio::vals::Ospeedr; | 14 | use crate::pac::gpio::vals::Ospeedr; |
| 15 | use crate::pac::{ETH, RCC, SYSCFG}; | 15 | use crate::pac::{ETH, RCC, SYSCFG}; |
| 16 | use crate::peripherals; | 16 | use crate::peripherals; |
| @@ -416,7 +416,7 @@ macro_rules! impl_pin { | |||
| 416 | fn configure(&mut self) { | 416 | fn configure(&mut self) { |
| 417 | // NOTE(unsafe) Exclusive access to the registers | 417 | // NOTE(unsafe) Exclusive access to the registers |
| 418 | critical_section::with(|_| unsafe { | 418 | critical_section::with(|_| unsafe { |
| 419 | self.set_as_af($af); | 419 | self.set_as_af($af, PushPull); |
| 420 | self.block() | 420 | self.block() |
| 421 | .ospeedr() | 421 | .ospeedr() |
| 422 | .modify(|w| w.set_ospeedr(self.pin() as usize, Ospeedr::VERYHIGHSPEED)); | 422 | .modify(|w| w.set_ospeedr(self.pin() as usize, Ospeedr::VERYHIGHSPEED)); |
diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs index 7812709ce..14f2559e1 100644 --- a/embassy-stm32/src/gpio.rs +++ b/embassy-stm32/src/gpio.rs | |||
| @@ -264,6 +264,14 @@ impl<'d, T: Pin> InputPin for OutputOpenDrain<'d, T> { | |||
| 264 | pub(crate) mod sealed { | 264 | pub(crate) mod sealed { |
| 265 | use super::*; | 265 | use super::*; |
| 266 | 266 | ||
| 267 | /// Output type settings | ||
| 268 | #[derive(Debug)] | ||
| 269 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 270 | pub enum OutputType { | ||
| 271 | PushPull, | ||
| 272 | OpenDrain, | ||
| 273 | } | ||
| 274 | |||
| 267 | pub trait Pin { | 275 | pub trait Pin { |
| 268 | fn pin_port(&self) -> u8; | 276 | fn pin_port(&self) -> u8; |
| 269 | 277 | ||
| @@ -299,7 +307,7 @@ pub(crate) mod sealed { | |||
| 299 | } | 307 | } |
| 300 | } | 308 | } |
| 301 | 309 | ||
| 302 | unsafe fn set_as_af(&self, af_num: u8) { | 310 | unsafe fn set_as_af(&self, af_num: u8, af_type: OutputType) { |
| 303 | let pin = self._pin() as usize; | 311 | let pin = self._pin() as usize; |
| 304 | let block = self.block(); | 312 | let block = self.block(); |
| 305 | block | 313 | block |
| @@ -308,6 +316,17 @@ pub(crate) mod sealed { | |||
| 308 | block | 316 | block |
| 309 | .afr(pin / 8) | 317 | .afr(pin / 8) |
| 310 | .modify(|w| w.set_afr(pin % 8, vals::Afr(af_num))); | 318 | .modify(|w| w.set_afr(pin % 8, vals::Afr(af_num))); |
| 319 | match af_type { | ||
| 320 | OutputType::PushPull => { | ||
| 321 | block.otyper().modify(|w| w.set_ot(pin, vals::Ot::PUSHPULL)) | ||
| 322 | } | ||
| 323 | OutputType::OpenDrain => block | ||
| 324 | .otyper() | ||
| 325 | .modify(|w| w.set_ot(pin, vals::Ot::OPENDRAIN)), | ||
| 326 | } | ||
| 327 | block | ||
| 328 | .pupdr() | ||
| 329 | .modify(|w| w.set_pupdr(pin, vals::Pupdr::FLOATING)); | ||
| 311 | } | 330 | } |
| 312 | 331 | ||
| 313 | unsafe fn set_as_analog(&self) { | 332 | unsafe fn set_as_analog(&self) { |
diff --git a/embassy-stm32/src/i2c/v1.rs b/embassy-stm32/src/i2c/v1.rs index 578536855..c5d5dee0c 100644 --- a/embassy-stm32/src/i2c/v1.rs +++ b/embassy-stm32/src/i2c/v1.rs | |||
| @@ -9,8 +9,7 @@ use embedded_hal::blocking::i2c::WriteRead; | |||
| 9 | 9 | ||
| 10 | use crate::pac::i2c; | 10 | use crate::pac::i2c; |
| 11 | 11 | ||
| 12 | use crate::pac::gpio::vals::{Afr, Moder, Ot}; | 12 | use crate::gpio::sealed::OutputType::OpenDrain; |
| 13 | use crate::pac::gpio::Gpio; | ||
| 14 | 13 | ||
| 15 | pub struct I2c<'d, T: Instance> { | 14 | pub struct I2c<'d, T: Instance> { |
| 16 | phantom: PhantomData<&'d mut T>, | 15 | phantom: PhantomData<&'d mut T>, |
| @@ -31,8 +30,8 @@ impl<'d, T: Instance> I2c<'d, T> { | |||
| 31 | T::enable(); | 30 | T::enable(); |
| 32 | 31 | ||
| 33 | unsafe { | 32 | unsafe { |
| 34 | Self::configure_pin(scl.block(), scl.pin() as _, scl.af_num()); | 33 | scl.set_as_af(scl.af_num(), OpenDrain); |
| 35 | Self::configure_pin(sda.block(), sda.pin() as _, sda.af_num()); | 34 | sda.set_as_af(sda.af_num(), OpenDrain); |
| 36 | } | 35 | } |
| 37 | 36 | ||
| 38 | unsafe { | 37 | unsafe { |
| @@ -69,13 +68,6 @@ impl<'d, T: Instance> I2c<'d, T> { | |||
| 69 | } | 68 | } |
| 70 | } | 69 | } |
| 71 | 70 | ||
| 72 | unsafe fn configure_pin(block: Gpio, pin: usize, af_num: u8) { | ||
| 73 | let (afr, n_af) = if pin < 8 { (0, pin) } else { (1, pin - 8) }; | ||
| 74 | block.moder().modify(|w| w.set_moder(pin, Moder::ALTERNATE)); | ||
| 75 | block.afr(afr).modify(|w| w.set_afr(n_af, Afr(af_num))); | ||
| 76 | block.otyper().modify(|w| w.set_ot(pin, Ot::OPENDRAIN)); | ||
| 77 | } | ||
| 78 | |||
| 79 | unsafe fn check_and_clear_error_flags(&self) -> Result<i2c::regs::Sr1, Error> { | 71 | unsafe fn check_and_clear_error_flags(&self) -> Result<i2c::regs::Sr1, Error> { |
| 80 | // Note that flags should only be cleared once they have been registered. If flags are | 72 | // Note that flags should only be cleared once they have been registered. If flags are |
| 81 | // cleared otherwise, there may be an inherent race condition and flags may be missed. | 73 | // cleared otherwise, there may be an inherent race condition and flags may be missed. |
diff --git a/embassy-stm32/src/spi/v1.rs b/embassy-stm32/src/spi/v1.rs index 302b7a2a1..53d8252e6 100644 --- a/embassy-stm32/src/spi/v1.rs +++ b/embassy-stm32/src/spi/v1.rs | |||
| @@ -1,7 +1,13 @@ | |||
| 1 | #![macro_use] | 1 | #![macro_use] |
| 2 | 2 | ||
| 3 | use crate::dma::NoDma; | 3 | use crate::dma::NoDma; |
| 4 | use crate::gpio::{sealed::Pin, AnyPin}; | 4 | use crate::gpio::{ |
| 5 | sealed::{ | ||
| 6 | OutputType::{OpenDrain, PushPull}, | ||
| 7 | Pin, | ||
| 8 | }, | ||
| 9 | AnyPin, | ||
| 10 | }; | ||
| 5 | use crate::pac::spi; | 11 | use crate::pac::spi; |
| 6 | use crate::spi::{ | 12 | use crate::spi::{ |
| 7 | ByteOrder, Config, Error, Instance, MisoPin, MosiPin, RxDmaChannel, SckPin, TxDmaChannel, | 13 | ByteOrder, Config, Error, Instance, MisoPin, MosiPin, RxDmaChannel, SckPin, TxDmaChannel, |
| @@ -53,9 +59,9 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { | |||
| 53 | unborrow!(sck, mosi, miso, txdma, rxdma); | 59 | unborrow!(sck, mosi, miso, txdma, rxdma); |
| 54 | 60 | ||
| 55 | unsafe { | 61 | unsafe { |
| 56 | sck.set_as_af(sck.af_num()); | 62 | sck.set_as_af(sck.af_num(), PushPull); |
| 57 | mosi.set_as_af(mosi.af_num()); | 63 | mosi.set_as_af(mosi.af_num(), PushPull); |
| 58 | miso.set_as_af(miso.af_num()); | 64 | miso.set_as_af(miso.af_num(), OpenDrain); |
| 59 | } | 65 | } |
| 60 | 66 | ||
| 61 | let sck = sck.degrade(); | 67 | let sck = sck.degrade(); |
diff --git a/embassy-stm32/src/usart/v1.rs b/embassy-stm32/src/usart/v1.rs index ec6677699..d1dd68305 100644 --- a/embassy-stm32/src/usart/v1.rs +++ b/embassy-stm32/src/usart/v1.rs | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | use crate::gpio::sealed::OutputType::{OpenDrain, PushPull}; | ||
| 1 | use core::future::Future; | 2 | use core::future::Future; |
| 2 | use core::marker::PhantomData; | 3 | use core::marker::PhantomData; |
| 3 | use embassy::util::Unborrow; | 4 | use embassy::util::Unborrow; |
| @@ -36,8 +37,8 @@ impl<'d, T: Instance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> { | |||
| 36 | let r = inner.regs(); | 37 | let r = inner.regs(); |
| 37 | 38 | ||
| 38 | unsafe { | 39 | unsafe { |
| 39 | rx.set_as_af(rx.af_num()); | 40 | rx.set_as_af(rx.af_num(), OpenDrain); |
| 40 | tx.set_as_af(tx.af_num()); | 41 | tx.set_as_af(tx.af_num(), PushPull); |
| 41 | 42 | ||
| 42 | r.brr().write_value(regs::Brr(div)); | 43 | r.brr().write_value(regs::Brr(div)); |
| 43 | r.cr1().write(|w| { | 44 | r.cr1().write(|w| { |
diff --git a/embassy-stm32/src/usart/v2.rs b/embassy-stm32/src/usart/v2.rs index fc3036404..5e6696351 100644 --- a/embassy-stm32/src/usart/v2.rs +++ b/embassy-stm32/src/usart/v2.rs | |||
| @@ -13,6 +13,7 @@ use futures::TryFutureExt; | |||
| 13 | 13 | ||
| 14 | use super::*; | 14 | use super::*; |
| 15 | use crate::dma::NoDma; | 15 | use crate::dma::NoDma; |
| 16 | use crate::gpio::sealed::OutputType::{OpenDrain, PushPull}; | ||
| 16 | use crate::pac::usart::{regs, vals}; | 17 | use crate::pac::usart::{regs, vals}; |
| 17 | 18 | ||
| 18 | pub struct Uart<'d, T: Instance, TxDma = NoDma, RxDma = NoDma> { | 19 | pub struct Uart<'d, T: Instance, TxDma = NoDma, RxDma = NoDma> { |
| @@ -42,8 +43,8 @@ impl<'d, T: Instance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> { | |||
| 42 | let r = inner.regs(); | 43 | let r = inner.regs(); |
| 43 | 44 | ||
| 44 | unsafe { | 45 | unsafe { |
| 45 | rx.set_as_af(rx.af_num()); | 46 | rx.set_as_af(rx.af_num(), OpenDrain); |
| 46 | tx.set_as_af(tx.af_num()); | 47 | tx.set_as_af(tx.af_num(), PushPull); |
| 47 | 48 | ||
| 48 | r.cr2().write(|_w| {}); | 49 | r.cr2().write(|_w| {}); |
| 49 | r.cr3().write(|_w| {}); | 50 | r.cr3().write(|_w| {}); |
