Merge pull request from embassy-rs/unsealedify

nrf,rp,stm32: remove mod sealed.
This commit is contained in:
Dario Nieuwenhuis 2024-04-04 22:55:31 +00:00 committed by GitHub
commit 8294bbc99d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
35 changed files with 659 additions and 771 deletions

View file

@ -20,8 +20,7 @@ use embassy_hal_internal::{into_ref, PeripheralRef};
// Re-export SVD variants to allow user to directly set values // Re-export SVD variants to allow user to directly set values
pub use pac::uarte0::{baudrate::BAUDRATE_A as Baudrate, config::PARITY_A as Parity}; pub use pac::uarte0::{baudrate::BAUDRATE_A as Baudrate, config::PARITY_A as Parity};
use crate::gpio::sealed::Pin; use crate::gpio::{AnyPin, Pin as GpioPin, PselBits, SealedPin};
use crate::gpio::{AnyPin, Pin as GpioPin, PselBits};
use crate::interrupt::typelevel::Interrupt; use crate::interrupt::typelevel::Interrupt;
use crate::ppi::{ use crate::ppi::{
self, AnyConfigurableChannel, AnyGroup, Channel, ConfigurableChannel, Event, Group, Ppi, PpiGroup, Task, self, AnyConfigurableChannel, AnyGroup, Channel, ConfigurableChannel, Event, Group, Ppi, PpiGroup, Task,
@ -30,19 +29,15 @@ use crate::timer::{Instance as TimerInstance, Timer};
use crate::uarte::{configure, drop_tx_rx, Config, Instance as UarteInstance}; use crate::uarte::{configure, drop_tx_rx, Config, Instance as UarteInstance};
use crate::{interrupt, pac, Peripheral}; use crate::{interrupt, pac, Peripheral};
mod sealed { pub(crate) struct State {
use super::*; tx_buf: RingBuffer,
tx_count: AtomicUsize,
pub struct State { rx_buf: RingBuffer,
pub tx_buf: RingBuffer, rx_started: AtomicBool,
pub tx_count: AtomicUsize, rx_started_count: AtomicU8,
rx_ended_count: AtomicU8,
pub rx_buf: RingBuffer, rx_ppi_ch: AtomicU8,
pub rx_started: AtomicBool,
pub rx_started_count: AtomicU8,
pub rx_ended_count: AtomicU8,
pub rx_ppi_ch: AtomicU8,
}
} }
/// UART error. /// UART error.
@ -53,8 +48,6 @@ pub enum Error {
// No errors for now // No errors for now
} }
pub(crate) use sealed::State;
impl State { impl State {
pub(crate) const fn new() -> Self { pub(crate) const fn new() -> Self {
Self { Self {

View file

@ -7,7 +7,6 @@ use core::hint::unreachable_unchecked;
use cfg_if::cfg_if; use cfg_if::cfg_if;
use embassy_hal_internal::{impl_peripheral, into_ref, PeripheralRef}; use embassy_hal_internal::{impl_peripheral, into_ref, PeripheralRef};
use self::sealed::Pin as _;
#[cfg(feature = "nrf51")] #[cfg(feature = "nrf51")]
use crate::pac::gpio; use crate::pac::gpio;
#[cfg(feature = "nrf51")] #[cfg(feature = "nrf51")]
@ -361,59 +360,56 @@ impl<'d> Drop for Flex<'d> {
} }
} }
pub(crate) mod sealed { pub(crate) trait SealedPin {
use super::*; fn pin_port(&self) -> u8;
pub trait Pin { #[inline]
fn pin_port(&self) -> u8; fn _pin(&self) -> u8 {
cfg_if! {
#[inline] if #[cfg(feature = "_gpio-p1")] {
fn _pin(&self) -> u8 { self.pin_port() % 32
cfg_if! { } else {
if #[cfg(feature = "_gpio-p1")] { self.pin_port()
self.pin_port() % 32
} else {
self.pin_port()
}
} }
} }
}
#[inline] #[inline]
fn block(&self) -> &gpio::RegisterBlock { fn block(&self) -> &gpio::RegisterBlock {
unsafe { unsafe {
match self.pin_port() / 32 { match self.pin_port() / 32 {
#[cfg(feature = "nrf51")] #[cfg(feature = "nrf51")]
0 => &*pac::GPIO::ptr(), 0 => &*pac::GPIO::ptr(),
#[cfg(not(feature = "nrf51"))] #[cfg(not(feature = "nrf51"))]
0 => &*pac::P0::ptr(), 0 => &*pac::P0::ptr(),
#[cfg(feature = "_gpio-p1")] #[cfg(feature = "_gpio-p1")]
1 => &*pac::P1::ptr(), 1 => &*pac::P1::ptr(),
_ => unreachable_unchecked(), _ => unreachable_unchecked(),
}
} }
} }
}
#[inline] #[inline]
fn conf(&self) -> &gpio::PIN_CNF { fn conf(&self) -> &gpio::PIN_CNF {
&self.block().pin_cnf[self._pin() as usize] &self.block().pin_cnf[self._pin() as usize]
} }
/// Set the output as high. /// Set the output as high.
#[inline] #[inline]
fn set_high(&self) { fn set_high(&self) {
unsafe { self.block().outset.write(|w| w.bits(1u32 << self._pin())) } unsafe { self.block().outset.write(|w| w.bits(1u32 << self._pin())) }
} }
/// Set the output as low. /// Set the output as low.
#[inline] #[inline]
fn set_low(&self) { fn set_low(&self) {
unsafe { self.block().outclr.write(|w| w.bits(1u32 << self._pin())) } unsafe { self.block().outclr.write(|w| w.bits(1u32 << self._pin())) }
}
} }
} }
/// Interface for a Pin that can be configured by an [Input] or [Output] driver, or converted to an [AnyPin]. /// Interface for a Pin that can be configured by an [Input] or [Output] driver, or converted to an [AnyPin].
pub trait Pin: Peripheral<P = Self> + Into<AnyPin> + sealed::Pin + Sized + 'static { #[allow(private_bounds)]
pub trait Pin: Peripheral<P = Self> + Into<AnyPin> + SealedPin + Sized + 'static {
/// Number of the pin within the port (0..31) /// Number of the pin within the port (0..31)
#[inline] #[inline]
fn pin(&self) -> u8 { fn pin(&self) -> u8 {
@ -464,7 +460,7 @@ impl AnyPin {
impl_peripheral!(AnyPin); impl_peripheral!(AnyPin);
impl Pin for AnyPin {} impl Pin for AnyPin {}
impl sealed::Pin for AnyPin { impl SealedPin for AnyPin {
#[inline] #[inline]
fn pin_port(&self) -> u8 { fn pin_port(&self) -> u8 {
self.pin_port self.pin_port
@ -502,7 +498,7 @@ pub(crate) fn deconfigure_pin(psel_bits: u32) {
macro_rules! impl_pin { macro_rules! impl_pin {
($type:ident, $port_num:expr, $pin_num:expr) => { ($type:ident, $port_num:expr, $pin_num:expr) => {
impl crate::gpio::Pin for peripherals::$type {} impl crate::gpio::Pin for peripherals::$type {}
impl crate::gpio::sealed::Pin for peripherals::$type { impl crate::gpio::SealedPin for peripherals::$type {
#[inline] #[inline]
fn pin_port(&self) -> u8 { fn pin_port(&self) -> u8 {
$port_num * 32 + $pin_num $port_num * 32 + $pin_num

View file

@ -7,8 +7,7 @@ use core::task::{Context, Poll};
use embassy_hal_internal::{impl_peripheral, into_ref, Peripheral, PeripheralRef}; use embassy_hal_internal::{impl_peripheral, into_ref, Peripheral, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker; use embassy_sync::waitqueue::AtomicWaker;
use crate::gpio::sealed::Pin as _; use crate::gpio::{AnyPin, Flex, Input, Output, Pin as GpioPin, SealedPin as _};
use crate::gpio::{AnyPin, Flex, Input, Output, Pin as GpioPin};
use crate::interrupt::InterruptExt; use crate::interrupt::InterruptExt;
use crate::ppi::{Event, Task}; use crate::ppi::{Event, Task};
use crate::{interrupt, pac, peripherals}; use crate::{interrupt, pac, peripherals};
@ -446,14 +445,13 @@ impl<'d> Flex<'d> {
// ======================= // =======================
mod sealed { trait SealedChannel {}
pub trait Channel {}
}
/// GPIOTE channel trait. /// GPIOTE channel trait.
/// ///
/// Implemented by all GPIOTE channels. /// Implemented by all GPIOTE channels.
pub trait Channel: sealed::Channel + Into<AnyChannel> + Sized + 'static { #[allow(private_bounds)]
pub trait Channel: SealedChannel + Into<AnyChannel> + Sized + 'static {
/// Get the channel number. /// Get the channel number.
fn number(&self) -> usize; fn number(&self) -> usize;
@ -478,7 +476,7 @@ pub struct AnyChannel {
number: u8, number: u8,
} }
impl_peripheral!(AnyChannel); impl_peripheral!(AnyChannel);
impl sealed::Channel for AnyChannel {} impl SealedChannel for AnyChannel {}
impl Channel for AnyChannel { impl Channel for AnyChannel {
fn number(&self) -> usize { fn number(&self) -> usize {
self.number as usize self.number as usize
@ -487,7 +485,7 @@ impl Channel for AnyChannel {
macro_rules! impl_channel { macro_rules! impl_channel {
($type:ident, $number:expr) => { ($type:ident, $number:expr) => {
impl sealed::Channel for peripherals::$type {} impl SealedChannel for peripherals::$type {}
impl Channel for peripherals::$type { impl Channel for peripherals::$type {
fn number(&self) -> usize { fn number(&self) -> usize {
$number as usize $number as usize

View file

@ -6,11 +6,12 @@ use core::future::poll_fn;
use core::marker::PhantomData; use core::marker::PhantomData;
use core::mem::size_of; use core::mem::size_of;
use core::ops::{Deref, DerefMut}; use core::ops::{Deref, DerefMut};
use core::sync::atomic::{compiler_fence, Ordering}; use core::sync::atomic::{compiler_fence, AtomicBool, Ordering};
use core::task::Poll; use core::task::Poll;
use embassy_hal_internal::drop::OnDrop; use embassy_hal_internal::drop::OnDrop;
use embassy_hal_internal::{into_ref, PeripheralRef}; use embassy_hal_internal::{into_ref, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker;
use crate::gpio::{AnyPin, Pin as GpioPin}; use crate::gpio::{AnyPin, Pin as GpioPin};
use crate::interrupt::typelevel::Interrupt; use crate::interrupt::typelevel::Interrupt;
@ -1140,50 +1141,45 @@ impl<S: Sample, const NB: usize, const NS: usize> MultiBuffering<S, NB, NS> {
} }
} }
pub(crate) mod sealed { /// Peripheral static state
use core::sync::atomic::AtomicBool; pub(crate) struct State {
started: AtomicBool,
rx_waker: AtomicWaker,
tx_waker: AtomicWaker,
stop_waker: AtomicWaker,
}
use embassy_sync::waitqueue::AtomicWaker; impl State {
pub(crate) const fn new() -> Self {
/// Peripheral static state Self {
pub struct State { started: AtomicBool::new(false),
pub started: AtomicBool, rx_waker: AtomicWaker::new(),
pub rx_waker: AtomicWaker, tx_waker: AtomicWaker::new(),
pub tx_waker: AtomicWaker, stop_waker: AtomicWaker::new(),
pub stop_waker: AtomicWaker,
}
impl State {
pub const fn new() -> Self {
Self {
started: AtomicBool::new(false),
rx_waker: AtomicWaker::new(),
tx_waker: AtomicWaker::new(),
stop_waker: AtomicWaker::new(),
}
} }
} }
}
pub trait Instance {
fn regs() -> &'static crate::pac::i2s::RegisterBlock; pub(crate) trait SealedInstance {
fn state() -> &'static State; fn regs() -> &'static crate::pac::i2s::RegisterBlock;
} fn state() -> &'static State;
} }
/// I2S peripheral instance. /// I2S peripheral instance.
pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static + Send { #[allow(private_bounds)]
pub trait Instance: Peripheral<P = Self> + SealedInstance + 'static + Send {
/// Interrupt for this peripheral. /// Interrupt for this peripheral.
type Interrupt: interrupt::typelevel::Interrupt; type Interrupt: interrupt::typelevel::Interrupt;
} }
macro_rules! impl_i2s { macro_rules! impl_i2s {
($type:ident, $pac_type:ident, $irq:ident) => { ($type:ident, $pac_type:ident, $irq:ident) => {
impl crate::i2s::sealed::Instance for peripherals::$type { impl crate::i2s::SealedInstance for peripherals::$type {
fn regs() -> &'static crate::pac::i2s::RegisterBlock { fn regs() -> &'static crate::pac::i2s::RegisterBlock {
unsafe { &*pac::$pac_type::ptr() } unsafe { &*pac::$pac_type::ptr() }
} }
fn state() -> &'static crate::i2s::sealed::State { fn state() -> &'static crate::i2s::State {
static STATE: crate::i2s::sealed::State = crate::i2s::sealed::State::new(); static STATE: crate::i2s::State = crate::i2s::State::new();
&STATE &STATE
} }
} }

View file

@ -9,11 +9,11 @@ use core::task::Poll;
use embassy_hal_internal::drop::OnDrop; use embassy_hal_internal::drop::OnDrop;
use embassy_hal_internal::{into_ref, PeripheralRef}; use embassy_hal_internal::{into_ref, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker;
use fixed::types::I7F1; use fixed::types::I7F1;
use crate::chip::EASY_DMA_SIZE; use crate::chip::EASY_DMA_SIZE;
use crate::gpio::sealed::Pin; use crate::gpio::{AnyPin, Pin as GpioPin, SealedPin};
use crate::gpio::{AnyPin, Pin as GpioPin};
use crate::interrupt::typelevel::Interrupt; use crate::interrupt::typelevel::Interrupt;
use crate::pac::pdm::mode::{EDGE_A, OPERATION_A}; use crate::pac::pdm::mode::{EDGE_A, OPERATION_A};
pub use crate::pac::pdm::pdmclkctrl::FREQ_A as Frequency; pub use crate::pac::pdm::pdmclkctrl::FREQ_A as Frequency;
@ -451,42 +451,39 @@ impl<'d, T: Instance> Drop for Pdm<'d, T> {
} }
} }
pub(crate) mod sealed { /// Peripheral static state
use embassy_sync::waitqueue::AtomicWaker; pub(crate) struct State {
waker: AtomicWaker,
}
/// Peripheral static state impl State {
pub struct State { pub(crate) const fn new() -> Self {
pub waker: AtomicWaker, Self {
} waker: AtomicWaker::new(),
impl State {
pub const fn new() -> Self {
Self {
waker: AtomicWaker::new(),
}
} }
} }
}
pub trait Instance {
fn regs() -> &'static crate::pac::pdm::RegisterBlock; pub(crate) trait SealedInstance {
fn state() -> &'static State; fn regs() -> &'static crate::pac::pdm::RegisterBlock;
} fn state() -> &'static State;
} }
/// PDM peripheral instance /// PDM peripheral instance
pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static + Send { #[allow(private_bounds)]
pub trait Instance: Peripheral<P = Self> + SealedInstance + 'static + Send {
/// Interrupt for this peripheral /// Interrupt for this peripheral
type Interrupt: interrupt::typelevel::Interrupt; type Interrupt: interrupt::typelevel::Interrupt;
} }
macro_rules! impl_pdm { macro_rules! impl_pdm {
($type:ident, $pac_type:ident, $irq:ident) => { ($type:ident, $pac_type:ident, $irq:ident) => {
impl crate::pdm::sealed::Instance for peripherals::$type { impl crate::pdm::SealedInstance for peripherals::$type {
fn regs() -> &'static crate::pac::pdm::RegisterBlock { fn regs() -> &'static crate::pac::pdm::RegisterBlock {
unsafe { &*pac::$pac_type::ptr() } unsafe { &*pac::$pac_type::ptr() }
} }
fn state() -> &'static crate::pdm::sealed::State { fn state() -> &'static crate::pdm::State {
static STATE: crate::pdm::sealed::State = crate::pdm::sealed::State::new(); static STATE: crate::pdm::State = crate::pdm::State::new();
&STATE &STATE
} }
} }

View file

@ -210,13 +210,12 @@ unsafe impl Send for Event<'_> {}
// ====================== // ======================
// traits // traits
pub(crate) mod sealed { pub(crate) trait SealedChannel {}
pub trait Channel {} pub(crate) trait SealedGroup {}
pub trait Group {}
}
/// Interface for PPI channels. /// Interface for PPI channels.
pub trait Channel: sealed::Channel + Peripheral<P = Self> + Sized + 'static { #[allow(private_bounds)]
pub trait Channel: SealedChannel + Peripheral<P = Self> + Sized + 'static {
/// Returns the number of the channel /// Returns the number of the channel
fn number(&self) -> usize; fn number(&self) -> usize;
} }
@ -234,7 +233,8 @@ pub trait StaticChannel: Channel + Into<AnyStaticChannel> {
} }
/// Interface for a group of PPI channels. /// Interface for a group of PPI channels.
pub trait Group: sealed::Group + Peripheral<P = Self> + Into<AnyGroup> + Sized + 'static { #[allow(private_bounds)]
pub trait Group: SealedGroup + Peripheral<P = Self> + Into<AnyGroup> + Sized + 'static {
/// Returns the number of the group. /// Returns the number of the group.
fn number(&self) -> usize; fn number(&self) -> usize;
/// Convert into a type erased group. /// Convert into a type erased group.
@ -254,7 +254,7 @@ pub struct AnyStaticChannel {
pub(crate) number: u8, pub(crate) number: u8,
} }
impl_peripheral!(AnyStaticChannel); impl_peripheral!(AnyStaticChannel);
impl sealed::Channel for AnyStaticChannel {} impl SealedChannel for AnyStaticChannel {}
impl Channel for AnyStaticChannel { impl Channel for AnyStaticChannel {
fn number(&self) -> usize { fn number(&self) -> usize {
self.number as usize self.number as usize
@ -272,7 +272,7 @@ pub struct AnyConfigurableChannel {
pub(crate) number: u8, pub(crate) number: u8,
} }
impl_peripheral!(AnyConfigurableChannel); impl_peripheral!(AnyConfigurableChannel);
impl sealed::Channel for AnyConfigurableChannel {} impl SealedChannel for AnyConfigurableChannel {}
impl Channel for AnyConfigurableChannel { impl Channel for AnyConfigurableChannel {
fn number(&self) -> usize { fn number(&self) -> usize {
self.number as usize self.number as usize
@ -287,7 +287,7 @@ impl ConfigurableChannel for AnyConfigurableChannel {
#[cfg(not(feature = "nrf51"))] #[cfg(not(feature = "nrf51"))]
macro_rules! impl_ppi_channel { macro_rules! impl_ppi_channel {
($type:ident, $number:expr) => { ($type:ident, $number:expr) => {
impl crate::ppi::sealed::Channel for peripherals::$type {} impl crate::ppi::SealedChannel for peripherals::$type {}
impl crate::ppi::Channel for peripherals::$type { impl crate::ppi::Channel for peripherals::$type {
fn number(&self) -> usize { fn number(&self) -> usize {
$number $number
@ -338,7 +338,7 @@ pub struct AnyGroup {
number: u8, number: u8,
} }
impl_peripheral!(AnyGroup); impl_peripheral!(AnyGroup);
impl sealed::Group for AnyGroup {} impl SealedGroup for AnyGroup {}
impl Group for AnyGroup { impl Group for AnyGroup {
fn number(&self) -> usize { fn number(&self) -> usize {
self.number as usize self.number as usize
@ -347,7 +347,7 @@ impl Group for AnyGroup {
macro_rules! impl_group { macro_rules! impl_group {
($type:ident, $number:expr) => { ($type:ident, $number:expr) => {
impl sealed::Group for peripherals::$type {} impl SealedGroup for peripherals::$type {}
impl Group for peripherals::$type { impl Group for peripherals::$type {
fn number(&self) -> usize { fn number(&self) -> usize {
$number $number

View file

@ -6,8 +6,7 @@ use core::sync::atomic::{compiler_fence, Ordering};
use embassy_hal_internal::{into_ref, PeripheralRef}; use embassy_hal_internal::{into_ref, PeripheralRef};
use crate::gpio::sealed::Pin as _; use crate::gpio::{AnyPin, Pin as GpioPin, PselBits, SealedPin as _};
use crate::gpio::{AnyPin, Pin as GpioPin, PselBits};
use crate::ppi::{Event, Task}; use crate::ppi::{Event, Task};
use crate::util::slice_in_ram_or; use crate::util::slice_in_ram_or;
use crate::{interrupt, pac, Peripheral}; use crate::{interrupt, pac, Peripheral};
@ -847,23 +846,20 @@ impl<'a, T: Instance> Drop for SimplePwm<'a, T> {
} }
} }
pub(crate) mod sealed { pub(crate) trait SealedInstance {
use super::*; fn regs() -> &'static pac::pwm0::RegisterBlock;
pub trait Instance {
fn regs() -> &'static pac::pwm0::RegisterBlock;
}
} }
/// PWM peripheral instance. /// PWM peripheral instance.
pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static { #[allow(private_bounds)]
pub trait Instance: Peripheral<P = Self> + SealedInstance + 'static {
/// Interrupt for this peripheral. /// Interrupt for this peripheral.
type Interrupt: interrupt::typelevel::Interrupt; type Interrupt: interrupt::typelevel::Interrupt;
} }
macro_rules! impl_pwm { macro_rules! impl_pwm {
($type:ident, $pac_type:ident, $irq:ident) => { ($type:ident, $pac_type:ident, $irq:ident) => {
impl crate::pwm::sealed::Instance for peripherals::$type { impl crate::pwm::SealedInstance for peripherals::$type {
fn regs() -> &'static pac::pwm0::RegisterBlock { fn regs() -> &'static pac::pwm0::RegisterBlock {
unsafe { &*pac::$pac_type::ptr() } unsafe { &*pac::$pac_type::ptr() }
} }

View file

@ -7,9 +7,9 @@ use core::marker::PhantomData;
use core::task::Poll; use core::task::Poll;
use embassy_hal_internal::{into_ref, PeripheralRef}; use embassy_hal_internal::{into_ref, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker;
use crate::gpio::sealed::Pin as _; use crate::gpio::{AnyPin, Pin as GpioPin, SealedPin as _};
use crate::gpio::{AnyPin, Pin as GpioPin};
use crate::interrupt::typelevel::Interrupt; use crate::interrupt::typelevel::Interrupt;
use crate::{interrupt, Peripheral}; use crate::{interrupt, Peripheral};
@ -245,42 +245,39 @@ pub enum LedPolarity {
ActiveLow, ActiveLow,
} }
pub(crate) mod sealed { /// Peripheral static state
use embassy_sync::waitqueue::AtomicWaker; pub(crate) struct State {
waker: AtomicWaker,
}
/// Peripheral static state impl State {
pub struct State { pub(crate) const fn new() -> Self {
pub waker: AtomicWaker, Self {
} waker: AtomicWaker::new(),
impl State {
pub const fn new() -> Self {
Self {
waker: AtomicWaker::new(),
}
} }
} }
}
pub trait Instance {
fn regs() -> &'static crate::pac::qdec::RegisterBlock; pub(crate) trait SealedInstance {
fn state() -> &'static State; fn regs() -> &'static crate::pac::qdec::RegisterBlock;
} fn state() -> &'static State;
} }
/// qdec peripheral instance. /// qdec peripheral instance.
pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static + Send { #[allow(private_bounds)]
pub trait Instance: Peripheral<P = Self> + SealedInstance + 'static + Send {
/// Interrupt for this peripheral. /// Interrupt for this peripheral.
type Interrupt: interrupt::typelevel::Interrupt; type Interrupt: interrupt::typelevel::Interrupt;
} }
macro_rules! impl_qdec { macro_rules! impl_qdec {
($type:ident, $pac_type:ident, $irq:ident) => { ($type:ident, $pac_type:ident, $irq:ident) => {
impl crate::qdec::sealed::Instance for peripherals::$type { impl crate::qdec::SealedInstance for peripherals::$type {
fn regs() -> &'static crate::pac::qdec::RegisterBlock { fn regs() -> &'static crate::pac::qdec::RegisterBlock {
unsafe { &*pac::$pac_type::ptr() } unsafe { &*pac::$pac_type::ptr() }
} }
fn state() -> &'static crate::qdec::sealed::State { fn state() -> &'static crate::qdec::State {
static STATE: crate::qdec::sealed::State = crate::qdec::sealed::State::new(); static STATE: crate::qdec::State = crate::qdec::State::new();
&STATE &STATE
} }
} }

View file

@ -9,6 +9,7 @@ use core::task::Poll;
use embassy_hal_internal::drop::OnDrop; use embassy_hal_internal::drop::OnDrop;
use embassy_hal_internal::{into_ref, PeripheralRef}; use embassy_hal_internal::{into_ref, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker;
use embedded_storage::nor_flash::{ErrorType, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash}; use embedded_storage::nor_flash::{ErrorType, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash};
use crate::gpio::{self, Pin as GpioPin}; use crate::gpio::{self, Pin as GpioPin};
@ -652,42 +653,39 @@ mod _eh1 {
impl<'d, T: Instance> embedded_storage_async::nor_flash::MultiwriteNorFlash for Qspi<'d, T> {} impl<'d, T: Instance> embedded_storage_async::nor_flash::MultiwriteNorFlash for Qspi<'d, T> {}
} }
pub(crate) mod sealed { /// Peripheral static state
use embassy_sync::waitqueue::AtomicWaker; pub(crate) struct State {
waker: AtomicWaker,
}
/// Peripheral static state impl State {
pub struct State { pub(crate) const fn new() -> Self {
pub waker: AtomicWaker, Self {
} waker: AtomicWaker::new(),
impl State {
pub const fn new() -> Self {
Self {
waker: AtomicWaker::new(),
}
} }
} }
}
pub trait Instance {
fn regs() -> &'static crate::pac::qspi::RegisterBlock; pub(crate) trait SealedInstance {
fn state() -> &'static State; fn regs() -> &'static crate::pac::qspi::RegisterBlock;
} fn state() -> &'static State;
} }
/// QSPI peripheral instance. /// QSPI peripheral instance.
pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static + Send { #[allow(private_bounds)]
pub trait Instance: Peripheral<P = Self> + SealedInstance + 'static + Send {
/// Interrupt for this peripheral. /// Interrupt for this peripheral.
type Interrupt: interrupt::typelevel::Interrupt; type Interrupt: interrupt::typelevel::Interrupt;
} }
macro_rules! impl_qspi { macro_rules! impl_qspi {
($type:ident, $pac_type:ident, $irq:ident) => { ($type:ident, $pac_type:ident, $irq:ident) => {
impl crate::qspi::sealed::Instance for peripherals::$type { impl crate::qspi::SealedInstance for peripherals::$type {
fn regs() -> &'static crate::pac::qspi::RegisterBlock { fn regs() -> &'static crate::pac::qspi::RegisterBlock {
unsafe { &*pac::$pac_type::ptr() } unsafe { &*pac::$pac_type::ptr() }
} }
fn state() -> &'static crate::qspi::sealed::State { fn state() -> &'static crate::qspi::State {
static STATE: crate::qspi::sealed::State = crate::qspi::sealed::State::new(); static STATE: crate::qspi::State = crate::qspi::State::new();
&STATE &STATE
} }
} }

View file

@ -19,6 +19,7 @@ pub mod ieee802154;
use core::marker::PhantomData; use core::marker::PhantomData;
use embassy_sync::waitqueue::AtomicWaker;
use pac::radio::state::STATE_A as RadioState; use pac::radio::state::STATE_A as RadioState;
pub use pac::radio::txpower::TXPOWER_A as TxPower; pub use pac::radio::txpower::TXPOWER_A as TxPower;
@ -56,36 +57,32 @@ impl<T: Instance> interrupt::typelevel::Handler<T::Interrupt> for InterruptHandl
} }
} }
pub(crate) mod sealed { pub(crate) struct State {
use embassy_sync::waitqueue::AtomicWaker; /// end packet transmission or reception
event_waker: AtomicWaker,
pub struct State { }
/// end packet transmission or reception impl State {
pub event_waker: AtomicWaker, pub(crate) const fn new() -> Self {
} Self {
impl State { event_waker: AtomicWaker::new(),
pub const fn new() -> Self {
Self {
event_waker: AtomicWaker::new(),
}
} }
} }
}
pub trait Instance { pub(crate) trait SealedInstance {
fn regs() -> &'static crate::pac::radio::RegisterBlock; fn regs() -> &'static crate::pac::radio::RegisterBlock;
fn state() -> &'static State; fn state() -> &'static State;
}
} }
macro_rules! impl_radio { macro_rules! impl_radio {
($type:ident, $pac_type:ident, $irq:ident) => { ($type:ident, $pac_type:ident, $irq:ident) => {
impl crate::radio::sealed::Instance for peripherals::$type { impl crate::radio::SealedInstance for peripherals::$type {
fn regs() -> &'static pac::radio::RegisterBlock { fn regs() -> &'static pac::radio::RegisterBlock {
unsafe { &*pac::$pac_type::ptr() } unsafe { &*pac::$pac_type::ptr() }
} }
fn state() -> &'static crate::radio::sealed::State { fn state() -> &'static crate::radio::State {
static STATE: crate::radio::sealed::State = crate::radio::sealed::State::new(); static STATE: crate::radio::State = crate::radio::State::new();
&STATE &STATE
} }
} }
@ -96,7 +93,8 @@ macro_rules! impl_radio {
} }
/// Radio peripheral instance. /// Radio peripheral instance.
pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static + Send { #[allow(private_bounds)]
pub trait Instance: Peripheral<P = Self> + SealedInstance + 'static + Send {
/// Interrupt for this peripheral. /// Interrupt for this peripheral.
type Interrupt: interrupt::typelevel::Interrupt; type Interrupt: interrupt::typelevel::Interrupt;
} }

View file

@ -2,13 +2,16 @@
#![macro_use] #![macro_use]
use core::cell::{RefCell, RefMut};
use core::future::poll_fn; use core::future::poll_fn;
use core::marker::PhantomData; use core::marker::PhantomData;
use core::ptr; use core::ptr;
use core::task::Poll; use core::task::Poll;
use critical_section::{CriticalSection, Mutex};
use embassy_hal_internal::drop::OnDrop; use embassy_hal_internal::drop::OnDrop;
use embassy_hal_internal::{into_ref, PeripheralRef}; use embassy_hal_internal::{into_ref, PeripheralRef};
use embassy_sync::waitqueue::WakerRegistration;
use crate::interrupt::typelevel::Interrupt; use crate::interrupt::typelevel::Interrupt;
use crate::{interrupt, Peripheral}; use crate::{interrupt, Peripheral};
@ -205,73 +208,61 @@ impl<'d, T: Instance> rand_core::RngCore for Rng<'d, T> {
impl<'d, T: Instance> rand_core::CryptoRng for Rng<'d, T> {} impl<'d, T: Instance> rand_core::CryptoRng for Rng<'d, T> {}
pub(crate) mod sealed { /// Peripheral static state
use core::cell::{Ref, RefCell, RefMut}; pub(crate) struct State {
inner: Mutex<RefCell<InnerState>>,
}
use critical_section::{CriticalSection, Mutex}; struct InnerState {
use embassy_sync::waitqueue::WakerRegistration; ptr: *mut u8,
end: *mut u8,
waker: WakerRegistration,
}
use super::*; unsafe impl Send for InnerState {}
/// Peripheral static state impl State {
pub struct State { pub(crate) const fn new() -> Self {
inner: Mutex<RefCell<InnerState>>, Self {
} inner: Mutex::new(RefCell::new(InnerState::new())),
pub struct InnerState {
pub ptr: *mut u8,
pub end: *mut u8,
pub waker: WakerRegistration,
}
unsafe impl Send for InnerState {}
impl State {
pub const fn new() -> Self {
Self {
inner: Mutex::new(RefCell::new(InnerState::new())),
}
}
pub fn borrow<'cs>(&'cs self, cs: CriticalSection<'cs>) -> Ref<'cs, InnerState> {
self.inner.borrow(cs).borrow()
}
pub fn borrow_mut<'cs>(&'cs self, cs: CriticalSection<'cs>) -> RefMut<'cs, InnerState> {
self.inner.borrow(cs).borrow_mut()
} }
} }
impl InnerState { fn borrow_mut<'cs>(&'cs self, cs: CriticalSection<'cs>) -> RefMut<'cs, InnerState> {
pub const fn new() -> Self { self.inner.borrow(cs).borrow_mut()
Self {
ptr: ptr::null_mut(),
end: ptr::null_mut(),
waker: WakerRegistration::new(),
}
}
}
pub trait Instance {
fn regs() -> &'static crate::pac::rng::RegisterBlock;
fn state() -> &'static State;
} }
} }
impl InnerState {
const fn new() -> Self {
Self {
ptr: ptr::null_mut(),
end: ptr::null_mut(),
waker: WakerRegistration::new(),
}
}
}
pub(crate) trait SealedInstance {
fn regs() -> &'static crate::pac::rng::RegisterBlock;
fn state() -> &'static State;
}
/// RNG peripheral instance. /// RNG peripheral instance.
pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static + Send { #[allow(private_bounds)]
pub trait Instance: Peripheral<P = Self> + SealedInstance + 'static + Send {
/// Interrupt for this peripheral. /// Interrupt for this peripheral.
type Interrupt: interrupt::typelevel::Interrupt; type Interrupt: interrupt::typelevel::Interrupt;
} }
macro_rules! impl_rng { macro_rules! impl_rng {
($type:ident, $pac_type:ident, $irq:ident) => { ($type:ident, $pac_type:ident, $irq:ident) => {
impl crate::rng::sealed::Instance for peripherals::$type { impl crate::rng::SealedInstance for peripherals::$type {
fn regs() -> &'static crate::pac::rng::RegisterBlock { fn regs() -> &'static crate::pac::rng::RegisterBlock {
unsafe { &*pac::$pac_type::ptr() } unsafe { &*pac::$pac_type::ptr() }
} }
fn state() -> &'static crate::rng::sealed::State { fn state() -> &'static crate::rng::State {
static STATE: crate::rng::sealed::State = crate::rng::sealed::State::new(); static STATE: crate::rng::State = crate::rng::State::new();
&STATE &STATE
} }
} }

View file

@ -16,7 +16,6 @@ pub(crate) use saadc::ch::pselp::PSELP_A as InputChannel;
use saadc::oversample::OVERSAMPLE_A; use saadc::oversample::OVERSAMPLE_A;
use saadc::resolution::VAL_A; use saadc::resolution::VAL_A;
use self::sealed::Input as _;
use crate::interrupt::InterruptExt; use crate::interrupt::InterruptExt;
use crate::ppi::{ConfigurableChannel, Event, Ppi, Task}; use crate::ppi::{ConfigurableChannel, Event, Ppi, Task};
use crate::timer::{Frequency, Instance as TimerInstance, Timer}; use crate::timer::{Frequency, Instance as TimerInstance, Timer};
@ -662,16 +661,13 @@ pub enum Resolution {
_14BIT = 3, _14BIT = 3,
} }
pub(crate) mod sealed { pub(crate) trait SealedInput {
use super::*; fn channel(&self) -> InputChannel;
pub trait Input {
fn channel(&self) -> InputChannel;
}
} }
/// An input that can be used as either or negative end of a ADC differential in the SAADC periperhal. /// An input that can be used as either or negative end of a ADC differential in the SAADC periperhal.
pub trait Input: sealed::Input + Into<AnyInput> + Peripheral<P = Self> + Sized + 'static { #[allow(private_bounds)]
pub trait Input: SealedInput + Into<AnyInput> + Peripheral<P = Self> + Sized + 'static {
/// Convert this SAADC input to a type-erased `AnyInput`. /// Convert this SAADC input to a type-erased `AnyInput`.
/// ///
/// This allows using several inputs in situations that might require /// This allows using several inputs in situations that might require
@ -693,7 +689,7 @@ pub struct AnyInput {
impl_peripheral!(AnyInput); impl_peripheral!(AnyInput);
impl sealed::Input for AnyInput { impl SealedInput for AnyInput {
fn channel(&self) -> InputChannel { fn channel(&self) -> InputChannel {
self.channel self.channel
} }
@ -706,7 +702,7 @@ macro_rules! impl_saadc_input {
impl_saadc_input!(@local, crate::peripherals::$pin, $ch); impl_saadc_input!(@local, crate::peripherals::$pin, $ch);
}; };
(@local, $pin:ty, $ch:ident) => { (@local, $pin:ty, $ch:ident) => {
impl crate::saadc::sealed::Input for $pin { impl crate::saadc::SealedInput for $pin {
fn channel(&self) -> crate::saadc::InputChannel { fn channel(&self) -> crate::saadc::InputChannel {
crate::saadc::InputChannel::$ch crate::saadc::InputChannel::$ch
} }

View file

@ -4,18 +4,20 @@
use core::future::poll_fn; use core::future::poll_fn;
use core::marker::PhantomData; use core::marker::PhantomData;
#[cfg(feature = "_nrf52832_anomaly_109")]
use core::sync::atomic::AtomicU8;
use core::sync::atomic::{compiler_fence, Ordering}; use core::sync::atomic::{compiler_fence, Ordering};
use core::task::Poll; use core::task::Poll;
use embassy_embedded_hal::SetConfig; use embassy_embedded_hal::SetConfig;
use embassy_hal_internal::{into_ref, PeripheralRef}; use embassy_hal_internal::{into_ref, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker;
pub use embedded_hal_02::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; pub use embedded_hal_02::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
pub use pac::spim0::config::ORDER_A as BitOrder; pub use pac::spim0::config::ORDER_A as BitOrder;
pub use pac::spim0::frequency::FREQUENCY_A as Frequency; pub use pac::spim0::frequency::FREQUENCY_A as Frequency;
use crate::chip::{EASY_DMA_SIZE, FORCE_COPY_BUFFER_SIZE}; use crate::chip::{EASY_DMA_SIZE, FORCE_COPY_BUFFER_SIZE};
use crate::gpio::sealed::Pin as _; use crate::gpio::{self, convert_drive, AnyPin, OutputDrive, Pin as GpioPin, PselBits, SealedPin as _};
use crate::gpio::{self, convert_drive, AnyPin, OutputDrive, Pin as GpioPin, PselBits};
use crate::interrupt::typelevel::Interrupt; use crate::interrupt::typelevel::Interrupt;
use crate::util::{slice_in_ram_or, slice_ptr_len, slice_ptr_parts, slice_ptr_parts_mut}; use crate::util::{slice_in_ram_or, slice_ptr_len, slice_ptr_parts, slice_ptr_parts_mut};
use crate::{interrupt, pac, Peripheral}; use crate::{interrupt, pac, Peripheral};
@ -487,54 +489,46 @@ impl<'d, T: Instance> Drop for Spim<'d, T> {
} }
} }
pub(crate) mod sealed { pub(crate) struct State {
waker: AtomicWaker,
#[cfg(feature = "_nrf52832_anomaly_109")] #[cfg(feature = "_nrf52832_anomaly_109")]
use core::sync::atomic::AtomicU8; rx: AtomicU8,
#[cfg(feature = "_nrf52832_anomaly_109")]
tx: AtomicU8,
}
use embassy_sync::waitqueue::AtomicWaker; impl State {
pub(crate) const fn new() -> Self {
use super::*; Self {
waker: AtomicWaker::new(),
pub struct State { #[cfg(feature = "_nrf52832_anomaly_109")]
pub waker: AtomicWaker, rx: AtomicU8::new(0),
#[cfg(feature = "_nrf52832_anomaly_109")] #[cfg(feature = "_nrf52832_anomaly_109")]
pub rx: AtomicU8, tx: AtomicU8::new(0),
#[cfg(feature = "_nrf52832_anomaly_109")]
pub tx: AtomicU8,
}
impl State {
pub const fn new() -> Self {
Self {
waker: AtomicWaker::new(),
#[cfg(feature = "_nrf52832_anomaly_109")]
rx: AtomicU8::new(0),
#[cfg(feature = "_nrf52832_anomaly_109")]
tx: AtomicU8::new(0),
}
} }
} }
}
pub trait Instance {
fn regs() -> &'static pac::spim0::RegisterBlock; pub(crate) trait SealedInstance {
fn state() -> &'static State; fn regs() -> &'static pac::spim0::RegisterBlock;
} fn state() -> &'static State;
} }
/// SPIM peripheral instance /// SPIM peripheral instance
pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static { #[allow(private_bounds)]
pub trait Instance: Peripheral<P = Self> + SealedInstance + 'static {
/// Interrupt for this peripheral. /// Interrupt for this peripheral.
type Interrupt: interrupt::typelevel::Interrupt; type Interrupt: interrupt::typelevel::Interrupt;
} }
macro_rules! impl_spim { macro_rules! impl_spim {
($type:ident, $pac_type:ident, $irq:ident) => { ($type:ident, $pac_type:ident, $irq:ident) => {
impl crate::spim::sealed::Instance for peripherals::$type { impl crate::spim::SealedInstance for peripherals::$type {
fn regs() -> &'static pac::spim0::RegisterBlock { fn regs() -> &'static pac::spim0::RegisterBlock {
unsafe { &*pac::$pac_type::ptr() } unsafe { &*pac::$pac_type::ptr() }
} }
fn state() -> &'static crate::spim::sealed::State { fn state() -> &'static crate::spim::State {
static STATE: crate::spim::sealed::State = crate::spim::sealed::State::new(); static STATE: crate::spim::State = crate::spim::State::new();
&STATE &STATE
} }
} }

View file

@ -8,12 +8,12 @@ use core::task::Poll;
use embassy_embedded_hal::SetConfig; use embassy_embedded_hal::SetConfig;
use embassy_hal_internal::{into_ref, PeripheralRef}; use embassy_hal_internal::{into_ref, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker;
pub use embedded_hal_02::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; pub use embedded_hal_02::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
pub use pac::spis0::config::ORDER_A as BitOrder; pub use pac::spis0::config::ORDER_A as BitOrder;
use crate::chip::{EASY_DMA_SIZE, FORCE_COPY_BUFFER_SIZE}; use crate::chip::{EASY_DMA_SIZE, FORCE_COPY_BUFFER_SIZE};
use crate::gpio::sealed::Pin as _; use crate::gpio::{self, AnyPin, Pin as GpioPin, SealedPin as _};
use crate::gpio::{self, AnyPin, Pin as GpioPin};
use crate::interrupt::typelevel::Interrupt; use crate::interrupt::typelevel::Interrupt;
use crate::util::{slice_in_ram_or, slice_ptr_parts, slice_ptr_parts_mut}; use crate::util::{slice_in_ram_or, slice_ptr_parts, slice_ptr_parts_mut};
use crate::{interrupt, pac, Peripheral}; use crate::{interrupt, pac, Peripheral};
@ -456,43 +456,38 @@ impl<'d, T: Instance> Drop for Spis<'d, T> {
} }
} }
pub(crate) mod sealed { pub(crate) struct State {
use embassy_sync::waitqueue::AtomicWaker; waker: AtomicWaker,
}
use super::*; impl State {
pub(crate) const fn new() -> Self {
pub struct State { Self {
pub waker: AtomicWaker, waker: AtomicWaker::new(),
}
impl State {
pub const fn new() -> Self {
Self {
waker: AtomicWaker::new(),
}
} }
} }
}
pub trait Instance {
fn regs() -> &'static pac::spis0::RegisterBlock; pub(crate) trait SealedInstance {
fn state() -> &'static State; fn regs() -> &'static pac::spis0::RegisterBlock;
} fn state() -> &'static State;
} }
/// SPIS peripheral instance /// SPIS peripheral instance
pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static { #[allow(private_bounds)]
pub trait Instance: Peripheral<P = Self> + SealedInstance + 'static {
/// Interrupt for this peripheral. /// Interrupt for this peripheral.
type Interrupt: interrupt::typelevel::Interrupt; type Interrupt: interrupt::typelevel::Interrupt;
} }
macro_rules! impl_spis { macro_rules! impl_spis {
($type:ident, $pac_type:ident, $irq:ident) => { ($type:ident, $pac_type:ident, $irq:ident) => {
impl crate::spis::sealed::Instance for peripherals::$type { impl crate::spis::SealedInstance for peripherals::$type {
fn regs() -> &'static pac::spis0::RegisterBlock { fn regs() -> &'static pac::spis0::RegisterBlock {
unsafe { &*pac::$pac_type::ptr() } unsafe { &*pac::$pac_type::ptr() }
} }
fn state() -> &'static crate::spis::sealed::State { fn state() -> &'static crate::spis::State {
static STATE: crate::spis::sealed::State = crate::spis::sealed::State::new(); static STATE: crate::spis::State = crate::spis::State::new();
&STATE &STATE
} }
} }

View file

@ -11,30 +11,25 @@ use embassy_hal_internal::{into_ref, PeripheralRef};
use crate::ppi::{Event, Task}; use crate::ppi::{Event, Task};
use crate::{pac, Peripheral}; use crate::{pac, Peripheral};
pub(crate) mod sealed { pub(crate) trait SealedInstance {
/// The number of CC registers this instance has.
use super::*; const CCS: usize;
fn regs() -> &'static pac::timer0::RegisterBlock;
pub trait Instance {
/// The number of CC registers this instance has.
const CCS: usize;
fn regs() -> &'static pac::timer0::RegisterBlock;
}
pub trait ExtendedInstance {}
} }
/// Basic Timer instance. /// Basic Timer instance.
pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static + Send { #[allow(private_bounds)]
pub trait Instance: Peripheral<P = Self> + SealedInstance + 'static + Send {
/// Interrupt for this peripheral. /// Interrupt for this peripheral.
type Interrupt: crate::interrupt::typelevel::Interrupt; type Interrupt: crate::interrupt::typelevel::Interrupt;
} }
/// Extended timer instance. /// Extended timer instance.
pub trait ExtendedInstance: Instance + sealed::ExtendedInstance {} pub trait ExtendedInstance: Instance {}
macro_rules! impl_timer { macro_rules! impl_timer {
($type:ident, $pac_type:ident, $irq:ident, $ccs:literal) => { ($type:ident, $pac_type:ident, $irq:ident, $ccs:literal) => {
impl crate::timer::sealed::Instance for peripherals::$type { impl crate::timer::SealedInstance for peripherals::$type {
const CCS: usize = $ccs; const CCS: usize = $ccs;
fn regs() -> &'static pac::timer0::RegisterBlock { fn regs() -> &'static pac::timer0::RegisterBlock {
unsafe { &*(pac::$pac_type::ptr() as *const pac::timer0::RegisterBlock) } unsafe { &*(pac::$pac_type::ptr() as *const pac::timer0::RegisterBlock) }
@ -49,7 +44,6 @@ macro_rules! impl_timer {
}; };
($type:ident, $pac_type:ident, $irq:ident, extended) => { ($type:ident, $pac_type:ident, $irq:ident, extended) => {
impl_timer!($type, $pac_type, $irq, 6); impl_timer!($type, $pac_type, $irq, 6);
impl crate::timer::sealed::ExtendedInstance for peripherals::$type {}
impl crate::timer::ExtendedInstance for peripherals::$type {} impl crate::timer::ExtendedInstance for peripherals::$type {}
}; };
} }

View file

@ -727,41 +727,38 @@ impl<'a, T: Instance> Drop for Twim<'a, T> {
} }
} }
pub(crate) mod sealed { pub(crate) struct State {
use super::*; end_waker: AtomicWaker,
}
pub struct State { impl State {
pub end_waker: AtomicWaker, pub(crate) const fn new() -> Self {
} Self {
end_waker: AtomicWaker::new(),
impl State {
pub const fn new() -> Self {
Self {
end_waker: AtomicWaker::new(),
}
} }
} }
}
pub trait Instance {
fn regs() -> &'static pac::twim0::RegisterBlock; pub(crate) trait SealedInstance {
fn state() -> &'static State; fn regs() -> &'static pac::twim0::RegisterBlock;
} fn state() -> &'static State;
} }
/// TWIM peripheral instance. /// TWIM peripheral instance.
pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static { #[allow(private_bounds)]
pub trait Instance: Peripheral<P = Self> + SealedInstance + 'static {
/// Interrupt for this peripheral. /// Interrupt for this peripheral.
type Interrupt: interrupt::typelevel::Interrupt; type Interrupt: interrupt::typelevel::Interrupt;
} }
macro_rules! impl_twim { macro_rules! impl_twim {
($type:ident, $pac_type:ident, $irq:ident) => { ($type:ident, $pac_type:ident, $irq:ident) => {
impl crate::twim::sealed::Instance for peripherals::$type { impl crate::twim::SealedInstance for peripherals::$type {
fn regs() -> &'static pac::twim0::RegisterBlock { fn regs() -> &'static pac::twim0::RegisterBlock {
unsafe { &*pac::$pac_type::ptr() } unsafe { &*pac::$pac_type::ptr() }
} }
fn state() -> &'static crate::twim::sealed::State { fn state() -> &'static crate::twim::State {
static STATE: crate::twim::sealed::State = crate::twim::sealed::State::new(); static STATE: crate::twim::State = crate::twim::State::new();
&STATE &STATE
} }
} }

View file

@ -754,41 +754,38 @@ impl<'a, T: Instance> Drop for Twis<'a, T> {
} }
} }
pub(crate) mod sealed { pub(crate) struct State {
use super::*; waker: AtomicWaker,
}
pub struct State { impl State {
pub waker: AtomicWaker, pub(crate) const fn new() -> Self {
} Self {
waker: AtomicWaker::new(),
impl State {
pub const fn new() -> Self {
Self {
waker: AtomicWaker::new(),
}
} }
} }
}
pub trait Instance {
fn regs() -> &'static pac::twis0::RegisterBlock; pub(crate) trait SealedInstance {
fn state() -> &'static State; fn regs() -> &'static pac::twis0::RegisterBlock;
} fn state() -> &'static State;
} }
/// TWIS peripheral instance. /// TWIS peripheral instance.
pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static { #[allow(private_bounds)]
pub trait Instance: Peripheral<P = Self> + SealedInstance + 'static {
/// Interrupt for this peripheral. /// Interrupt for this peripheral.
type Interrupt: interrupt::typelevel::Interrupt; type Interrupt: interrupt::typelevel::Interrupt;
} }
macro_rules! impl_twis { macro_rules! impl_twis {
($type:ident, $pac_type:ident, $irq:ident) => { ($type:ident, $pac_type:ident, $irq:ident) => {
impl crate::twis::sealed::Instance for peripherals::$type { impl crate::twis::SealedInstance for peripherals::$type {
fn regs() -> &'static pac::twis0::RegisterBlock { fn regs() -> &'static pac::twis0::RegisterBlock {
unsafe { &*pac::$pac_type::ptr() } unsafe { &*pac::$pac_type::ptr() }
} }
fn state() -> &'static crate::twis::sealed::State { fn state() -> &'static crate::twis::State {
static STATE: crate::twis::sealed::State = crate::twis::sealed::State::new(); static STATE: crate::twis::State = crate::twis::State::new();
&STATE &STATE
} }
} }

View file

@ -15,18 +15,18 @@
use core::future::poll_fn; use core::future::poll_fn;
use core::marker::PhantomData; use core::marker::PhantomData;
use core::sync::atomic::{compiler_fence, Ordering}; use core::sync::atomic::{compiler_fence, AtomicU8, Ordering};
use core::task::Poll; use core::task::Poll;
use embassy_hal_internal::drop::OnDrop; use embassy_hal_internal::drop::OnDrop;
use embassy_hal_internal::{into_ref, PeripheralRef}; use embassy_hal_internal::{into_ref, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker;
use pac::uarte0::RegisterBlock; use pac::uarte0::RegisterBlock;
// Re-export SVD variants to allow user to directly set values. // Re-export SVD variants to allow user to directly set values.
pub use pac::uarte0::{baudrate::BAUDRATE_A as Baudrate, config::PARITY_A as Parity}; pub use pac::uarte0::{baudrate::BAUDRATE_A as Baudrate, config::PARITY_A as Parity};
use crate::chip::{EASY_DMA_SIZE, FORCE_COPY_BUFFER_SIZE}; use crate::chip::{EASY_DMA_SIZE, FORCE_COPY_BUFFER_SIZE};
use crate::gpio::sealed::Pin as _; use crate::gpio::{self, AnyPin, Pin as GpioPin, PselBits, SealedPin as _};
use crate::gpio::{self, AnyPin, Pin as GpioPin, PselBits};
use crate::interrupt::typelevel::Interrupt; use crate::interrupt::typelevel::Interrupt;
use crate::ppi::{AnyConfigurableChannel, ConfigurableChannel, Event, Ppi, Task}; use crate::ppi::{AnyConfigurableChannel, ConfigurableChannel, Event, Ppi, Task};
use crate::timer::{Frequency, Instance as TimerInstance, Timer}; use crate::timer::{Frequency, Instance as TimerInstance, Timer};
@ -939,7 +939,7 @@ pub(crate) fn apply_workaround_for_enable_anomaly(r: &crate::pac::uarte0::Regist
} }
} }
pub(crate) fn drop_tx_rx(r: &pac::uarte0::RegisterBlock, s: &sealed::State) { pub(crate) fn drop_tx_rx(r: &pac::uarte0::RegisterBlock, s: &State) {
if s.tx_rx_refcount.fetch_sub(1, Ordering::Relaxed) == 1 { if s.tx_rx_refcount.fetch_sub(1, Ordering::Relaxed) == 1 {
// Finally we can disable, and we do so for the peripheral // Finally we can disable, and we do so for the peripheral
// i.e. not just rx concerns. // i.e. not just rx concerns.
@ -954,49 +954,42 @@ pub(crate) fn drop_tx_rx(r: &pac::uarte0::RegisterBlock, s: &sealed::State) {
} }
} }
pub(crate) mod sealed { pub(crate) struct State {
use core::sync::atomic::AtomicU8; pub(crate) rx_waker: AtomicWaker,
pub(crate) tx_waker: AtomicWaker,
use embassy_sync::waitqueue::AtomicWaker; pub(crate) tx_rx_refcount: AtomicU8,
}
use super::*; impl State {
pub(crate) const fn new() -> Self {
pub struct State { Self {
pub rx_waker: AtomicWaker, rx_waker: AtomicWaker::new(),
pub tx_waker: AtomicWaker, tx_waker: AtomicWaker::new(),
pub tx_rx_refcount: AtomicU8, tx_rx_refcount: AtomicU8::new(0),
}
impl State {
pub const fn new() -> Self {
Self {
rx_waker: AtomicWaker::new(),
tx_waker: AtomicWaker::new(),
tx_rx_refcount: AtomicU8::new(0),
}
} }
} }
}
pub trait Instance {
fn regs() -> &'static pac::uarte0::RegisterBlock; pub(crate) trait SealedInstance {
fn state() -> &'static State; fn regs() -> &'static pac::uarte0::RegisterBlock;
fn buffered_state() -> &'static crate::buffered_uarte::State; fn state() -> &'static State;
} fn buffered_state() -> &'static crate::buffered_uarte::State;
} }
/// UARTE peripheral instance. /// UARTE peripheral instance.
pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static + Send { #[allow(private_bounds)]
pub trait Instance: Peripheral<P = Self> + SealedInstance + 'static + Send {
/// Interrupt for this peripheral. /// Interrupt for this peripheral.
type Interrupt: interrupt::typelevel::Interrupt; type Interrupt: interrupt::typelevel::Interrupt;
} }
macro_rules! impl_uarte { macro_rules! impl_uarte {
($type:ident, $pac_type:ident, $irq:ident) => { ($type:ident, $pac_type:ident, $irq:ident) => {
impl crate::uarte::sealed::Instance for peripherals::$type { impl crate::uarte::SealedInstance for peripherals::$type {
fn regs() -> &'static pac::uarte0::RegisterBlock { fn regs() -> &'static pac::uarte0::RegisterBlock {
unsafe { &*pac::$pac_type::ptr() } unsafe { &*pac::$pac_type::ptr() }
} }
fn state() -> &'static crate::uarte::sealed::State { fn state() -> &'static crate::uarte::State {
static STATE: crate::uarte::sealed::State = crate::uarte::sealed::State::new(); static STATE: crate::uarte::State = crate::uarte::State::new();
&STATE &STATE
} }
fn buffered_state() -> &'static crate::buffered_uarte::State { fn buffered_state() -> &'static crate::buffered_uarte::State {

View file

@ -793,23 +793,20 @@ impl Allocator {
} }
} }
pub(crate) mod sealed { pub(crate) trait SealedInstance {
use super::*; fn regs() -> &'static pac::usbd::RegisterBlock;
pub trait Instance {
fn regs() -> &'static pac::usbd::RegisterBlock;
}
} }
/// USB peripheral instance. /// USB peripheral instance.
pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static + Send { #[allow(private_bounds)]
pub trait Instance: Peripheral<P = Self> + SealedInstance + 'static + Send {
/// Interrupt for this peripheral. /// Interrupt for this peripheral.
type Interrupt: interrupt::typelevel::Interrupt; type Interrupt: interrupt::typelevel::Interrupt;
} }
macro_rules! impl_usb { macro_rules! impl_usb {
($type:ident, $pac_type:ident, $irq:ident) => { ($type:ident, $pac_type:ident, $irq:ident) => {
impl crate::usb::sealed::Instance for peripherals::$type { impl crate::usb::SealedInstance for peripherals::$type {
fn regs() -> &'static pac::usbd::RegisterBlock { fn regs() -> &'static pac::usbd::RegisterBlock {
unsafe { &*pac::$pac_type::ptr() } unsafe { &*pac::$pac_type::ptr() }
} }

View file

@ -8,8 +8,7 @@ use core::task::Poll;
use embassy_hal_internal::{into_ref, PeripheralRef}; use embassy_hal_internal::{into_ref, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker; use embassy_sync::waitqueue::AtomicWaker;
use crate::gpio::sealed::Pin as GpioPin; use crate::gpio::{self, AnyPin, Pull, SealedPin as GpioPin};
use crate::gpio::{self, AnyPin, Pull};
use crate::interrupt::typelevel::Binding; use crate::interrupt::typelevel::Binding;
use crate::interrupt::InterruptExt; use crate::interrupt::InterruptExt;
use crate::peripherals::{ADC, ADC_TEMP_SENSOR}; use crate::peripherals::{ADC, ADC_TEMP_SENSOR};
@ -334,29 +333,28 @@ impl interrupt::typelevel::Handler<interrupt::typelevel::ADC_IRQ_FIFO> for Inter
} }
} }
mod sealed { trait SealedAdcSample: crate::dma::Word {}
pub trait AdcSample: crate::dma::Word {} trait SealedAdcChannel {}
pub trait AdcChannel {}
}
/// ADC sample. /// ADC sample.
pub trait AdcSample: sealed::AdcSample {} #[allow(private_bounds)]
pub trait AdcSample: SealedAdcSample {}
impl sealed::AdcSample for u16 {} impl SealedAdcSample for u16 {}
impl AdcSample for u16 {} impl AdcSample for u16 {}
impl sealed::AdcSample for u8 {} impl SealedAdcSample for u8 {}
impl AdcSample for u8 {} impl AdcSample for u8 {}
/// ADC channel. /// ADC channel.
pub trait AdcChannel: sealed::AdcChannel {} #[allow(private_bounds)]
pub trait AdcChannel: SealedAdcChannel {}
/// ADC pin. /// ADC pin.
pub trait AdcPin: AdcChannel + gpio::Pin {} pub trait AdcPin: AdcChannel + gpio::Pin {}
macro_rules! impl_pin { macro_rules! impl_pin {
($pin:ident, $channel:expr) => { ($pin:ident, $channel:expr) => {
impl sealed::AdcChannel for peripherals::$pin {} impl SealedAdcChannel for peripherals::$pin {}
impl AdcChannel for peripherals::$pin {} impl AdcChannel for peripherals::$pin {}
impl AdcPin for peripherals::$pin {} impl AdcPin for peripherals::$pin {}
}; };
@ -367,5 +365,5 @@ impl_pin!(PIN_27, 1);
impl_pin!(PIN_28, 2); impl_pin!(PIN_28, 2);
impl_pin!(PIN_29, 3); impl_pin!(PIN_29, 3);
impl sealed::AdcChannel for peripherals::ADC_TEMP_SENSOR {} impl SealedAdcChannel for peripherals::ADC_TEMP_SENSOR {}
impl AdcChannel for peripherals::ADC_TEMP_SENSOR {} impl AdcChannel for peripherals::ADC_TEMP_SENSOR {}

View file

@ -6,8 +6,7 @@ use core::sync::atomic::{AtomicU16, AtomicU32, Ordering};
use embassy_hal_internal::{into_ref, PeripheralRef}; use embassy_hal_internal::{into_ref, PeripheralRef};
use pac::clocks::vals::*; use pac::clocks::vals::*;
use crate::gpio::sealed::Pin; use crate::gpio::{AnyPin, SealedPin};
use crate::gpio::AnyPin;
use crate::pac::common::{Reg, RW}; use crate::pac::common::{Reg, RW};
use crate::{pac, reset, Peripheral}; use crate::{pac, reset, Peripheral};
@ -788,14 +787,14 @@ impl_gpinpin!(PIN_20, 20, 0);
impl_gpinpin!(PIN_22, 22, 1); impl_gpinpin!(PIN_22, 22, 1);
/// General purpose clock input driver. /// General purpose clock input driver.
pub struct Gpin<'d, T: Pin> { pub struct Gpin<'d, T: GpinPin> {
gpin: PeripheralRef<'d, AnyPin>, gpin: PeripheralRef<'d, AnyPin>,
_phantom: PhantomData<T>, _phantom: PhantomData<T>,
} }
impl<'d, T: Pin> Gpin<'d, T> { impl<'d, T: GpinPin> Gpin<'d, T> {
/// Create new gpin driver. /// Create new gpin driver.
pub fn new<P: GpinPin>(gpin: impl Peripheral<P = P> + 'd) -> Gpin<'d, P> { pub fn new(gpin: impl Peripheral<P = T> + 'd) -> Self {
into_ref!(gpin); into_ref!(gpin);
gpin.gpio().ctrl().write(|w| w.set_funcsel(0x08)); gpin.gpio().ctrl().write(|w| w.set_funcsel(0x08));
@ -811,7 +810,7 @@ impl<'d, T: Pin> Gpin<'d, T> {
// } // }
} }
impl<'d, T: Pin> Drop for Gpin<'d, T> { impl<'d, T: GpinPin> Drop for Gpin<'d, T> {
fn drop(&mut self) { fn drop(&mut self) {
self.gpin self.gpin
.gpio() .gpio()

View file

@ -208,14 +208,12 @@ pub(crate) const CHANNEL_COUNT: usize = 12;
const NEW_AW: AtomicWaker = AtomicWaker::new(); const NEW_AW: AtomicWaker = AtomicWaker::new();
static CHANNEL_WAKERS: [AtomicWaker; CHANNEL_COUNT] = [NEW_AW; CHANNEL_COUNT]; static CHANNEL_WAKERS: [AtomicWaker; CHANNEL_COUNT] = [NEW_AW; CHANNEL_COUNT];
mod sealed { trait SealedChannel {}
pub trait Channel {} trait SealedWord {}
pub trait Word {}
}
/// DMA channel interface. /// DMA channel interface.
pub trait Channel: Peripheral<P = Self> + sealed::Channel + Into<AnyChannel> + Sized + 'static { #[allow(private_bounds)]
pub trait Channel: Peripheral<P = Self> + SealedChannel + Into<AnyChannel> + Sized + 'static {
/// Channel number. /// Channel number.
fn number(&self) -> u8; fn number(&self) -> u8;
@ -231,26 +229,27 @@ pub trait Channel: Peripheral<P = Self> + sealed::Channel + Into<AnyChannel> + S
} }
/// DMA word. /// DMA word.
pub trait Word: sealed::Word { #[allow(private_bounds)]
pub trait Word: SealedWord {
/// Word size. /// Word size.
fn size() -> vals::DataSize; fn size() -> vals::DataSize;
} }
impl sealed::Word for u8 {} impl SealedWord for u8 {}
impl Word for u8 { impl Word for u8 {
fn size() -> vals::DataSize { fn size() -> vals::DataSize {
vals::DataSize::SIZE_BYTE vals::DataSize::SIZE_BYTE
} }
} }
impl sealed::Word for u16 {} impl SealedWord for u16 {}
impl Word for u16 { impl Word for u16 {
fn size() -> vals::DataSize { fn size() -> vals::DataSize {
vals::DataSize::SIZE_HALFWORD vals::DataSize::SIZE_HALFWORD
} }
} }
impl sealed::Word for u32 {} impl SealedWord for u32 {}
impl Word for u32 { impl Word for u32 {
fn size() -> vals::DataSize { fn size() -> vals::DataSize {
vals::DataSize::SIZE_WORD vals::DataSize::SIZE_WORD
@ -264,7 +263,7 @@ pub struct AnyChannel {
impl_peripheral!(AnyChannel); impl_peripheral!(AnyChannel);
impl sealed::Channel for AnyChannel {} impl SealedChannel for AnyChannel {}
impl Channel for AnyChannel { impl Channel for AnyChannel {
fn number(&self) -> u8 { fn number(&self) -> u8 {
self.number self.number
@ -273,7 +272,7 @@ impl Channel for AnyChannel {
macro_rules! channel { macro_rules! channel {
($name:ident, $num:expr) => { ($name:ident, $num:expr) => {
impl sealed::Channel for peripherals::$name {} impl SealedChannel for peripherals::$name {}
impl Channel for peripherals::$name { impl Channel for peripherals::$name {
fn number(&self) -> u8 { fn number(&self) -> u8 {
$num $num

View file

@ -903,22 +903,22 @@ pub(crate) unsafe fn in_ram(operation: impl FnOnce()) -> Result<(), Error> {
Ok(()) Ok(())
} }
mod sealed { trait SealedInstance {}
pub trait Instance {} trait SealedMode {}
pub trait Mode {}
}
/// Flash instance. /// Flash instance.
pub trait Instance: sealed::Instance {} #[allow(private_bounds)]
pub trait Instance: SealedInstance {}
/// Flash mode. /// Flash mode.
pub trait Mode: sealed::Mode {} #[allow(private_bounds)]
pub trait Mode: SealedMode {}
impl sealed::Instance for FLASH {} impl SealedInstance for FLASH {}
impl Instance for FLASH {} impl Instance for FLASH {}
macro_rules! impl_mode { macro_rules! impl_mode {
($name:ident) => { ($name:ident) => {
impl sealed::Mode for $name {} impl SealedMode for $name {}
impl Mode for $name {} impl Mode for $name {}
}; };
} }

View file

@ -8,7 +8,6 @@ use core::task::{Context, Poll};
use embassy_hal_internal::{impl_peripheral, into_ref, PeripheralRef}; use embassy_hal_internal::{impl_peripheral, into_ref, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker; use embassy_sync::waitqueue::AtomicWaker;
use self::sealed::Pin as _;
use crate::interrupt::InterruptExt; use crate::interrupt::InterruptExt;
use crate::pac::common::{Reg, RW}; use crate::pac::common::{Reg, RW};
use crate::pac::SIO; use crate::pac::SIO;
@ -802,68 +801,65 @@ impl<'w> Drop for DormantWake<'w> {
} }
} }
pub(crate) mod sealed { pub(crate) trait SealedPin: Sized {
use super::*; fn pin_bank(&self) -> u8;
pub trait Pin: Sized { #[inline]
fn pin_bank(&self) -> u8; fn _pin(&self) -> u8 {
self.pin_bank() & 0x1f
}
#[inline] #[inline]
fn _pin(&self) -> u8 { fn _bank(&self) -> Bank {
self.pin_bank() & 0x1f match self.pin_bank() >> 5 {
#[cfg(feature = "qspi-as-gpio")]
1 => Bank::Qspi,
_ => Bank::Bank0,
} }
}
#[inline] fn io(&self) -> pac::io::Io {
fn _bank(&self) -> Bank { match self._bank() {
match self.pin_bank() >> 5 { Bank::Bank0 => crate::pac::IO_BANK0,
#[cfg(feature = "qspi-as-gpio")] #[cfg(feature = "qspi-as-gpio")]
1 => Bank::Qspi, Bank::Qspi => crate::pac::IO_QSPI,
_ => Bank::Bank0,
}
} }
}
fn io(&self) -> pac::io::Io { fn gpio(&self) -> pac::io::Gpio {
match self._bank() { self.io().gpio(self._pin() as _)
Bank::Bank0 => crate::pac::IO_BANK0, }
#[cfg(feature = "qspi-as-gpio")]
Bank::Qspi => crate::pac::IO_QSPI,
}
}
fn gpio(&self) -> pac::io::Gpio { fn pad_ctrl(&self) -> Reg<pac::pads::regs::GpioCtrl, RW> {
self.io().gpio(self._pin() as _) let block = match self._bank() {
} Bank::Bank0 => crate::pac::PADS_BANK0,
#[cfg(feature = "qspi-as-gpio")]
Bank::Qspi => crate::pac::PADS_QSPI,
};
block.gpio(self._pin() as _)
}
fn pad_ctrl(&self) -> Reg<pac::pads::regs::GpioCtrl, RW> { fn sio_out(&self) -> pac::sio::Gpio {
let block = match self._bank() { SIO.gpio_out(self._bank() as _)
Bank::Bank0 => crate::pac::PADS_BANK0, }
#[cfg(feature = "qspi-as-gpio")]
Bank::Qspi => crate::pac::PADS_QSPI,
};
block.gpio(self._pin() as _)
}
fn sio_out(&self) -> pac::sio::Gpio { fn sio_oe(&self) -> pac::sio::Gpio {
SIO.gpio_out(self._bank() as _) SIO.gpio_oe(self._bank() as _)
} }
fn sio_oe(&self) -> pac::sio::Gpio { fn sio_in(&self) -> Reg<u32, RW> {
SIO.gpio_oe(self._bank() as _) SIO.gpio_in(self._bank() as _)
} }
fn sio_in(&self) -> Reg<u32, RW> { fn int_proc(&self) -> pac::io::Int {
SIO.gpio_in(self._bank() as _) let proc = SIO.cpuid().read();
} self.io().int_proc(proc as _)
fn int_proc(&self) -> pac::io::Int {
let proc = SIO.cpuid().read();
self.io().int_proc(proc as _)
}
} }
} }
/// Interface for a Pin that can be configured by an [Input] or [Output] driver, or converted to an [AnyPin]. /// Interface for a Pin that can be configured by an [Input] or [Output] driver, or converted to an [AnyPin].
pub trait Pin: Peripheral<P = Self> + Into<AnyPin> + sealed::Pin + Sized + 'static { #[allow(private_bounds)]
pub trait Pin: Peripheral<P = Self> + Into<AnyPin> + SealedPin + Sized + 'static {
/// Degrade to a generic pin struct /// Degrade to a generic pin struct
fn degrade(self) -> AnyPin { fn degrade(self) -> AnyPin {
AnyPin { AnyPin {
@ -903,7 +899,7 @@ impl AnyPin {
impl_peripheral!(AnyPin); impl_peripheral!(AnyPin);
impl Pin for AnyPin {} impl Pin for AnyPin {}
impl sealed::Pin for AnyPin { impl SealedPin for AnyPin {
fn pin_bank(&self) -> u8 { fn pin_bank(&self) -> u8 {
self.pin_bank self.pin_bank
} }
@ -914,7 +910,7 @@ impl sealed::Pin for AnyPin {
macro_rules! impl_pin { macro_rules! impl_pin {
($name:ident, $bank:expr, $pin_num:expr) => { ($name:ident, $bank:expr, $pin_num:expr) => {
impl Pin for peripherals::$name {} impl Pin for peripherals::$name {}
impl sealed::Pin for peripherals::$name { impl SealedPin for peripherals::$name {
#[inline] #[inline]
fn pin_bank(&self) -> u8 { fn pin_bank(&self) -> u8 {
($bank as u8) * 32 + $pin_num ($bank as u8) * 32 + $pin_num

View file

@ -784,34 +784,24 @@ pub fn i2c_reserved_addr(addr: u16) -> bool {
((addr & 0x78) == 0 || (addr & 0x78) == 0x78) && addr != 0 ((addr & 0x78) == 0 || (addr & 0x78) == 0x78) && addr != 0
} }
mod sealed { pub(crate) trait SealedInstance {
use embassy_sync::waitqueue::AtomicWaker; const TX_DREQ: u8;
const RX_DREQ: u8;
use crate::interrupt; fn regs() -> crate::pac::i2c::I2c;
fn reset() -> crate::pac::resets::regs::Peripherals;
pub trait Instance { fn waker() -> &'static AtomicWaker;
const TX_DREQ: u8;
const RX_DREQ: u8;
type Interrupt: interrupt::typelevel::Interrupt;
fn regs() -> crate::pac::i2c::I2c;
fn reset() -> crate::pac::resets::regs::Peripherals;
fn waker() -> &'static AtomicWaker;
}
pub trait Mode {}
pub trait SdaPin<T: Instance> {}
pub trait SclPin<T: Instance> {}
} }
trait SealedMode {}
/// Driver mode. /// Driver mode.
pub trait Mode: sealed::Mode {} #[allow(private_bounds)]
pub trait Mode: SealedMode {}
macro_rules! impl_mode { macro_rules! impl_mode {
($name:ident) => { ($name:ident) => {
impl sealed::Mode for $name {} impl SealedMode for $name {}
impl Mode for $name {} impl Mode for $name {}
}; };
} }
@ -825,16 +815,18 @@ impl_mode!(Blocking);
impl_mode!(Async); impl_mode!(Async);
/// I2C instance. /// I2C instance.
pub trait Instance: sealed::Instance {} #[allow(private_bounds)]
pub trait Instance: SealedInstance {
/// Interrupt for this peripheral.
type Interrupt: interrupt::typelevel::Interrupt;
}
macro_rules! impl_instance { macro_rules! impl_instance {
($type:ident, $irq:ident, $reset:ident, $tx_dreq:expr, $rx_dreq:expr) => { ($type:ident, $irq:ident, $reset:ident, $tx_dreq:expr, $rx_dreq:expr) => {
impl sealed::Instance for peripherals::$type { impl SealedInstance for peripherals::$type {
const TX_DREQ: u8 = $tx_dreq; const TX_DREQ: u8 = $tx_dreq;
const RX_DREQ: u8 = $rx_dreq; const RX_DREQ: u8 = $rx_dreq;
type Interrupt = crate::interrupt::typelevel::$irq;
#[inline] #[inline]
fn regs() -> pac::i2c::I2c { fn regs() -> pac::i2c::I2c {
pac::$type pac::$type
@ -854,7 +846,9 @@ macro_rules! impl_instance {
&WAKER &WAKER
} }
} }
impl Instance for peripherals::$type {} impl Instance for peripherals::$type {
type Interrupt = crate::interrupt::typelevel::$irq;
}
}; };
} }
@ -862,13 +856,12 @@ impl_instance!(I2C0, I2C0_IRQ, set_i2c0, 32, 33);
impl_instance!(I2C1, I2C1_IRQ, set_i2c1, 34, 35); impl_instance!(I2C1, I2C1_IRQ, set_i2c1, 34, 35);
/// SDA pin. /// SDA pin.
pub trait SdaPin<T: Instance>: sealed::SdaPin<T> + crate::gpio::Pin {} pub trait SdaPin<T: Instance>: crate::gpio::Pin {}
/// SCL pin. /// SCL pin.
pub trait SclPin<T: Instance>: sealed::SclPin<T> + crate::gpio::Pin {} pub trait SclPin<T: Instance>: crate::gpio::Pin {}
macro_rules! impl_pin { macro_rules! impl_pin {
($pin:ident, $instance:ident, $function:ident) => { ($pin:ident, $instance:ident, $function:ident) => {
impl sealed::$function<peripherals::$instance> for peripherals::$pin {}
impl $function<peripherals::$instance> for peripherals::$pin {} impl $function<peripherals::$instance> for peripherals::$pin {}
}; };
} }

View file

@ -15,8 +15,7 @@ use pac::pio::vals::SmExecctrlStatusSel;
use pio::{Program, SideSet, Wrap}; use pio::{Program, SideSet, Wrap};
use crate::dma::{Channel, Transfer, Word}; use crate::dma::{Channel, Transfer, Word};
use crate::gpio::sealed::Pin as SealedPin; use crate::gpio::{self, AnyPin, Drive, Level, Pull, SealedPin, SlewRate};
use crate::gpio::{self, AnyPin, Drive, Level, Pull, SlewRate};
use crate::interrupt::typelevel::{Binding, Handler, Interrupt}; use crate::interrupt::typelevel::{Binding, Handler, Interrupt};
use crate::pac::dma::vals::TreqSel; use crate::pac::dma::vals::TreqSel;
use crate::relocate::RelocatedProgram; use crate::relocate::RelocatedProgram;
@ -695,6 +694,12 @@ impl<'d, PIO: Instance + 'd, const SM: usize> StateMachine<'d, PIO, SM> {
} }
} }
/// Set the clock divider for this state machine.
pub fn set_clock_divider(&mut self, clock_divider: FixedU32<U8>) {
let sm = Self::this_sm();
sm.clkdiv().write(|w| w.0 = clock_divider.to_bits() << 8);
}
#[inline(always)] #[inline(always)]
fn this_sm() -> crate::pac::pio::StateMachine { fn this_sm() -> crate::pac::pio::StateMachine {
PIO::PIO.sm(SM) PIO::PIO.sm(SM)
@ -1148,49 +1153,47 @@ fn on_pio_drop<PIO: Instance>() {
} }
} }
mod sealed { trait SealedInstance {
use super::*; const PIO_NO: u8;
const PIO: &'static crate::pac::pio::Pio;
const FUNCSEL: crate::pac::io::vals::Gpio0ctrlFuncsel;
pub trait PioPin {} #[inline]
fn wakers() -> &'static Wakers {
const NEW_AW: AtomicWaker = AtomicWaker::new();
static WAKERS: Wakers = Wakers([NEW_AW; 12]);
pub trait Instance { &WAKERS
const PIO_NO: u8; }
const PIO: &'static crate::pac::pio::Pio;
const FUNCSEL: crate::pac::io::vals::Gpio0ctrlFuncsel;
type Interrupt: crate::interrupt::typelevel::Interrupt;
#[inline] #[inline]
fn wakers() -> &'static Wakers { fn state() -> &'static State {
const NEW_AW: AtomicWaker = AtomicWaker::new(); static STATE: State = State {
static WAKERS: Wakers = Wakers([NEW_AW; 12]); users: AtomicU8::new(0),
used_pins: AtomicU32::new(0),
};
&WAKERS &STATE
}
#[inline]
fn state() -> &'static State {
static STATE: State = State {
users: AtomicU8::new(0),
used_pins: AtomicU32::new(0),
};
&STATE
}
} }
} }
/// PIO instance. /// PIO instance.
pub trait Instance: sealed::Instance + Sized + Unpin {} #[allow(private_bounds)]
pub trait Instance: SealedInstance + Sized + Unpin {
/// Interrupt for this peripheral.
type Interrupt: crate::interrupt::typelevel::Interrupt;
}
macro_rules! impl_pio { macro_rules! impl_pio {
($name:ident, $pio:expr, $pac:ident, $funcsel:ident, $irq:ident) => { ($name:ident, $pio:expr, $pac:ident, $funcsel:ident, $irq:ident) => {
impl sealed::Instance for peripherals::$name { impl SealedInstance for peripherals::$name {
const PIO_NO: u8 = $pio; const PIO_NO: u8 = $pio;
const PIO: &'static pac::pio::Pio = &pac::$pac; const PIO: &'static pac::pio::Pio = &pac::$pac;
const FUNCSEL: pac::io::vals::Gpio0ctrlFuncsel = pac::io::vals::Gpio0ctrlFuncsel::$funcsel; const FUNCSEL: pac::io::vals::Gpio0ctrlFuncsel = pac::io::vals::Gpio0ctrlFuncsel::$funcsel;
}
impl Instance for peripherals::$name {
type Interrupt = crate::interrupt::typelevel::$irq; type Interrupt = crate::interrupt::typelevel::$irq;
} }
impl Instance for peripherals::$name {}
}; };
} }
@ -1198,12 +1201,11 @@ impl_pio!(PIO0, 0, PIO0, PIO0_0, PIO0_IRQ_0);
impl_pio!(PIO1, 1, PIO1, PIO1_0, PIO1_IRQ_0); impl_pio!(PIO1, 1, PIO1, PIO1_0, PIO1_IRQ_0);
/// PIO pin. /// PIO pin.
pub trait PioPin: sealed::PioPin + gpio::Pin {} pub trait PioPin: gpio::Pin {}
macro_rules! impl_pio_pin { macro_rules! impl_pio_pin {
($( $pin:ident, )*) => { ($( $pin:ident, )*) => {
$( $(
impl sealed::PioPin for peripherals::$pin {}
impl PioPin for peripherals::$pin {} impl PioPin for peripherals::$pin {}
)* )*
}; };

View file

@ -6,8 +6,7 @@ use fixed::FixedU16;
use pac::pwm::regs::{ChDiv, Intr}; use pac::pwm::regs::{ChDiv, Intr};
use pac::pwm::vals::Divmode; use pac::pwm::vals::Divmode;
use crate::gpio::sealed::Pin as _; use crate::gpio::{AnyPin, Pin as GpioPin, SealedPin as _};
use crate::gpio::{AnyPin, Pin as GpioPin};
use crate::{pac, peripherals, RegExt}; use crate::{pac, peripherals, RegExt};
/// The configuration of a PWM slice. /// The configuration of a PWM slice.
@ -300,12 +299,11 @@ impl<'d, T: Slice> Drop for Pwm<'d, T> {
} }
} }
mod sealed { trait SealedSlice {}
pub trait Slice {}
}
/// PWM Slice. /// PWM Slice.
pub trait Slice: Peripheral<P = Self> + sealed::Slice + Sized + 'static { #[allow(private_bounds)]
pub trait Slice: Peripheral<P = Self> + SealedSlice + Sized + 'static {
/// Slice number. /// Slice number.
fn number(&self) -> u8; fn number(&self) -> u8;
@ -317,7 +315,7 @@ pub trait Slice: Peripheral<P = Self> + sealed::Slice + Sized + 'static {
macro_rules! slice { macro_rules! slice {
($name:ident, $num:expr) => { ($name:ident, $num:expr) => {
impl sealed::Slice for peripherals::$name {} impl SealedSlice for peripherals::$name {}
impl Slice for peripherals::$name { impl Slice for peripherals::$name {
fn number(&self) -> u8 { fn number(&self) -> u8 {
$num $num

View file

@ -188,16 +188,15 @@ pub enum RtcError {
NotRunning, NotRunning,
} }
mod sealed { trait SealedInstance {
pub trait Instance { fn regs(&self) -> crate::pac::rtc::Rtc;
fn regs(&self) -> crate::pac::rtc::Rtc;
}
} }
/// RTC peripheral instance. /// RTC peripheral instance.
pub trait Instance: sealed::Instance {} #[allow(private_bounds)]
pub trait Instance: SealedInstance {}
impl sealed::Instance for crate::peripherals::RTC { impl SealedInstance for crate::peripherals::RTC {
fn regs(&self) -> crate::pac::rtc::Rtc { fn regs(&self) -> crate::pac::rtc::Rtc {
crate::pac::RTC crate::pac::RTC
} }

View file

@ -7,8 +7,7 @@ use embassy_hal_internal::{into_ref, PeripheralRef};
pub use embedded_hal_02::spi::{Phase, Polarity}; pub use embedded_hal_02::spi::{Phase, Polarity};
use crate::dma::{AnyChannel, Channel}; use crate::dma::{AnyChannel, Channel};
use crate::gpio::sealed::Pin as _; use crate::gpio::{AnyPin, Pin as GpioPin, SealedPin as _};
use crate::gpio::{AnyPin, Pin as GpioPin};
use crate::{pac, peripherals, Peripheral}; use crate::{pac, peripherals, Peripheral};
/// SPI errors. /// SPI errors.
@ -443,28 +442,26 @@ impl<'d, T: Instance> Spi<'d, T, Async> {
} }
} }
mod sealed { trait SealedMode {}
use super::*;
pub trait Mode {} trait SealedInstance {
const TX_DREQ: u8;
const RX_DREQ: u8;
pub trait Instance { fn regs(&self) -> pac::spi::Spi;
const TX_DREQ: u8;
const RX_DREQ: u8;
fn regs(&self) -> pac::spi::Spi;
}
} }
/// Mode. /// Mode.
pub trait Mode: sealed::Mode {} #[allow(private_bounds)]
pub trait Mode: SealedMode {}
/// SPI instance trait. /// SPI instance trait.
pub trait Instance: sealed::Instance {} #[allow(private_bounds)]
pub trait Instance: SealedInstance {}
macro_rules! impl_instance { macro_rules! impl_instance {
($type:ident, $irq:ident, $tx_dreq:expr, $rx_dreq:expr) => { ($type:ident, $irq:ident, $tx_dreq:expr, $rx_dreq:expr) => {
impl sealed::Instance for peripherals::$type { impl SealedInstance for peripherals::$type {
const TX_DREQ: u8 = $tx_dreq; const TX_DREQ: u8 = $tx_dreq;
const RX_DREQ: u8 = $rx_dreq; const RX_DREQ: u8 = $rx_dreq;
@ -527,7 +524,7 @@ impl_pin!(PIN_29, SPI1, CsPin);
macro_rules! impl_mode { macro_rules! impl_mode {
($name:ident) => { ($name:ident) => {
impl sealed::Mode for $name {} impl SealedMode for $name {}
impl Mode for $name {} impl Mode for $name {}
}; };
} }

View file

@ -12,8 +12,7 @@ use pac::uart::regs::Uartris;
use crate::clocks::clk_peri_freq; use crate::clocks::clk_peri_freq;
use crate::dma::{AnyChannel, Channel}; use crate::dma::{AnyChannel, Channel};
use crate::gpio::sealed::Pin; use crate::gpio::{AnyPin, SealedPin};
use crate::gpio::AnyPin;
use crate::interrupt::typelevel::{Binding, Interrupt}; use crate::interrupt::typelevel::{Binding, Interrupt};
use crate::pac::io::vals::{Inover, Outover}; use crate::pac::io::vals::{Inover, Outover};
use crate::{interrupt, pac, peripherals, Peripheral, RegExt}; use crate::{interrupt, pac, peripherals, Peripheral, RegExt};
@ -1107,35 +1106,26 @@ impl<'d, T: Instance, M: Mode> embedded_hal_nb::serial::Write for Uart<'d, T, M>
} }
} }
mod sealed { trait SealedMode {}
use super::*;
pub trait Mode {} trait SealedInstance {
const TX_DREQ: u8;
const RX_DREQ: u8;
pub trait Instance { fn regs() -> pac::uart::Uart;
const TX_DREQ: u8;
const RX_DREQ: u8;
type Interrupt: interrupt::typelevel::Interrupt; fn buffered_state() -> &'static buffered::State;
fn regs() -> pac::uart::Uart; fn dma_state() -> &'static DmaState;
fn buffered_state() -> &'static buffered::State;
fn dma_state() -> &'static DmaState;
}
pub trait TxPin<T: Instance> {}
pub trait RxPin<T: Instance> {}
pub trait CtsPin<T: Instance> {}
pub trait RtsPin<T: Instance> {}
} }
/// UART mode. /// UART mode.
pub trait Mode: sealed::Mode {} #[allow(private_bounds)]
pub trait Mode: SealedMode {}
macro_rules! impl_mode { macro_rules! impl_mode {
($name:ident) => { ($name:ident) => {
impl sealed::Mode for $name {} impl SealedMode for $name {}
impl Mode for $name {} impl Mode for $name {}
}; };
} }
@ -1149,16 +1139,18 @@ impl_mode!(Blocking);
impl_mode!(Async); impl_mode!(Async);
/// UART instance. /// UART instance.
pub trait Instance: sealed::Instance {} #[allow(private_bounds)]
pub trait Instance: SealedInstance {
/// Interrupt for this instance.
type Interrupt: interrupt::typelevel::Interrupt;
}
macro_rules! impl_instance { macro_rules! impl_instance {
($inst:ident, $irq:ident, $tx_dreq:expr, $rx_dreq:expr) => { ($inst:ident, $irq:ident, $tx_dreq:expr, $rx_dreq:expr) => {
impl sealed::Instance for peripherals::$inst { impl SealedInstance for peripherals::$inst {
const TX_DREQ: u8 = $tx_dreq; const TX_DREQ: u8 = $tx_dreq;
const RX_DREQ: u8 = $rx_dreq; const RX_DREQ: u8 = $rx_dreq;
type Interrupt = crate::interrupt::typelevel::$irq;
fn regs() -> pac::uart::Uart { fn regs() -> pac::uart::Uart {
pac::$inst pac::$inst
} }
@ -1176,7 +1168,9 @@ macro_rules! impl_instance {
&STATE &STATE
} }
} }
impl Instance for peripherals::$inst {} impl Instance for peripherals::$inst {
type Interrupt = crate::interrupt::typelevel::$irq;
}
}; };
} }
@ -1184,17 +1178,16 @@ impl_instance!(UART0, UART0_IRQ, 20, 21);
impl_instance!(UART1, UART1_IRQ, 22, 23); impl_instance!(UART1, UART1_IRQ, 22, 23);
/// Trait for TX pins. /// Trait for TX pins.
pub trait TxPin<T: Instance>: sealed::TxPin<T> + crate::gpio::Pin {} pub trait TxPin<T: Instance>: crate::gpio::Pin {}
/// Trait for RX pins. /// Trait for RX pins.
pub trait RxPin<T: Instance>: sealed::RxPin<T> + crate::gpio::Pin {} pub trait RxPin<T: Instance>: crate::gpio::Pin {}
/// Trait for Clear To Send (CTS) pins. /// Trait for Clear To Send (CTS) pins.
pub trait CtsPin<T: Instance>: sealed::CtsPin<T> + crate::gpio::Pin {} pub trait CtsPin<T: Instance>: crate::gpio::Pin {}
/// Trait for Request To Send (RTS) pins. /// Trait for Request To Send (RTS) pins.
pub trait RtsPin<T: Instance>: sealed::RtsPin<T> + crate::gpio::Pin {} pub trait RtsPin<T: Instance>: crate::gpio::Pin {}
macro_rules! impl_pin { macro_rules! impl_pin {
($pin:ident, $instance:ident, $function:ident) => { ($pin:ident, $instance:ident, $function:ident) => {
impl sealed::$function<peripherals::$instance> for peripherals::$pin {}
impl $function<peripherals::$instance> for peripherals::$pin {} impl $function<peripherals::$instance> for peripherals::$pin {}
}; };
} }

View file

@ -14,20 +14,19 @@ use embassy_usb_driver::{
use crate::interrupt::typelevel::{Binding, Interrupt}; use crate::interrupt::typelevel::{Binding, Interrupt};
use crate::{interrupt, pac, peripherals, Peripheral, RegExt}; use crate::{interrupt, pac, peripherals, Peripheral, RegExt};
pub(crate) mod sealed { trait SealedInstance {
pub trait Instance { fn regs() -> crate::pac::usb::Usb;
fn regs() -> crate::pac::usb::Usb; fn dpram() -> crate::pac::usb_dpram::UsbDpram;
fn dpram() -> crate::pac::usb_dpram::UsbDpram;
}
} }
/// USB peripheral instance. /// USB peripheral instance.
pub trait Instance: sealed::Instance + 'static { #[allow(private_bounds)]
pub trait Instance: SealedInstance + 'static {
/// Interrupt for this peripheral. /// Interrupt for this peripheral.
type Interrupt: interrupt::typelevel::Interrupt; type Interrupt: interrupt::typelevel::Interrupt;
} }
impl crate::usb::sealed::Instance for peripherals::USB { impl crate::usb::SealedInstance for peripherals::USB {
fn regs() -> pac::usb::Usb { fn regs() -> pac::usb::Usb {
pac::USBCTRL_REGS pac::USBCTRL_REGS
} }

View file

@ -3,6 +3,7 @@
use embassy_hal_internal::drop::OnDrop; use embassy_hal_internal::drop::OnDrop;
use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef}; use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef};
use crate::pac::cordic::vals;
use crate::{dma, peripherals}; use crate::{dma, peripherals};
mod enums; mod enums;
@ -11,9 +12,6 @@ pub use enums::*;
mod errors; mod errors;
pub use errors::*; pub use errors::*;
mod sealed;
use self::sealed::SealedInstance;
pub mod utils; pub mod utils;
/// CORDIC driver /// CORDIC driver
@ -22,6 +20,120 @@ pub struct Cordic<'d, T: Instance> {
config: Config, config: Config,
} }
/// Cordic instance
trait SealedInstance {
/// Get access to CORDIC registers
fn regs() -> crate::pac::cordic::Cordic;
/// Set Function value
fn set_func(&self, func: Function) {
Self::regs()
.csr()
.modify(|v| v.set_func(vals::Func::from_bits(func as u8)));
}
/// Set Precision value
fn set_precision(&self, precision: Precision) {
Self::regs()
.csr()
.modify(|v| v.set_precision(vals::Precision::from_bits(precision as u8)))
}
/// Set Scale value
fn set_scale(&self, scale: Scale) {
Self::regs()
.csr()
.modify(|v| v.set_scale(vals::Scale::from_bits(scale as u8)))
}
/// Enable global interrupt
fn enable_irq(&self) {
Self::regs().csr().modify(|v| v.set_ien(true))
}
/// Disable global interrupt
fn disable_irq(&self) {
Self::regs().csr().modify(|v| v.set_ien(false))
}
/// Enable Read DMA
fn enable_read_dma(&self) {
Self::regs().csr().modify(|v| {
v.set_dmaren(true);
})
}
/// Disable Read DMA
fn disable_read_dma(&self) {
Self::regs().csr().modify(|v| {
v.set_dmaren(false);
})
}
/// Enable Write DMA
fn enable_write_dma(&self) {
Self::regs().csr().modify(|v| {
v.set_dmawen(true);
})
}
/// Disable Write DMA
fn disable_write_dma(&self) {
Self::regs().csr().modify(|v| {
v.set_dmawen(false);
})
}
/// Set NARGS value
fn set_argument_count(&self, n: AccessCount) {
Self::regs().csr().modify(|v| {
v.set_nargs(match n {
AccessCount::One => vals::Num::NUM1,
AccessCount::Two => vals::Num::NUM2,
})
})
}
/// Set NRES value
fn set_result_count(&self, n: AccessCount) {
Self::regs().csr().modify(|v| {
v.set_nres(match n {
AccessCount::One => vals::Num::NUM1,
AccessCount::Two => vals::Num::NUM2,
});
})
}
/// Set ARGSIZE and RESSIZE value
fn set_data_width(&self, arg: Width, res: Width) {
Self::regs().csr().modify(|v| {
v.set_argsize(match arg {
Width::Bits32 => vals::Size::BITS32,
Width::Bits16 => vals::Size::BITS16,
});
v.set_ressize(match res {
Width::Bits32 => vals::Size::BITS32,
Width::Bits16 => vals::Size::BITS16,
})
})
}
/// Read RRDY flag
fn ready_to_read(&self) -> bool {
Self::regs().csr().read().rrdy()
}
/// Write value to WDATA
fn write_argument(&self, arg: u32) {
Self::regs().wdata().write_value(arg)
}
/// Read value from RDATA
fn read_result(&self) -> u32 {
Self::regs().rdata().read()
}
}
/// CORDIC instance trait /// CORDIC instance trait
#[allow(private_bounds)] #[allow(private_bounds)]
pub trait Instance: SealedInstance + Peripheral<P = Self> + crate::rcc::RccPeripheral {} pub trait Instance: SealedInstance + Peripheral<P = Self> + crate::rcc::RccPeripheral {}

View file

@ -1,116 +0,0 @@
use super::*;
use crate::pac::cordic::vals;
/// Cordic instance
pub(super) trait SealedInstance {
/// Get access to CORDIC registers
fn regs() -> crate::pac::cordic::Cordic;
/// Set Function value
fn set_func(&self, func: Function) {
Self::regs()
.csr()
.modify(|v| v.set_func(vals::Func::from_bits(func as u8)));
}
/// Set Precision value
fn set_precision(&self, precision: Precision) {
Self::regs()
.csr()
.modify(|v| v.set_precision(vals::Precision::from_bits(precision as u8)))
}
/// Set Scale value
fn set_scale(&self, scale: Scale) {
Self::regs()
.csr()
.modify(|v| v.set_scale(vals::Scale::from_bits(scale as u8)))
}
/// Enable global interrupt
fn enable_irq(&self) {
Self::regs().csr().modify(|v| v.set_ien(true))
}
/// Disable global interrupt
fn disable_irq(&self) {
Self::regs().csr().modify(|v| v.set_ien(false))
}
/// Enable Read DMA
fn enable_read_dma(&self) {
Self::regs().csr().modify(|v| {
v.set_dmaren(true);
})
}
/// Disable Read DMA
fn disable_read_dma(&self) {
Self::regs().csr().modify(|v| {
v.set_dmaren(false);
})
}
/// Enable Write DMA
fn enable_write_dma(&self) {
Self::regs().csr().modify(|v| {
v.set_dmawen(true);
})
}
/// Disable Write DMA
fn disable_write_dma(&self) {
Self::regs().csr().modify(|v| {
v.set_dmawen(false);
})
}
/// Set NARGS value
fn set_argument_count(&self, n: AccessCount) {
Self::regs().csr().modify(|v| {
v.set_nargs(match n {
AccessCount::One => vals::Num::NUM1,
AccessCount::Two => vals::Num::NUM2,
})
})
}
/// Set NRES value
fn set_result_count(&self, n: AccessCount) {
Self::regs().csr().modify(|v| {
v.set_nres(match n {
AccessCount::One => vals::Num::NUM1,
AccessCount::Two => vals::Num::NUM2,
});
})
}
/// Set ARGSIZE and RESSIZE value
fn set_data_width(&self, arg: Width, res: Width) {
Self::regs().csr().modify(|v| {
v.set_argsize(match arg {
Width::Bits32 => vals::Size::BITS32,
Width::Bits16 => vals::Size::BITS16,
});
v.set_ressize(match res {
Width::Bits32 => vals::Size::BITS32,
Width::Bits16 => vals::Size::BITS16,
})
})
}
/// Read RRDY flag
fn ready_to_read(&self) -> bool {
Self::regs().csr().read().rrdy()
}
/// Write value to WDATA
fn write_argument(&self, arg: u32) {
Self::regs().wdata().write_value(arg)
}
/// Read value from RDATA
fn read_result(&self) -> u32 {
Self::regs().rdata().read()
}
}

View file

@ -105,27 +105,23 @@ impl<T: BasicInstance> interrupt::typelevel::Handler<T::Interrupt> for Interrupt
} }
} }
pub(crate) use sealed::State; pub(crate) struct State {
pub(crate) mod sealed { pub(crate) rx_waker: AtomicWaker,
use super::*; pub(crate) rx_buf: RingBuffer,
pub struct State { pub(crate) tx_waker: AtomicWaker,
pub(crate) rx_waker: AtomicWaker, pub(crate) tx_buf: RingBuffer,
pub(crate) rx_buf: RingBuffer, pub(crate) tx_done: AtomicBool,
pub(crate) tx_waker: AtomicWaker, }
pub(crate) tx_buf: RingBuffer,
pub(crate) tx_done: AtomicBool,
}
impl State { impl State {
/// Create new state /// Create new state
pub const fn new() -> Self { pub(crate) const fn new() -> Self {
Self { Self {
rx_buf: RingBuffer::new(), rx_buf: RingBuffer::new(),
tx_buf: RingBuffer::new(), tx_buf: RingBuffer::new(),
rx_waker: AtomicWaker::new(), rx_waker: AtomicWaker::new(),
tx_waker: AtomicWaker::new(), tx_waker: AtomicWaker::new(),
tx_done: AtomicBool::new(true), tx_done: AtomicBool::new(true),
}
} }
} }
} }

View file

@ -69,7 +69,7 @@ impl<'d, T: Instance, const SM: usize> PioStepper<'d, T, SM> {
let clock_divider: FixedU32<U8> = (125_000_000 / (freq * 136)).to_fixed(); let clock_divider: FixedU32<U8> = (125_000_000 / (freq * 136)).to_fixed();
assert!(clock_divider <= 65536, "clkdiv must be <= 65536"); assert!(clock_divider <= 65536, "clkdiv must be <= 65536");
assert!(clock_divider >= 1, "clkdiv must be >= 1"); assert!(clock_divider >= 1, "clkdiv must be >= 1");
T::PIO.sm(SM).clkdiv().write(|w| w.0 = clock_divider.to_bits() << 8); self.sm.set_clock_divider(clock_divider);
self.sm.clkdiv_restart(); self.sm.clkdiv_restart();
} }