Skip to content

Commit

Permalink
alpha.7
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Feb 12, 2022
1 parent 119ed38 commit e6cee3b
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 290 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Pwm channels now constants [#432]
- Add channel events, make Event use bitflags (simplify interrupt handling) [#425]
- reexport `digital::v2::PinState` again [#428]
- reexport `digital::v2::PinState` again
- Timer impls with time based on `fugit` moved to `fugit` module, added `Pwm` and `fugit-timer` impls [#423]

### Fixed
Expand All @@ -29,6 +28,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

- Missing `DelayMs<u8>` / `DelayUs<u8>` impls for fugit::Delay
- Support of embedded-hal 1.0.0-alpha.7
- Aliases for peripheral wrappers [#434]
- `WithPwm` trait implemented for timers with channels (internals) [#425]
- `Pwm` struct with `split` method and implementation of embedded-hal::Pwm (similar to f1xx-hal) [#425]
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ embedded-dma = "0.2.0"
bare-metal = { version = "1" }
cast = { default-features = false, version = "0.3.0" }
void = { default-features = false, version = "1.0.2" }
embedded-hal = { features = ["unproven"], version = "0.2.6" }
embedded-hal = { features = ["unproven"], version = "0.2.7" }
display-interface = { version = "0.4.1", optional = true }
fugit = "0.3.3"
fugit-timer = "0.1.3"
Expand All @@ -52,7 +52,7 @@ version = "0.3"
default-features = false

[dependencies.embedded-hal-one]
version = "=1.0.0-alpha.6"
version = "1.0.0-alpha.7"
package = "embedded-hal"

[dependencies.stm32_i2s_v12x]
Expand Down
181 changes: 50 additions & 131 deletions src/fmpi2c/hal_1.rs
Original file line number Diff line number Diff line change
@@ -1,148 +1,67 @@
use embedded_hal_one::i2c::ErrorType;

impl<I2C, PINS> ErrorType for super::FMPI2c<I2C, PINS> {
type Error = super::Error;
}

mod blocking {
use super::super::{fmpi2c1, Error, FMPI2c};
use super::super::{fmpi2c1, FMPI2c};
use core::ops::Deref;
use embedded_hal_one::i2c::blocking::{Read, Write, WriteRead};
use embedded_hal_one::i2c::blocking::Operation;

impl<I2C, PINS> WriteRead for FMPI2c<I2C, PINS>
impl<I2C, PINS> embedded_hal_one::i2c::blocking::I2c for FMPI2c<I2C, PINS>
where
I2C: Deref<Target = fmpi2c1::RegisterBlock>,
{
type Error = Error;

fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Error> {
// Set up current slave address for writing and disable autoending
self.i2c.cr2.modify(|_, w| {
w.sadd()
.bits(u16::from(addr) << 1)
.nbytes()
.bits(bytes.len() as u8)
.rd_wrn()
.clear_bit()
.autoend()
.clear_bit()
});

// Send a START condition
self.i2c.cr2.modify(|_, w| w.start().set_bit());

// Wait until the transmit buffer is empty and there hasn't been any error condition
while {
let isr = self.i2c.isr.read();
self.check_and_clear_error_flags(&isr)
.map_err(Error::nack_addr)?;
isr.txis().bit_is_clear() && isr.tc().bit_is_clear()
} {}

// Send out all individual bytes
for c in bytes {
self.send_byte(*c)?;
}

// Wait until data was sent
while {
let isr = self.i2c.isr.read();
self.check_and_clear_error_flags(&isr)
.map_err(Error::nack_data)?;
isr.tc().bit_is_clear()
} {}

// Set up current address for reading
self.i2c.cr2.modify(|_, w| {
w.sadd()
.bits(u16::from(addr) << 1)
.nbytes()
.bits(buffer.len() as u8)
.rd_wrn()
.set_bit()
});

// Send another START condition
self.i2c.cr2.modify(|_, w| w.start().set_bit());

// Send the autoend after setting the start to get a restart
self.i2c.cr2.modify(|_, w| w.autoend().set_bit());

// Now read in all bytes
for c in buffer.iter_mut() {
*c = self.recv_byte()?;
}

// Check and clear flags if they somehow ended up set
self.check_and_clear_error_flags(&self.i2c.isr.read())
.map_err(Error::nack_data)?;

Ok(())
fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
self.read(addr, buffer)
}
}

impl<I2C, PINS> Read for FMPI2c<I2C, PINS>
where
I2C: Deref<Target = fmpi2c1::RegisterBlock>,
{
type Error = Error;

fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Error> {
// Set up current address for reading
self.i2c.cr2.modify(|_, w| {
w.sadd()
.bits(u16::from(addr) << 1)
.nbytes()
.bits(buffer.len() as u8)
.rd_wrn()
.set_bit()
});

// Send a START condition
self.i2c.cr2.modify(|_, w| w.start().set_bit());

// Send the autoend after setting the start to get a restart
self.i2c.cr2.modify(|_, w| w.autoend().set_bit());

// Now read in all bytes
for c in buffer.iter_mut() {
*c = self.recv_byte()?;
}

// Check and clear flags if they somehow ended up set
self.check_and_clear_error_flags(&self.i2c.isr.read())
.map_err(Error::nack_data)?;

Ok(())
fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error> {
self.write(addr, bytes)
}
}

impl<I2C, PINS> Write for FMPI2c<I2C, PINS>
where
I2C: Deref<Target = fmpi2c1::RegisterBlock>,
{
type Error = Error;

fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error> {
// Set up current slave address for writing and enable autoending
self.i2c.cr2.modify(|_, w| {
w.sadd()
.bits(u16::from(addr) << 1)
.nbytes()
.bits(bytes.len() as u8)
.rd_wrn()
.clear_bit()
.autoend()
.set_bit()
});
fn write_iter<B>(&mut self, _addr: u8, _bytes: B) -> Result<(), Self::Error>
where
B: IntoIterator<Item = u8>,
{
todo!()
}

// Send a START condition
self.i2c.cr2.modify(|_, w| w.start().set_bit());
fn write_read(
&mut self,
addr: u8,
bytes: &[u8],
buffer: &mut [u8],
) -> Result<(), Self::Error> {
self.write_read(addr, bytes, buffer)
}

// Send out all individual bytes
for c in bytes {
self.send_byte(*c)?;
}
fn write_iter_read<B>(
&mut self,
_addr: u8,
_bytes: B,
_buffer: &mut [u8],
) -> Result<(), Self::Error>
where
B: IntoIterator<Item = u8>,
{
todo!()
}

// Check and clear flags if they somehow ended up set
self.check_and_clear_error_flags(&self.i2c.isr.read())
.map_err(Error::nack_data)?;
fn transaction<'a>(
&mut self,
_addr: u8,
_operations: &mut [Operation<'a>],
) -> Result<(), Self::Error> {
todo!()
}

Ok(())
fn transaction_iter<'a, O>(&mut self, _addr: u8, _operations: O) -> Result<(), Self::Error>
where
O: IntoIterator<Item = Operation<'a>>,
{
todo!()
}
}
}
17 changes: 17 additions & 0 deletions src/fugit/hal_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use super::{Delay, Error, Instance};

use embedded_hal_one::delay::blocking::DelayUs;

use fugit::ExtU32;

impl<TIM: Instance, const FREQ: u32> DelayUs for Delay<TIM, FREQ> {
type Error = Error;

fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> {
self.delay(us.micros())
}

fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> {
self.delay(ms.millis())
}
}
1 change: 1 addition & 0 deletions src/fugit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub mod pwm;
pub use pwm::*;

mod hal_02;
mod hal_1;

/// Timer wrapper
pub struct Timer<TIM, const FREQ: u32> {
Expand Down
40 changes: 13 additions & 27 deletions src/gpio/hal_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use super::{
PushPull,
};

use embedded_hal_one::digital::blocking::{
InputPin, IoPin, OutputPin, StatefulOutputPin, ToggleableOutputPin,
};
pub use embedded_hal_one::digital::PinState;
use embedded_hal_one::digital::{
blocking::{InputPin, IoPin, OutputPin, StatefulOutputPin, ToggleableOutputPin},
ErrorType,
};

fn into_state(state: PinState) -> super::PinState {
match state {
Expand All @@ -18,10 +19,11 @@ fn into_state(state: PinState) -> super::PinState {
}

// Implementations for `Pin`

impl<MODE, const P: char, const N: u8> OutputPin for Pin<Output<MODE>, P, N> {
impl<MODE, const P: char, const N: u8> ErrorType for Pin<MODE, P, N> {
type Error = Infallible;
}

impl<MODE, const P: char, const N: u8> OutputPin for Pin<Output<MODE>, P, N> {
#[inline(always)]
fn set_high(&mut self) -> Result<(), Self::Error> {
self.set_high();
Expand All @@ -48,8 +50,6 @@ impl<MODE, const P: char, const N: u8> StatefulOutputPin for Pin<Output<MODE>, P
}

impl<MODE, const P: char, const N: u8> ToggleableOutputPin for Pin<Output<MODE>, P, N> {
type Error = Infallible;

#[inline(always)]
fn toggle(&mut self) -> Result<(), Self::Error> {
self.toggle();
Expand All @@ -58,8 +58,6 @@ impl<MODE, const P: char, const N: u8> ToggleableOutputPin for Pin<Output<MODE>,
}

impl<const P: char, const N: u8> InputPin for Pin<Output<OpenDrain>, P, N> {
type Error = Infallible;

#[inline(always)]
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_high())
Expand All @@ -72,8 +70,6 @@ impl<const P: char, const N: u8> InputPin for Pin<Output<OpenDrain>, P, N> {
}

impl<MODE, const P: char, const N: u8> InputPin for Pin<Input<MODE>, P, N> {
type Error = Infallible;

#[inline(always)]
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_high())
Expand Down Expand Up @@ -197,10 +193,11 @@ impl<const P: char, const N: u8> IoPin<Self, Pin<Output<PushPull>, P, N>>
}

// Implementations for `ErasedPin`

impl<MODE> OutputPin for ErasedPin<Output<MODE>> {
impl<MODE> ErrorType for ErasedPin<MODE> {
type Error = core::convert::Infallible;
}

impl<MODE> OutputPin for ErasedPin<Output<MODE>> {
#[inline(always)]
fn set_high(&mut self) -> Result<(), Self::Error> {
self.set_high();
Expand All @@ -227,8 +224,6 @@ impl<MODE> StatefulOutputPin for ErasedPin<Output<MODE>> {
}

impl<MODE> ToggleableOutputPin for ErasedPin<Output<MODE>> {
type Error = Infallible;

#[inline(always)]
fn toggle(&mut self) -> Result<(), Self::Error> {
self.toggle();
Expand All @@ -237,8 +232,6 @@ impl<MODE> ToggleableOutputPin for ErasedPin<Output<MODE>> {
}

impl InputPin for ErasedPin<Output<OpenDrain>> {
type Error = core::convert::Infallible;

#[inline(always)]
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_high())
Expand All @@ -251,8 +244,6 @@ impl InputPin for ErasedPin<Output<OpenDrain>> {
}

impl<MODE> InputPin for ErasedPin<Input<MODE>> {
type Error = core::convert::Infallible;

#[inline(always)]
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_high())
Expand All @@ -265,10 +256,11 @@ impl<MODE> InputPin for ErasedPin<Input<MODE>> {
}

// Implementations for `PartiallyErasedPin`

impl<MODE, const P: char> OutputPin for PartiallyErasedPin<Output<MODE>, P> {
impl<MODE, const P: char> ErrorType for PartiallyErasedPin<MODE, P> {
type Error = Infallible;
}

impl<MODE, const P: char> OutputPin for PartiallyErasedPin<Output<MODE>, P> {
#[inline(always)]
fn set_high(&mut self) -> Result<(), Self::Error> {
self.set_high();
Expand All @@ -295,8 +287,6 @@ impl<MODE, const P: char> StatefulOutputPin for PartiallyErasedPin<Output<MODE>,
}

impl<MODE, const P: char> ToggleableOutputPin for PartiallyErasedPin<Output<MODE>, P> {
type Error = Infallible;

#[inline(always)]
fn toggle(&mut self) -> Result<(), Self::Error> {
self.toggle();
Expand All @@ -305,8 +295,6 @@ impl<MODE, const P: char> ToggleableOutputPin for PartiallyErasedPin<Output<MODE
}

impl<const P: char> InputPin for PartiallyErasedPin<Output<OpenDrain>, P> {
type Error = Infallible;

#[inline(always)]
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_high())
Expand All @@ -319,8 +307,6 @@ impl<const P: char> InputPin for PartiallyErasedPin<Output<OpenDrain>, P> {
}

impl<MODE, const P: char> InputPin for PartiallyErasedPin<Input<MODE>, P> {
type Error = Infallible;

#[inline(always)]
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_high())
Expand Down
Loading

0 comments on commit e6cee3b

Please sign in to comment.