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;
40 use common::usb_serial;
45 fn enable_interrupts();
47 static mut cdc_tx_buf: Buffer;
53 last_update_ticks: u32,
57 pub fn new(delay_ms: u32) -> Timer {
61 last_update_ticks: systick::now(),
65 pub fn update<F>(&mut self, func: F)
66 where F: FnOnce(u32) -> u32
68 if systick::has_timeout_ms(self.last_update_ticks, self.delay_ms) {
69 self.state = func(self.state);
71 self.last_update_ticks = systick::now();
77 pub unsafe extern "C" fn _start() -> ! {
83 // Configure pins for I2C0.
84 port::set_af(port::PORTC, 8, 2);
85 port::set_af(port::PORTC, 9, 2);
87 i2c::configure(i2c::I2C0);
89 nvic::disable_irq(8); // I2C0
91 // Configure pin for the display's reset line.
92 gpio::set_direction(gpio::GPIOB, 1 << 16, gpio::Direction::Output);
93 port::set_af(port::PORTB, 16, 1);
95 // Configure pins for UART0.
96 port::set_af(port::PORTE, 20, 4);
97 port::set_af(port::PORTE, 21, 4);
99 // Configure pin for the GPS's reset line.
100 gpio::set_direction(gpio::GPIOB, 1 << 1, gpio::Direction::Output);
101 port::set_af(port::PORTB, 1, 1);
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);
110 usb_serial::init(0xf055, 0x635d);
112 cdc_tx_buf.write(b"\n");
115 let mut display = display::Display::new(gpio::GPIOB, 1 << 16, 0x3c);
120 // Hold GPS in reset while configuring its UART.
121 gpio::clear(gpio::GPIOB, 1);
122 systick::delay_ms(50);
123 uart::configure(uart::UART0);
124 systick::delay_ms(50);
125 gpio::set(gpio::GPIOB, 1);
127 nvic::enable_irq(12); // UART0
129 let mut gps = gps::Gps::new();
131 let mut gps_has_fix = false;
132 let mut gps_has_fix_ticks = 0;
134 let mut heart_icon_timer = Timer::new(1000);
135 let mut gps_icon_timer = Timer::new(500);
138 let mut tap = gps::TimeAndPos::new();
139 let old_gps_has_fix = gps_has_fix;
141 while gps.update(&mut tap) {
143 gps_has_fix_ticks = systick::now();
146 // Did GPS fix information expire?
147 if gps_has_fix && systick::has_timeout_ms(gps_has_fix_ticks, 1500) {
151 if gps_has_fix && !old_gps_has_fix {
152 display.show_icon(display::Icon::SatelliteBody);
153 display.show_icon(display::Icon::SatelliteWave1);
154 display.show_icon(display::Icon::SatelliteWave2);
155 } else if !gps_has_fix {
156 gps_icon_timer.update(|state| {
158 display.show_icon(display::Icon::SatelliteWave1);
160 } else if state == 2 {
161 display.show_icon(display::Icon::SatelliteWave2);
163 } else if state == 3 {
164 display.hide_icon(display::Icon::SatelliteBody);
165 display.hide_icon(display::Icon::SatelliteWave1);
166 display.hide_icon(display::Icon::SatelliteWave2);
169 display.show_icon(display::Icon::SatelliteBody);
175 heart_icon_timer.update(|state| {
177 display.hide_icon(display::Icon::Heart);
180 display.show_icon(display::Icon::Heart);
185 if (gpio::get(gpio::GPIOA) & (1 << 12)) == 0 {
186 nvic::system_reset();