application: Set up pins for the MX25L flash memory.
[gps-watch.git] / src / application / main.rs
index bd1091f3c2bbca3632de41c8c536bd7619b34d80..8f7bd6d1da6cdf5b76a16927b34cd5d0ad92b1b1 100644 (file)
  */
 
 #![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;
 use common::port;
 use common::gpio;
 use common::nvic;
+use common::i2c;
+use common::spi;
+use common::uart;
 use common::usb_serial;
+use common::display;
+use common::gps;
+use common::screen;
+use common::time::Time;
 
 extern {
     fn enable_interrupts();
@@ -41,12 +50,83 @@ 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<F>(&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();
+        }
+    }
+}
+
+#[inline(never)]
+#[panic_handler]
+fn panic(_info: &core::panic::PanicInfo) -> ! {
+    loop {
+        if (gpio::get(gpio::GPIOA) & (1 << 12)) == 0 {
+            nvic::system_reset();
+        }
+    }
+}
+
 #[no_mangle]
-pub unsafe extern fn main() {
+pub unsafe extern "C" fn _start() -> ! {
     clock::configure();
+    clock::enable_osc0();
     systick::init();
     port::init();
 
+    // Configure pins for I2C0.
+    port::set_af(port::PORTC, 8, 2);
+    port::set_af(port::PORTC, 9, 2);
+
+    i2c::configure(i2c::I2C0);
+
+    nvic::disable_irq(8); // I2C0
+
+    // Configure pin for the display's reset line.
+    gpio::set_direction(gpio::GPIOB, 1 << 16, gpio::Direction::Output);
+    port::set_af(port::PORTB, 16, 1);
+
+    // Configure pin for the MX25L's chip select line.
+    gpio::set_direction(gpio::GPIOD, 1 << 0, gpio::Direction::Output);
+    port::set_af(port::PORTD, 0, 1);
+    gpio::set(gpio::GPIOD, 1 << 0);
+
+    // Configure pins for SPI0.
+    port::set_af(port::PORTD, 1, 2);
+    port::set_af(port::PORTD, 2, 5);
+    port::set_af(port::PORTD, 3, 5);
+
+    spi::configure(spi::SPI0);
+
+    nvic::disable_irq(10); // SPI0
+
+    // 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);
@@ -59,11 +139,96 @@ pub unsafe extern fn main() {
     cdc_tx_buf.write(b"\n");
     cdc_tx_buf.flush();
 
+    let mut display = display::Display::new(gpio::GPIOB, 1 << 16, 0x3c);
+
+    display.init();
+    display.clear();
+
+    let mut screen = screen::Screen::new();
+
+    // 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 gps_has_fix = false;
+    let mut gps_has_fix_ticks = 0;
+
+    let mut heart_icon_timer = Timer::new(1000);
+    let mut gps_icon_timer = Timer::new(500);
+
+    let mut prev_tap = gps::TimeAndPos::new();
+
     loop {
-        systick::delay_ms(1000);
+        let mut tap = gps::TimeAndPos::new();
+        let mut show_time = false;
+        let old_gps_has_fix = gps_has_fix;
+
+        while gps.update(&mut tap) {
+            prev_tap = tap;
+
+            show_time = true;
+
+            gps_has_fix = true;
+            gps_has_fix_ticks = systick::now();
+        }
+
+        // Did GPS fix information expire?
+        if gps_has_fix && systick::has_timeout_ms(gps_has_fix_ticks, 1500) {
+            gps_has_fix = false;
+        }
+
+        if gps_has_fix && !old_gps_has_fix {
+            display.show_icon(display::Icon::SatelliteBody);
+            display.show_icon(display::Icon::SatelliteWave1);
+            display.show_icon(display::Icon::SatelliteWave2);
+        } else if !gps_has_fix {
+            gps_icon_timer.update(|state| {
+                if state == 1 {
+                    display.show_icon(display::Icon::SatelliteWave1);
+                    2
+                } else if state == 2 {
+                    display.show_icon(display::Icon::SatelliteWave2);
+                    3
+                } else if state == 3 {
+                    display.hide_icon(display::Icon::SatelliteBody);
+                    display.hide_icon(display::Icon::SatelliteWave1);
+                    display.hide_icon(display::Icon::SatelliteWave2);
+                    0
+                } else {
+                    display.show_icon(display::Icon::SatelliteBody);
+                    1
+                }
+            });
+        }
+
+        if show_time {
+            if let Some(tm) = Time::from_unix_time(prev_tap.unix_time) {
+                let mut time_s = [b' '; 8];
+                tm.fmt_time(&mut time_s);
+
+                screen.clear();
+                screen.draw_text(&time_s);
+
+                display.draw(&screen);
+            }
+        }
 
-        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();