#[cfg(boot_req_gpio)]
#[derive(Copy, Clone)]
pub struct Config {
pub pin: crate::Pin,
pub active_high: bool,
}
#[cfg(not(boot_req_gpio))]
#[derive(Copy, Clone, Default)]
pub struct Config;
pub fn init(_config: &Config) {
#[cfg(boot_req_gpio)]
{
use crate::gpio::{self, PinMode};
crate::rcc::enable_gpio(_config.pin.port_index());
gpio::configure(_config.pin, PinMode::OUTPUT_PUSH_PULL);
drive_boot_pin(_config.pin, _config.active_high, true);
}
}
pub fn is_boot_requested() -> bool {
#[cfg(boot_req_reg)]
return crate::flash::boot_mode();
#[cfg(boot_req_ram)]
return unsafe { core::ptr::read_volatile(&raw const __tb_boot_request) == BOOT_REQUEST_MAGIC };
}
pub fn set_boot_request(_config: &Config, request: bool) {
#[cfg(boot_req_reg)]
crate::flash::set_boot_mode(request);
#[cfg(boot_req_ram)]
{
let val = if request { BOOT_REQUEST_MAGIC } else { 0 };
unsafe { core::ptr::write_volatile(&raw mut __tb_boot_request, val) };
}
#[cfg(boot_req_gpio)]
drive_boot_pin(_config.pin, _config.active_high, request);
}
#[cfg(boot_req_ram)]
const BOOT_REQUEST_MAGIC: u32 = 0xB007_CAFE;
#[cfg(boot_req_ram)]
unsafe extern "C" {
static mut __tb_boot_request: u32;
}
#[cfg(boot_req_gpio)]
fn drive_boot_pin(pin: crate::Pin, active_high: bool, system_flash: bool) {
use crate::gpio::{self, Level};
let level = if active_high == system_flash {
Level::High
} else {
Level::Low
};
gpio::set_level(pin, level);
}