Skip to content

Commit

Permalink
Example documentation and cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
cr1901 committed Jan 11, 2020
1 parent 0a5732d commit 9405234
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 3 deletions.
8 changes: 8 additions & 0 deletions examples/blinky.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
//! Basic "hello world" blink demo for the [MSP-EXP430G2](http://www.ti.com/tool/MSP-EXP430G2)
//! development kit using a software delay loop- in Rust!
//!
//! Although unnecessary for running the demo, this example also shows the syntax for declaring
//! an interrupt.
//!
//! ---
#![no_main]
#![no_std]
#![feature(abi_msp430_interrupt)]
Expand Down
12 changes: 12 additions & 0 deletions examples/override-default-handler.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
//! Overriding the default interrupt handler with user code.
//!
//! The default handler is used for _any_ interrupts whose handler is not explicitly defined
//! in the application. Normally an infinite loop, `DefaultHandler` can be overridden based on
//! a user's needs.
//!
//! This demo is meant to more to be compiled and then examined with `msp430-elf-objdump` and
//! other [binutils](https://www.gnu.org/software/binutils/) programs, rather than run on
//! a development board.
//!
//! ---
#![no_main]
#![no_std]
#![feature(abi_msp430_interrupt)]
Expand Down
28 changes: 28 additions & 0 deletions examples/timer-oncecell.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
//! Sharing data between a main thread and an interrupt handler safely.
//!
//! This example uses the externally-provided [once_cell][once] to safely share access to msp430
//! peripherals between a main thread and interrupt.
//!
//! The different between [OnceCell][once] and [RefCell][ref] is that setting the data contained
//! inside a [OnceCell][once] can be deferred to run time, but can only be set once. In contrast,
//! the data contained within a [RefCell][ref] can be set multiple times throughout a program, but
//! the contained data must be initialized at compile time. Additionally, [RefCell][ref] will
//! panic if a second thread tries to change its value while the first thread is mutating the
//! variable.
//!
//! The [Periperhals](msp430g2553::Peripherals) type, and individual peripherals never need
//! to be modified. Therefore, [Periperhals](msp430g2553::Peripherals) (or a subset of the
//! Periperhals _moved_ to another `struct`, if
//! [building](https://blog.japaric.io/brave-new-io/#freezing-the-clock-configuration)
//! higher-level abstractions) are good candidates to [`Send`](core::marker::Send) to a
//! [OnceCell][once]. [OnceCell][once] in general seems to have better space usage than
//! [RefCell][ref] due to its invariants.
//!
//! As with [timer] and [timer-unsafe], this example uses the `TIMER0_A1` interrupt to
//! blink LEDs on the [MSP-EXP430G2](http://www.ti.com/tool/MSP-EXP430G2) development kit.
//!
//! [once]: once_cell::unsync::OnceCell
//! [ref]: core::cell::RefCell
//!
//! ---
#![no_main]
#![no_std]
#![feature(abi_msp430_interrupt)]
Expand Down
19 changes: 17 additions & 2 deletions examples/timer-unsafe.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
//! Sharing data between a main thread and an interrupt handler safely using `unsafe`
//! code blocks in contexts where they can't cause
//! (Undefined Behavior)[https://doc.rust-lang.org/reference/behavior-considered-undefined.html].
//!
//! This example uses the normally `unsafe`
//! [`Peripherals::steal()`][steal] method to safely share access to msp430 peripherals between a
//! main thread and interrupt. All uses of [`steal()`] are commented to explain _why_ its usage
//! is safe in that particular context.
//!
//! As with [timer] and [timer-oncecell], this example uses the `TIMER0_A1` interrupt to blink
//! LEDs on the [MSP-EXP430G2](http://www.ti.com/tool/MSP-EXP430G2) development kit.
//!
//! [steal]: msp430g2553::Peripherals::steal
//!
//! ---
#![no_main]
#![no_std]
#![feature(abi_msp430_interrupt)]
Expand Down Expand Up @@ -44,9 +60,8 @@ fn main() -> ! {
}

#[interrupt]
#[allow(unused_variables)]
fn TIMER0_A1() {
mspint::free(|cs| {
mspint::free(|_cs| {
// Safe because msp430 disables interrupts on handler entry. Therefore the handler
// has full control/access to peripherals without data races.
let p = unsafe { Peripherals::steal() };
Expand Down
11 changes: 10 additions & 1 deletion examples/timer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//! Sharing data between a main thread and an interrupt handler safely.
//!
//! This example uses the [libcore](core)-provided [RefCell](core::cell::RefCell) to safely share
//! access to msp430 peripherals between a main thread and interrupt.
//!
//! As with [timer-unsafe] and [timer-oncecell], this example uses the `TIMER0_A1` interrupt to
//! blink LEDs on the [MSP-EXP430G2](http://www.ti.com/tool/MSP-EXP430G2) development kit.
//!
//! ---
#![no_main]
#![no_std]
#![feature(abi_msp430_interrupt)]
Expand Down Expand Up @@ -51,7 +61,6 @@ fn main() -> ! {
}

#[interrupt]
#[allow(unused_variables)]
fn TIMER0_A1() {
mspint::free(|cs| {
let p_ref = PERIPHERALS.borrow(&cs).borrow();
Expand Down

0 comments on commit 9405234

Please sign in to comment.