Skip to content

SCK, MISO, MOSI #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions examples/spi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#![deny(unsafe_code)]
#![no_std]
#![no_main]

use cortex_m_rt::entry;
use panic_halt as _;

use embedded_hal::spi::{Mode, Phase, Polarity};
pub const MODE: Mode = Mode {
phase: Phase::CaptureOnSecondTransition,
polarity: Polarity::IdleHigh,
};

use stm32f1xx_hal::{
gpio::gpioa::PA4,
gpio::{Output, PushPull},
pac::{Peripherals, SPI1},
prelude::*,
spi::{Pins, Spi, Spi1NoRemap},
};

fn setup() -> (
Spi<SPI1, Spi1NoRemap, impl Pins<Spi1NoRemap>, u8>,
PA4<Output<PushPull>>,
) {
let dp = Peripherals::take().unwrap();

let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();

let clocks = rcc.cfgr.freeze(&mut flash.acr);

let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
let mut gpioa = dp.GPIOA.split(&mut rcc.apb2);

// SPI1
let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl);
let miso = gpioa.pa6;
let mosi = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl);
let cs = gpioa.pa4.into_push_pull_output(&mut gpioa.crl);

let spi = Spi::spi1(
dp.SPI1,
(sck, miso, mosi),
&mut afio.mapr,
MODE,
1_u32.mhz(),
clocks,
&mut rcc.apb2,
);

(spi, cs)
}

#[entry]
fn main() -> ! {
let (_spi, _cs) = setup();

loop {}
}
54 changes: 18 additions & 36 deletions src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,37 +79,19 @@ mod sealed {
pub trait Sck<REMAP> {}
pub trait Miso<REMAP> {}
pub trait Mosi<REMAP> {}
pub struct _Sck;
pub struct _Miso;
pub struct _Mosi;
}
use sealed::{Miso, Mosi, Remap, Sck};
pub use sealed::Remap;
use sealed::{Miso, Mosi, Sck};

pub trait Pins<REMAP, P> {
type _Pos;
}
macro_rules! pins_impl {
( $( ( $($PINX:ident),+ ), ( $($TRAIT:ident),+ ), ( $($POS:ident),* ); )+ ) => {
$(
#[allow(unused_parens)]
impl<REMAP, $($PINX,)+> Pins<REMAP, ($(sealed::$POS),+)> for ($($PINX),+)
where
$($PINX: $TRAIT<REMAP>,)+
{
type _Pos = ($(sealed::$POS),+);
}
)+
};
}
pub trait Pins<REMAP> {}

pins_impl!(
(SCK, MISO, MOSI), (Sck, Miso, Mosi), (_Sck, _Miso, _Mosi);
(SCK, MOSI, MISO), (Sck, Mosi, Miso), (_Sck, _Mosi, _Miso);
(MOSI, SCK, MISO), (Mosi, Sck, Miso), (_Mosi, _Sck, _Miso);
(MOSI, MISO, SCK), (Mosi, Miso, Sck), (_Mosi, _Miso, _Sck);
(MISO, MOSI, SCK), (Miso, Mosi, Sck), (_Miso, _Mosi, _Sck);
(MISO, SCK, MOSI), (Miso, Sck, Mosi), (_Miso, _Sck, _Mosi);
);
impl<REMAP, SCK, MISO, MOSI> Pins<REMAP> for (SCK, MISO, MOSI)
where
SCK: Sck<REMAP>,
MISO: Miso<REMAP>,
MOSI: Mosi<REMAP>,
{
}

pub struct Spi<SPI, REMAP, PINS, FRAMESIZE> {
spi: SPI,
Expand Down Expand Up @@ -169,7 +151,7 @@ impl<REMAP, PINS> Spi<SPI1, REMAP, PINS, u8> {

You can also use `NoSck`, `NoMiso` or `NoMosi` if you don't want to use the pins
*/
pub fn spi1<F, POS>(
pub fn spi1<F>(
spi: SPI1,
pins: PINS,
mapr: &mut MAPR,
Expand All @@ -181,7 +163,7 @@ impl<REMAP, PINS> Spi<SPI1, REMAP, PINS, u8> {
where
F: Into<Hertz>,
REMAP: Remap<Periph = SPI1>,
PINS: Pins<REMAP, POS>,
PINS: Pins<REMAP>,
{
mapr.modify_mapr(|_, w| w.spi1_remap().bit(REMAP::REMAP));
Spi::<SPI1, _, _, u8>::_spi(spi, pins, mode, freq.into(), clocks, apb)
Expand All @@ -196,7 +178,7 @@ impl<REMAP, PINS> Spi<SPI2, REMAP, PINS, u8> {

You can also use `NoSck`, `NoMiso` or `NoMosi` if you don't want to use the pins
*/
pub fn spi2<F, POS>(
pub fn spi2<F>(
spi: SPI2,
pins: PINS,
mode: Mode,
Expand All @@ -207,7 +189,7 @@ impl<REMAP, PINS> Spi<SPI2, REMAP, PINS, u8> {
where
F: Into<Hertz>,
REMAP: Remap<Periph = SPI2>,
PINS: Pins<REMAP, POS>,
PINS: Pins<REMAP>,
{
Spi::<SPI2, _, _, u8>::_spi(spi, pins, mode, freq.into(), clocks, apb)
}
Expand All @@ -223,7 +205,7 @@ impl<REMAP, PINS> Spi<SPI3, REMAP, PINS, u8> {
You can also use `NoSck`, `NoMiso` or `NoMosi` if you don't want to use the pins
*/
#[cfg(not(feature = "connectivity"))]
pub fn spi3<F, POS>(
pub fn spi3<F>(
spi: SPI3,
pins: PINS,
mode: Mode,
Expand All @@ -234,7 +216,7 @@ impl<REMAP, PINS> Spi<SPI3, REMAP, PINS, u8> {
where
F: Into<Hertz>,
REMAP: Remap<Periph = SPI3>,
PINS: Pins<REMAP, POS>,
PINS: Pins<REMAP>,
{
Spi::<SPI3, _, _, u8>::_spi(spi, pins, mode, freq.into(), clocks, apb)
}
Expand All @@ -247,7 +229,7 @@ impl<REMAP, PINS> Spi<SPI3, REMAP, PINS, u8> {
You can also use `NoSck`, `NoMiso` or `NoMosi` if you don't want to use the pins
*/
#[cfg(feature = "connectivity")]
pub fn spi3<F, POS>(
pub fn spi3<F>(
spi: SPI3,
pins: PINS,
mapr: &mut MAPR,
Expand All @@ -259,7 +241,7 @@ impl<REMAP, PINS> Spi<SPI3, REMAP, PINS, u8> {
where
F: Into<Hertz>,
REMAP: Remap<Periph = SPI3>,
PINS: Pins<REMAP, POS>,
PINS: Pins<REMAP>,
{
mapr.modify_mapr(|_, w| w.spi3_remap().bit(REMAP::REMAP));
Spi::<SPI3, _, _, u8>::_spi(spi, pins, mode, freq.into(), clocks, apb)
Expand Down