Skip to main content

embassy_atmega328p/
lib.rs

1#![no_std]
2#![doc = include_str!("../README.md")]
3#![allow(non_snake_case)]
4#![cfg_attr(target_arch = "avr", feature(abi_avr_interrupt))]
5
6#[cfg(all(feature = "clock-16mhz", feature = "clock-8mhz"))]
7compile_error!("enable only one CPU clock feature: `clock-16mhz` or `clock-8mhz`");
8#[cfg(not(any(feature = "clock-16mhz", feature = "clock-8mhz")))]
9compile_error!("enable one CPU clock feature: `clock-16mhz` or `clock-8mhz`");
10
11pub mod adc;
12pub mod delay;
13pub mod gpio;
14pub mod i2c;
15pub mod onewire;
16pub mod pwm;
17pub mod spi;
18pub mod usart;
19
20#[cfg(any(feature = "time-driver-16mhz", feature = "time-driver-8mhz"))]
21mod time_driver;
22
23/// Low-level peripheral access crate for advanced use.
24///
25/// This API is not covered by this crate's semantic-versioning guarantees.
26#[cfg(feature = "unstable-pac")]
27pub use avr_device::atmega328p as pac;
28
29/// All peripheral ownership tokens exposed by this HAL.
30///
31/// There can be only one safe instance. Obtain it with [`init`] or
32/// [`try_init`].
33pub struct Peripherals {
34    pub ADC: adc::AdcPeripheral,
35    pub TWI: i2c::TwiPeripheral,
36    pub SPI: spi::SpiPeripheral,
37    pub USART0: usart::Usart0Peripheral,
38    pub TIMER0: pwm::Timer0,
39    pub TIMER2: pwm::Timer2,
40    pub PB0: gpio::PB0,
41    pub PB1: gpio::PB1,
42    pub PB2: gpio::PB2,
43    pub PB3: gpio::PB3,
44    pub PB4: gpio::PB4,
45    pub PB5: gpio::PB5,
46    pub PB6: gpio::PB6,
47    pub PB7: gpio::PB7,
48    pub PC0: gpio::PC0,
49    pub PC1: gpio::PC1,
50    pub PC2: gpio::PC2,
51    pub PC3: gpio::PC3,
52    pub PC4: gpio::PC4,
53    pub PC5: gpio::PC5,
54    pub PC6: gpio::PC6,
55    pub PD0: gpio::PD0,
56    pub PD1: gpio::PD1,
57    pub PD2: gpio::PD2,
58    pub PD3: gpio::PD3,
59    pub PD4: gpio::PD4,
60    pub PD5: gpio::PD5,
61    pub PD6: gpio::PD6,
62    pub PD7: gpio::PD7,
63}
64
65impl Peripherals {
66    fn new() -> Self {
67        Self {
68            ADC: adc::AdcPeripheral::new(),
69            TWI: i2c::TwiPeripheral::new(),
70            SPI: spi::SpiPeripheral::new(),
71            USART0: usart::Usart0Peripheral::new(),
72            TIMER0: pwm::Timer0::new(),
73            TIMER2: pwm::Timer2::new(),
74            PB0: gpio::PB0::new(),
75            PB1: gpio::PB1::new(),
76            PB2: gpio::PB2::new(),
77            PB3: gpio::PB3::new(),
78            PB4: gpio::PB4::new(),
79            PB5: gpio::PB5::new(),
80            PB6: gpio::PB6::new(),
81            PB7: gpio::PB7::new(),
82            PC0: gpio::PC0::new(),
83            PC1: gpio::PC1::new(),
84            PC2: gpio::PC2::new(),
85            PC3: gpio::PC3::new(),
86            PC4: gpio::PC4::new(),
87            PC5: gpio::PC5::new(),
88            PC6: gpio::PC6::new(),
89            PD0: gpio::PD0::new(),
90            PD1: gpio::PD1::new(),
91            PD2: gpio::PD2::new(),
92            PD3: gpio::PD3::new(),
93            PD4: gpio::PD4::new(),
94            PD5: gpio::PD5::new(),
95            PD6: gpio::PD6::new(),
96            PD7: gpio::PD7::new(),
97        }
98    }
99}
100
101/// CPU clock frequency selected at compile time.
102#[cfg(feature = "clock-16mhz")]
103pub const CPU_HZ: u32 = 16_000_000;
104
105/// CPU clock frequency selected at compile time.
106#[cfg(feature = "clock-8mhz")]
107pub const CPU_HZ: u32 = 8_000_000;
108
109/// Initializes the HAL and returns its peripheral ownership tokens.
110///
111/// # Panics
112///
113/// Panics if the HAL or the underlying PAC has already been taken.
114pub fn init() -> Peripherals {
115    try_init().expect("embassy-atmega328p is already initialized")
116}
117
118/// Attempts to initialize the HAL.
119///
120/// Returns `None` if the HAL or the underlying PAC has already been taken.
121pub fn try_init() -> Option<Peripherals> {
122    avr_device::atmega328p::Peripherals::take().map(|_| {
123        #[cfg(any(feature = "time-driver-16mhz", feature = "time-driver-8mhz"))]
124        time_driver::init();
125
126        Peripherals::new()
127    })
128}