Skip to content

Commit

Permalink
Force swm to be enabled on lpc845, fix lpc-rs#183
Browse files Browse the repository at this point in the history
Also makes the new functions public, to stay in line with gpio and
bypass unused function warnings
  • Loading branch information
david-sawatzke committed Jan 9, 2020
1 parent ae3f278 commit ce87ffd
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 84 deletions.
14 changes: 10 additions & 4 deletions examples/gpio_delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ fn main() -> ! {
let p = Peripherals::take().unwrap();

// Initialize the APIs of the peripherals we need.
let swm = p.SWM.split();
let mut delay = Delay::new(p.SYST);
#[cfg(feature = "82x")]
let gpio = p.GPIO; // GPIO is initialized by default on LPC82x.
let (gpio, swm) = (
// GPIO & SWM are initialized by default on LPC82x.
p.GPIO,
p.SWM.split(),
);
#[cfg(feature = "845")]
let gpio = {
let (gpio, swm) = {
let mut syscon = p.SYSCON.split();
p.GPIO.enable(&mut syscon.handle)
(
p.GPIO.enable(&mut syscon.handle),
p.SWM.enable(&mut syscon.handle).split(),
)
};

// Select pin for LED
Expand Down
14 changes: 10 additions & 4 deletions examples/gpio_simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ fn main() -> ! {
let p = Peripherals::take().unwrap();

// Initialize the APIs of the peripherals we need.
let swm = p.SWM.split();
#[cfg(feature = "82x")]
let gpio = p.GPIO; // GPIO is initialized by default on LPC82x.
let (gpio, swm) = (
// GPIO & SWM are initialized by default on LPC82x.
p.GPIO,
p.SWM.split(),
);
#[cfg(feature = "845")]
let gpio = {
let (gpio, swm) = {
let mut syscon = p.SYSCON.split();
p.GPIO.enable(&mut syscon.handle)
(
p.GPIO.enable(&mut syscon.handle),
p.SWM.enable(&mut syscon.handle).split(),
)
};

// Select pin for LED
Expand Down
12 changes: 9 additions & 3 deletions examples/gpio_sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ fn main() -> ! {
let p = Peripherals::take().unwrap();

// Initialize the APIs of the peripherals we need.
let swm = p.SWM.split();
let mut syscon = p.SYSCON.split();
let mut wkt = p.WKT.enable(&mut syscon.handle);
#[cfg(feature = "82x")]
let gpio = p.GPIO; // GPIO is initialized by default on LPC82x.
let (gpio, swm) = (
// GPIO & SWM are initialized by default on LPC82x.
p.GPIO,
p.SWM.split(),
);
#[cfg(feature = "845")]
let gpio = p.GPIO.enable(&mut syscon.handle);
let (gpio, swm) = (
p.GPIO.enable(&mut syscon.handle),
p.SWM.enable(&mut syscon.handle).split(),
);

// We're going to need a clock for sleeping. Let's use the internal oscillator/IRC/FRO-derived clock
// that runs at 750 kHz.
Expand Down
14 changes: 4 additions & 10 deletions examples/usart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,11 @@ use lpc8xx_hal::pac::syscon::frg::frgclksel::SEL_A;
fn main() -> ! {
let p = Peripherals::take().unwrap();

let mut swm = p.SWM.split();
let mut syscon = p.SYSCON.split();

// TODO
//
// For some reason, the clock for swm need to be enabled, even though
// it should be enabled from the start
swm.handle = swm
.handle
.disable(&mut syscon.handle)
.enable(&mut syscon.handle);
#[cfg(feature = "82x")]
let mut swm = p.SWM.split(); // SWM is initialized by default on LPC82x.
#[cfg(feature = "845")]
let mut swm = p.SWM.enable(&mut syscon.handle).split();

#[cfg(feature = "82x")]
// Set baud rate to 115200 baud
Expand Down
12 changes: 11 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,14 @@ pub struct Peripherals {
pub PMU: PMU,

/// Switch matrix
pub SWM: SWM,
///
/// The Switch matrix is enabled by default.
#[cfg(feature = "82x")]
pub SWM: SWM<init_state::Enabled>,

/// Switch matrix
#[cfg(feature = "845")]
pub SWM: SWM<init_state::Disabled>,

/// System configuration
pub SYSCON: SYSCON,
Expand Down Expand Up @@ -512,6 +519,9 @@ impl Peripherals {
#[cfg(feature = "82x")]
I2C0: I2C::new(p.I2C0),
PMU: PMU::new(p.PMU),
#[cfg(feature = "82x")]
SWM: unsafe { SWM::new_enabled(p.SWM0) },
#[cfg(feature = "845")]
SWM: SWM::new(p.SWM0),
SYSCON: SYSCON::new(p.SYSCON),
USART0: USART::new(p.USART0),
Expand Down
144 changes: 82 additions & 62 deletions src/swm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,64 @@ use self::pin_state::PinState;
/// [`swm::Parts`]: struct.Parts.html
/// [`Peripherals`]: ../struct.Peripherals.html
/// [module documentation]: index.html
pub struct SWM {
pub struct SWM<State = init_state::Enabled> {
swm: pac::SWM0,
_state: State,
}

impl SWM {
pub(crate) fn new(swm: pac::SWM0) -> Self {
SWM { swm }
impl SWM<init_state::Disabled> {
/// Create an disabled swm peripheral
///
/// This method creates an `SWM` instance that it assumes is in the
/// [`Disabled`] state. As it's only possible to enable a [`Disabled`] `SWM`
/// instance, it's also safe to pass an already [`Enabled`] instance.
///
/// [`Disabled`]: ../init_state/struct.Enabled.html
/// [`Enabled`]: ../init_state/struct.Enabled.html
pub fn new(swm: pac::SWM0) -> Self {
SWM {
swm,
_state: init_state::Disabled,
}
}

/// Enable the switch matrix
///
/// This method is only available, if `SWM` is in the [`Disabled`] state.
/// Code that attempts to call this method when the peripheral is already
/// enabled will not compile.
///
/// Consumes this instance of `SWM` and returns another instance that has
/// its `State` type parameter set to [`Enabled`].
///
/// [`Disabled`]: ../init_state/struct.Disabled.html
/// [`Enabled`]: ../init_state/struct.Enabled.html
pub fn enable(
self,
syscon: &mut syscon::Handle,
) -> SWM<init_state::Enabled> {
syscon.enable_clock(&self.swm);

SWM {
swm: self.swm,
_state: init_state::Enabled(()),
}
}
}
impl SWM<init_state::Enabled> {
/// Create an enabled swm peripheral
///
/// # Safety
///
/// This method creates an `SWM` instance that it assumes is already in the
/// [`Enabled`] state. It's up to the caller to verify this assumption.
///
/// [`Enabled`]: ../init_state/struct.Enabled.html
pub unsafe fn new_enabled(swm: pac::SWM0) -> Self {
SWM {
swm,
_state: init_state::Enabled(()),
}
}

/// Splits the SWM API into its component parts
Expand All @@ -53,6 +104,29 @@ impl SWM {
}
}

/// Disable the switch matrix
///
/// This method is only available, if `SWM` is in the [`Enabled`] state.
/// Code that attempts to call this method when the peripheral is already
/// disabled will not compile.
///
/// Consumes this instance of `SWM` and returns another instance that has
/// its `State` type parameter set to [`Disabled`].
///
/// [`Enabled`]: ../init_state/struct.Enabled.html
/// [`Disabled`]: ../init_state/struct.Disabled.html
pub fn disable(
self,
syscon: &mut syscon::Handle,
) -> SWM<init_state::Disabled> {
syscon.disable_clock(&self.swm);

SWM {
swm: self.swm,
_state: init_state::Disabled,
}
}

/// Return the raw peripheral
///
/// This method serves as an escape hatch from the HAL API. It returns the
Expand All @@ -78,7 +152,7 @@ impl SWM {
/// [module documentation]: index.html
pub struct Parts {
/// Handle to the switch matrix
pub handle: Handle<init_state::Enabled>,
pub handle: Handle,

/// Pins that can be used for GPIO or other functions
pub pins: Pins,
Expand All @@ -100,67 +174,13 @@ pub struct Parts {
/// PMU.
///
/// [module documentation]: index.html
pub struct Handle<State = init_state::Enabled> {
pub struct Handle {
swm: pac::SWM0,
_state: State,
}

impl Handle<init_state::Enabled> {
impl Handle {
pub(crate) fn new(swm: pac::SWM0) -> Self {
Handle {
swm,
_state: init_state::Enabled(()),
}
}
}

impl Handle<init_state::Disabled> {
/// Enable the switch matrix
///
/// This method is only available, if `SWM` is in the [`Disabled`] state.
/// Code that attempts to call this method when the peripheral is already
/// enabled will not compile.
///
/// Consumes this instance of `SWM` and returns another instance that has
/// its `State` type parameter set to [`Enabled`].
///
/// [`Disabled`]: ../init_state/struct.Disabled.html
/// [`Enabled`]: ../init_state/struct.Enabled.html
pub fn enable(
self,
syscon: &mut syscon::Handle,
) -> Handle<init_state::Enabled> {
syscon.enable_clock(&self.swm);

Handle {
swm: self.swm,
_state: init_state::Enabled(()),
}
}
}

impl Handle<init_state::Enabled> {
/// Disable the switch matrix
///
/// This method is only available, if `SWM` is in the [`Enabled`] state.
/// Code that attempts to call this method when the peripheral is already
/// disabled will not compile.
///
/// Consumes this instance of `SWM` and returns another instance that has
/// its `State` type parameter set to [`Disabled`].
///
/// [`Enabled`]: ../init_state/struct.Enabled.html
/// [`Disabled`]: ../init_state/struct.Disabled.html
pub fn disable(
self,
syscon: &mut syscon::Handle,
) -> Handle<init_state::Disabled> {
syscon.disable_clock(&self.swm);

Handle {
swm: self.swm,
_state: init_state::Disabled,
}
Handle { swm }
}
}

Expand Down

0 comments on commit ce87ffd

Please sign in to comment.