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