Skip to content

Commit

Permalink
Add non-existent pins
Browse files Browse the repository at this point in the history
  • Loading branch information
david-sawatzke committed Jan 11, 2019
1 parent ac4c91b commit 9a325cc
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Support for stm32f0x1 line - @jessebraham
- Support for HSE as a system clocksource (#25 - breaking change) - @zklapow
- Added non-existent pins for SPI & I2C

### Changed

Expand Down
36 changes: 36 additions & 0 deletions examples/unused.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! This is not intended to be used on a real system
#![no_main]
#![no_std]

#[allow(unused)]
use panic_halt;

use stm32f0xx_hal as hal;

use crate::hal::i2c::*;
use crate::hal::prelude::*;
use crate::hal::spi::*;
use crate::hal::stm32;

use cortex_m_rt::entry;
#[entry]
fn main() -> ! {
const MODE: Mode = Mode {
polarity: Polarity::IdleHigh,
phase: Phase::CaptureOnSecondTransition,
};

if let Some(p) = stm32::Peripherals::take() {
let rcc = p.RCC.constrain();
let clocks = rcc.cfgr.freeze();

let pins = unsafe { (NoSck::new(), NoMiso::new(), NoMosi::new()) };
let _ = Spi::spi1(p.SPI1, pins, MODE, 100_000.hz(), clocks);

let pins = unsafe { (NoScl::new(), NoSda::new()) };
let _ = I2c::i2c1(p.I2C1, pins, 400.khz());
}
loop {
continue;
}
}
34 changes: 34 additions & 0 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,44 @@ pub enum Error {
NACK,
}

/// Filler for a SDA Pin
///
/// Usefull if you don't want to utilize the sda pin,
/// but a pin parameter is required
pub struct NoSda {
_1: (),
}

impl NoSda {
/// Create a new unused pin
pub unsafe fn new() -> Self {
Self { _1: () }
}
}

/// Filler for a SCL Pin
///
/// Usefull if you don't want to utilize the scl pin,
/// but a pin parameter is required
pub struct NoScl {
_1: (),
}

impl NoScl {
/// Create a new unused pin
pub unsafe fn new() -> Self {
Self { _1: () }
}
}

macro_rules! i2c {
($($I2C:ident: ($i2c:ident, $i2cXen:ident, $i2cXrst:ident, $apbenr:ident, $apbrstr:ident),)+) => {
$(
use crate::stm32::$I2C;

impl SclPin<$I2C> for NoScl {}
impl SdaPin<$I2C> for NoSda {}

impl<SCLPIN, SDAPIN> I2c<$I2C, SCLPIN, SDAPIN> {
pub fn $i2c(i2c: $I2C, pins: (SCLPIN, SDAPIN), speed: KiloHertz, rcc: &mut Rcc) -> Self
where
Expand Down
49 changes: 49 additions & 0 deletions src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,58 @@ spi_pins! {
}
}

/// Filler for a SCK pin
///
/// Usefull if you don't want to utilize the sck pin,
/// but a pin parameter is required
pub struct NoSck {
_1: (),
}

impl NoSck {
/// Create a new filler sck pin
pub unsafe fn new() -> Self {
Self { _1: () }
}
}

/// Filler for a MISO Pin
///
/// Usefull if you don't want to utilize the miso pin,
/// but a pin parameter is required
pub struct NoMiso {
_1: (),
}

impl NoMiso {
/// Create a new filler mosi pin
pub unsafe fn new() -> Self {
Self { _1: () }
}
}

/// Filler for a MOSI Pin
///
/// Usefull if you don't want to utilize the miso pin,
/// but a pin parameter is required
pub struct NoMosi {
_1: (),
}

impl NoMosi {
/// Create a new filler mosi pin
pub unsafe fn new() -> Self {
Self { _1: () }
}
}

macro_rules! spi {
($($SPI:ident: ($spi:ident, $spiXen:ident, $spiXrst:ident, $apbenr:ident, $apbrstr:ident),)+) => {
$(
impl SckPin<$SPI> for NoSck {}
impl MisoPin<$SPI> for NoMiso {}
impl MosiPin<$SPI> for NoMosi {}

impl<SCKPIN, MISOPIN, MOSIPIN> Spi<$SPI, SCKPIN, MISOPIN, MOSIPIN> {
/// Creates a new spi instance
pub fn $spi<F>(
Expand Down

0 comments on commit 9a325cc

Please sign in to comment.