Skip to content

Commit 6ebcfe8

Browse files
committed
Add spi config mutation methods
1 parent b166c76 commit 6ebcfe8

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/spi/config.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::time::rate::{self, Extensions};
44
use core::fmt;
55

66
use crate::hal::spi::{self, Mode};
7+
use crate::time::rate::Generic;
78

89
/// Configuration struct for [`Spi`](super::Spi) providing all
910
/// communication-related / parameters.
@@ -43,6 +44,19 @@ pub struct Config {
4344
pub mode: Mode,
4445
}
4546

47+
impl Config {
48+
/// Set the operating frequency of the SPI
49+
pub fn frequency(mut self, frequency: impl Into<Generic<u32>>) -> Self {
50+
self.frequency = frequency.into();
51+
self
52+
}
53+
/// Set the Operation Mode
54+
pub fn mode(mut self, mode: Mode) -> Self {
55+
self.mode = mode;
56+
self
57+
}
58+
}
59+
4660
impl fmt::Debug for Config {
4761
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4862
let mode = if self.mode == spi::MODE_0 {

testsuite/tests/spi.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ use hal::prelude::*;
1010

1111
use hal::gpio::{PushPull, AF6};
1212
use hal::gpio::{PC10, PC11, PC12};
13+
use hal::hal::spi::MODE_0;
1314
use hal::pac;
1415
use hal::pac::SPI3;
15-
use hal::spi::Spi;
16+
use hal::spi::{config::Config, Mode, Phase, Polarity, Spi};
1617
use hal::time::rate::{self, Extensions};
1718

1819
const TEST_MSG: [u8; 8] = [0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0xaa, 0xbb];
@@ -95,4 +96,19 @@ mod tests {
9596
state.spi = Some(spi);
9697
}
9798
}
99+
100+
#[test]
101+
fn config_builder() {
102+
let config = Config::default();
103+
104+
assert!(config.frequency == 1.MHz().into());
105+
assert!(config.mode == MODE_0);
106+
107+
let config = config.frequency(1.MHz()).mode(Mode {
108+
polarity: Polarity::IdleLow,
109+
phase: Phase::CaptureOnFirstTransition,
110+
});
111+
112+
assert!(config == Config::default());
113+
}
98114
}

0 commit comments

Comments
 (0)