Skip to content
Draft

Flags #231

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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fixed = { version = "1.28.0", optional = true }
embedded-io = "0.6"
stm32-hrtim = { version = "0.1.0", optional = true }
rand_core = { version = "0.9", default-features = false }
enumflags2 = "0.7.12"

[dependencies.cortex-m]
version = "0.7.7"
Expand Down
1 change: 0 additions & 1 deletion src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,6 @@ macro_rules! gpio {
pub use $gpiox::{ $($PXi,)+ };
}
}
use gpio;

mod g4;
pub use g4::*;
Expand Down
80 changes: 77 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ compile_error!(
stm32g4a1"
);

pub extern crate cortex_m;
pub extern crate nb;
pub extern crate stm32g4;
pub use cortex_m;
pub use nb;
pub use stm32g4;

pub use embedded_hal as hal;
pub use embedded_hal_old as hal_02;
pub use nb::block;

use enumflags2::{BitFlag, BitFlags};

#[cfg(feature = "stm32g431")]
pub use stm32g4::stm32g431 as stm32;

Expand Down Expand Up @@ -107,6 +109,78 @@ pub mod independent_watchdog;
#[cfg(feature = "usb")]
pub mod usb;

pub trait ReadFlags {
/// Enum of bit flags
type Flag: BitFlag;

/// Get all interrupts flags a once.
fn flags(&self) -> BitFlags<Self::Flag>;
}

pub trait ClearFlags {
/// Enum of manually clearable flags
type Flag: BitFlag;

/// Clear interrupts flags with `Self::Flags`s
///
/// If event flag is not cleared, it will immediately retrigger interrupt
/// after interrupt handler has finished.
fn clear_flags(&mut self, flags: impl Into<BitFlags<Self::Flag>>);

/// Clears all interrupts flags
#[inline(always)]
fn clear_all_flags(&mut self) {
self.clear_flags(BitFlags::ALL)
}
}

pub trait Listen {
/// Enum of bit flags associated with events
type Event: BitFlag;

#[doc(hidden)]
fn listen_event(
&mut self,
disable: Option<BitFlags<Self::Event>>,
enable: Option<BitFlags<Self::Event>>,
);

/// Start listening for `Event`s
///
/// Note, you will also have to enable the appropriate interrupt in the NVIC to start
/// receiving events.
#[inline(always)]
fn listen(&mut self, event: impl Into<BitFlags<Self::Event>>) {
self.listen_event(None, Some(event.into()));
}

/// Start listening for `Event`s, stop all other
///
/// Note, you will also have to enable the appropriate interrupt in the NVIC to start
/// receiving events.
#[inline(always)]
fn listen_only(&mut self, event: impl Into<BitFlags<Self::Event>>) {
self.listen_event(Some(BitFlags::ALL), Some(event.into()));
}

/// Stop listening for `Event`s
fn unlisten(&mut self, event: impl Into<BitFlags<Self::Event>>) {
self.listen_event(Some(event.into()), None);
}

/// Start listening all `Event`s
#[inline(always)]
fn listen_all(&mut self) {
self.listen(BitFlags::ALL)
}

/// Stop listening all `Event`s
#[inline(always)]
fn unlisten_all(&mut self) {
self.unlisten(BitFlags::ALL)
}
}

mod sealed {
pub trait Sealed {}
}
Expand Down
4 changes: 4 additions & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ pub use crate::pwm::PwmExt as _;
// pub use crate::watchdog::IWDGExt as _;
// pub use crate::watchdog::WWDGExt as _;
pub use crate::pwr::PwrExt as _;

pub use crate::ClearFlags as _;
pub use crate::Listen as _;
pub use crate::ReadFlags as _;
1 change: 0 additions & 1 deletion src/rcc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,6 @@ macro_rules! bus_struct {
)+
};
}
use bus_struct;

bus_struct! {
AHB1 => (AHB1ENR, ahb1enr, AHB1SMENR, ahb1smenr, AHB1RSTR, ahb1rstr, "Advanced High-performance Bus 1 (AHB1) registers"),
Expand Down
Loading
Loading