Skip to content

Commit cb4a39e

Browse files
committed
Improve the device generator script
1 parent 078b7d4 commit cb4a39e

File tree

7 files changed

+57
-35
lines changed

7 files changed

+57
-35
lines changed

Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ categories = ["embedded"]
2323
[features]
2424
default = []
2525
# Enable all microcontrollers.
26-
all_mcus = []
26+
all-mcus = []
2727

2828
[dependencies]
2929

@@ -34,3 +34,6 @@ avr-mcu = "0.3"
3434
# Most of the build consists of generating the mcu description files. So
3535
# enable optimizations to speed this process.
3636
opt-level = 2
37+
38+
[package.metadata.docs.rs]
39+
all-features = true

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ volatile_store(PORTB, 0x1f);
2828

2929
Just include the crate as a dependency and it will work.
3030

31-
## Usage on other architectures
31+
## Enabling all microcontrollers at once
3232

33-
You need to compile with the `all_mcus` feature enabled, otherwise it will attempt
34-
to target the current AVR microcontroller, which isn't set in these cases.
33+
You may want to compile with the `all-mcus` feature enabled, which enables
34+
modules for all microcontrollers to be compiled and included at once.
3535

build.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const DEFAULT_MCU_FOR_NON_AVR_DOCS: &'static str = "atmega328";
2+
3+
fn main() {
4+
let current_mcu = if avr_mcu::current::is_compiling_for_avr() {
5+
avr_mcu::current::mcu()
6+
.expect("no target cpu specified")
7+
} else {
8+
avr_mcu::microcontroller(DEFAULT_MCU_FOR_NON_AVR_DOCS)
9+
};
10+
let current_mcu_name = current_mcu.device.name.clone().to_lowercase();
11+
12+
println!("cargo:rustc-cfg=avr_mcu_{}", &current_mcu_name);
13+
}

device_generator/src/main.rs

+27-28
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use clap::{App, Arg};
44

55
use std::path::{Path, PathBuf};
66

