2017-12-13 13:53:28 +00:00
|
|
|
extern crate avr_mcu;
|
|
|
|
|
2017-12-13 13:57:45 +00:00
|
|
|
mod gen;
|
|
|
|
|
2017-12-13 13:53:28 +00:00
|
|
|
use avr_mcu::*;
|
|
|
|
use std::fs::{self, File};
|
2018-11-05 11:03:06 +00:00
|
|
|
use std::{env, io};
|
2017-12-13 13:53:28 +00:00
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
2018-11-05 09:23:57 +00:00
|
|
|
/// The MCU that will be assumed when running 'cargo doc' targeting
|
|
|
|
/// archicectures that are not AVR.
|
|
|
|
const DEFAULT_MCU_FOR_NON_AVR_DOCS: &'static str = "atmega328";
|
|
|
|
|
2018-11-05 11:03:06 +00:00
|
|
|
const DEFAULT_FREQUENCY_HZ_FOR_NON_AVR_DOCS: u64 = 16_000_000;
|
2018-11-05 10:48:19 +00:00
|
|
|
|
2017-12-13 13:53:28 +00:00
|
|
|
fn src_path() -> PathBuf {
|
|
|
|
Path::new(env!("CARGO_MANIFEST_DIR")).join("src")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cores_path() -> PathBuf {
|
|
|
|
src_path().join("cores")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn core_module_name(mcu: &Mcu) -> String {
|
2020-07-02 13:37:00 +00:00
|
|
|
normalize_device_name(&mcu.device.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn normalize_device_name(device_name: &str) -> String {
|
|
|
|
device_name.to_lowercase().to_owned()
|
2017-12-13 13:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
if !cores_path().exists() {
|
|
|
|
fs::create_dir_all(&cores_path()).expect("could not create cores directory");
|
|
|
|
}
|
|
|
|
|
2020-07-02 13:37:00 +00:00
|
|
|
let current_mcu = if avr_mcu::current::is_compiling_for_avr() {
|
2018-11-05 09:23:57 +00:00
|
|
|
avr_mcu::current::mcu()
|
|
|
|
.expect("no target cpu specified")
|
|
|
|
} else {
|
|
|
|
avr_mcu::microcontroller(DEFAULT_MCU_FOR_NON_AVR_DOCS)
|
|
|
|
};
|
2020-07-02 13:55:15 +00:00
|
|
|
let current_mcu_name = current_mcu.device.name.clone();
|
2018-11-05 09:23:57 +00:00
|
|
|
|
2017-12-13 13:53:28 +00:00
|
|
|
generate_config_module().unwrap();
|
|
|
|
generate_cores(&[current_mcu]).unwrap();
|
2020-07-02 13:55:15 +00:00
|
|
|
|
|
|
|
println!("cargo:rustc-cfg=avr_mcu_{}", normalize_device_name(¤t_mcu_name));
|
2017-12-13 13:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_cores(mcus: &[Mcu]) -> Result<(), io::Error> {
|
|
|
|
for mcu in mcus {
|
|
|
|
generate_core_module(mcu).expect("failed to generate mcu core");
|
|
|
|
}
|
|
|
|
generate_cores_mod_rs(mcus)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_config_module() -> Result<(), io::Error> {
|
|
|
|
let path = src_path().join("config.rs");
|
|
|
|
let mut f = File::create(&path)?;
|
|
|
|
|
2018-11-05 11:03:06 +00:00
|
|
|
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")
|
2018-11-05 10:48:19 +00:00
|
|
|
} else {
|
|
|
|
DEFAULT_FREQUENCY_HZ_FOR_NON_AVR_DOCS
|
|
|
|
};
|
2018-11-05 10:05:34 +00:00
|
|
|
writeln!(f, "/// The clock frequency of device being targeted in Hertz.")?;
|
2018-11-05 07:16:51 +00:00
|
|
|
writeln!(f, "pub const CPU_FREQUENCY_HZ: u32 = {};", clock)?;
|
2017-12-13 13:53:28 +00:00
|
|
|
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)?;
|
|
|
|
write_core_module(mcu, &mut file)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_cores_mod_rs(mcus: &[Mcu]) -> Result<(), io::Error> {
|
|
|
|
let path = cores_path().join("mod.rs");
|
|
|
|
let mut w = File::create(&path)?;
|
|
|
|
|
|
|
|
writeln!(w)?;
|
|
|
|
for mcu in mcus {
|
|
|
|
let module_name = core_module_name(mcu);
|
|
|
|
writeln!(w, "/// The {}.", mcu.device.name)?;
|
|
|
|
writeln!(w, "pub mod {};", module_name)?;
|
2020-07-02 13:55:15 +00:00
|
|
|
|
|
|
|
writeln!(w, "#[cfg(avr_mcu_{})]", module_name)?;
|
|
|
|
writeln!(w, "pub use self::{} as current;", module_name)?;
|
2017-12-13 13:53:28 +00:00
|
|
|
}
|
|
|
|
writeln!(w)
|
|
|
|
}
|
|
|
|
|
2020-06-19 17:59:12 +00:00
|
|
|
fn write_core_module(mcu: &Mcu, w: &mut dyn Write) -> Result<(), io::Error> {
|
2017-12-13 13:53:28 +00:00
|
|
|
writeln!(w, "//! Core for {}.", mcu.device.name)?;
|
|
|
|
writeln!(w)?;
|
2020-06-19 18:03:43 +00:00
|
|
|
writeln!(w, "use crate::{{modules, RegisterBits, Register}};")?;
|
2017-12-13 13:53:28 +00:00
|
|
|
writeln!(w)?;
|
|
|
|
|
|
|
|
gen::write_registers(mcu, w)?;
|
|
|
|
gen::write_pins(mcu, w)?;
|
|
|
|
gen::write_spi_modules(mcu, w)?;
|
|
|
|
gen::write_usarts(mcu, w)?;
|
|
|
|
gen::write_timers(mcu, w)?;
|
|
|
|
|
|
|
|
writeln!(w)
|
|
|
|
}
|
|
|
|
|