Skip to content

Commit

Permalink
Move BITS constant from CharSize to FixedCharSize trait
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeaurivage committed Jul 30, 2021
1 parent f9654ec commit acc6714
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
35 changes: 19 additions & 16 deletions hal/src/sercom/v2/uart/charsize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ use num_traits::{AsPrimitive, PrimInt};
pub trait CharSize: Sealed {
/// Word size for the character size
type Word: 'static + PrimInt + AsPrimitive<DataReg>;

/// Bits to write into the `LENGTH` register
const BITS: CharSizeEnum;
}

/// Type-level `enum` indicating a [`CharSize`] that is not dynamic
pub trait FixedCharSize: CharSize {}
pub trait FixedCharSize: CharSize {
/// Bits to write into the `LENGTH` register
const SIZE: CharSizeEnum;
}

/// Type alias to recover the `Word` type from an implementation of [`CharSize`]
pub type Word<C> = <C as CharSize>::Word;
Expand Down Expand Up @@ -54,41 +54,44 @@ pub enum CharSizeEnum {
impl Sealed for FiveBit {}
impl CharSize for FiveBit {
type Word = u8;
const BITS: CharSizeEnum = CharSizeEnum::FiveBit;
}
impl FixedCharSize for FiveBit {}
impl FixedCharSize for FiveBit {
const SIZE: CharSizeEnum = CharSizeEnum::FiveBit;
}

impl Sealed for SixBit {}
impl CharSize for SixBit {
type Word = u8;
const BITS: CharSizeEnum = CharSizeEnum::SixBit;
}
impl FixedCharSize for SixBit {}
impl FixedCharSize for SixBit {
const SIZE: CharSizeEnum = CharSizeEnum::SixBit;
}

impl Sealed for SevenBit {}
impl CharSize for SevenBit {
type Word = u8;
const BITS: CharSizeEnum = CharSizeEnum::SevenBit;
}
impl FixedCharSize for SevenBit {}
impl FixedCharSize for SevenBit {
const SIZE: CharSizeEnum = CharSizeEnum::SevenBit;
}

impl Sealed for EightBit {}
impl CharSize for EightBit {
type Word = u8;
const BITS: CharSizeEnum = CharSizeEnum::EightBit;
}
impl FixedCharSize for EightBit {}
impl FixedCharSize for EightBit {
const SIZE: CharSizeEnum = CharSizeEnum::EightBit;
}

impl Sealed for NineBit {}
impl CharSize for NineBit {
type Word = u16;
const BITS: CharSizeEnum = CharSizeEnum::NineBit;
}
impl FixedCharSize for NineBit {}
impl FixedCharSize for NineBit {
const SIZE: CharSizeEnum = CharSizeEnum::NineBit;
}

impl Sealed for DynCharSize {}
impl CharSize for DynCharSize {
type Word = u16;
// Irrelevant for DynCharSize
const BITS: CharSizeEnum = CharSizeEnum::EightBit;
}
4 changes: 2 additions & 2 deletions hal/src/sercom/v2/uart/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl<P: ValidPads> Config<P> {
// Enable internal clock mode
registers.configure_mode();
registers.configure_pads(P::RXPO as u8, P::TXPO as u8);
registers.set_char_size(EightBit::BITS);
registers.set_char_size(EightBit::SIZE);

Self {
registers,
Expand Down Expand Up @@ -137,7 +137,7 @@ where
/// Change the [`CharSize`].
#[inline]
pub fn char_size<C2: FixedCharSize>(mut self) -> Config<P, C2> {
self.registers.set_char_size(C2::BITS);
self.registers.set_char_size(C2::SIZE);
self.change()
}

Expand Down

0 comments on commit acc6714

Please sign in to comment.