atsam4_hal/
lib.rs

1//! HAL for the ATSAM4 series of microcontrollers
2//!
3//! This is an implementation of the [`embedded-hal`] traits for the ATSAM4 microcontrollers
4//!
5//! [`embedded-hal`]: https://github.com/japaric/embedded-hal
6//!
7//! # Requirements
8//!
9//! This crate requires `arm-none-eabi-gcc` to be installed and available in `$PATH` to build.
10//!
11//! # Usage
12//!
13//! To build applications (binary crates) using this crate follow the [cortex-m-quickstart]
14//! instructions and add this crate as a dependency in step number 5 and make sure you enable the
15//! "rt" Cargo feature of this crate.
16//!
17//! [cortex-m-quickstart]: https://docs.rs/cortex-m-quickstart/~0.3
18//!
19
20//#![deny(missing_docs)]
21#![no_std]
22
23pub extern crate embedded_hal as hal;
24pub use hal::digital::v2::*;
25
26#[cfg(feature = "atsam4e16c")]
27pub use atsam4e16c_pac as pac;
28#[cfg(feature = "atsam4e16e")]
29pub use atsam4e16e_pac as pac;
30#[cfg(feature = "atsam4e8c")]
31pub use atsam4e8c_pac as pac;
32#[cfg(feature = "atsam4e8e")]
33pub use atsam4e8e_pac as pac;
34
35#[cfg(feature = "atsam4n16b")]
36pub use atsam4n16b_pac as pac;
37#[cfg(feature = "atsam4n16c")]
38pub use atsam4n16c_pac as pac;
39#[cfg(feature = "atsam4n8a")]
40pub use atsam4n8a_pac as pac;
41#[cfg(feature = "atsam4n8b")]
42pub use atsam4n8b_pac as pac;
43#[cfg(feature = "atsam4n8c")]
44pub use atsam4n8c_pac as pac;
45
46#[cfg(feature = "atsam4s2a")]
47pub use atsam4s2a_pac as pac;
48#[cfg(feature = "atsam4s2b")]
49pub use atsam4s2b_pac as pac;
50#[cfg(feature = "atsam4s2c")]
51pub use atsam4s2c_pac as pac;
52#[cfg(feature = "atsam4s4a")]
53pub use atsam4s4a_pac as pac;
54#[cfg(feature = "atsam4s4b")]
55pub use atsam4s4b_pac as pac;
56#[cfg(feature = "atsam4s4c")]
57pub use atsam4s4c_pac as pac;
58#[cfg(feature = "atsam4s8b")]
59pub use atsam4s8b_pac as pac;
60#[cfg(feature = "atsam4s8c")]
61pub use atsam4s8c_pac as pac;
62#[cfg(feature = "atsam4sa16b")]
63pub use atsam4sa16b_pac as pac;
64#[cfg(feature = "atsam4sa16c")]
65pub use atsam4sa16c_pac as pac;
66#[cfg(feature = "atsam4sd16b")]
67pub use atsam4sd16b_pac as pac;
68#[cfg(feature = "atsam4sd16c")]
69pub use atsam4sd16c_pac as pac;
70#[cfg(feature = "atsam4sd32b")]
71pub use atsam4sd32b_pac as pac;
72#[cfg(feature = "atsam4sd32c")]
73pub use atsam4sd32c_pac as pac;
74
75use core::mem;
76
77// NOTE: In ASF atsam4s uses sam/drivers/adc/adc.c whereas atsam4n uses sam/drivers/adc/adc2.c
78#[cfg(feature = "atsam4s")]
79pub mod adc;
80pub mod chipid;
81pub mod clock;
82pub mod delay;
83pub mod efc;
84pub mod gpio;
85pub mod pdc;
86pub mod prelude;
87pub mod rtt;
88pub mod serial;
89pub mod spi;
90pub mod static_memory_controller;
91pub mod timer;
92#[cfg(all(feature = "usb", any(feature = "atsam4e", feature = "atsam4s")))]
93pub mod udp;
94pub mod watchdog;
95
96mod sealed;
97
98/// Borrows a peripheral without checking if it has already been taken
99/// # Safety
100unsafe trait BorrowUnchecked {
101    fn borrow_unchecked<T>(f: impl FnOnce(&mut Self) -> T) -> T;
102}
103
104macro_rules! borrow_unchecked {
105    ($($peripheral:ident),*) => {
106        $(
107            unsafe impl BorrowUnchecked for pac::$peripheral {
108                fn borrow_unchecked<T>(f: impl FnOnce(&mut Self) -> T) -> T {
109                    let mut p = unsafe { mem::transmute(()) };
110                    f(&mut p)
111                }
112            }
113        )*
114    }
115}
116
117borrow_unchecked!(TC0);
118
119#[cfg(any(feature = "atsam4e_e", feature = "atsam4n_c", feature = "atsam4s_c"))]
120borrow_unchecked!(TC1);
121
122#[cfg(feature = "atsam4e_e")]
123borrow_unchecked!(TC2);
124
125#[cfg(all(feature = "usb", any(feature = "atsam4e", feature = "atsam4s")))]
126borrow_unchecked!(PMC, UDP);