use crate::spec::CLK_FREQUENCY_HZ;
use crate::spec::registers::{FifoTriggerLevel, IER, Parity, WordLength};
use core::cmp::Ordering;
#[allow(missing_docs)]
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
pub enum BaudRate {
Baud115200,
Baud57600,
Baud38400,
#[default]
Baud9600,
Baud4800,
Baud2400,
Baud1200,
Baud600,
Baud300,
Baud150,
Baud110,
Custom(u32),
}
impl BaudRate {
#[must_use]
pub const fn to_integer(self) -> u32 {
match self {
Self::Baud115200 => 115200,
Self::Baud57600 => 57600,
Self::Baud38400 => 38400,
Self::Baud9600 => 9600,
Self::Baud4800 => 4800,
Self::Baud2400 => 2400,
Self::Baud1200 => 1200,
Self::Baud600 => 600,
Self::Baud300 => 300,
Self::Baud150 => 150,
Self::Baud110 => 110,
Self::Custom(val) => val,
}
}
#[must_use]
pub const fn from_integer(value: u32) -> Self {
match value {
115200 => Self::Baud115200,
57600 => Self::Baud57600,
38400 => Self::Baud38400,
9600 => Self::Baud9600,
4800 => Self::Baud4800,
2400 => Self::Baud2400,
1200 => Self::Baud1200,
600 => Self::Baud600,
300 => Self::Baud300,
150 => Self::Baud150,
110 => Self::Baud110,
baud_rate => Self::Custom(baud_rate),
}
}
}
impl PartialOrd for BaudRate {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for BaudRate {
fn cmp(&self, other: &Self) -> Ordering {
self.to_integer().cmp(&other.to_integer())
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Config {
pub interrupts: IER,
pub frequency: u32,
pub prescaler_division_factor: Option<u32>,
pub fifo_trigger_level: Option<FifoTriggerLevel>,
pub baud_rate: BaudRate,
pub data_bits: WordLength,
pub extra_stop_bits: bool,
pub parity: Parity,
}
impl Config {
pub const DEFAULT: Self = Self {
interrupts: IER::DATA_READY,
frequency: CLK_FREQUENCY_HZ,
prescaler_division_factor: None,
fifo_trigger_level: Some(FifoTriggerLevel::Fourteen),
baud_rate: BaudRate::Baud9600,
data_bits: WordLength::EightBits,
extra_stop_bits: false,
parity: Parity::Disabled,
};
}
impl Default for Config {
fn default() -> Self {
Self::DEFAULT
}
}