~ruther/avr-device

dc45b3ccdc30e40d28368ab1a9554cedf8e5459f — Rahix 5 years ago f77864d
Switch to a new design as outlined in #17

Signed-off-by: Rahix <rahix@rahix.de>
5 files changed, 125 insertions(+), 84 deletions(-)

M Cargo.toml
M Makefile
M src/devices/mod.rs
M src/interrupt.rs
M src/lib.rs
M Cargo.toml => Cargo.toml +0 -1
@@ 15,4 15,3 @@ attiny85 = []
[dependencies]
bare-metal = "0.2.4"
vcell = "0.1.0"
cfg-if = "0.1.7"

M Makefile => Makefile +3 -2
@@ 42,8 42,9 @@ src/devices/%/mod.rs: src/devices/%/mod.full.rs
	@RUSTUP_TOOLCHAIN=nightly rustfmt $@
	@# Remove the `extern crate` lines
	@sed -i'' -e "1,7d" $@
	@# Make DEVICE_PERIPHERALS visible crate-wide
	@sed -i'' -e 's/\(static mut DEVICE_PERIPHERALS\)/pub(crate) \1/' $@
	@# Remove DEVICE_PERIPHERALS declaration and replace it with a reference
	@# to the global version
	@sed -i'' -e '/^\#\[allow(renamed_and_removed_lints)\]/,+3cuse crate::devices::DEVICE_PERIPHERALS;' $@

clean:
	@echo -e "\tCLEAN\t\t./svd/"

M src/devices/mod.rs => src/devices/mod.rs +97 -11
@@ 1,13 1,99 @@
cfg_if::cfg_if! {
    if #[cfg(feature = "atmega1280")] {
        pub mod atmega1280;
    } else if #[cfg(feature = "atmega8")] {
        pub mod atmega8;
    } else if #[cfg(feature = "atmega328p")] {
        pub mod atmega328p;
    } else if #[cfg(feature = "atmega32u4")] {
        pub mod atmega32u4;
    } else if #[cfg(feature = "attiny85")] {
        pub mod attiny85;
#[allow(renamed_and_removed_lints)]
#[allow(private_no_mangle_statics)]
#[no_mangle]
pub(crate) static mut DEVICE_PERIPHERALS: bool = false;

/// [ATmega1280](https://www.microchip.com/wwwproducts/en/ATmega1280)
#[cfg(feature = "atmega1280")]
pub mod atmega1280;

#[cfg(feature = "atmega1280")]
impl atmega1280::Peripherals {
    /// Returns all the peripherals *once*
    #[inline]
    pub fn take() -> Option<Self> {
        crate::interrupt::free(|_| {
            if unsafe { DEVICE_PERIPHERALS } {
                None
            } else {
                Some(unsafe { atmega1280::Peripherals::steal() })
            }
        })
    }
}

/// [ATmega328P](https://www.microchip.com/wwwproducts/en/ATmega328P)
#[cfg(feature = "atmega328p")]
pub mod atmega328p;

#[cfg(feature = "atmega328p")]
impl atmega328p::Peripherals {
    /// Returns all the peripherals *once*
    #[inline]
    pub fn take() -> Option<Self> {
        crate::interrupt::free(|_| {
            if unsafe { DEVICE_PERIPHERALS } {
                None
            } else {
                Some(unsafe { atmega328p::Peripherals::steal() })
            }
        })
    }
}

/// [ATmega32U4](https://www.microchip.com/wwwproducts/en/ATmega32U4)
#[cfg(feature = "atmega32u4")]
pub mod atmega32u4;

#[cfg(feature = "atmega32u4")]
impl atmega32u4::Peripherals {
    /// Returns all the peripherals *once*
    #[inline]
    pub fn take() -> Option<Self> {
        crate::interrupt::free(|_| {
            if unsafe { DEVICE_PERIPHERALS } {
                None
            } else {
                Some(unsafe { atmega32u4::Peripherals::steal() })
            }
        })
    }
}

/// [ATmega8](https://www.microchip.com/wwwproducts/en/ATmega8)
#[cfg(feature = "atmega8")]
pub mod atmega8;

#[cfg(feature = "atmega8")]
impl atmega8::Peripherals {
    /// Returns all the peripherals *once*
    #[inline]
    pub fn take() -> Option<Self> {
        crate::interrupt::free(|_| {
            if unsafe { DEVICE_PERIPHERALS } {
                None
            } else {
                Some(unsafe { atmega8::Peripherals::steal() })
            }
        })
    }
}

/// [ATtiny85](https://www.microchip.com/wwwproducts/en/ATtiny85)
#[cfg(feature = "attiny85")]
pub mod attiny85;

