From 45355f184a8397730a3bd04a0abbebc685af63fe Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Mon, 1 Mar 2021 00:44:38 +0100 Subject: [PATCH] move most interrupt methods to InterruptExt extension trait. Fixes #35 --- embassy-nrf-examples/src/bin/multiprio.rs | 2 +- embassy-nrf/src/buffered_uarte.rs | 1 + embassy-nrf/src/gpiote.rs | 2 +- embassy-nrf/src/rtc.rs | 1 + embassy-nrf/src/uarte.rs | 1 + embassy-nrf/src/util/peripheral.rs | 2 ++ embassy-stm32f4/src/rtc.rs | 5 +++-- embassy-stm32f4/src/serial.rs | 4 +--- embassy/src/executor/mod.rs | 2 +- embassy/src/interrupt.rs | 17 +++++++++++++++++ embassy/src/util/signal.rs | 7 ++++--- 11 files changed, 33 insertions(+), 11 deletions(-) diff --git a/embassy-nrf-examples/src/bin/multiprio.rs b/embassy-nrf-examples/src/bin/multiprio.rs index 850ef8fc0..d652633ab 100644 --- a/embassy-nrf-examples/src/bin/multiprio.rs +++ b/embassy-nrf-examples/src/bin/multiprio.rs @@ -66,9 +66,9 @@ use defmt::panic; use nrf52840_hal::clocks; use embassy::executor::{task, Executor, IrqExecutor}; +use embassy::interrupt::InterruptExt; use embassy::time::{Duration, Instant, Timer}; use embassy::util::Forever; -use embassy_nrf::interrupt::Interrupt; use embassy_nrf::{interrupt, pac, rtc}; #[task] diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs index 26a40507b..5cbba486e 100644 --- a/embassy-nrf/src/buffered_uarte.rs +++ b/embassy-nrf/src/buffered_uarte.rs @@ -10,6 +10,7 @@ use core::ops::Deref; use core::pin::Pin; use core::sync::atomic::{compiler_fence, Ordering}; use core::task::{Context, Poll}; +use embassy::interrupt::InterruptExt; use embassy::io::{AsyncBufRead, AsyncWrite, Result}; use embassy::util::WakerRegistration; use embedded_hal::digital::v2::OutputPin; diff --git a/embassy-nrf/src/gpiote.rs b/embassy-nrf/src/gpiote.rs index 01e61bd5c..78bb2aeb6 100644 --- a/embassy-nrf/src/gpiote.rs +++ b/embassy-nrf/src/gpiote.rs @@ -5,11 +5,11 @@ use core::pin::Pin; use core::ptr; use core::task::{Context, Poll}; use embassy::gpio::{WaitForHigh, WaitForLow}; +use embassy::interrupt::InterruptExt; use embassy::util::Signal; use crate::hal::gpio::{Input, Level, Output, Pin as GpioPin, Port}; use crate::interrupt; -use crate::interrupt::Interrupt; use crate::pac; use crate::pac::generic::Reg; use crate::pac::gpiote::_TASKS_OUT; diff --git a/embassy-nrf/src/rtc.rs b/embassy-nrf/src/rtc.rs index 7a79580cc..e0e645fe8 100644 --- a/embassy-nrf/src/rtc.rs +++ b/embassy-nrf/src/rtc.rs @@ -2,6 +2,7 @@ use core::cell::Cell; use core::ops::Deref; use core::sync::atomic::{compiler_fence, AtomicU32, Ordering}; +use embassy::interrupt::InterruptExt; use embassy::time::Clock; use crate::interrupt; diff --git a/embassy-nrf/src/uarte.rs b/embassy-nrf/src/uarte.rs index 07718a1dd..9d5ff81aa 100644 --- a/embassy-nrf/src/uarte.rs +++ b/embassy-nrf/src/uarte.rs @@ -9,6 +9,7 @@ use core::ops::Deref; use core::sync::atomic::{compiler_fence, Ordering}; use core::task::{Context, Poll}; +use embassy::interrupt::InterruptExt; use embassy::util::Signal; use crate::fmt::{assert, *}; diff --git a/embassy-nrf/src/util/peripheral.rs b/embassy-nrf/src/util/peripheral.rs index bfb023f56..bb88f0820 100644 --- a/embassy-nrf/src/util/peripheral.rs +++ b/embassy-nrf/src/util/peripheral.rs @@ -4,6 +4,8 @@ use core::mem::MaybeUninit; use core::pin::Pin; use core::sync::atomic::{compiler_fence, Ordering}; +use embassy::interrupt::InterruptExt; + use crate::fmt::{assert, *}; use crate::interrupt::Interrupt; diff --git a/embassy-stm32f4/src/rtc.rs b/embassy-stm32f4/src/rtc.rs index 21589f5d6..824a47072 100644 --- a/embassy-stm32f4/src/rtc.rs +++ b/embassy-stm32f4/src/rtc.rs @@ -1,11 +1,12 @@ use core::cell::Cell; use core::convert::TryInto; use core::sync::atomic::{compiler_fence, AtomicU32, Ordering}; - -use embassy::time::{Clock, TICKS_PER_SECOND}; use stm32f4xx_hal::bb; use stm32f4xx_hal::rcc::Clocks; +use embassy::interrupt::InterruptExt; +use embassy::time::{Clock, TICKS_PER_SECOND}; + use crate::interrupt; use crate::interrupt::{CriticalSection, Interrupt, Mutex}; diff --git a/embassy-stm32f4/src/serial.rs b/embassy-stm32f4/src/serial.rs index 669d7856c..8f594ca13 100644 --- a/embassy-stm32f4/src/serial.rs +++ b/embassy-stm32f4/src/serial.rs @@ -8,7 +8,7 @@ use core::future::Future; use core::ptr; use core::sync::atomic::{self, Ordering}; -use embassy::interrupt::Interrupt; +use embassy::interrupt::InterruptExt; use embassy::uart::{Error, Uart}; use embassy::util::Signal; @@ -19,9 +19,7 @@ use crate::hal::rcc::Clocks; use crate::hal::serial::config::{Config as SerialConfig, DmaConfig as SerialDmaConfig}; use crate::hal::serial::Pins; use crate::hal::serial::{Event as SerialEvent, Serial as HalSerial}; - use crate::interrupt; - use crate::pac::{DMA2, USART1}; /// Interface to the Serial peripheral diff --git a/embassy/src/executor/mod.rs b/embassy/src/executor/mod.rs index be0d1b9e7..5f016f703 100644 --- a/embassy/src/executor/mod.rs +++ b/embassy/src/executor/mod.rs @@ -17,7 +17,7 @@ mod waker; use self::util::UninitCell; use crate::fmt::panic; -use crate::interrupt::Interrupt; +use crate::interrupt::{Interrupt, InterruptExt}; use crate::time::Alarm; // repr(C) is needed to guarantee that the raw::Task is located at offset 0 diff --git a/embassy/src/interrupt.rs b/embassy/src/interrupt.rs index 53413d41d..ba25ec8ec 100644 --- a/embassy/src/interrupt.rs +++ b/embassy/src/interrupt.rs @@ -37,7 +37,24 @@ pub unsafe trait Interrupt { /// Implementation detail, do not use outside embassy crates. #[doc(hidden)] unsafe fn __handler(&self) -> &'static Handler; +} +pub trait InterruptExt: Interrupt { + fn set_handler(&self, func: unsafe fn(*mut ())); + fn remove_handler(&self); + fn set_handler_context(&self, ctx: *mut ()); + fn enable(&self); + fn disable(&self); + fn is_active(&self) -> bool; + fn is_enabled(&self) -> bool; + fn is_pending(&self) -> bool; + fn pend(&self); + fn unpend(&self); + fn get_priority(&self) -> Self::Priority; + fn set_priority(&self, prio: Self::Priority); +} + +impl InterruptExt for T { fn set_handler(&self, func: unsafe fn(*mut ())) { let handler = unsafe { self.__handler() }; handler.func.store(func as *mut (), Ordering::Release); diff --git a/embassy/src/util/signal.rs b/embassy/src/util/signal.rs index 0abcc75f3..41e27d4ca 100644 --- a/embassy/src/util/signal.rs +++ b/embassy/src/util/signal.rs @@ -1,6 +1,3 @@ -use crate::executor; -use crate::fmt::panic; -use crate::interrupt::Interrupt; use core::cell::UnsafeCell; use core::future::Future; use core::mem; @@ -9,6 +6,10 @@ use core::task::{Context, Poll, Waker}; use cortex_m::peripheral::NVIC; use cortex_m::peripheral::{scb, SCB}; +use crate::executor; +use crate::fmt::panic; +use crate::interrupt::{Interrupt, InterruptExt}; + pub struct Signal { state: UnsafeCell>, }