Skip to content

Commit

Permalink
Modularize the app (#8)
Browse files Browse the repository at this point in the history
* refactor: replace match w/ if let (unused Ok value)

* refactor: no need for allocation

* feat: add an error case

* refactor: factorize code

* refactor: rename a function

* refactor: use slices to avoid allocation

* refactor: shorten formatting literal

* refactor: improve readability

* refactor: modularize MemRegion

* refactor: modularize Family

* refactor: modularize Error

* refactor: modularize Target

* refactor: modularize Chip

* refactor: modularize PanicHandler

* refactor: modularize SoftDevice

* refactor: modularize InitArgs

* refactor: reorder imports

* refactor: modularize clap stuff

* refactor: remove the types module

* refactor: use Strings for error variants' data
  • Loading branch information
cyril-marpaud authored Feb 16, 2024
1 parent 834cfe8 commit 4a738bd
Show file tree
Hide file tree
Showing 12 changed files with 321 additions and 304 deletions.
71 changes: 71 additions & 0 deletions src/chip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
pub mod family;
pub mod target;

use crate::error::{Error, InvalidChip};
use std::str::FromStr;

pub(crate) struct Chip {
pub family: family::Family,
pub target: target::Target,
pub name: String,
}

impl FromStr for Chip {
type Err = Error;

fn from_str(chip: &str) -> Result<Self, Self::Err> {
use family::{mem_region::MemRegion, Family::*};
use target::Target::*;

let chips = [
// nRF
("nrf52805", (NRF(MemRegion::NRF52805), Thumbv7f)),
("nrf52810", (NRF(MemRegion::NRF52810), Thumbv7f)),
("nrf52811", (NRF(MemRegion::NRF52811), Thumbv7f)),
("nrf52820", (NRF(MemRegion::NRF52820), Thumbv7f)),
("nrf52832_xxaa", (NRF(MemRegion::NRF52832_XXAA), Thumbv7f)),
("nrf52832_xxab", (NRF(MemRegion::NRF52832_XXAB), Thumbv7f)),
("nrf52833", (NRF(MemRegion::NRF52833), Thumbv7f)),
("nrf52840", (NRF(MemRegion::NRF52840), Thumbv7f)),
// TODO: nrf53x and nrf91x
// STM
("stm32c0", (STM32, Thumbv6)),
("stm32f0", (STM32, Thumbv6)),
("stm32f1", (STM32, Thumbv7)),
("stm32f2", (STM32, Thumbv7)),
("stm32f3", (STM32, Thumbv7e)),
("stm32f4", (STM32, Thumbv7e)),
("stm32f7", (STM32, Thumbv7e)),
("stm32g0", (STM32, Thumbv6)),
("stm32g4", (STM32, Thumbv7e)),
("stm32h5", (STM32, Thumbv8)),
("stm32h7", (STM32, Thumbv7e)),
("stm32l0", (STM32, Thumbv6)),
("stm32l1", (STM32, Thumbv7)),
("stm32l4", (STM32, Thumbv7e)),
("stm32l5", (STM32, Thumbv8)),
("stm32u5", (STM32, Thumbv8)),
("stm32wb", (STM32, Thumbv7e)),
("stm32wba", (STM32, Thumbv8)),
("stm32wl", (STM32, Thumbv7e)),
];

let (family, target) = chips
.iter()
.find_map(|(s, (f, t))| chip.starts_with(s).then(|| (f.clone(), t.clone())))
.ok_or(match chip {
"nrf52832" => Error::InvalidChip(InvalidChip::Ambiguous),
_ => Error::InvalidChip(InvalidChip::Unknown),
})?;

Ok(Self {
name: match family {
STM32 => chip.to_string(),
// FRAGILE: "_" is used to coerce probe-rs chip search
NRF(_) => chip.split('_').next().unwrap().to_string(),
},
family,
target,
})
}
}
20 changes: 20 additions & 0 deletions src/chip/family.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
pub mod mem_region;

use mem_region::MemRegion;
use std::fmt::Display;

#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone)]
pub enum Family {
STM32,
NRF(MemRegion),
}

impl Display for Family {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::STM32 => "stm32",
Self::NRF(_) => "nrf",
})
}
}
48 changes: 48 additions & 0 deletions src/chip/family/mem_region.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#[derive(Clone, Debug)]
pub struct MemRegion {
pub flash_origin: usize,
pub flash_length: usize,

pub ram_origin: usize,
pub ram_length: usize,
}

impl MemRegion {
pub const NRF52805: Self = Self {
flash_origin: 0,
flash_length: 192,
ram_origin: 0x2 << 28,
ram_length: 24,
};
pub const NRF52810: Self = Self::NRF52805;
pub const NRF52811: Self = Self::NRF52805;

pub const NRF52820: Self = Self {
flash_origin: 0,
flash_length: 256,
ram_origin: 0x2 << 28,
ram_length: 32,
};

pub const NRF52832_XXAA: Self = Self {
flash_origin: 0,
flash_length: 512,
ram_origin: 0x2 << 28,
ram_length: 64,
};
pub const NRF52832_XXAB: Self = Self::NRF52820;

pub const NRF52833: Self = Self {
flash_origin: 0,
flash_length: 512,
ram_origin: 0x2 << 28,
ram_length: 128,
};

pub const NRF52840: Self = Self {
flash_origin: 0,
flash_length: 1024,
ram_origin: 0x2 << 28,
ram_length: 256,
};
}
24 changes: 24 additions & 0 deletions src/chip/target.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use clap::ValueEnum;
use std::fmt::Display;

#[derive(Debug, Clone, ValueEnum)]
#[value()]
pub enum Target {
Thumbv6,
Thumbv7,
Thumbv7e,
Thumbv7f,
Thumbv8,
}

impl Display for Target {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::Thumbv6 => "thumbv6m-none-eabi",
Self::Thumbv7 => "thumbv7m-none-eabi",
Self::Thumbv7e => "thumbv7em-none-eabi",
Self::Thumbv7f => "thumbv7em-none-eabihf",
Self::Thumbv8 => "thumbv8m.main-none-eabihf",
})
}
}
16 changes: 16 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#[derive(Debug)]
pub enum Error {
CargoAdd(String),
ChangeDir,
CreateCargo,
CreateFile(String),
CreateFolder(String),
ErroneousSoftdevice,
InvalidChip(InvalidChip),
}

#[derive(Debug)]
pub enum InvalidChip {
Unknown,
Ambiguous,
}
Loading

0 comments on commit 4a738bd

Please sign in to comment.