Skip to content

Commit

Permalink
Add enums for codegen types
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeeter committed Apr 15, 2024
1 parent 1ae8ec5 commit 48ada9e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 51 deletions.
4 changes: 2 additions & 2 deletions app/lpc55xpresso/app-sprot.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/oxide-rot-1/app-dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/oxide-rot-1/app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/rot-carrier/app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
113 changes: 70 additions & 43 deletions build/lpc55pins/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<String>,
slew: Option<String>,
invert: Option<String>,
digimode: Option<String>,
opendrain: Option<String>,
direction: Option<String>,
mode: Option<Mode>,
slew: Option<Slew>,
invert: Option<Invert>,
digimode: Option<Digimode>,
opendrain: Option<Opendrain>,
direction: Option<Direction>,
name: Option<String>,
}

#[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 {
Expand All @@ -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,
Expand Down Expand Up @@ -135,16 +165,13 @@ pub fn codegen(pins: Vec<PinConfig>) -> 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, ");")?;
}
}
Expand Down

0 comments on commit 48ada9e

Please sign in to comment.