Skip to content
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

Add an unused pin #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

- Support for stm32f0x8 line - @jessebraham
- Added non-existent pins for SPI & I2C (#27) - @david-sawatzke

### 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 mut flash = p.FLASH;
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash);

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

let pins = unsafe { (NoScl::new(), NoSda::new()) };
let _ = I2c::i2c1(p.I2C1, pins, 400.khz(), &mut rcc);
}
loop {
continue;
}
}
34 changes: 34 additions & 0 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,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 @@ -201,9 +201,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