Move pin configuration to gpio mod
This commit is contained in:
parent
ee869efcb5
commit
8bb1bc3507
2 changed files with 27 additions and 21 deletions
|
@ -184,6 +184,25 @@ pub(crate) mod sealed {
|
||||||
self.block().bsrr().write(|w| w.set_br(n, true));
|
self.block().bsrr().write(|w| w.set_br(n, true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsafe fn set_as_af(&self, af_num: u8) {
|
||||||
|
let pin = self._pin() as usize;
|
||||||
|
let block = self.block();
|
||||||
|
block
|
||||||
|
.moder()
|
||||||
|
.modify(|w| w.set_moder(pin, vals::Moder::ALTERNATE));
|
||||||
|
block
|
||||||
|
.afr(pin / 8)
|
||||||
|
.modify(|w| w.set_afr(pin % 8, vals::Afr(af_num)));
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn set_as_analog(&self) {
|
||||||
|
let pin = self._pin() as usize;
|
||||||
|
let block = self.block();
|
||||||
|
block
|
||||||
|
.moder()
|
||||||
|
.modify(|w| w.set_moder(pin, vals::Moder::ANALOG));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait OptionalPin {}
|
pub trait OptionalPin {}
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
#![macro_use]
|
#![macro_use]
|
||||||
|
|
||||||
use crate::gpio::{AnyPin, Pin};
|
use crate::gpio::{sealed::Pin, AnyPin};
|
||||||
use crate::pac::gpio::vals::{Afr, Moder};
|
|
||||||
use crate::pac::gpio::Gpio;
|
|
||||||
use crate::pac::spi;
|
use crate::pac::spi;
|
||||||
use crate::spi::{ByteOrder, Config, Error, Instance, MisoPin, MosiPin, SckPin, WordSize};
|
use crate::spi::{ByteOrder, Config, Error, Instance, MisoPin, MosiPin, SckPin, WordSize};
|
||||||
use crate::time::Hertz;
|
use crate::time::Hertz;
|
||||||
|
@ -42,13 +40,12 @@ impl<'d, T: Instance> Spi<'d, T> {
|
||||||
where
|
where
|
||||||
F: Into<Hertz>,
|
F: Into<Hertz>,
|
||||||
{
|
{
|
||||||
unborrow!(peri);
|
unborrow!(peri, sck, mosi, miso);
|
||||||
unborrow!(sck, mosi, miso);
|
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
Self::configure_pin(sck.block(), sck.pin() as _, sck.af());
|
sck.set_as_af(sck.af());
|
||||||
Self::configure_pin(mosi.block(), mosi.pin() as _, mosi.af());
|
mosi.set_as_af(mosi.af());
|
||||||
Self::configure_pin(miso.block(), miso.pin() as _, miso.af());
|
miso.set_as_af(miso.af());
|
||||||
}
|
}
|
||||||
|
|
||||||
let sck = sck.degrade();
|
let sck = sck.degrade();
|
||||||
|
@ -101,16 +98,6 @@ impl<'d, T: Instance> Spi<'d, T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn configure_pin(block: Gpio, pin: usize, af_num: u8) {
|
|
||||||
let (afr, n_af) = if pin < 8 { (0, pin) } else { (1, pin - 8) };
|
|
||||||
block.moder().modify(|w| w.set_moder(pin, Moder::ALTERNATE));
|
|
||||||
block.afr(afr).modify(|w| w.set_afr(n_af, Afr(af_num)));
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe fn unconfigure_pin(block: Gpio, pin: usize) {
|
|
||||||
block.moder().modify(|w| w.set_moder(pin, Moder::ANALOG));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn compute_baud_rate(clocks: Hertz, freq: Hertz) -> u8 {
|
fn compute_baud_rate(clocks: Hertz, freq: Hertz) -> u8 {
|
||||||
match clocks.0 / freq.0 {
|
match clocks.0 / freq.0 {
|
||||||
0 => unreachable!(),
|
0 => unreachable!(),
|
||||||
|
@ -145,9 +132,9 @@ impl<'d, T: Instance> Spi<'d, T> {
|
||||||
impl<'d, T: Instance> Drop for Spi<'d, T> {
|
impl<'d, T: Instance> Drop for Spi<'d, T> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
unsafe {
|
unsafe {
|
||||||
Self::unconfigure_pin(self.sck.block(), self.sck.pin() as _);
|
self.sck.set_as_analog();
|
||||||
Self::unconfigure_pin(self.mosi.block(), self.mosi.pin() as _);
|
self.mosi.set_as_analog();
|
||||||
Self::unconfigure_pin(self.miso.block(), self.miso.pin() as _);
|
self.miso.set_as_analog();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue