-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
834cfe8
commit 4a738bd
Showing
12 changed files
with
321 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
Oops, something went wrong.