Skip to content

Commit 14d3c6f

Browse files
authored
Merge pull request #159 from ceigel/spi2
Enable SPI2 for stm32l4x1
2 parents 54238fe + 8cea9db commit 14d3c6f

File tree

1 file changed

+50
-15
lines changed

1 file changed

+50
-15
lines changed

src/spi.rs

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,7 @@ macro_rules! hal {
8888
w.frxth().set_bit().ds().bits(0b111).ssoe().clear_bit()
8989
});
9090

91-
let br = match clocks.$pclkX().0 / freq.into().0 {
92-
0 => unreachable!(),
93-
1..=2 => 0b000,
94-
3..=5 => 0b001,
95-
6..=11 => 0b010,
96-
12..=23 => 0b011,
97-
24..=39 => 0b100,
98-
40..=95 => 0b101,
99-
96..=191 => 0b110,
100-
_ => 0b111,
101-
};
91+
let br = Self::compute_baud_rate(clocks.$pclkX(), freq.into());
10292

10393
// CPHA: phase
10494
// CPOL: polarity
@@ -136,6 +126,31 @@ macro_rules! hal {
136126
Spi { spi, pins }
137127
}
138128

129+
/// Change the baud rate of the SPI
130+
pub fn reclock<F>(&mut self, freq: F, clocks: Clocks)
131+
where F: Into<Hertz>
132+
{
133+
self.spi.cr1.modify(|_, w| w.spe().clear_bit());
134+
self.spi.cr1.modify(|_, w| {
135+
unsafe {w.br().bits(Self::compute_baud_rate(clocks.$pclkX(), freq.into()));}
136+
w.spe().set_bit()
137+
});
138+
}
139+
140+
fn compute_baud_rate(clocks: Hertz, freq: Hertz) -> u8 {
141+
match clocks.0 / freq.0 {
142+
0 => unreachable!(),
143+
1..=2 => 0b000,
144+
3..=5 => 0b001,
145+
6..=11 => 0b010,
146+
12..=23 => 0b011,
147+
24..=39 => 0b100,
148+
40..=95 => 0b101,
149+
96..=191 => 0b110,
150+
_ => 0b111,
151+
}
152+
}
153+
139154
/// Releases the SPI peripheral and associated pins
140155
pub fn free(self) -> ($SPIX, (SCK, MISO, MOSI)) {
141156
(self.spi, self.pins)
@@ -191,7 +206,12 @@ macro_rules! hal {
191206
}
192207
}
193208

194-
#[cfg(any(feature = "stm32l4x3", feature = "stm32l4x5", feature = "stm32l4x6",))]
209+
#[cfg(any(
210+
feature = "stm32l4x1",
211+
feature = "stm32l4x3",
212+
feature = "stm32l4x5",
213+
feature = "stm32l4x6",
214+
))]
195215
use crate::gpio::gpiod::*;
196216
#[cfg(any(feature = "stm32l4x5", feature = "stm32l4x6"))]
197217
use crate::gpio::gpiog::*;
@@ -256,15 +276,30 @@ pins!(SPI3, AF6,
256276
#[cfg(any(feature = "stm32l4x5", feature = "stm32l4x6",))]
257277
pins!(SPI3, AF6, SCK: [PG9], MISO: [PG10], MOSI: [PG11]);
258278

259-
#[cfg(any(feature = "stm32l4x3", feature = "stm32l4x5", feature = "stm32l4x6",))]
279+
#[cfg(any(
280+
feature = "stm32l4x1",
281+
feature = "stm32l4x3",
282+
feature = "stm32l4x5",
283+
feature = "stm32l4x6",
284+
))]
260285
use crate::stm32::SPI2;
261286

262-
#[cfg(any(feature = "stm32l4x3", feature = "stm32l4x5", feature = "stm32l4x6",))]
287+
#[cfg(any(
288+
feature = "stm32l4x1",
289+
feature = "stm32l4x3",
290+
feature = "stm32l4x5",
291+
feature = "stm32l4x6",
292+
))]
263293
hal! {
264294
SPI2: (spi2, APB1R1, spi2en, spi2rst, pclk1),
265295
}
266296

267-
#[cfg(any(feature = "stm32l4x3", feature = "stm32l4x5", feature = "stm32l4x6",))]
297+
#[cfg(any(
298+
feature = "stm32l4x1",
299+
feature = "stm32l4x3",
300+
feature = "stm32l4x5",
301+
feature = "stm32l4x6",
302+
))]
268303
pins!(SPI2, AF5,
269304
SCK: [PB13, PB10, PD1],
270305
MISO: [PB14, PC2, PD3],

0 commit comments

Comments
 (0)