atsamd_hal/sercom/spi/
char_size.rs

1//! Define a trait to track the [`CharSize`], which represents the [`Config`]
2//! [`Size`] for SAMD11 and SAMD21 chips
3//!
4//! [`Config`]: super::Config
5//! [`Size`]: super::Size
6
7use crate::typelevel::Sealed;
8
9//=============================================================================
10// Character size
11//=============================================================================
12
13/// Type-level enum representing the SPI character size
14///
15/// This trait acts as both a [type-level enum], forming a type class for
16/// character sizes, as well as a [type-level function] mapping the
17/// corresponding word size.
18///
19/// The SPI character size affects the word size for the embedded HAL traits.
20/// Eight-bit transactions use a `u8` word, while nine-bit transactions use a
21/// `u16` word.
22///
23/// [type-level enum]: crate::typelevel#type-level-enums
24/// [type-level function]: crate::typelevel#type-level-functions
25pub trait CharSize: Sealed {
26    /// Word size for the character size
27    type Word: 'static + Copy;
28
29    /// Register bit pattern for the corresponding `CharSize`
30    const BITS: u8;
31}
32
33/// Type alias to recover the [`Word`](CharSize::Word) type from an
34/// implementation of [`CharSize`]
35pub type Word<C> = <C as CharSize>::Word;
36
37/// [`CharSize`] variant for 8-bit transactions
38pub enum EightBit {}
39
40/// [`CharSize`] variant for 9-bit transactions
41pub enum NineBit {}
42
43impl Sealed for EightBit {}
44impl CharSize for EightBit {
45    type Word = u8;
46    const BITS: u8 = 0;
47}
48
49impl Sealed for NineBit {}
50impl CharSize for NineBit {
51    type Word = u16;
52    const BITS: u8 = 1;
53}