X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=src%2Fbootloader%2Fmain.rs;h=1a0a7cc3d53ebdf1194ec88bd11a3eec541837c7;hb=08e2e672414121f839ed0519c2e322d6a562e1c5;hp=0f863fc4af6338c033edb1df7f847c7465196ce1;hpb=dbda961b1cc62d8bbc155bd53d74a1fb52fed654;p=gps-watch.git diff --git a/src/bootloader/main.rs b/src/bootloader/main.rs index 0f863fc..1a0a7cc 100644 --- a/src/bootloader/main.rs +++ b/src/bootloader/main.rs @@ -30,29 +30,63 @@ 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(); systick::init(); - if true { + // Configure lower left push button. + gpio::set_direction(gpio::GPIOE, 1 << 24, gpio::Direction::Input); + port::set_af(port::PORTE, 24, 1); + port::set_pull(port::PORTE, 24, port::Pull::Up); + + 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); }