From 48ada9ef065c30eb674f8ef0135034e4cf153158 Mon Sep 17 00:00:00 2001 From: Matt Keeter Date: Fri, 5 Apr 2024 09:20:55 -0400 Subject: [PATCH] Add enums for codegen types --- app/lpc55xpresso/app-sprot.toml | 4 +- app/oxide-rot-1/app-dev.toml | 4 +- app/oxide-rot-1/app.toml | 4 +- app/rot-carrier/app.toml | 4 +- build/lpc55pins/src/lib.rs | 113 ++++++++++++++++++++------------ 5 files changed, 78 insertions(+), 51 deletions(-) diff --git a/app/lpc55xpresso/app-sprot.toml b/app/lpc55xpresso/app-sprot.toml index 097e9a810..7a98140df 100644 --- a/app/lpc55xpresso/app-sprot.toml +++ b/app/lpc55xpresso/app-sprot.toml @@ -157,12 +157,12 @@ interrupts = {"flexcomm5.irq" = "spi-irq"} # Out = MOSI on, MISO off out_cfg = [ { pin = { port = 0, pin = 8 }, alt = 3 }, - { pin = { port = 0, pin = 9 }, alt = 0, mode = "PullDown" }, + { pin = { port = 0, pin = 9 }, alt = 0, mode = "pulldown" }, ] # In = MISO on, MOSI off in_cfg = [ { pin = { port = 0, pin = 9 }, alt = 3 }, - { pin = { port = 0, pin = 8 }, alt = 0, mode = "PullDown" }, + { pin = { port = 0, pin = 8 }, alt = 0, mode = "pulldown" }, ] pins = [ # SCK diff --git a/app/oxide-rot-1/app-dev.toml b/app/oxide-rot-1/app-dev.toml index 1d0097b86..563230c2a 100644 --- a/app/oxide-rot-1/app-dev.toml +++ b/app/oxide-rot-1/app-dev.toml @@ -121,12 +121,12 @@ interrupts = {"flexcomm5.irq" = "spi-irq"} # Out = MOSI on, MISO off out_cfg = [ { pin = { port = 0, pin = 8 }, alt = 3 }, - { pin = { port = 0, pin = 9 }, alt = 0, mode = "PullDown" }, + { pin = { port = 0, pin = 9 }, alt = 0, mode = "pulldown" }, ] # In = MISO on, MOSI off in_cfg = [ { pin = { port = 0, pin = 9 }, alt = 3 }, - { pin = { port = 0, pin = 8 }, alt = 0, mode = "PullDown" }, + { pin = { port = 0, pin = 8 }, alt = 0, mode = "pulldown" }, ] pins = [ # SCK diff --git a/app/oxide-rot-1/app.toml b/app/oxide-rot-1/app.toml index cf17974ad..bb7db15e7 100644 --- a/app/oxide-rot-1/app.toml +++ b/app/oxide-rot-1/app.toml @@ -111,12 +111,12 @@ interrupts = {"flexcomm5.irq" = "spi-irq"} # Out = MOSI on, MISO off out_cfg = [ { pin = { port = 0, pin = 8 }, alt = 3 }, - { pin = { port = 0, pin = 9 }, alt = 0, mode = "PullDown" }, + { pin = { port = 0, pin = 9 }, alt = 0, mode = "pulldown" }, ] # In = MISO on, MOSI off in_cfg = [ { pin = { port = 0, pin = 9 }, alt = 3 }, - { pin = { port = 0, pin = 8 }, alt = 0, mode = "PullDown" }, + { pin = { port = 0, pin = 8 }, alt = 0, mode = "pulldown" }, ] pins = [ # SCK diff --git a/app/rot-carrier/app.toml b/app/rot-carrier/app.toml index b5a92d3d2..a42dfac9d 100644 --- a/app/rot-carrier/app.toml +++ b/app/rot-carrier/app.toml @@ -151,12 +151,12 @@ interrupts = {"flexcomm3.irq" = "spi-irq"} # Out = MOSI on, MISO off out_cfg = [ { pin = { port = 0, pin = 3 }, alt = 1 }, - { pin = { port = 0, pin = 2 }, alt = 0, mode = "PullDown" }, + { pin = { port = 0, pin = 2 }, alt = 0, mode = "pulldown" }, ] # In = MISO on, MOSI off in_cfg = [ { pin = { port = 0, pin = 2 }, alt = 1 }, - { pin = { port = 0, pin = 3 }, alt = 0, mode = "PullDown" }, + { pin = { port = 0, pin = 3 }, alt = 0, mode = "pulldown" }, ] pins = [ # SCK diff --git a/build/lpc55pins/src/lib.rs b/build/lpc55pins/src/lib.rs index d0e6e8d70..e1954a820 100644 --- a/build/lpc55pins/src/lib.rs +++ b/build/lpc55pins/src/lib.rs @@ -9,7 +9,7 @@ use serde::Deserialize; use std::io::{BufWriter, Write}; #[derive(Clone, Debug, Deserialize)] -#[serde(rename_all = "kebab-case", deny_unknown_fields)] +#[serde(rename_all = "lowercase", deny_unknown_fields)] struct Pin { port: usize, pin: usize, @@ -36,53 +36,82 @@ impl ToTokens for Pin { } #[derive(Clone, Debug, Deserialize)] -#[serde(rename_all = "kebab-case", deny_unknown_fields)] +#[serde(rename_all = "lowercase", deny_unknown_fields)] pub struct PinConfig { pin: Pin, alt: usize, - mode: Option, - slew: Option, - invert: Option, - digimode: Option, - opendrain: Option, - direction: Option, + mode: Option, + slew: Option, + invert: Option, + digimode: Option, + opendrain: Option, + direction: Option, name: Option, } +#[derive(Copy, Clone, Debug, Deserialize)] +#[serde(rename_all = "lowercase")] +enum Mode { + NoPull, + PullDown, + PullUp, + Repeater, +} + +#[derive(Copy, Clone, Debug, Deserialize)] +#[serde(rename_all = "lowercase")] +enum Slew { + Standard, + Fast, +} + +#[derive(Copy, Clone, Debug, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum Invert { + Disable, + Enabled, +} + +#[derive(Copy, Clone, Debug, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum Opendrain { + Normal, + Opendrain, +} + +#[derive(Copy, Clone, Debug, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum Digimode { + Analog, + Digital, +} + +#[derive(Copy, Clone, Debug, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum Direction { + Input, + Output, +} + impl PinConfig { - fn get_mode(&self) -> String { - match &self.mode { - None => "NoPull".to_string(), - Some(s) => s.to_string(), - } + fn get_mode(&self) -> Mode { + self.mode.unwrap_or(Mode::NoPull) } - fn get_slew(&self) -> String { - match &self.slew { - None => "Standard".to_string(), - Some(s) => s.to_string(), - } + fn get_slew(&self) -> Slew { + self.slew.unwrap_or(Slew::Standard) } - fn get_invert(&self) -> String { - match &self.invert { - None => "Disable".to_string(), - Some(s) => s.to_string(), - } + fn get_invert(&self) -> Invert { + self.invert.unwrap_or(Invert::Disable) } - fn get_digimode(&self) -> String { - match &self.digimode { - None => "Digital".to_string(), - Some(s) => s.to_string(), - } + fn get_digimode(&self) -> Digimode { + self.digimode.unwrap_or(Digimode::Digital) } - fn get_opendrain(&self) -> String { - match &self.opendrain { - None => "Normal".to_string(), - Some(s) => s.to_string(), - } + fn get_opendrain(&self) -> Opendrain { + self.opendrain.unwrap_or(Opendrain::Normal) } fn get_alt(&self) -> usize { @@ -98,12 +127,13 @@ impl ToTokens for PinConfig { fn to_tokens(&self, tokens: &mut TokenStream) { let final_pin = self.pin.to_token_stream(); let alt_num = format_ident!("Alt{}", self.get_alt()); - let mode = format_ident!("{}", self.get_mode()); - let slew = format_ident!("{}", self.get_slew()); - let invert = format_ident!("{}", self.get_invert()); - let digimode = format_ident!("{}", self.get_digimode()); - let od = format_ident!("{}", self.get_opendrain()); + let mode = format_ident!("{}", format!("{:?}", self.get_mode())); + let slew = format_ident!("{}", format!("{:?}", self.get_slew())); + let invert = format_ident!("{}", format!("{:?}", self.get_invert())); + let digimode = + format_ident!("{}", format!("{:?}", self.get_digimode())); + let od = format_ident!("{}", format!("{:?}", self.get_opendrain())); tokens.append_all(final_pin); tokens.append_all(quote::quote! { AltFn::#alt_num, @@ -135,16 +165,13 @@ pub fn codegen(pins: Vec) -> Result<()> { writeln!(&mut file, "iocon.iocon_configure(")?; writeln!(&mut file, "{}", p.to_token_stream())?; writeln!(&mut file, ");")?; + match p.direction { None => (), Some(d) => { writeln!(&mut file, "iocon.set_dir(")?; writeln!(&mut file, "{}", p.pin.to_token_stream())?; - if d == "output" { - writeln!(&mut file, "Direction::Output")?; - } else { - writeln!(&mut file, "Direction::Input")?; - } + writeln!(&mut file, "Direction::{d:?}")?; writeln!(&mut file, ");")?; } }