X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=src%2Fbootloader%2Fmain.rs;h=1a0a7cc3d53ebdf1194ec88bd11a3eec541837c7;hb=08e2e672414121f839ed0519c2e322d6a562e1c5;hp=2e43b67eb9dc3762f3301d39d07ecdc79090fe45;hpb=8483f69a801c3424943842ea2b3ccd7a7a2be3af;p=gps-watch.git diff --git a/src/bootloader/main.rs b/src/bootloader/main.rs index 2e43b67..1a0a7cc 100644 --- a/src/bootloader/main.rs +++ b/src/bootloader/main.rs @@ -30,18 +30,43 @@ extern crate common; mod flash; mod bootloader; +use common::register; use common::clock; use common::systick; use common::port; use common::gpio; use common::usb_serial; +type Reg32 = register::Register; + extern { fn jump_to_application(address: u32); } +#[cfg(bootloader_type = "intermediate")] const APPLICATION_ADDR: u32 = 0x0; +#[cfg(bootloader_type = "final")] +const APPLICATION_ADDR: u32 = 0x8000; + +unsafe fn application_missing() -> bool { + let first_app_word = Reg32::new(APPLICATION_ADDR); + + first_app_word.read() == 0xffffffff +} + +fn bootloader_requested() -> bool { + let start_ticks = systick::now(); + + while !systick::has_timeout_ms(start_ticks, 3000) { + if (gpio::get(gpio::GPIOE) & (1 << 24)) == 0 { + return true; + } + } + + false +} + #[no_mangle] pub unsafe extern fn main() { clock::configure(); @@ -52,14 +77,16 @@ pub unsafe extern fn main() { port::set_af(port::PORTE, 24, 1); port::set_pull(port::PORTE, 24, port::Pull::Up); - if true { + if application_missing() || bootloader_requested() { usb_serial::init(0xf055, 0x635c); let mut b = bootloader::Bootloader::new(); - while b.run() { + while b.run() || application_missing() { } + + usb_serial::shutdown(); } - jump_to_application(__application_addr); + jump_to_application(APPLICATION_ADDR); }