Skip to content

Commit 6ab6f94

Browse files
mbueschRahix
authored andcommitted
Add basic documentation for beginners
1 parent 9286b40 commit 6ab6f94

File tree

2 files changed

+121
-31
lines changed

2 files changed

+121
-31
lines changed

src/interrupt.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,29 @@
33
//! For the most part, [crate::interrupt::free] is what you want:
44
//!
55
//! ```
6-
//! atmega32u4::interrupt::free(|cs| {
6+
//! avr_device::interrupt::free(|cs| {
77
//! // Interrupts are disabled here
8-
//! })
8+
//! });
9+
//! ```
10+
//!
11+
//! To access shared state, Mutex can be used:
12+
//!
13+
//! ```
14+
//! use avr_device::interrupt::Mutex;
15+
//! use core::cell::Cell;
16+
//!
17+
//! // Use Cell, if the wrapped type is Copy.
18+
//! // Use RefCell, if the wrapped type is not Copy or if you need a reference to it for other reasons.
19+
//! static MYGLOBAL: Mutex<Cell<u16>> = Mutex::new(Cell::new(0));
20+
//!
21+
//! avr_device::interrupt::free(|cs| {
22+
//! // Interrupts are disabled here
23+
//!
24+
//! // Acquire mutex to global variable.
25+
//! let myglobal_ref = MYGLOBAL.borrow(&cs);
26+
//! // Write to the global variable.
27+
//! myglobal_ref.set(42);
28+
//! });
929
//! ```
1030
1131
pub use bare_metal::{CriticalSection, Mutex, Nr};

src/lib.rs

