Skip to content

Commit

Permalink
Implement initial state for output pins
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier-varez committed Mar 22, 2021
1 parent a202778 commit 531801d
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 2 deletions.
28 changes: 28 additions & 0 deletions avr-hal-generic/src/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
//! // Convert into output
//! let pd2: PD2<mode::Output> = pd2.into_output(&mut portd.ddr);
//!
//! // Convert into output with an initial state
//! let pd2: PD2<mode::Output> = pd2.into_output_with_state(&mut portd.ddr, State::High);
//!
//! // Convert into tri-state input and output.
//! let pd2: PD2<mode::TriState> = pd2.into_tri_state(&mut portd.ddr);
//! ```
Expand Down Expand Up @@ -172,6 +175,12 @@ pub mod mode {
}
}

/// State of an output pin.
pub enum State {
High,
Low,
}

/// Create a generic pin to be used for downgrading
#[macro_export]
macro_rules! impl_generic_pin {
Expand Down Expand Up @@ -434,6 +443,7 @@ macro_rules! impl_port {
use $crate::void::Void;
use $crate::hal::digital::v2 as digital;
use $crate::port::mode;
use $crate::port::State;

// We have to "use" the port-ext trait so we can implement it inside
// the macro.
Expand Down Expand Up @@ -477,6 +487,7 @@ macro_rules! impl_port {
///
/// - Mode is changed with
/// - `.into_output()`
/// - `.into_output_with_state()`
/// - `.into_floating_input()`
/// - `.into_pull_up_input()`
/// - Input pins are sampled with
Expand Down Expand Up @@ -516,6 +527,23 @@ macro_rules! impl_port {
$PXi { _mode: marker::PhantomData }
}

/// Make this pin a digital output with the provided initial state
pub fn into_output_with_state<D: AsDDR>(self, ddr: &D, initial_state: State) -> $PXi<mode::Output> {
unsafe {
(*<$PORTX>::ptr()).$reg_port.modify(|r, w| {
match initial_state {
State::High => w.bits(r.bits() | (1 << $i)),
State::Low => w.bits(r.bits() & !(1 << $i))
}
});
(*<$PORTX>::ptr()).$reg_ddr.modify(|r, w| {
w.bits(r.bits() | (1 << $i))
});
}
$PXi { _mode: marker::PhantomData }
}


/// Make this pin a digital input **without** enabling the internal pull-up.
pub fn into_floating_input<D: AsDDR>(self, ddr: &D) -> $PXi<mode::Input<mode::Floating>> {
unsafe {
Expand Down
1 change: 1 addition & 0 deletions chips/atmega1280-hal/src/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! [1]: ../../avr_hal_generic/port/index.html

pub use avr_hal_generic::port::mode;
pub use avr_hal_generic::port::State;

pub trait PortExt {
type Parts;
Expand Down
2 changes: 1 addition & 1 deletion chips/atmega168-hal/src/port.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! `PORTB` - `PORTD` digital IO
pub use avr_hal_generic::port::mode;
pub use avr_hal_generic::port::State;

pub trait PortExt {
type Parts;
Expand Down Expand Up @@ -79,4 +80,3 @@ avr_hal_generic::impl_port! {
}
}
}

1 change: 1 addition & 0 deletions chips/atmega2560-hal/src/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! [1]: ../../avr_hal_generic/port/index.html

pub use avr_hal_generic::port::mode;
pub use avr_hal_generic::port::State;

pub trait PortExt {
type Parts;
Expand Down
2 changes: 1 addition & 1 deletion chips/atmega328p-hal/src/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! [1]: ../../avr_hal_generic/port/index.html

pub use avr_hal_generic::port::mode;
pub use avr_hal_generic::port::State;

pub trait PortExt {
type Parts;
Expand All @@ -31,7 +32,6 @@ avr_hal_generic::impl_generic_pin! {
}
}


avr_hal_generic::impl_port! {
pub mod portb {
#[port_ext]
Expand Down
1 change: 1 addition & 0 deletions chips/atmega32u4-hal/src/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! [1]: ../../avr_hal_generic/port/index.html

pub use avr_hal_generic::port::mode;
pub use avr_hal_generic::port::State;

pub trait PortExt {
type Parts;
Expand Down
1 change: 1 addition & 0 deletions chips/atmega48p-hal/src/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! [1]: ../../avr_hal_generic/port/index.html

pub use avr_hal_generic::port::mode;
pub use avr_hal_generic::port::State;

pub trait PortExt {
type Parts;
Expand Down
1 change: 1 addition & 0 deletions chips/attiny85-hal/src/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! [1]: ../../avr_hal_generic/port/index.html

pub use avr_hal_generic::port::mode;
pub use avr_hal_generic::port::State;

pub trait PortExt {
type Parts;
Expand Down
1 change: 1 addition & 0 deletions chips/attiny88-hal/src/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! [1]: ../../avr_hal_generic/port/index.html

pub use avr_hal_generic::port::mode;
pub use avr_hal_generic::port::State;

pub trait PortExt {
type Parts;
Expand Down

0 comments on commit 531801d

Please sign in to comment.