stm32f3xx_hal/spi/
config.rs1use crate::time::rate::{self, Extensions};
4use core::fmt;
5
6use crate::hal::spi::{self, Mode};
7use crate::time::rate::Generic;
8
9#[derive(Clone, Copy, PartialEq, Eq)]
39#[non_exhaustive]
40pub struct Config {
41 pub frequency: rate::Generic<u32>,
43 pub mode: Mode,
45}
46
47impl Config {
48 #[must_use]
50 pub fn frequency(mut self, frequency: impl Into<Generic<u32>>) -> Self {
51 self.frequency = frequency.into();
52 self
53 }
54 #[must_use]
56 pub fn mode(mut self, mode: Mode) -> Self {
57 self.mode = mode;
58 self
59 }
60}
61
62impl fmt::Debug for Config {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 let mode = if self.mode == spi::MODE_0 {
65 0
66 } else if self.mode == spi::MODE_1 {
67 1
68 } else if self.mode == spi::MODE_2 {
69 2
70 } else {
71 3
72 };
73
74 f.debug_struct("Config")
75 .field("frequency", &format_args!("{:?}", self.frequency))
76 .field("mode", &format_args!("MODE_{mode}"))
77 .finish()
78 }
79}
80
81#[cfg(feature = "defmt")]
82impl defmt::Format for Config {
83 fn format(&self, f: defmt::Formatter) {
84 let mode = if self.mode == spi::MODE_0 {
85 0
86 } else if self.mode == spi::MODE_1 {
87 1
88 } else if self.mode == spi::MODE_2 {
89 2
90 } else {
91 3
92 };
93
94 defmt::write!(
95 f,
96 "Config {{ frequency: {} Hz, mode: MODE_{}}}",
97 self.frequency.integer() * *self.frequency.scaling_factor(),
98 mode,
99 );
100 }
101}
102
103impl Default for Config {
104 fn default() -> Self {
105 Self {
106 frequency: 1.MHz().into(),
107 mode: spi::MODE_0,
108 }
109 }
110}
111
112impl<T: Into<rate::Generic<u32>>> From<T> for Config {
113 fn from(f: T) -> Config {
114 Config {
115 frequency: f.into(),
116 ..Default::default()
117 }
118 }
119}