Lines changed: 99 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,101 @@
3535
)]
3636
//! Which chips the crate is built for depends on the feature flag used.
3737
//! The following chips are available (using feature flags of the same name):
38-
//! * `at90usb1286`
39-
//! * `atmega1280`
40-
//! * `atmega1284p`
41-
//! * `atmega128rfa1`
42-
//! * `atmega164pa`
43-
//! * `atmega168`
44-
//! * `atmega2560`
45-
//! * `atmega8`
46-
//! * `atmega8u2`
47-
//! * `atmega328p`
48-
//! * `atmega328pb`
49-
//! * `atmega32u4`
50-
//! * `atmega4809`
51-
//! * `atmega48p`
52-
//! * `atmega64`
53-
//! * `atmega644`
54-
//! * `attiny13a`
55-
//! * `attiny167`
56-
//! * `attiny1614`
57-
//! * `attiny202`
58-
//! * `attiny2313`
59-
//! * `attiny2313a`
60-
//! * `attiny816`
61-
//! * `attiny84`
62-
//! * `attiny841`
63-
//! * `attiny85`
64-
//! * `attiny861`
65-
//! * `attiny88`
38+
//! `at90usb1286`,
39+
//! `atmega1280`,
40+
//! `atmega1284p`,
41+
//! `atmega128rfa1`,
42+
//! `atmega164pa`,
43+
//! `atmega168`,
44+
//! `atmega2560`,
45+
//! `atmega8`,
46+
//! `atmega8u2`,
47+
//! `atmega328p`,
48+
//! `atmega328pb`,
49+
//! `atmega32u4`,
50+
//! `atmega4809`,
51+
//! `atmega48p`,
52+
//! `atmega64`,
53+
//! `atmega644`,
54+
//! `attiny13a`,
55+
//! `attiny167`,
56+
//! `attiny1614`,
57+
//! `attiny202`,
58+
//! `attiny2313`,
59+
//! `attiny2313a`,
60+
//! `attiny816`,
61+
//! `attiny84`,
62+
//! `attiny841`,
63+
//! `attiny85`,
64+
//! `attiny861`,
65+
//! `attiny88`,
66+
//!
67+
//! # How to use this crate?
68+
//!
69+
//! In most cases you probably don't want to use this crate directly.
70+
//!
71+
//! This is a low level peripheral access crate (PAC).
72+
//! There are more high level crates, like `avr-hal`, that implement a more convenient
73+
//! and higher level API built ontop of `avr-device`.
74+
//! However, sometimes it's required to operate on bare device register level.
75+
//! That's what this crate is for.
76+
//!
77+
//! ## Main program entry point
78+
//!
79+
//! ```
80+
//! #[avr_device::entry] // requires avr_device's rt feature.
81+
//! fn main() -> ! {
82+
//! loop {
83+
//! // Your code here.
84+
//! }
85+
//! }
86+
//! ```
87+
//!
88+
//! ## Get access to the device peripherals
89+
//!
90+
//! ```ignore
91+
//! // To get access to the Peripherals struct, use this *once*:
92+
//! let dp = avr_device::atmega328p::Peripherals::take().unwrap();
93+
//! ```
94+
//!
95+
//! ## Example: Digital I/O port access
96+
//!
97+
//! ```ignore
98+
//! // Configure bit 5 of port B as output:
99+
//! dp.PORTB.ddrb.write(|w| w.pb5().set_bit());
100+
//! // Clear bit 5 of port B:
101+
//! dp.PORTB.portb.write(|w| w.pb5().clear_bit());
102+
//! // Set bit 5 of port B:
103+
//! dp.PORTB.portb.write(|w| w.pb5().set_bit());
104+
//!
105+
//! // Configure bit 6 of port B as input with pullup:
106+
//! dp.PORTB.ddrb.write(|w| w.pb6().clear_bit());
107+
//! dp.PORTB.portb.write(|w| w.pb6().set_bit());
108+
//! // Read bit 6 of pin B:
109+
//! let _mybit = dp.PORTB.pinb.read().pb6().bit_is_set();
110+
//! // Read bit 6 and write to bit 5 of port B:
111+
//! dp.PORTB.portb.modify(|r, w| w.pb6().bit(r.pb5().bit_is_set()))
112+
//! ```
113+
//!
114+
//! ## Example: Other peripheral register access
115+
//!
116+
//! Other peripheral register accesses are similar to I/O port access.
117+
//! Please read the documentation of the `struct R` and `struct W`
118+
//! for the register of interest.
119+
//!
120+
//! e.g. [crate::atmega328p::spi::spcr::W]
121+
//!
122+
//! ```ignore
123+
//! // Set SPE in SPCR (Enable SPI):
124+
//! dp.SPI.spcr.write(|w| w.spe().set_bit());
125+
//! ```
126+
//!
127+
//! # Crate feature flags
128+
//!
129+
//! * Device selection: To enable your device, select the crate feature that matches your device.
130+
//! For a full list of supported devices, see the list at the beginning of this documentation.
131+
//! * To enable the crate's runtime environment, use the `rt` feature.
132+
66133
#![no_std]
67134
#![cfg_attr(avr_device_asm_macro, feature(asm_experimental_arch))]
68135
#![cfg_attr(not(avr_device_asm_macro), feature(llvm_asm))]
@@ -91,6 +158,7 @@ pub mod generic;
91158
/// vector. This is an unfortunate requirement of the current crate
92159
/// architecture and might change in the future.
93160
/// - The function must have a signature of `[unsafe] fn() [-> !]`.
161+
/// - This macro requires the avr-device `rt` crate feature.
94162
#[cfg(feature = "rt")]
95163
pub use avr_device_macros::interrupt;
96164

@@ -105,7 +173,9 @@ pub use avr_device_macros::interrupt;
105173
/// }
106174
/// ```
107175
///
108-
/// The entry function must have a signature of `[unsafe] fn() -> !`.
176+
/// # Constraints
177+
/// - The entry function must have a signature of `[unsafe] fn() -> !`.
178+
/// - This macro requires the avr-device `rt` crate feature.
109179
#[cfg(feature = "rt")]
110180
pub use avr_device_macros::entry;
111181

0 commit comments

Comments
 (0)