rust-embedded-talk/example-source/msp432-app/src/main.rs

82 lines
2.2 KiB
Rust

#![no_std]
#![no_main]
// pick a panicking behavior
extern crate panic_halt; // you can put a breakpoint on `rust_begin_unwind` to catch panics
// extern crate panic_abort; // requires nightly
// extern crate panic_itm; // logs messages over ITM; requires ITM support
// extern crate panic_semihosting; // logs messages to the host stderr; requires a debugger
use cortex_m::{self, peripheral::syst::SystClkSource};
use cortex_m_rt::entry;
use msp432p401r;
use core::num::Wrapping;
const PIN0: u8 = (1u8 << 0);
const PIN1: u8 = (1u8 << 1);
const PIN2: u8 = (1u8 << 2);
//const PIN3: u8 = (1u8 << 3);
const PIN4: u8 = (1u8 << 4);
//const PIN5: u8 = (1u8 << 5);
//const PIN6: u8 = (1u8 << 6);
//const PIN7: u8 = (1u8 << 7);
const WDOG_PWORD: u8 = 0x5a;
#[entry]
fn main() -> ! {
let cp = cortex_m::Peripherals::take().unwrap();
let p = msp432p401r::Peripherals::take().unwrap();
let mut syst = cp.SYST;
let wdt = p.WDT_A;
let dio = p.DIO;
// disable watchdog timer
wdt.wdtctl.write(|w| unsafe {
w.wdtpw().bits(WDOG_PWORD)
.wdthold().wdthold_1()
});
// configure the system timer to wrap around every ~1 second
syst.set_clock_source(SystClkSource::Core);
syst.set_reload(20_000);
syst.enable_counter();
// configure digital IO pins
dio.padir.modify(|_r, w| unsafe {
w.p1dir().bits(PIN0)
.p2dir().bits(PIN0 | PIN1 | PIN2)
});
// Enable pull-up on P1.4 switch
dio.paren.modify(|_r, w| unsafe { w.p1ren().bits(PIN4) });
dio.pads.modify(|_r, w| unsafe { w.p1ds().bits(PIN4) });
// Turn on LED1 on P1.0 and enable pull-up on P1.1 switch
dio.paout.write(|w| unsafe {
w.p1out().bits(PIN0 | PIN4)
.p2out().bits(0)
});
let mut count: Wrapping<u16> = Wrapping(0);
let mut input: u8;
loop {
while !syst.has_wrapped() {}
// The button pulls the input to GND
input = (dio.pain.read().p1in().bits() & PIN4) >> 4;
count += Wrapping(1);
let _led1: u8 = ((count.0 & 0x0100) >> 8) as u8;
let led2: u8 = ((count.0 & 0b0000_0111_0000_0000) >> 8) as u8;
// toggle LEDs
dio.paout.write(|w| unsafe {
w.p1out().bits(input | PIN4)
.p2out().bits(led2)
});
}
}