diff --git a/CHANGELOG.md b/CHANGELOG.md index dec4485..f5e24a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,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 - Add ability to use a Tx/Rx only serial instance - @david-sawatzke +- Added non-existent pins for SPI & I2C (#27) - @david-sawatzke ### Changed diff --git a/examples/unused.rs b/examples/unused.rs new file mode 100644 index 0000000..8c391b7 --- /dev/null +++ b/examples/unused.rs @@ -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; + } +} diff --git a/src/i2c.rs b/src/i2c.rs index 60c738e..3f80562 100644 --- a/src/i2c.rs +++ b/src/i2c.rs @@ -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 I2c<$I2C, SCLPIN, SDAPIN> { pub fn $i2c(i2c: $I2C, pins: (SCLPIN, SDAPIN), speed: KiloHertz, rcc: &mut Rcc) -> Self where diff --git a/src/spi.rs b/src/spi.rs index 7ed5c1b..9fd2fe2 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -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 Spi<$SPI, SCKPIN, MISOPIN, MOSIPIN> { /// Creates a new spi instance pub fn $spi(