nrf/gpiote: initialize automatically

This commit is contained in:
Dario Nieuwenhuis 2021-05-12 01:01:08 +02:00
parent 92be72e0e3
commit 97b01f1c47
12 changed files with 21 additions and 55 deletions

View File

@ -18,30 +18,25 @@ use embassy_nrf::{interrupt, Peripherals};
#[embassy::main]
async fn main(spawner: Spawner) {
let p = Peripherals::take().unwrap();
let g = gpiote::initialize(p.GPIOTE, interrupt::take!(GPIOTE));
info!("Starting!");
let ch1 = InputChannel::new(
g,
p.GPIOTE_CH0,
Input::new(p.P0_11, Pull::Up),
InputChannelPolarity::HiToLo,
);
let ch2 = InputChannel::new(
g,
p.GPIOTE_CH1,
Input::new(p.P0_12, Pull::Up),
InputChannelPolarity::LoToHi,
);
let ch3 = InputChannel::new(
g,
p.GPIOTE_CH2,
Input::new(p.P0_24, Pull::Up),
InputChannelPolarity::Toggle,
);
let ch4 = InputChannel::new(
g,
p.GPIOTE_CH3,
Input::new(p.P0_25, Pull::Up),
InputChannelPolarity::Toggle,

View File

@ -8,12 +8,11 @@
#[path = "../example_common.rs"]
mod example_common;
use core::pin::Pin;
use defmt::panic;
use embassy::executor::Spawner;
use embassy::traits::gpio::{WaitForHigh, WaitForLow};
use embassy_nrf::gpio::{AnyPin, Input, Pin as _, Pull};
use embassy_nrf::gpiote::{self, PortInput};
use embassy_nrf::gpiote::PortInput;
use embassy_nrf::interrupt;
use embassy_nrf::Peripherals;
use example_common::*;
@ -32,12 +31,12 @@ async fn button_task(n: usize, mut pin: PortInput<'static, AnyPin>) {
async fn main(spawner: Spawner) {
let p = Peripherals::take().unwrap();
let g = gpiote::initialize(p.GPIOTE, interrupt::take!(GPIOTE));
info!("Starting!");
let btn1 = PortInput::new(g, Input::new(p.P0_11.degrade(), Pull::Up));
let btn2 = PortInput::new(g, Input::new(p.P0_12.degrade(), Pull::Up));
let btn3 = PortInput::new(g, Input::new(p.P0_24.degrade(), Pull::Up));
let btn4 = PortInput::new(g, Input::new(p.P0_25.degrade(), Pull::Up));
let btn1 = PortInput::new(Input::new(p.P0_11.degrade(), Pull::Up));
let btn2 = PortInput::new(Input::new(p.P0_12.degrade(), Pull::Up));
let btn3 = PortInput::new(Input::new(p.P0_24.degrade(), Pull::Up));
let btn4 = PortInput::new(Input::new(p.P0_25.degrade(), Pull::Up));
spawner.spawn(button_task(1, btn1)).unwrap();
spawner.spawn(button_task(2, btn2)).unwrap();

View File

@ -21,44 +21,37 @@ use gpiote::{OutputChannel, OutputChannelPolarity};
#[embassy::main]
async fn main(spawner: Spawner) {
let p = Peripherals::take().unwrap();
let g = gpiote::initialize(p.GPIOTE, interrupt::take!(GPIOTE));
info!("Starting!");
let button1 = InputChannel::new(
g,
p.GPIOTE_CH0,
Input::new(p.P0_11, Pull::Up),
InputChannelPolarity::HiToLo,
);
let button2 = InputChannel::new(
g,
p.GPIOTE_CH1,
Input::new(p.P0_12, Pull::Up),
InputChannelPolarity::HiToLo,
);
let button3 = InputChannel::new(
g,
p.GPIOTE_CH2,
Input::new(p.P0_24, Pull::Up),
InputChannelPolarity::HiToLo,
);
let button4 = InputChannel::new(
g,
p.GPIOTE_CH3,
Input::new(p.P0_25, Pull::Up),
InputChannelPolarity::HiToLo,
);
let led1 = OutputChannel::new(
g,
p.GPIOTE_CH4,
Output::new(p.P0_13, Level::Low, OutputDrive::Standard),
OutputChannelPolarity::Toggle,
);
let led2 = OutputChannel::new(
g,
p.GPIOTE_CH5,
Output::new(p.P0_14, Level::Low, OutputDrive::Standard),
OutputChannelPolarity::Toggle,

View File

@ -24,7 +24,6 @@ embassy_extras::peripherals! {
TIMER2,
// GPIOTE
GPIOTE,
GPIOTE_CH0,
GPIOTE_CH1,
GPIOTE_CH2,

View File

@ -24,7 +24,6 @@ embassy_extras::peripherals! {
TIMER2,
// GPIOTE
GPIOTE,
GPIOTE_CH0,
GPIOTE_CH1,
GPIOTE_CH2,

View File

@ -24,7 +24,6 @@ embassy_extras::peripherals! {
TIMER2,
// GPIOTE
GPIOTE,
GPIOTE_CH0,
GPIOTE_CH1,
GPIOTE_CH2,

View File

@ -25,7 +25,6 @@ embassy_extras::peripherals! {
TIMER3,
// GPIOTE
GPIOTE,
GPIOTE_CH0,
GPIOTE_CH1,
GPIOTE_CH2,

View File

@ -29,7 +29,6 @@ embassy_extras::peripherals! {
TIMER4,
// GPIOTE
GPIOTE,
GPIOTE_CH0,
GPIOTE_CH1,
GPIOTE_CH2,

View File

@ -30,7 +30,6 @@ embassy_extras::peripherals! {
TIMER4,
// GPIOTE
GPIOTE,
GPIOTE_CH0,
GPIOTE_CH1,
GPIOTE_CH2,

View File

@ -33,7 +33,6 @@ embassy_extras::peripherals! {
TIMER4,
// GPIOTE
GPIOTE,
GPIOTE_CH0,
GPIOTE_CH1,
GPIOTE_CH2,

View File

@ -2,7 +2,7 @@ use core::convert::Infallible;
use core::future::Future;
use core::marker::PhantomData;
use core::task::{Context, Poll};
use embassy::interrupt::InterruptExt;
use embassy::interrupt::{Interrupt, InterruptExt};
use embassy::traits::gpio::{WaitForAnyEdge, WaitForHigh, WaitForLow};
use embassy::util::AtomicWaker;
use embassy_extras::impl_unborrow;
@ -40,15 +40,7 @@ pub enum OutputChannelPolarity {
Toggle,
}
/// Token indicating GPIOTE has been correctly initialized.
///
/// This is not an owned singleton, it is Copy. Drivers that make use of GPIOTE require it.
#[derive(Clone, Copy)]
pub struct Initialized {
_private: (),
}
pub fn initialize(_gpiote: peripherals::GPIOTE, irq: interrupt::GPIOTE) -> Initialized {
pub(crate) fn init() {
#[cfg(any(feature = "nrf52833", feature = "nrf52840"))]
let ports = unsafe { &[&*pac::P0::ptr(), &*pac::P1::ptr()] };
#[cfg(not(any(feature = "nrf52833", feature = "nrf52840")))]
@ -62,17 +54,18 @@ pub fn initialize(_gpiote: peripherals::GPIOTE, irq: interrupt::GPIOTE) -> Initi
}
// Enable interrupts
let g = unsafe { &*pac::GPIOTE::ptr() };
g.events_port.write(|w| w);
g.intenset.write(|w| w.port().set());
irq.set_handler(on_irq);
let irq = unsafe { interrupt::GPIOTE::steal() };
irq.unpend();
irq.enable();
Initialized { _private: () }
let g = unsafe { &*pac::GPIOTE::ptr() };
g.events_port.write(|w| w);
g.intenset.write(|w| w.port().set());
}
unsafe fn on_irq(_ctx: *mut ()) {
#[interrupt]
unsafe fn GPIOTE() {
let g = &*pac::GPIOTE::ptr();
for i in 0..CHANNEL_COUNT {
@ -133,12 +126,7 @@ impl<'d, C: Channel, T: GpioPin> Drop for InputChannel<'d, C, T> {
}
impl<'d, C: Channel, T: GpioPin> InputChannel<'d, C, T> {
pub fn new(
_init: Initialized,
ch: C,
pin: Input<'d, T>,
polarity: InputChannelPolarity,
) -> Self {
pub fn new(ch: C, pin: Input<'d, T>, polarity: InputChannelPolarity) -> Self {
let g = unsafe { &*pac::GPIOTE::ptr() };
let num = ch.number();
@ -217,12 +205,7 @@ impl<'d, C: Channel, T: GpioPin> Drop for OutputChannel<'d, C, T> {
}
impl<'d, C: Channel, T: GpioPin> OutputChannel<'d, C, T> {
pub fn new(
_init: Initialized,
ch: C,
pin: Output<'d, T>,
polarity: OutputChannelPolarity,
) -> Self {
pub fn new(ch: C, pin: Output<'d, T>, polarity: OutputChannelPolarity) -> Self {
let g = unsafe { &*pac::GPIOTE::ptr() };
let num = ch.number();
@ -297,7 +280,7 @@ pub struct PortInput<'d, T: GpioPin> {
impl<'d, T: GpioPin> Unpin for PortInput<'d, T> {}
impl<'d, T: GpioPin> PortInput<'d, T> {
pub fn new(_init: Initialized, pin: Input<'d, T>) -> Self {
pub fn new(pin: Input<'d, T>) -> Self {
Self { pin }
}
}

View File

@ -73,4 +73,7 @@ pub unsafe fn configure(config: Config) {
r.events_lfclkstarted.write(|w| unsafe { w.bits(0) });
r.tasks_lfclkstart.write(|w| unsafe { w.bits(1) });
while r.events_lfclkstarted.read().bits() == 0 {}
// Init GPIOTE
crate::gpiote::init();
}