stm32f3xx_hal/spi/
config.rs

1//! Types for configuring a spi interface.
2
3use crate::time::rate::{self, Extensions};
4use core::fmt;
5
6use crate::hal::spi::{self, Mode};
7use crate::time::rate::Generic;
8
9/// Configuration struct for [`Spi`](super::Spi) providing all
10/// communication-related / parameters.
11///
12/// The default configruation can be obtain by:
13///
14/// ```
15/// # use stm32f3xx_hal::spi::config::Config;
16/// let config = Config::default();
17/// ````
18///
19/// [`Spi`](super::Spi) defaults to [`spi::MODE_0`] and a frequency of 1 `MHz`.
20///
21/// ```
22/// # use stm32f3xx_hal::spi::config::Config;
23/// let config = Config::default();
24/// ```
25///
26/// Create a configuration by using `default` in combination with the
27/// builder methods. The following snippet shows creating a configuration
28/// for 19,200 Baud, 8N1 by deriving it from the default value:
29/// ```
30/// # use stm32f3xx_hal::serial::config::*;
31/// # use stm32f3xx_hal::time::rate::{Baud, Extensions};
32/// let config = Config::default().baudrate(19_200.Bd());
33///
34/// assert!(config.baudrate == 19_200.Bd());
35/// assert!(config.parity == Parity::None);
36/// assert!(config.stopbits == StopBits::STOP1);
37/// ```
38#[derive(Clone, Copy, PartialEq, Eq)]
39#[non_exhaustive]
40pub struct Config {
41    /// Operating frequency of the SPI peripheral.
42    pub frequency: rate::Generic<u32>,
43    /// Operation Mode as defined by the [`embedded-hal`]
44    pub mode: Mode,
45}
46
47impl Config {
48    /// Set the operating frequency of the SPI
49    #[must_use]
50    pub fn frequency(mut self, frequency: impl Into<Generic<u32>>) -> Self {
51        self.frequency = frequency.into();
52        self
53    }
54    /// Set the Operation Mode
55    #[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}