application: Set up pins for the MX25L flash memory.
[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 #[inline(never)]
80 #[panic_handler]
81 fn panic(_info: &core::panic::PanicInfo) -> ! {
82     loop {
83         if (gpio::get(gpio::GPIOA) & (1 << 12)) == 0 {
84             nvic::system_reset();
85         }
86     }
87 }
88
89 #[no_mangle]
90 pub unsafe extern "C" fn _start() -> ! {
91     clock::configure();
92     clock::enable_osc0();
93     systick::init();
94     port::init();
95
96     // Configure pins for I2C0.
97     port::set_af(port::PORTC, 8, 2);
98     port::set_af(port::PORTC, 9, 2);
99
100     i2c::configure(i2c::I2C0);
101
102     nvic::disable_irq(8); // I2C0
103
104     // Configure pin for the display's reset line.
105     gpio::set_direction(gpio::GPIOB, 1 << 16, gpio::Direction::Output);
106     port::set_af(port::PORTB, 16, 1);
107
108     // Configure pin for the MX25L's chip select line.
109     gpio::set_direction(gpio::GPIOD, 1 << 0, gpio::Direction::Output);
110     port::set_af(port::PORTD, 0, 1);
111     gpio::set(gpio::GPIOD, 1 << 0);
112
113     // Configure pins for SPI0.
114     port::set_af(port::PORTD, 1, 2);
115     port::set_af(port::PORTD, 2, 5);
116     port::set_af(port::PORTD, 3, 5);
117
118     spi::configure(spi::SPI0);
119
120     nvic::disable_irq(10); // SPI0
121
122     // Configure pins for UART0.
123     port::set_af(port::PORTE, 20, 4);
124     port::set_af(port::PORTE, 21, 4);
125
126     // Configure pin for the GPS's reset line.
127     gpio::set_direction(gpio::GPIOB, 1 << 1, gpio::Direction::Output);
128     port::set_af(port::PORTB, 1, 1);
129
130     // Configure upper right push button.
131     gpio::set_direction(gpio::GPIOA, 1 << 12, gpio::Direction::Input);
132     port::set_af(port::PORTA, 12, 1);
133     port::set_pull(port::PORTA, 12, port::Pull::Up);
134
135     enable_interrupts();
136
137     usb_serial::init(0xf055, 0x635d);
138
139     cdc_tx_buf.write(b"\n");
140     cdc_tx_buf.flush();
141
142     let mut display = display::Display::new(gpio::GPIOB, 1 << 16, 0x3c);
143
144     display.init();
145     display.clear();
146
147     let mut screen = screen::Screen::new();
148
149     // Hold GPS in reset while configuring its UART.
150     gpio::clear(gpio::GPIOB, 1);
151     systick::delay_ms(50);
152     uart::configure(uart::UART0);
153     systick::delay_ms(50);
154     gpio::set(gpio::GPIOB, 1);
155
156     nvic::enable_irq(12); // UART0
157
158     let mut gps = gps::Gps::new();
159
160     let mut gps_has_fix = false;
161     let mut gps_has_fix_ticks = 0;
162
163     let mut heart_icon_timer = Timer::new(1000);
164     let mut gps_icon_timer = Timer::new(500);
165
166     let mut prev_tap = gps::TimeAndPos::new();
167
168     loop {
169         let mut tap = gps::TimeAndPos::new();
170         let mut show_time = false;
171         let old_gps_has_fix = gps_has_fix;
172
173         while gps.update(&mut tap) {
174             prev_tap = tap;
175
176             show_time = true;
177
178             gps_has_fix = true;
179             gps_has_fix_ticks = systick::now();
180         }
181
182         // Did GPS fix information expire?
183         if gps_has_fix && systick::has_timeout_ms(gps_has_fix_ticks, 1500) {
184             gps_has_fix = false;
185         }
186
187         if gps_has_fix && !old_gps_has_fix {
188             display.show_icon(display::Icon::SatelliteBody);
189             display.show_icon(display::Icon::SatelliteWave1);
190             display.show_icon(display::Icon::SatelliteWave2);
191         } else if !gps_has_fix {
192             gps_icon_timer.update(|state| {
193                 if state == 1 {
194                     display.show_icon(display::Icon::SatelliteWave1);
195                     2
196                 } else if state == 2 {
197                     display.show_icon(display::Icon::SatelliteWave2);
198                     3
199                 } else if state == 3 {
200                     display.hide_icon(display::Icon::SatelliteBody);
201                     display.hide_icon(display::Icon::SatelliteWave1);
202                     display.hide_icon(display::Icon::SatelliteWave2);
203                     0
204                 } else {
205                     display.show_icon(display::Icon::SatelliteBody);
206                     1
207                 }
208             });
209         }
210
211         if show_time {
212             if let Some(tm) = Time::from_unix_time(prev_tap.unix_time) {
213                 let mut time_s = [b' '; 8];
214                 tm.fmt_time(&mut time_s);
215
216                 screen.clear();
217                 screen.draw_text(&time_s);
218
219                 display.draw(&screen);
220             }
221         }
222
223         heart_icon_timer.update(|state| {
224             if state == 1 {
225                 display.hide_icon(display::Icon::Heart);
226                 0
227             } else {
228                 display.show_icon(display::Icon::Heart);
229                 1
230             }
231         });
232
233         if (gpio::get(gpio::GPIOA) & (1 << 12)) == 0 {
234             nvic::system_reset();
235         }
236     }
237 }