2 * Copyright (c) 2019 Tilman Sauerbeck (tilman at code-monkey de)
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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.
26 #[link(name="libcommon.rlib")]
32 use common::buffer::Buffer;
41 use common::usb_serial;
45 use common::time::Time;
46 use common::shell::Shell;
49 fn enable_interrupts();
51 static mut cdc_tx_buf: Buffer;
57 last_update_ticks: u32,
61 pub fn new(delay_ms: u32) -> Timer {
65 last_update_ticks: systick::now(),
69 pub fn update<F>(&mut self, func: F)
70 where F: FnOnce(u32) -> u32
72 if systick::has_timeout_ms(self.last_update_ticks, self.delay_ms) {
73 self.state = func(self.state);
75 self.last_update_ticks = systick::now();
80 fn reset_requested() -> bool {
81 let pta1 = gpio::get(gpio::GPIOA) & (1 << 1);
82 let pte25 = gpio::get(gpio::GPIOE) & (1 << 25);
89 fn panic(_info: &core::panic::PanicInfo) -> ! {
91 if reset_requested() {
97 fn configure_push_buttons() {
98 // Configure lower right push button.
99 gpio::set_direction(gpio::GPIOA, 1 << 1, gpio::Direction::Input);
100 port::set_af(port::PORTA, 1, 1);
101 port::set_pull(port::PORTA, 1, port::Pull::Up);
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);
108 // Configure lower left push button.
109 gpio::set_direction(gpio::GPIOE, 1 << 24, gpio::Direction::Input);
110 port::set_af(port::PORTE, 24, 1);
111 port::set_pull(port::PORTE, 24, port::Pull::Up);
113 // Configure upper left push button.
114 gpio::set_direction(gpio::GPIOE, 1 << 25, gpio::Direction::Input);
115 port::set_af(port::PORTE, 25, 1);
116 port::set_pull(port::PORTE, 25, port::Pull::Up);
118 // Configure middle left push button.
119 gpio::set_direction(gpio::GPIOE, 1 << 31, gpio::Direction::Input);
120 port::set_af(port::PORTE, 31, 1);
121 port::set_pull(port::PORTE, 31, port::Pull::Up);
125 pub unsafe extern "C" fn _start() -> ! {
127 clock::enable_osc0();
131 // Configure pins for I2C0.
132 port::set_af(port::PORTC, 8, 2);
133 port::set_af(port::PORTC, 9, 2);
135 i2c::configure(i2c::I2C0);
137 nvic::disable_irq(8); // I2C0
139 // Configure pin for the display's reset line.
140 gpio::set_direction(gpio::GPIOB, 1 << 16, gpio::Direction::Output);
141 port::set_af(port::PORTB, 16, 1);
143 // Configure pin for the MX25L's chip select line.
144 gpio::set_direction(gpio::GPIOD, 1 << 0, gpio::Direction::Output);
145 port::set_af(port::PORTD, 0, 1);
146 gpio::set(gpio::GPIOD, 1 << 0);
148 // Configure pins for SPI0.
149 port::set_af(port::PORTD, 1, 2);
150 port::set_af(port::PORTD, 2, 5);
151 port::set_af(port::PORTD, 3, 5);
153 spi::configure(spi::SPI0);
155 nvic::disable_irq(10); // SPI0
157 // Configure pins for UART0.
158 port::set_af(port::PORTE, 20, 4);
159 port::set_af(port::PORTE, 21, 4);
161 // Configure pin for the GPS's reset line.
162 gpio::set_direction(gpio::GPIOB, 1 << 1, gpio::Direction::Output);
163 port::set_af(port::PORTB, 1, 1);
165 configure_push_buttons();
169 usb_serial::init(0xf055, 0x635d);
171 cdc_tx_buf.write(b"\n");
174 let mut display = display::Display::new(gpio::GPIOB, 1 << 16, 0x3c);
179 let mut screen = screen::Screen::new();
181 // Hold GPS in reset while configuring its UART.
182 gpio::clear(gpio::GPIOB, 1);
183 systick::delay_ms(50);
184 uart::configure(uart::UART0);
185 systick::delay_ms(50);
186 gpio::set(gpio::GPIOB, 1);
188 nvic::enable_irq(12); // UART0
190 let mut shell = Shell::new(&mut cdc_tx_buf);
192 let mut gps = gps::Gps::new();
194 let mut gps_has_fix = false;
195 let mut gps_has_fix_ticks = 0;
197 let mut heart_icon_timer = Timer::new(1000);
198 let mut gps_icon_timer = Timer::new(500);
200 let mut prev_tap = gps::TimeAndPos::new();
203 let mut tap = gps::TimeAndPos::new();
204 let mut show_time = false;
205 let old_gps_has_fix = gps_has_fix;
207 while gps.update(&mut tap) {
213 gps_has_fix_ticks = systick::now();
216 // Did GPS fix information expire?
217 if gps_has_fix && systick::has_timeout_ms(gps_has_fix_ticks, 1500) {
221 if gps_has_fix && !old_gps_has_fix {
222 display.show_icon(display::Icon::SatelliteBody);
223 display.show_icon(display::Icon::SatelliteWave1);
224 display.show_icon(display::Icon::SatelliteWave2);
225 } else if !gps_has_fix {
226 gps_icon_timer.update(|state| {
228 display.show_icon(display::Icon::SatelliteWave1);
230 } else if state == 2 {
231 display.show_icon(display::Icon::SatelliteWave2);
233 } else if state == 3 {
234 display.hide_icon(display::Icon::SatelliteBody);
235 display.hide_icon(display::Icon::SatelliteWave1);
236 display.hide_icon(display::Icon::SatelliteWave2);
239 display.show_icon(display::Icon::SatelliteBody);
246 if let Some(tm) = Time::from_unix_time(prev_tap.unix_time) {
247 let mut time_s = [b' '; 8];
248 tm.fmt_time(&mut time_s);
251 screen.draw_text(&time_s);
253 display.draw(&screen);
257 heart_icon_timer.update(|state| {
259 display.hide_icon(display::Icon::Heart);
262 display.show_icon(display::Icon::Heart);
269 if reset_requested() {
270 nvic::system_reset();