From a03a5c8aa3d1c935af11116c4ca0ad31a26b3660 Mon Sep 17 00:00:00 2001 From: Dennis Marttinen Date: Wed, 21 Jul 2021 19:32:20 +0300 Subject: [PATCH 1/4] Utilize `clap` for the KiCad evaluator CLI --- tools/kicad/kicad_rs/Cargo.toml | 1 + tools/kicad/kicad_rs/src/bin/evaluator.rs | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/tools/kicad/kicad_rs/Cargo.toml b/tools/kicad/kicad_rs/Cargo.toml index 62eb209..0cf6f41 100644 --- a/tools/kicad/kicad_rs/Cargo.toml +++ b/tools/kicad/kicad_rs/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +clap = "2.33" evalexpr = "6" kicad_functions = { path = "../kicad_functions" } kicad_parse_gen = { git = "https://github.com/racklet/kicad-parse-gen" } diff --git a/tools/kicad/kicad_rs/src/bin/evaluator.rs b/tools/kicad/kicad_rs/src/bin/evaluator.rs index 56c8a4b..4a4575f 100644 --- a/tools/kicad/kicad_rs/src/bin/evaluator.rs +++ b/tools/kicad/kicad_rs/src/bin/evaluator.rs @@ -1,12 +1,28 @@ +use clap::{App, Arg}; use kicad_rs::error::DynamicResult; use kicad_rs::eval; use kicad_rs::parser::SchematicTree; -use std::env; + +// Get crate version information from Cargo +const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION"); // Main function, can return different kinds of errors fn main() -> DynamicResult<()> { - let args: Vec = env::args().collect(); - let path = std::path::Path::new(args.get(1).ok_or("expected file as first argument")?); + let matches = App::new("KiCad evaluator") + .about("Evaluates expressions in KiCad Eeschema schematics") + .author("Dennis Marttinen (@twelho), The Racklet Project") + .version(VERSION.unwrap_or("unknown")) + .version_short("v") + .arg( + Arg::with_name("SCHEMATIC") + .help("Path to the schematic file to process") + .required(true), + ) + .get_matches(); + + // Calling .unwrap() is safe here because "SCHEMATIC" is required (if "SCHEMATIC" + // wasn't required we could have used an 'if let' to conditionally get the value) + let path = std::path::Path::new(matches.value_of("SCHEMATIC").unwrap()); // Load the hierarchical schematic tree and parse it let mut tree = SchematicTree::load(path)?; From 5a4e9bae7b9c0eabe56fe2e2a1480b085aee0a5a Mon Sep 17 00:00:00 2001 From: Dennis Marttinen Date: Mon, 26 Jul 2021 18:46:13 +0300 Subject: [PATCH 2/4] Fix evaluator dependency loop detection --- tools/kicad/kicad_rs/src/eval/entry.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/kicad/kicad_rs/src/eval/entry.rs b/tools/kicad/kicad_rs/src/eval/entry.rs index 119d9ab..9471ade 100644 --- a/tools/kicad/kicad_rs/src/eval/entry.rs +++ b/tools/kicad/kicad_rs/src/eval/entry.rs @@ -53,7 +53,7 @@ impl<'a> Entry<'a> { } pub fn value_defined(&self) -> DynamicResult { - if *self.set_in_progress.borrow() { + if self.value.is_none() && *self.set_in_progress.borrow() { return Err(errorf("dependency loop detected")); } From c12cad1681579005cfe042d5fc8b898eb5ac3d8a Mon Sep 17 00:00:00 2001 From: Dennis Marttinen Date: Mon, 26 Jul 2021 18:50:58 +0300 Subject: [PATCH 3/4] Use the file name instead of ID for indexing sub-schematics --- tools/kicad/kicad_rs/src/eval.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tools/kicad/kicad_rs/src/eval.rs b/tools/kicad/kicad_rs/src/eval.rs index 06c1a9d..e39d6e6 100644 --- a/tools/kicad/kicad_rs/src/eval.rs +++ b/tools/kicad/kicad_rs/src/eval.rs @@ -6,6 +6,7 @@ use crate::error::{errorf, DynamicResult}; use crate::eval::index::{ComponentIndex, Node, SheetIndex}; use crate::eval::path::Path; use crate::types::Schematic; +use std::path::Path as StdPath; pub fn index_schematic(sch: &mut Schematic) -> DynamicResult { let mut index = SheetIndex::new(); @@ -25,15 +26,22 @@ pub fn index_schematic(sch: &mut Schematic) -> DynamicResult { } for (sch_id, sub_sch) in sch.sub_schematics.iter_mut() { - if index.map.contains_key(sch_id) { + let sch_name: &str = sub_sch + .meta + .filename + .as_ref() + .map(|s| StdPath::new(s).file_stem().map(|s| s.to_str()).flatten()) + .flatten() + .unwrap_or(&sch_id); + if index.map.contains_key(sch_name) { return Err(errorf(&format!( "component and schematic name collision: {}", - sch_id + sch_name ))); } index .map - .insert(sch_id.into(), Node::Sheet(index_schematic(sub_sch)?)); + .insert(sch_name.into(), Node::Sheet(index_schematic(sub_sch)?)); } Ok(index) @@ -52,7 +60,7 @@ pub fn evaluate_schematic(index: &mut SheetIndex) -> DynamicResult<()> { for (node_ref, node) in index.map.iter() { if let Node::Component(component_index) = node { for a in component_index.keys() { - paths.push(vec![node_ref.into(), a.into()].into()) + paths.push(vec![node_ref.into(), a.into()].into()); } } } From 2b2d7555562eb9c42712729c0b5301c4bb5ed52b Mon Sep 17 00:00:00 2001 From: Dennis Marttinen Date: Mon, 26 Jul 2021 19:05:20 +0300 Subject: [PATCH 4/4] Update schematics used for evaluator testing --- README.md | 6 +- ...{BD9E302EFJ-5V1.sch => BD9E302EFJ_5V1.sch} | 28 +++-- hat-psu/hat-psu/BMC.sch | 8 +- hat-psu/hat-psu/Properties.sch | 31 ++++++ .../{compute-sbc.sch => compute_sbc.sch} | 2 +- .../{hat-psu.kicad_pcb => hat_psu.kicad_pcb} | 0 hat-psu/hat-psu/{hat-psu.pro => hat_psu.pro} | 0 hat-psu/hat-psu/{hat-psu.sch => hat_psu.sch} | 102 ++++++++++-------- .../{ideal-diode.pro => ideal_diode.pro} | 0 .../{ideal-diode.sch => ideal_diode.sch} | 4 +- 10 files changed, 118 insertions(+), 63 deletions(-) rename hat-psu/hat-psu/{BD9E302EFJ-5V1.sch => BD9E302EFJ_5V1.sch} (94%) create mode 100644 hat-psu/hat-psu/Properties.sch rename hat-psu/hat-psu/{compute-sbc.sch => compute_sbc.sch} (99%) rename hat-psu/hat-psu/{hat-psu.kicad_pcb => hat_psu.kicad_pcb} (100%) rename hat-psu/hat-psu/{hat-psu.pro => hat_psu.pro} (100%) rename hat-psu/hat-psu/{hat-psu.sch => hat_psu.sch} (94%) rename hat-psu/hat-psu/{ideal-diode.pro => ideal_diode.pro} (100%) rename hat-psu/hat-psu/{ideal-diode.sch => ideal_diode.sch} (99%) diff --git a/README.md b/README.md index 3c48f03..7ce7189 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Command line arguments: ```bash # This command will update the file in place -cargo run --bin=evaluator ../../../hat-psu/hat-psu/BD9E302EFJ-5V1.sch +cargo run --bin=evaluator ../../../hat-psu/hat-psu/BD9E302EFJ_5V1.sch ``` ### Parser @@ -30,7 +30,7 @@ Command line arguments: ```bash # Or any other .sch file. This command writes to stdout, you can save it in a # file like this or pipe it to the classifier. -cargo run --bin=parser ../../../hat-psu/hat-psu/BD9E302EFJ-5V1.sch > parsed.yaml +cargo run --bin=parser ../../../hat-psu/hat-psu/BD9E302EFJ_5V1.sch > parsed.yaml ``` ### Classifier @@ -46,7 +46,7 @@ Command line arguments: cat parsed.yaml | cargo run --bin=classifier sample_policy.cue > classified.yaml # ... pipe the output from the parser like this (writes to stdout) -cargo run --bin=parser ../../../hat-psu/hat-psu/BD9E302EFJ-5V1.sch | cargo run --bin=classifier sample_policy.cue +cargo run --bin=parser ../../../hat-psu/hat-psu/BD9E302EFJ_5V1.sch | cargo run --bin=classifier sample_policy.cue ``` ## Contributing diff --git a/hat-psu/hat-psu/BD9E302EFJ-5V1.sch b/hat-psu/hat-psu/BD9E302EFJ_5V1.sch similarity index 94% rename from hat-psu/hat-psu/BD9E302EFJ-5V1.sch rename to hat-psu/hat-psu/BD9E302EFJ_5V1.sch index ee410d5..1d4e279 100644 --- a/hat-psu/hat-psu/BD9E302EFJ-5V1.sch +++ b/hat-psu/hat-psu/BD9E302EFJ_5V1.sch @@ -3,7 +3,7 @@ EELAYER 30 0 EELAYER END $Descr A4 11693 8268 encoding utf-8 -Sheet 2 2 +Sheet 7 9 Title "Compute unit HAT attachment" Date "2021-06-13" Rev "0.1.1" @@ -20,7 +20,7 @@ P 6700 3600 AR Path="/60A118D6" Ref="L2" Part="1" AR Path="/60A0E5A4/60A118D6" Ref="L2" Part="1" AR Path="/60F06CF1/60A118D6" Ref="L?" Part="1" -F 0 "L?" V 6890 3600 50 0000 C CNN +F 0 "L2" V 6890 3600 50 0000 C CNN F 1 "8.2" V 6799 3600 50 0000 C CNN F 2 "racklet:DEM8045C" H 6700 3600 50 0001 C CNN F 3 "https://media.digikey.com/pdf/Data%20Sheets/Murata%20PDFs/DEMO80(30,40,45)C%20Type.pdf" H 6700 3600 50 0001 C CNN @@ -36,7 +36,7 @@ L Device:C_Small C? U 1 1 60A1617F P 7150 3750 F 0 "C?" H 7242 3796 50 0000 L CNN -F 1 "47" H 7242 3705 50 0000 L CNN +F 1 "0.1" H 7242 3705 50 0000 L CNN F 2 "Capacitor_SMD:C_1206_3216Metric" H 7150 3750 50 0001 C CNN F 3 "~" H 7150 3750 50 0001 C CNN F 4 "true" H 7150 3750 50 0001 C CNN "ICCC_Show" @@ -187,15 +187,15 @@ $EndComp Wire Wire Line 7700 4250 7550 4250 $Comp -L Device:C_Small C? +L Device:C_Small C3 U 1 1 60A2522A P 3900 4000 -F 0 "C?" H 3992 4046 50 0000 L CNN -F 1 "0.1" H 3992 3955 50 0000 L CNN +F 0 "C3" H 3992 4046 50 0000 L CNN +F 1 "51" H 3992 3955 50 0000 L CNN F 2 "Capacitor_SMD:C_0603_1608Metric" H 3900 4000 50 0001 C CNN F 3 "~" H 3900 4000 50 0001 C CNN F 4 "true" H 3900 4000 50 0001 C CNN "ICCC_Show" -F 5 "0.1" H 3900 4000 50 0001 C CNN "value_expr" +F 5 "Properties.Globals.TargetVoltage * 10" H 3900 4000 50 0001 C CNN "value_expr" F 6 "35" H 3900 4000 50 0001 C CNN "voltagerating_expr" F 7 "V" H 3900 4000 50 0001 C CNN "voltagerating_unit" F 8 "35 V" H 3900 4000 50 0001 C CNN "voltagerating" @@ -288,15 +288,15 @@ Wire Wire Line Wire Wire Line 3500 3900 3500 3700 $Comp -L Device:C_Small C? +L Device:C_Small C2 U 1 1 60A24C15 P 3500 4000 -F 0 "C?" H 3592 4046 50 0000 L CNN -F 1 "10 F" H 3592 3955 50 0000 L CNN +F 0 "C2" H 3592 4046 50 0000 L CNN +F 1 "5100 F" H 3592 3955 50 0000 L CNN F 2 "Capacitor_SMD:C_1206_3216Metric" H 3500 4000 50 0001 C CNN F 3 "~" H 3500 4000 50 0001 C CNN F 4 "true" H 3500 4000 50 0001 C CNN "ICCC_Show" -F 5 "C3.Value*100" H 3500 4000 50 0001 C CNN "value_expr" +F 5 "C3*100" H 3500 4000 50 0001 C CNN "value_expr" F 6 "F" H 3500 4000 50 0001 C CNN "value_unit" 1 3500 4000 1 0 0 -1 @@ -349,4 +349,10 @@ Wire Wire Line Connection ~ 6250 3600 Wire Wire Line 6250 3600 6550 3600 +$Sheet +S 2950 2600 500 150 +U 60FFED3D +F0 "Properties_BD9E302EFJ_5V1" 50 +F1 "Properties.sch" 50 +$EndSheet $EndSCHEMATC diff --git a/hat-psu/hat-psu/BMC.sch b/hat-psu/hat-psu/BMC.sch index 937bb5c..7af637a 100644 --- a/hat-psu/hat-psu/BMC.sch +++ b/hat-psu/hat-psu/BMC.sch @@ -3,7 +3,7 @@ EELAYER 30 0 EELAYER END $Descr A4 11693 8268 encoding utf-8 -Sheet 1 2 +Sheet 1 9 Title "Compute unit HAT attachment" Date "2021-06-13" Rev "0.1.1" @@ -168,9 +168,9 @@ Wire Wire Line Text HLabel 3250 5450 0 50 Input ~ 0 3V3 $Sheet -S 7700 3750 950 350 +S 7700 3750 950 350 U 60F06CF1 -F0 "BD9E302EFJ" 50 -F1 "BD9E302EFJ-5V1.sch" 50 +F0 "BD9E302EFJ_5V1" 50 +F1 "BD9E302EFJ_5V1.sch" 50 $EndSheet $EndSCHEMATC diff --git a/hat-psu/hat-psu/Properties.sch b/hat-psu/hat-psu/Properties.sch new file mode 100644 index 0000000..130651a --- /dev/null +++ b/hat-psu/hat-psu/Properties.sch @@ -0,0 +1,31 @@ +EESchema Schematic File Version 4 +EELAYER 30 0 +EELAYER END +$Descr A4 11693 8268 +encoding utf-8 +Sheet 9 9 +Title "" +Date "" +Rev "" +Comp "" +Comment1 "" +Comment2 "" +Comment3 "" +Comment4 "" +$EndDescr +$Comp +L Device:R Globals +U 1 1 610002DE +P 4400 3350 +AR Path="/60A0E5A4/60FFED3D/610002DE" Ref="Globals" Part="1" +AR Path="/60FF8D0D/610002DE" Ref="Globals" Part="1" +F 0 "Globals" H 4470 3396 50 0000 L CNN +F 1 "R" H 4470 3305 50 0000 L CNN +F 2 "Resistor_SMD:R_0603_1608Metric" V 4330 3350 50 0001 C CNN +F 3 "~" H 4400 3350 50 0001 C CNN +F 4 "5.1" H 4400 3350 50 0001 C CNN "TargetVoltage" +F 5 "5.1" H 4400 3350 50 0001 C CNN "TargetVoltage_expr" + 1 4400 3350 + 1 0 0 -1 +$EndComp +$EndSCHEMATC diff --git a/hat-psu/hat-psu/compute-sbc.sch b/hat-psu/hat-psu/compute_sbc.sch similarity index 99% rename from hat-psu/hat-psu/compute-sbc.sch rename to hat-psu/hat-psu/compute_sbc.sch index 513a6d0..973608e 100644 --- a/hat-psu/hat-psu/compute-sbc.sch +++ b/hat-psu/hat-psu/compute_sbc.sch @@ -3,7 +3,7 @@ EELAYER 30 0 EELAYER END $Descr A4 11693 8268 encoding utf-8 -Sheet 2 5 +Sheet 2 9 Title "Compute unit HAT attachment" Date "2021-06-13" Rev "0.1.1" diff --git a/hat-psu/hat-psu/hat-psu.kicad_pcb b/hat-psu/hat-psu/hat_psu.kicad_pcb similarity index 100% rename from hat-psu/hat-psu/hat-psu.kicad_pcb rename to hat-psu/hat-psu/hat_psu.kicad_pcb diff --git a/hat-psu/hat-psu/hat-psu.pro b/hat-psu/hat-psu/hat_psu.pro similarity index 100% rename from hat-psu/hat-psu/hat-psu.pro rename to hat-psu/hat-psu/hat_psu.pro diff --git a/hat-psu/hat-psu/hat-psu.sch b/hat-psu/hat-psu/hat_psu.sch similarity index 94% rename from hat-psu/hat-psu/hat-psu.sch rename to hat-psu/hat-psu/hat_psu.sch index 391d059..9dc587a 100644 --- a/hat-psu/hat-psu/hat-psu.sch +++ b/hat-psu/hat-psu/hat_psu.sch @@ -3,7 +3,7 @@ EELAYER 30 0 EELAYER END $Descr A4 11693 8268 encoding utf-8 -Sheet 1 5 +Sheet 1 9 Title "Compute unit HAT attachment" Date "2021-06-13" Rev "0.1.1" @@ -125,14 +125,6 @@ Wire Wire Line 3800 3000 3850 3000 Text Notes 4950 2700 0 50 ~ 0 current sense shunt\nTODO: Does this\nhave to be 10mohm? -$Sheet -S 9200 2700 950 600 -U 60AD3F6A -F0 "compute-sbc" 50 -F1 "compute-sbc.sch" 50 -F2 "5.1V" I L 9200 2850 50 -F3 "GND" U L 9200 3200 50 -$EndSheet Wire Wire Line 9200 3200 9050 3200 Wire Wire Line @@ -177,14 +169,6 @@ Text Label 3800 3000 2 50 ~ 0 DCDC_EN Text GLabel 4850 2800 1 50 Input ~ 0 5V1 -$Sheet -S 7900 2750 800 200 -U 60A9D2A2 -F0 "ideal-diode" 50 -F1 "ideal-diode.sch" 50 -F2 "CATHODE" O R 8700 2850 50 -F3 "ANODE" I L 7900 2850 50 -$EndSheet Wire Wire Line 8700 2850 9200 2850 Text Notes 7900 2600 0 50 ~ 0 @@ -424,21 +408,6 @@ F 3 "~" H 7100 3650 50 0001 C CNN $EndComp Wire Wire Line 6300 3850 6900 3850 -$Sheet -S 7250 3350 800 2050 -U 60AC90CC -F0 "BMC" 50 -F1 "BMC.sch" 50 -F2 "PAC_CLK" O L 7250 3750 50 -F3 "PAC_DATA" B L 7250 3850 50 -F4 "PAC_INT" O L 7250 3950 50 -F5 "DCDC_EN" O L 7250 4300 50 -F6 "PAC_DAC" I L 7250 4050 50 -F7 "USB_D-" I L 7250 4700 50 -F8 "USB_D+" I L 7250 4600 50 -F9 "USB_5V" I L 7250 4500 50 -F10 "3V3" I L 7250 3450 50 -$EndSheet Text Notes 7250 3250 0 50 ~ 0 TODO: Design BMC Text Notes 7350 5100 0 50 ~ 0 @@ -508,15 +477,6 @@ Wire Wire Line 5800 3950 7250 3950 Wire Wire Line 7250 4050 5800 4050 -$Sheet -S 3850 2700 550 400 -U 60A0E5A4 -F0 "BD9E302EFJ-5V1" 50 -F1 "BD9E302EFJ-5V1.sch" 50 -F2 "VOUT" O R 4400 2850 50 -F3 "VIN" I L 3850 2850 50 -F4 "EN" I L 3850 3000 50 -$EndSheet $Comp L power:VBUS #PWR0104 U 1 1 60BFD071 @@ -679,7 +639,7 @@ Wire Wire Line 1350 4250 1350 4200 Connection ~ 1350 4200 Wire Wire Line - 1350 4200 950 4200 + 1350 4200 950 4200 $Comp L power:VBUS #PWR014 U 1 1 60C85D7C @@ -817,4 +777,62 @@ F 3 "" H 8650 1400 50 0001 C CNN $EndComp Text Label 6150 3350 2 50 ~ 0 BMC_3V3 +$Comp +L Device:R R99 +U 1 1 60FFD411 +P 5050 1700 +F 0 "R99" H 5120 1746 50 0000 L CNN +F 1 "5.1" H 5120 1655 50 0000 L CNN +F 2 "Resistor_SMD:R_0603_1608Metric" V 4980 1700 50 0001 C CNN +F 3 "~" H 5050 1700 50 0001 C CNN +F 4 "Properties.Globals.TargetVoltage" H 5050 1700 50 0001 C CNN "Value_expr" + 1 5050 1700 + 1 0 0 -1 +$EndComp +$Sheet +S 9200 2700 950 600 +U 60AD3F6A +F0 "compute_sbc" 50 +F1 "compute_sbc.sch" 50 +F2 "5.1V" I L 9200 2850 50 +F3 "GND" U L 9200 3200 50 +$EndSheet +$Sheet +S 7900 2750 800 200 +U 60A9D2A2 +F0 "ideal_diode" 50 +F1 "ideal_diode.sch" 50 +F2 "CATHODE" O R 8700 2850 50 +F3 "ANODE" I L 7900 2850 50 +$EndSheet +$Sheet +S 7250 3350 800 2050 +U 60AC90CC +F0 "BMC" 50 +F1 "BMC.sch" 50 +F2 "PAC_CLK" O L 7250 3750 50 +F3 "PAC_DATA" B L 7250 3850 50 +F4 "PAC_INT" O L 7250 3950 50 +F5 "DCDC_EN" O L 7250 4300 50 +F6 "PAC_DAC" I L 7250 4050 50 +F7 "USB_D-" I L 7250 4700 50 +F8 "USB_D+" I L 7250 4600 50 +F9 "USB_5V" I L 7250 4500 50 +F10 "3V3" I L 7250 3450 50 +$EndSheet +$Sheet +S 3850 2700 550 400 +U 60A0E5A4 +F0 "BD9E302EFJ_5V1" 50 +F1 "BD9E302EFJ_5V1.sch" 50 +F2 "VOUT" O R 4400 2850 50 +F3 "VIN" I L 3850 2850 50 +F4 "EN" I L 3850 3000 50 +$EndSheet +$Sheet +S 3850 1600 500 150 +U 60FF8D0D +F0 "Properties_hat_psu" 50 +F1 "Properties.sch" 50 +$EndSheet $EndSCHEMATC diff --git a/hat-psu/hat-psu/ideal-diode.pro b/hat-psu/hat-psu/ideal_diode.pro similarity index 100% rename from hat-psu/hat-psu/ideal-diode.pro rename to hat-psu/hat-psu/ideal_diode.pro diff --git a/hat-psu/hat-psu/ideal-diode.sch b/hat-psu/hat-psu/ideal_diode.sch similarity index 99% rename from hat-psu/hat-psu/ideal-diode.sch rename to hat-psu/hat-psu/ideal_diode.sch index 751a638..0ada6ad 100644 --- a/hat-psu/hat-psu/ideal-diode.sch +++ b/hat-psu/hat-psu/ideal_diode.sch @@ -3,7 +3,7 @@ EELAYER 30 0 EELAYER END $Descr A4 11693 8268 encoding utf-8 -Sheet 3 5 +Sheet 3 9 Title "Compute unit HAT attachment" Date "2021-06-13" Rev "0.1.1" @@ -49,7 +49,7 @@ F 0 "Q1" H 5840 4104 50 0000 L CNN F 1 "BCM857BS" H 5840 4195 50 0000 L CNN F 2 "Package_TO_SOT_SMD:SOT-363_SC-70-6" H 5850 4250 50 0001 C CNN F 3 "https://www.diodes.com/assets/Datasheets/BCM857BS.pdf" H 5650 4150 50 0001 C CNN - 2 5650 4150 + 1 5650 4150 1 0 0 1 $EndComp $Comp