X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=src%2Fapplication%2Fmain.rs;h=74f199906853468fd5c6ac22fd41f25bc333480d;hb=45b7bcb737ad4bedf4f93548869f5037a6a2c1c2;hp=b1bf3afd24818147146c5a2a89e3773adcdd9372;hpb=ce5666c12eb4333fd71adb58d8d5ad740fcffe8b;p=gps-watch.git diff --git a/src/application/main.rs b/src/application/main.rs index b1bf3af..74f1999 100644 --- a/src/application/main.rs +++ b/src/application/main.rs @@ -22,11 +22,13 @@ */ #![no_std] -#![crate_type="staticlib"] +#![no_main] #[link(name="libcommon.rlib")] extern crate common; +mod uart0; + use common::buffer::Buffer; use common::clock; use common::systick; @@ -34,8 +36,10 @@ use common::port; use common::gpio; use common::nvic; use common::i2c; +use common::uart; use common::usb_serial; use common::display; +use common::gps; extern { fn enable_interrupts(); @@ -43,9 +47,36 @@ extern { static mut cdc_tx_buf: Buffer; } +struct Timer { + state: u32, + delay_ms: u32, + last_update_ticks: u32, +} + +impl Timer { + pub fn new(delay_ms: u32) -> Timer { + Timer { + state: 0, + delay_ms: delay_ms, + last_update_ticks: systick::now(), + } + } + + pub fn update(&mut self, func: F) + where F: FnOnce(u32) -> u32 + { + if systick::has_timeout_ms(self.last_update_ticks, self.delay_ms) { + self.state = func(self.state); + + self.last_update_ticks = systick::now(); + } + } +} + #[no_mangle] -pub unsafe extern fn main() { +pub unsafe extern "C" fn _start() -> ! { clock::configure(); + clock::enable_osc0(); systick::init(); port::init(); @@ -61,6 +92,14 @@ pub unsafe extern fn main() { gpio::set_direction(gpio::GPIOB, 1 << 16, gpio::Direction::Output); port::set_af(port::PORTB, 16, 1); + // Configure pins for UART0. + port::set_af(port::PORTE, 20, 4); + port::set_af(port::PORTE, 21, 4); + + // Configure pin for the GPS's reset line. + gpio::set_direction(gpio::GPIOB, 1 << 1, gpio::Direction::Output); + port::set_af(port::PORTB, 1, 1); + // Configure upper right push button. gpio::set_direction(gpio::GPIOA, 1 << 12, gpio::Direction::Input); port::set_af(port::PORTA, 12, 1); @@ -78,11 +117,34 @@ pub unsafe extern fn main() { display.init(); display.clear(); + // Hold GPS in reset while configuring its UART. + gpio::clear(gpio::GPIOB, 1); + systick::delay_ms(50); + uart::configure(uart::UART0); + systick::delay_ms(50); + gpio::set(gpio::GPIOB, 1); + + nvic::enable_irq(12); // UART0 + + let mut gps = gps::Gps::new(); + + let mut heart_icon_timer = Timer::new(1000); + loop { - systick::delay_ms(1000); + let mut tap = gps::TimeAndPos::new(); + + while gps.update(&mut tap) { + } - cdc_tx_buf.write(b".\n"); - cdc_tx_buf.flush(); + heart_icon_timer.update(|state| { + if state == 1 { + display.hide_icon(display::Icon::Heart); + 0 + } else { + display.show_icon(display::Icon::Heart); + 1 + } + }); if (gpio::get(gpio::GPIOA) & (1 << 12)) == 0 { nvic::system_reset();