7+
const DEFAULT_MCU_FOR_NON_AVR: &'static str = "atmega328";
8+
79
#[derive(Clone, Debug)]
810
struct Config {
911
output_directory: PathBuf,
@@ -74,38 +76,35 @@ mod gen {
7476
fn generate_entry_module(output_path: &Path, module_names: &[String]) -> Result<(), io::Error> {
7577
let mut mod_rs = File::create(output_path.join("mod.rs"))?;
7678

77-
writeln!(mod_rs, "// Device definitions")?;
78-
for module_name in module_names {
79-
writeln!(mod_rs, "pub mod {};", module_name)?;
80-
}
81-
writeln!(mod_rs)?;
79+
const CURRENT_MOD_SUMMARY: &'static str = "Contains definitions for the current AVR device being targeted.";
8280

83-
const CURRENT_MOD_SUMMARY: &'static str = "Contains definitions for the current AVR device";
84-
85-
writeln!(mod_rs, "/// {}", CURRENT_MOD_SUMMARY)?;
86-
writeln!(mod_rs, "///")?;
87-
writeln!(mod_rs, "/// **NOTE**: We are showing the ATmega328 here, even though the library")?;
88-
writeln!(mod_rs, "/// is not targeting a real AVR device. If you compile this library for")?;
89-
writeln!(mod_rs, "/// a specific AVR MCU, the module for that device will aliased here.")?;
90-
writeln!(mod_rs, "// If we are targeting a non-AVR device, just pick the ATmega328p so")?;
91-
writeln!(mod_rs, "// that users can see what the API would look like")?;
92-
writeln!(mod_rs, "//")?;
93-
writeln!(mod_rs, "// Note that we reexport rather than alias so that we can add a note about")?;
94-
writeln!(mod_rs, "// this behaviour to the documentation.")?;
95-
writeln!(mod_rs, "#[cfg(not(target_arch = \"avr\"))]")?;
96-
writeln!(mod_rs, "pub mod current {{ pub use super::atmega328::*; }}")?;
81+
writeln!(mod_rs, "// Device definitions")?;
9782
writeln!(mod_rs)?;
98-
writeln!(mod_rs, "/// {}", CURRENT_MOD_SUMMARY)?;
99-
writeln!(mod_rs, "// If we are targeting AVR, lookup the current device's module")?;
100-
writeln!(mod_rs, "// and alias it to the `current` module.")?;
101-
writeln!(mod_rs, "#[cfg(target_arch = \"avr\")]")?;
102-
writeln!(mod_rs, "pub mod current {{")?;
103-
writeln!(mod_rs, " // NOTE: 'target_cpu' is a cfg flag specific to the avr-rust fork")?;
10483
for module_name in module_names {
105-
writeln!(mod_rs, " #[cfg(target_cpu = \"{}\")] pub use super::{} as current;",
106-
module_name, module_name)?;
84+
if module_name == crate::DEFAULT_MCU_FOR_NON_AVR {
85+
writeln!(mod_rs, "/// The module containing the values for the '{}' microcontroller", module_name)?;
86+
writeln!(mod_rs, "///")?;
87+
writeln!(mod_rs, "/// This is the default MCU when targeting a non-AVR target.")?;
88+
89+
writeln!(mod_rs, "#[cfg(any(not(target_arch = \"avr\"), avr_mcu_{}, feature = \"all-mcus\"))] pub mod {};", module_name, module_name)?;
90+
91+
writeln!(mod_rs, "/// {} **The '{}' is the default when targeting non-AVR devices.**", CURRENT_MOD_SUMMARY, module_name)?;
92+
93+
writeln!(mod_rs, "#[cfg(not(target_arch = \"avr\"))] pub mod current {{ pub use super::{}::*; }}", module_name)?;
94+
95+
writeln!(mod_rs, "/// {} **This is currently the '{}'**.", CURRENT_MOD_SUMMARY, module_name)?;
96+
writeln!(mod_rs, "#[cfg(all(target_arch = \"avr\", avr_mcu_{}))] pub mod current {{ pub use super::{}::*; }}",
97+
module_name, module_name)?;
98+
} else {
99+
writeln!(mod_rs, "/// The module containing the values for the '{}' microcontroller", module_name)?;
100+
writeln!(mod_rs, "#[cfg(any(avr_mcu_{}, feature = \"all-mcus\"))] pub mod {};", module_name, module_name)?;
101+
writeln!(mod_rs, "/// {} **This is currently the '{}'**.", CURRENT_MOD_SUMMARY, module_name)?;
102+
writeln!(mod_rs, "#[cfg(all(target_arch = \"avr\", avr_mcu_{}))] pub mod current {{ pub use super::{}::*; }}",
103+
module_name, module_name)?;
104+
}
105+
writeln!(mod_rs)?;
107106
}
108-
writeln!(mod_rs, "}}")?;
107+
writeln!(mod_rs)?;
109108

110109
Ok(())
111110
}

regenerate-devices.sh

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
CRATE_DIR=$(realpath $(dirname $0))
44

5-
echo "create dir: '${CRATE_DIR}'"
6-
75
cd "${CRATE_DIR}/device_generator"
86

97
echo "Generating cores"
108

11-
cargo run -- -o "${CRATE_DIR}/src/gen"
9+
cargo run -- -o "${CRATE_DIR}/src/gen" $@

src/gen/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Contains autogenerated device-specific Rust modules.

src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
//!
77
//! The information for each device is separated into submodules, named after
88
//! the microcontroller itself.
9+
//!
10+
//! The [`current`][crate::current] module is an alias to whatever microcontroller
11+
//! is currently being targeted.
12+
//!
13+
//! # Enabling all microcontrollers at once
14+
//!
15+
//! You may want to compile with the `all-mcus` feature enabled, which enables
16+
//! modules for all microcontrollers to be compiled and included at once.
917
1018
#![no_std]
1119

0 commit comments

Comments
 (0)