74f199906853468fd5c6ac22fd41f25bc333480d
[gps-watch.git] / src / application / main.rs
1 /*
2  * Copyright (c) 2019 Tilman Sauerbeck (tilman at code-monkey de)
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23
24 #![no_std]
25 #![no_main]
26 #[link(name="libcommon.rlib")]
27
28 extern crate common;
29
30 mod uart0;
31
32 use common::buffer::Buffer;
33 use common::clock;
34 use common::systick;
35 use common::port;
36 use common::gpio;
37 use common::nvic;
38 use common::i2c;
39 use common::uart;
40 use common::usb_serial;
41 use common::display;
42 use common::gps;
43
44 extern {
45     fn enable_interrupts();
46
47     static mut cdc_tx_buf: Buffer;
48 }
49
50 struct Timer {
51     state: u32,
52     delay_ms: u32,
53     last_update_ticks: u32,
54 }
55
56 impl Timer {
57     pub fn new(delay_ms: u32) -> Timer {
58         Timer {
59             state: 0,
60             delay_ms: delay_ms,
61             last_update_ticks: systick::now(),
62         }
63     }
64
65     pub fn update<F>(&mut self, func: F)
66         where F: FnOnce(u32) -> u32
67     {
68         if systick::has_timeout_ms(self.last_update_ticks, self.delay_ms) {
69             self.state = func(self.state);
70
71             self.last_update_ticks = systick::now();
72         }
73     }
74 }
75
76 #[no_mangle]
77 pub unsafe extern "C" fn _start() -> ! {
78     clock::configure();
79     clock::enable_osc0();
80     systick::init();
81     port::init();
82
83     // Configure pins for I2C0.
84     port::set_af(port::PORTC, 8, 2);
85     port::set_af(port::PORTC, 9, 2);
86
87     i2c::configure(i2c::I2C0);
88
89     nvic::disable_irq(8); // I2C0
90
91     // Configure pin for the display's reset line.
92     gpio::set_direction(gpio::GPIOB, 1 << 16, gpio::Direction::Output);
93     port::set_af(port::PORTB, 16, 1);
94
95     // Configure pins for UART0.
96     port::set_af(port::PORTE, 20, 4);
97     port::set_af(port::PORTE, 21, 4);
98
99     // Configure pin for the GPS's reset line.
100     gpio::set_direction(gpio::GPIOB, 1 << 1, gpio::Direction::Output);
101     port::set_af(port::PORTB, 1, 1);
102
103     // Configure upper right push button.
104     gpio::set_direction(gpio::GPIOA, 1 << 12, gpio::Direction::Input);
105     port::set_af(port::PORTA, 12, 1);
106     port::set_pull(port::PORTA, 12, port::Pull::Up);
107
108     enable_interrupts();
109
110     usb_serial::init(0xf055, 0x635d);
111
112     cdc_tx_buf.write(b"\n");
113     cdc_tx_buf.flush();
114
115     let mut display = display::Display::new(gpio::GPIOB, 1 << 16, 0x3c);
116
117     display.init();
118     display.clear();
119
120     // Hold GPS in reset while configuring its UART.
121     gpio::clear(gpio::GPIOB, 1);
122     systick::delay_ms(50);
123     uart::configure(uart::UART0);
124     systick::delay_ms(50);
125     gpio::set(gpio::GPIOB, 1);
126
127     nvic::enable_irq(12); // UART0
128
129     let mut gps = gps::Gps::new();
130
131     let mut heart_icon_timer = Timer::new(1000);
132
133     loop {
134         let mut tap = gps::TimeAndPos::new();
135
136         while gps.update(&mut tap) {
137         }
138
139         heart_icon_timer.update(|state| {
140             if state == 1 {
141                 display.hide_icon(display::Icon::Heart);
142                 0
143             } else {
144                 display.show_icon(display::Icon::Heart);
145                 1
146             }
147         });
148
149         if (gpio::get(gpio::GPIOA) & (1 << 12)) == 0 {
150             nvic::system_reset();
151         }
152     }
153 }