From d458a337df51cf07cfb93ce50f27a9b9bad76cb2 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Thu, 4 May 2017 18:59:35 +0800 Subject: [PATCH] LED access function --- src/main.rs | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index f3e79ef..9e843ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,20 @@ use cortex_m::ctxt::Local; use cortex_m::exception::Handlers as ExceptionHandlers; use tm4c129x::interrupt::Handlers as InterruptHandlers; +const LED1: u8 = 0x10; +const LED2: u8 = 0x40; + +fn set_led(nr: u8, state: bool) { + cortex_m::interrupt::free(|cs| { + let gpio_k = tm4c129x::GPIO_PORTK.borrow(cs); + if state { + gpio_k.data.modify(|r, w| w.data().bits(r.data().bits() | nr)) + } else { + gpio_k.data.modify(|r, w| w.data().bits(r.data().bits() & !nr)) + } + }); +} + fn main() { hprintln!("Hello, world!"); @@ -19,17 +33,18 @@ fn main() { let sysctl = tm4c129x::SYSCTL.borrow(cs); let gpio_k = tm4c129x::GPIO_PORTK.borrow(cs); + // Bring up GPIO port K + sysctl.rcgcgpio.modify(|_, w| w.r9().bit(true)); + while !sysctl.prgpio.read().r9().bit() {} + + // Set up LEDs + gpio_k.dir.write(|w| w.dir().bits(LED1|LED2)); + gpio_k.den.write(|w| w.den().bits(LED1|LED2)); + // Set up system timer systick.set_reload(systick.get_ticks_per_10ms()); systick.enable_counter(); systick.enable_interrupt(); - - // Set up LED - sysctl.rcgcgpio.modify(|_, w| w.r9().bit(true)); - while !sysctl.prgpio.read().r9().bit() {} - - gpio_k.dir.write(|w| w.dir().bits(0x10)); - gpio_k.den.write(|w| w.den().bits(0x10)); }); } @@ -40,14 +55,14 @@ extern fn sys_tick(ctxt: SysTick) { let elapsed = ELAPSED.borrow(&ctxt); elapsed.set(elapsed.get() + 1); - - cortex_m::interrupt::free(|cs| { - let gpio_k = tm4c129x::GPIO_PORTK.borrow(cs); - - if elapsed.get() % 100 == 0 { - gpio_k.data.modify(|r, w| w.data().bits(r.data().bits() ^ 0x10)); - } - }) + if elapsed.get() % 100 == 0 { + set_led(LED1, true); + set_led(LED2, false); + } + if elapsed.get() % 100 == 50 { + set_led(LED1, false); + set_led(LED2, true); + } } #[used]