#[cfg(feature = "attiny85")]
impl attiny85::Peripherals {
    /// Returns all the peripherals *once*
    #[inline]
    pub fn take() -> Option<Self> {
        crate::interrupt::free(|_| {
            if unsafe { DEVICE_PERIPHERALS } {
                None
            } else {
                Some(unsafe { attiny85::Peripherals::steal() })
            }
        })
    }
}

M src/interrupt.rs => src/interrupt.rs +3 -3
@@ 1,6 1,6 @@
//! Interrupts
//! Chip-Generic Interrupt Utilities
//!
//! For the most part, [interrupt::free] is what you want:
//! For the most part, [crate::interrupt::free] is what you want:
//!
//! ```
//! atmega32u4::interrupt::free(|cs| {


@@ 25,7 25,7 @@ pub fn disable() {
///
/// # Safety
///
/// - Do not call this function inside an `interrupt::free` critical section
/// - Do not call this function inside an [crate::interrupt::free] critical section
pub fn enable() {
    unsafe {
        asm!(

M src/lib.rs => src/lib.rs +22 -67
@@ 1,11 1,12 @@
//! This crate contains register definitions for
#![cfg_attr(feature = "atmega1280", doc = "**atmega1280**.")]
#![cfg_attr(feature = "atmega8", doc = "**atmega8**.")]
#![cfg_attr(feature = "atmega328p", doc = "**atmega328p**.")]
#![cfg_attr(feature = "atmega32u4", doc = "**atmega32u4**.")]
#![cfg_attr(feature = "attiny85", doc = "**attiny85**.")]
//! Which chip the crate is built for depends on the feature flag used.
#![cfg_attr(feature = "atmega1280", doc = "**atmega1280**,")]
#![cfg_attr(feature = "atmega8", doc = "**atmega8**,")]
#![cfg_attr(feature = "atmega328p", doc = "**atmega328p**,")]
#![cfg_attr(feature = "atmega32u4", doc = "**atmega32u4**,")]
#![cfg_attr(feature = "attiny85", doc = "**attiny85**,")]
//! and a few things which apply to AVR microcontrollers generally.
//!
//! Which chips the crate is built for depends on the feature flag used.
//! The following chips are available (using feature flags of the same name):
//! * `atmega1280`
//! * `atmega8`


@@ 15,73 16,27 @@
#![no_std]
#![feature(asm)]

#[allow(non_camel_case_types, unused_attributes)]
mod devices;

pub mod interrupt;

cfg_if::cfg_if! {
    if #[cfg(feature= "atmega1280")] {
        pub use crate::devices::atmega1280::*;
    } else if #[cfg(feature = "atmega8")] {
        pub use crate::devices::atmega8::*;
    } else if #[cfg(feature = "atmega328p")] {
        pub use crate::devices::atmega328p::*;
    } else if #[cfg(feature = "atmega32u4")] {
        pub use crate::devices::atmega32u4::*;
    } else if #[cfg(feature = "attiny85")] {
        pub use crate::devices::attiny85::*;
    } else {
        compile_error!("You need to select a chip as a feature!");
    }
}
#[allow(non_camel_case_types, unused_attributes, unreachable_patterns)]
mod devices;

// TODO: Find a better way to do this
#[cfg(any(
    all(feature = "atmega1280", any(
        feature = "atmega8",
        feature = "atmega328p",
        feature = "atmega32u4",
        feature = "attiny85",
    )),
    all(feature = "atmega8", any(
        feature = "atmega1280",
        feature = "atmega328p",
        feature = "atmega32u4",
        feature = "attiny85",
    )),
    all(feature = "atmega328p", any(
        feature = "atmega1280",
        feature = "atmega8",
        feature = "atmega32u4",
        feature = "attiny85",
    )),
    all(feature = "atmega32u4", any(
        feature = "atmega1280",
        feature = "atmega8",
        feature = "atmega329p",
        feature = "attiny85",
    )),
))]
compile_error!("You cannot select multiple chips at once!");
#[cfg(feature = "atmega1280")]
pub use crate::devices::atmega1280;
#[cfg(feature = "atmega328p")]
pub use crate::devices::atmega328p;
#[cfg(feature = "atmega32u4")]
pub use crate::devices::atmega32u4;
#[cfg(feature = "atmega8")]
pub use crate::devices::atmega8;
#[cfg(feature = "attiny85")]
pub use crate::devices::attiny85;

#[cfg(any(
#[cfg(not(any(
    feature = "atmega1280",
    feature = "atmega8",
    feature = "atmega328p",
    feature = "atmega32u4",
    feature = "attiny85",
))]
impl Peripherals {
    /// Returns all the peripherals *once*
    #[inline]
    pub fn take() -> Option<Self> {
        interrupt::free(|_| {
            if unsafe { DEVICE_PERIPHERALS } {
                None
            } else {
                Some(unsafe { Peripherals::steal() })
            }
        })
    }
}
)))]
compile_error!("You need to select at least one chip as a feature!");

Do not follow this link