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