Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 29 additions & 31 deletions crates/sbd-gen-schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,60 +63,60 @@ pub struct Target {
pub debugger: Option<Debugger>,

// peripheral types
#[serde_as(as = "Option<KeyValueMap<_>>")]
pub leds: Option<Vec<Led>>,
#[serde_as(as = "Option<KeyValueMap<_>>")]
pub buttons: Option<Vec<Button>>,
#[serde_as(as = "Option<KeyValueMap<_>>")]
pub uarts: Option<Vec<Uart>>,
#[serde(default)]
pub leds: Vec<Led>,
#[serde(default)]
pub buttons: Vec<Button>,
#[serde(default)]
pub uarts: Vec<Uart>,
}

impl Target {
#[must_use]
pub fn has_leds(&self) -> bool {
if let Some(leds) = &self.leds {
!leds.is_empty()
} else {
false
}
!self.leds.is_empty()
}

#[must_use]

pub fn has_buttons(&self) -> bool {
if let Some(buttons) = &self.buttons {
!buttons.is_empty()
} else {
false
}
!self.buttons.is_empty()
}

/// Returns true if there are any UARTs listed for this board.
#[must_use]
pub fn has_uarts(&self) -> bool {
if let Some(uarts) = &self.uarts {
!uarts.is_empty()
} else {
false
}
!self.uarts.is_empty()
}

/// Returns true if there are any UARTs listed for this board that have the
/// [`Uart::host_facing`] property.
#[must_use]
pub fn has_host_facing_uart(&self) -> bool {
if let Some(uarts) = &self.uarts {
uarts.iter().any(|u| u.host_facing)
} else {
false
self.uarts.iter().any(|u| u.host_facing)
}

pub fn test_default() -> Self {
Self {
name: "test-target".to_string(),
ariel: ArielTargetExt::default(),
buttons: vec![],
chip: "test-chip".to_string(),
debugger: None,
description: None,
flags: BTreeSet::default(),
include: None,
leds: vec![],
quirks: vec![],
riot: RiotTargetExt::default(),
uarts: vec![],
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Led {
#[serde(rename = "$key$")]
pub name: String,
pub pin: String,
pub color: Option<String>,
pub active: Option<PinActive>,
Expand All @@ -127,8 +127,6 @@ pub struct Led {
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Button {
#[serde(rename = "$key$")]
pub name: String,
pub pin: String,
pub active: Option<PinActive>,
#[serde(default)]
Expand Down Expand Up @@ -180,8 +178,8 @@ pub struct Debugger {
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Uart {
#[serde(rename = "$key$")]
pub name: Option<String>,
#[serde(default)]
pub aliases: Vec<String>,
pub rx_pin: String,
pub tx_pin: String,
pub cts_pin: Option<String>,
Expand Down
25 changes: 8 additions & 17 deletions crates/sbd-gen/sbd-test-files/nrf52840dk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,23 @@ targets:
flags:
- has_usb_device_port
leds:
led0:
- pin: P0_13
color: green
pin: P0_13
active: low
led1:
- pin: P0_14
color: green
pin: P0_14
active: low
led2:
- pin: P0_15
color: green
pin: P0_15
active: low
led3:
- pin: P0_16
color: green
pin: P0_16

buttons:
button0:
pin: P0_11
- pin: P0_11
active: low
button1:
pin: P0_12
- pin: P0_12
active: low
button2:
pin: P0_24
- pin: P0_24
active: low
button3:
pin: P0_25
- pin: P0_25
active: low
56 changes: 27 additions & 29 deletions crates/sbd-gen/src/ariel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ use crate::{
resources::Resources,
};

use sbd_gen_schema::{
Button, Led, PinLevel, Quirk, SbdFile, SetPinOp, Target, common::StringOrVecString,
};
use sbd_gen_schema::{PinLevel, Quirk, SbdFile, SetPinOp, Target, common::StringOrVecString};

#[derive(argh::FromArgs, Debug)]
#[argh(subcommand, name = "generate-ariel")]
Expand Down Expand Up @@ -242,13 +240,13 @@ impl<'a> RenderTarget<'a> {

if target.has_leds() || target.has_buttons() || target.has_uarts() {
pins.push_str("use ariel_os_hal::hal::peripherals;\n\n");
if let Some(leds) = target.leds.as_ref() {
pins.push_str(&self.render_led_pins(leds)?);
if target.has_leds() {
pins.push_str(&self.render_led_pins()?);
}
if let Some(buttons) = target.buttons.as_ref() {
pins.push_str(&self.render_button_pins(buttons)?);
if target.has_buttons() {
pins.push_str(&self.render_button_pins()?);
}
if target.uarts.is_some() {
if target.has_uarts() {
pins.push_str(&self.render_uarts()?);
}
}
Expand All @@ -258,29 +256,33 @@ impl<'a> RenderTarget<'a> {
Ok(pins)
}

fn render_led_pins(&mut self, leds: &'a [Led]) -> Result<String> {
fn render_led_pins(&mut self) -> Result<String> {
let leds = &self.target.leds;
let mut leds_rs = String::new();

leds_rs.push_str("ariel_os_hal::define_peripherals!(LedPeripherals {\n");

for led in leds {
self.resources.claim(&led.pin, &led.name)?;
let _ = writeln!(leds_rs, "{}: {},", led.name, led.pin);
for (n, led) in leds.iter().enumerate() {
let name = format!("led{n}");
self.resources.claim(&led.pin, &name)?;
let _ = writeln!(leds_rs, "{}: {},", name, led.pin);
}

leds_rs.push_str("});\n");

Ok(leds_rs)
}

fn render_button_pins(&mut self, buttons: &'a [Button]) -> Result<String> {
fn render_button_pins(&mut self) -> Result<String> {
let buttons = &self.target.buttons;
let mut buttons_rs = String::new();

buttons_rs.push_str("ariel_os_hal::define_peripherals!(ButtonPeripherals {\n");

for button in buttons {
self.resources.claim(&button.pin, &button.name)?;
let _ = writeln!(buttons_rs, "{}: {},", button.name, button.pin);
for (n, button) in buttons.iter().enumerate() {
let name = format!("button{n}");
self.resources.claim(&button.pin, &name)?;
let _ = writeln!(buttons_rs, "{}: {},", name, button.pin);
}

buttons_rs.push_str("});\n");
Expand All @@ -289,17 +291,13 @@ impl<'a> RenderTarget<'a> {
}

fn render_uarts(&mut self) -> Result<String> {
let uarts = self.target.uarts.as_ref().unwrap();
let uarts = &self.target.uarts;
let mut code = String::new();

code.push_str("ariel_os_hal::define_uarts![\n");

for (uart_number, uart) in uarts.iter().enumerate() {
let name = uart.name.as_ref().map_or_else(
|| format!("_unnamed_uart_{uart_number}").into(),
std::borrow::Cow::from,
);

for (n, uart) in uarts.iter().enumerate() {
let name = format!("uart{n}");
{
// claim this UART's resources
// TODO: "by" could be more specific ("claimed by uart FOO as rx_pin" vs "claimed
Expand Down Expand Up @@ -443,9 +441,9 @@ pub fn test_default_target() -> Target {
#[test]
fn test_render_uarts() {
use sbd_gen_schema::Uart;
let uarts = Some(vec![
let uarts = vec![
Uart {
name: Some("CON0".to_string()),
aliases: vec!["CON0".to_string()],
rx_pin: "PA08".to_owned(),
tx_pin: "PC99".to_owned(),
cts_pin: None,
Expand All @@ -454,15 +452,15 @@ fn test_render_uarts() {
host_facing: false,
},
Uart {
name: Some("VCOM".to_string()),
aliases: vec!["VCOM".to_string()],
rx_pin: "P0_04".to_owned(),
tx_pin: "P1_23".to_owned(),
cts_pin: Some("P7.89".to_owned()),
rts_pin: Some("D5".to_owned()),
possible_peripherals: vec!["UART1".to_owned(), "LEUART0".to_owned()],
host_facing: true,
},
]);
];

let target = Target {
uarts,
Expand All @@ -475,8 +473,8 @@ fn test_render_uarts() {
assert_eq!(
rendered,
"ariel_os_hal::define_uarts![
{ name: CON0, device: UART2, tx: PC99, rx: PA08, host_facing: false },
{ name: VCOM, device: UART1, tx: P1_23, rx: P0_04, host_facing: true },
{ name: uart0, device: UART2, tx: PC99, rx: PA08, host_facing: false },
{ name: uart1, device: UART1, tx: P1_23, rx: P0_04, host_facing: true },
];
"
);
Expand Down
10 changes: 3 additions & 7 deletions crates/sbd-gen/src/riot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ fn generate_riot_target(sbd: &SbdFile, target: &Target) -> Result<RiotTarget> {

// UARTs
//let mut uart_isrs = Vec::new();
uarts.extend(target.uarts.iter().flatten());
uarts.extend(target.uarts.iter());
let mut uart_peripherals = riot_chip.peripherals.as_ref().unwrap().uarts.clone();
let mut uarts_configured = Vec::new();
for uart in uarts {
for (n, uart) in uarts.iter().enumerate() {
// get the map key of the peripheral that works for this UART's pins
#[expect(clippy::unnecessary_find_map)]
let peripheral_key = uart_peripherals
Expand All @@ -228,11 +228,7 @@ fn generate_riot_target(sbd: &SbdFile, target: &Target) -> Result<RiotTarget> {

uarts_configured.push((uart_cfg, uart_peripheral.isr));
} else {
println!(
"warning: {}: no peripheral found for UART {}",
target.name,
uart.name.as_deref().unwrap_or("unnamed")
);
println!("warning: {}: no peripheral found for UART{n}", target.name);
}
}

Expand Down
Loading