444ffd464da0219263f11038f4abd6df6561704d
[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::ringbuf::Ringbuf;
34 use common::clock;
35 use common::systick;
36 use common::port;
37 use common::gpio;
38 use common::nvic;
39 use common::i2c;
40 use common::spi;
41 use common::uart;
42 use common::usb_serial;
43 use common::display;
44 use common::gps;
45 use common::screen;
46 use common::time::Time;
47 use common::mx25l::Mx25l;
48 use common::shell::Shell;
49
50 extern {
51     fn enable_interrupts();
52
53     static mut cdc_tx_buf: Buffer;
54 }
55
56 struct Timer {
57     state: u32,
58     delay_ms: u32,
59     last_update_ticks: u32,
60 }
61
62 impl Timer {
63     pub fn new(delay_ms: u32) -> Timer {
64         Timer {
65             state: 0,
66             delay_ms: delay_ms,
67             last_update_ticks: systick::now(),
68         }
69     }
70
71     pub fn update<F>(&mut self, func: F)
72         where F: FnOnce(u32) -> u32
73     {
74         if systick::has_timeout_ms(self.last_update_ticks, self.delay_ms) {
75             self.state = func(self.state);
76
77             self.last_update_ticks = systick::now();
78         }
79     }
80 }
81
82 fn reset_requested() -> bool {
83     let pta1 = gpio::get(gpio::GPIOA) & (1 << 1);
84     let pte25 = gpio::get(gpio::GPIOE) & (1 << 25);
85
86     (pta1 | pte25) == 0
87 }
88
89 #[inline(never)]
90 #[panic_handler]
91 fn panic(_info: &core::panic::PanicInfo) -> ! {
92     loop {
93         if reset_requested() {
94             nvic::system_reset();
95         }
96     }
97 }
98
99 fn configure_push_buttons() {
100     // Configure lower right push button.
101     gpio::set_direction(gpio::GPIOA, 1 << 1, gpio::Direction::Input);
102     port::set_af(port::PORTA, 1, 1);
103     port::set_pull(port::PORTA, 1, port::Pull::Up);
104
105     // Configure upper right push button.
106     gpio::set_direction(gpio::GPIOA, 1 << 12, gpio::Direction::Input);
107     port::set_af(port::PORTA, 12, 1);
108     port::set_pull(port::PORTA, 12, port::Pull::Up);
109
110     // Configure lower left push button.
111     gpio::set_direction(gpio::GPIOE, 1 << 24, gpio::Direction::Input);
112     port::set_af(port::PORTE, 24, 1);
113     port::set_pull(port::PORTE, 24, port::Pull::Up);
114
115     // Configure upper left push button.
116     gpio::set_direction(gpio::GPIOE, 1 << 25, gpio::Direction::Input);
117     port::set_af(port::PORTE, 25, 1);
118     port::set_pull(port::PORTE, 25, port::Pull::Up);
119
120     // Configure middle left push button.
121     gpio::set_direction(gpio::GPIOE, 1 << 31, gpio::Direction::Input);
122     port::set_af(port::PORTE, 31, 1);
123     port::set_pull(port::PORTE, 31, port::Pull::Up);
124 }
125
126 fn uart0_try_read() -> Option<u8> {
127     extern {
128         static mut uart0_rx_buf: Ringbuf;
129     }
130
131     unsafe {
132         if uart0_rx_buf.is_empty() {
133             None
134         } else {
135             Some(uart0_rx_buf.read())
136         }
137     }
138 }
139
140 #[no_mangle]
141 pub unsafe extern "C" fn _start() -> ! {
142     clock::configure();
143     clock::enable_osc0();
144     systick::init();
145     port::init();
146
147     // Configure pins for I2C0.
148     port::set_af(port::PORTC, 8, 2);
149     port::set_af(port::PORTC, 9, 2);
150
151     i2c::configure(i2c::I2C0);
152
153     nvic::disable_irq(8); // I2C0
154
155     // Configure pin for the display's reset line.
156     gpio::set_direction(gpio::GPIOB, 1 << 16, gpio::Direction::Output);
157     port::set_af(port::PORTB, 16, 1);
158
159     // Configure pin for the MX25L's chip select line.
160     gpio::set_direction(gpio::GPIOD, 1 << 0, gpio::Direction::Output);
161     port::set_af(port::PORTD, 0, 1);
162     gpio::set(gpio::GPIOD, 1 << 0);
163
164     // Configure pins for SPI0.
165     port::set_af(port::PORTD, 1, 2);
166     port::set_af(port::PORTD, 2, 5);
167     port::set_af(port::PORTD, 3, 5);
168
169     spi::configure(spi::SPI0);
170
171     nvic::disable_irq(10); // SPI0
172
173     // Configure pins for UART0.
174     port::set_af(port::PORTE, 20, 4);
175     port::set_af(port::PORTE, 21, 4);
176
177     // Configure pin for the GPS's reset line.
178     gpio::set_direction(gpio::GPIOB, 1 << 1, gpio::Direction::Output);
179     port::set_af(port::PORTB, 1, 1);
180
181     configure_push_buttons();
182
183     enable_interrupts();
184
185     usb_serial::init(0xf055, 0x635d);
186
187     cdc_tx_buf.write(b"\n");
188     cdc_tx_buf.flush();
189
190     let mut display = display::Display::new(gpio::GPIOB, 1 << 16, 0x3c);
191
192     display.init();
193     display.clear();
194
195     let mut screen = screen::Screen::new();
196
197     // Hold GPS in reset while configuring its UART.
198     gpio::clear(gpio::GPIOB, 1);
199     systick::delay_ms(50);
200     uart::configure(uart::UART0);
201     systick::delay_ms(50);
202     gpio::set(gpio::GPIOB, 1);
203
204     nvic::enable_irq(12); // UART0
205
206     let mut shell = Shell::new(&mut cdc_tx_buf);
207
208     let mut mx25l = Mx25l::new(gpio::GPIOD, 1 << 0);
209
210     let mut gps = gps::Gps::new();
211
212     let mut gps_has_fix = false;
213     let mut gps_has_fix_ticks = 0;
214
215     let mut heart_icon_timer = Timer::new(1000);
216     let mut gps_icon_timer = Timer::new(500);
217
218     let mut prev_tap = gps::TimeAndPos::new();
219
220     loop {
221         let mut tap = gps::TimeAndPos::new();
222         let mut show_time = false;
223         let old_gps_has_fix = gps_has_fix;
224
225         while gps.update(&mut tap, uart0_try_read) {
226             prev_tap = tap;
227
228             show_time = true;
229
230             gps_has_fix = true;
231             gps_has_fix_ticks = systick::now();
232         }
233
234         // Did GPS fix information expire?
235         if gps_has_fix && systick::has_timeout_ms(gps_has_fix_ticks, 1500) {
236             gps_has_fix = false;
237         }
238
239         if gps_has_fix && !old_gps_has_fix {
240             display.show_icon(display::Icon::SatelliteBody);
241             display.show_icon(display::Icon::SatelliteWave1);
242             display.show_icon(display::Icon::SatelliteWave2);
243         } else if !gps_has_fix {
244             gps_icon_timer.update(|state| {
245                 if state == 1 {
246                     display.show_icon(display::Icon::SatelliteWave1);
247                     2
248                 } else if state == 2 {
249                     display.show_icon(display::Icon::SatelliteWave2);
250                     3
251                 } else if state == 3 {
252                     display.hide_icon(display::Icon::SatelliteBody);
253                     display.hide_icon(display::Icon::SatelliteWave1);
254                     display.hide_icon(display::Icon::SatelliteWave2);
255                     0
256                 } else {
257                     display.show_icon(display::Icon::SatelliteBody);
258                     1
259                 }
260             });
261         }
262
263         if show_time {
264             if let Some(tm) = Time::from_unix_time(prev_tap.unix_time) {
265                 let mut time_s = [b' '; 8];
266                 tm.fmt_time(&mut time_s);
267
268                 screen.clear();
269                 screen.draw_text(&time_s);
270
271                 display.draw(&screen);
272             }
273         }
274
275         heart_icon_timer.update(|state| {
276             if state == 1 {
277                 display.hide_icon(display::Icon::Heart);
278                 0
279             } else {
280                 display.show_icon(display::Icon::Heart);
281                 1
282             }
283         });
284
285         shell.update(&mut mx25l);
286
287         if reset_requested() {
288             nvic::system_reset();
289         }
290     }
291 }