diff --git a/Cargo.toml b/Cargo.toml index 8f222dc..9714c40 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,4 +28,5 @@ default = ["avr-std-stub"] [dependencies] avr-std-stub = { version = "1.0", optional = true } +const_env--value = "0.1" target-cpu-macro = "0.1" diff --git a/core_generator/src/main.rs b/core_generator/src/main.rs index 39e0e2c..6f043c0 100644 --- a/core_generator/src/main.rs +++ b/core_generator/src/main.rs @@ -46,7 +46,6 @@ fn main() { }; let current_mcu_name = current_mcu.device.name.clone(); - generate_config_module().unwrap(); generate_cores(&[current_mcu]).unwrap(); println!("cargo:rustc-cfg=avr_mcu_{}", normalize_device_name(¤t_mcu_name)); @@ -59,23 +58,6 @@ fn generate_cores(mcus: &[Mcu]) -> Result<(), io::Error> { generate_cores_mod_rs(mcus) } -fn generate_config_module() -> Result<(), io::Error> { - let path = base_output_path().join("config.rs"); - let mut f = File::create(&path)?; - - let clock: u64 = if cfg!(arch = "avr") { - env::var("AVR_CPU_FREQUENCY_HZ") - .expect("Please set the '$AVR_CPU_FREQUENCY_HZ' environment variable") - .parse() - .expect("The $AVR_CPU_FREQUENCY_HZ environment variable is not an integer") - } else { - DEFAULT_FREQUENCY_HZ_FOR_NON_AVR_DOCS - }; - writeln!(f, "/// The clock frequency of device being targeted in Hertz.")?; - writeln!(f, "pub const CPU_FREQUENCY_HZ: u32 = {};", clock)?; - Ok(()) -} - fn generate_core_module(mcu: &Mcu) -> Result<(), io::Error> { let path = cores_path().join(format!("{}.rs", core_module_name(mcu))); let mut file = File::create(&path)?; diff --git a/src/config.rs b/src/config.rs index ef737df..bb7723f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,2 +1,13 @@ +#[cfg(target_arch = "avr")] use const_env::value_from_env; + /// The clock frequency of device being targeted in Hertz. -pub const CPU_FREQUENCY_HZ: u32 = 16000000; +#[cfg(target_arch = "avr")] +pub const CPU_FREQUENCY_HZ: u32 = value_from_env!("AVR_CPU_FREQUENCY_HZ": u32); + +/// The clock frequency of device being targeted in Hertz. +/// +/// This value is arbitrarily set to 16MHz as this crate is currently being targeted for a non-AVR +/// device. +#[cfg(not(target_arch = "avr"))] +pub const CPU_FREQUENCY_HZ: u32 = 16_000_000; // Choose a nice default when not targeting AVR. + diff --git a/src/lib.rs b/src/lib.rs index 2d6afff..b0a1c26 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,8 @@ #[cfg(feature = "avr-std-stub")] extern crate avr_std_stub; +extern crate const_env__value as const_env; + pub use self::register::{Register, RegisterBits, RegisterValue}; pub use self::pin::{DataDirection, Pin};