Skip to content

Commit 14aab2a

Browse files
Implement multiple spi instances (BROKEN)
1 parent ad830af commit 14aab2a

File tree

1 file changed

+69
-36
lines changed

1 file changed

+69
-36
lines changed

src/spi.rs

+69-36
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::ops::Deref;
12
use core::ptr;
23

34
use nb;
@@ -72,41 +73,71 @@ spi_pins! {
7273
}
7374
}
7475

76+
macro_rules! spi {
77+
($($SPI:ident: ($spi:ident, $spiXen:ident, $spiXrst:ident, $apbrstr:ident, $apbenr:ident),)+) => {
78+
$(
79+
use crate::stm32::$SPI;
80+
impl<SCKPIN, MISOPIN, MOSIPIN> Spi<SPI1, SCKPIN, MISOPIN, MOSIPIN> {
81+
pub fn $spi<F>(
82+
spi: $SPI,
83+
pins: (SCKPIN, MISOPIN, MOSIPIN),
84+
mode: Mode,
85+
speed: F,
86+
clocks: Clocks,
87+
) -> Self
88+
where
89+
SCKPIN: SckPin<$SPI>,
90+
MISOPIN: MisoPin<$SPI>,
91+
MOSIPIN: MosiPin<$SPI>,
92+
F: Into<Hertz>,
93+
{
94+
// NOTE(unsafe) This executes only during initialisation
95+
let rcc = unsafe { &(*RCC::ptr()) };
96+
97+
/* Enable clock for SPI1 */
98+
rcc.$apbenr.modify(|_, w| w.$spiXen().set_bit());
99+
100+
/* Reset SPI1 */
101+
rcc.$apbrstr.modify(|_, w| w.$spiXrst().set_bit());
102+
rcc.$apbrstr.modify(|_, w| w.$spiXrst().clear_bit());
103+
Spi { spi, pins }.spi_init(mode, speed, clocks)
104+
}
105+
}
106+
)+
107+
}
108+
}
75109
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
76-
impl<SCKPIN, MISOPIN, MOSIPIN> Spi<SPI1, SCKPIN, MISOPIN, MOSIPIN> {
77-
pub fn spi1<F>(
78-
spi: SPI1,
79-
pins: (SCKPIN, MISOPIN, MOSIPIN),
80-
mode: Mode,
81-
speed: F,
82-
clocks: Clocks,
83-
) -> Self
110+
spi! {
111+
SPI1: (spi1, spi1en, spi1rst, apb2enr, apb2rstr),
112+
}
113+
#[cfg(any(feature = "stm32f030x8", feature = "stm32f030xc"))]
114+
spi! {
115+
SPI2: (spi2, spi2en, spi2rst, apb1enr, apb1rstr),
116+
}
117+
118+
// It's s needed for the impls, but rustc doesn't recognize that
119+
#[allow(dead_code)]
120+
type SpiRegisterBlock = stm32::spi1::RegisterBlock;
121+
122+
impl<SPI, SCKPIN, MISOPIN, MOSIPIN> Spi<SPI, SCKPIN, MISOPIN, MOSIPIN>
123+
where
124+
SPI: Deref<Target = SpiRegisterBlock>,
125+
{
126+
fn spi_init<F>(self: Self, mode: Mode, speed: F, clocks: Clocks) -> Self
84127
where
85-
SCKPIN: SckPin<SPI1>,
86-
MISOPIN: MisoPin<SPI1>,
87-
MOSIPIN: MosiPin<SPI1>,
88128
F: Into<Hertz>,
89129
{
90-
// NOTE(unsafe) This executes only during initialisation
91-
let rcc = unsafe { &(*RCC::ptr()) };
92-
93-
/* Enable clock for SPI1 */
94-
rcc.apb2enr.modify(|_, w| w.spi1en().set_bit());
95-
96-
/* Reset SPI1 */
97-
rcc.apb2rstr.modify(|_, w| w.spi1rst().set_bit());
98-
rcc.apb2rstr.modify(|_, w| w.spi1rst().clear_bit());
99-
100130
/* Make sure the SPI unit is disabled so we can configure it */
101-
spi.cr1.modify(|_, w| w.spe().clear_bit());
131+
self.spi.cr1.modify(|_, w| w.spe().clear_bit());
102132

103133
// FRXTH: 8-bit threshold on RX FIFO
104134
// DS: 8-bit data size
105135
// SSOE: cleared to disable SS output
106136
//
107137
// NOTE(unsafe): DS reserved bit patterns are 0b0000, 0b0001, and 0b0010. 0b0111 is valid
108138
// (reference manual, pp 804)
109-
spi.cr2
139+
self.spi
140+
.cr2
110141
.write(|w| unsafe { w.frxth().set_bit().ds().bits(0b0111).ssoe().clear_bit() });
111142

112143
let br = match clocks.pclk().0 / speed.into().0 {
@@ -128,7 +159,7 @@ impl<SCKPIN, MISOPIN, MOSIPIN> Spi<SPI1, SCKPIN, MISOPIN, MOSIPIN> {
128159
// dff: 8 bit frames
129160
// bidimode: 2-line unidirectional
130161
// spe: enable the SPI bus
131-
spi.cr1.write(|w| unsafe {
162+
self.spi.cr1.write(|w| unsafe {
132163
w.cpha()
133164
.bit(mode.phase == Phase::CaptureOnSecondTransition)
134165
.cpol()
@@ -151,17 +182,17 @@ impl<SCKPIN, MISOPIN, MOSIPIN> Spi<SPI1, SCKPIN, MISOPIN, MOSIPIN> {
151182
.set_bit()
152183
});
153184

154-
Spi { spi, pins }
185+
self
155186
}
156-
157-
pub fn release(self) -> (SPI1, (SCKPIN, MISOPIN, MOSIPIN)) {
187+
pub fn release(self) -> (SPI, (SCKPIN, MISOPIN, MOSIPIN)) {
158188
(self.spi, self.pins)
159189
}
160190
}
161191

162-
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
163-
impl<SCKPIN, MISOPIN, MOSIPIN> ::embedded_hal::spi::FullDuplex<u8>
164-
for Spi<SPI1, SCKPIN, MISOPIN, MOSIPIN>
192+
impl<SPI, SCKPIN, MISOPIN, MOSIPIN> ::embedded_hal::spi::FullDuplex<u8>
193+
for Spi<SPI, SCKPIN, MISOPIN, MOSIPIN>
194+
where
195+
SPI: Deref<Target = SpiRegisterBlock>,
165196
{
166197
type Error = Error;
167198

@@ -202,13 +233,15 @@ impl<SCKPIN, MISOPIN, MOSIPIN> ::embedded_hal::spi::FullDuplex<u8>
202233
}
203234
}
204235

205-
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
206-
impl<SCKPIN, MISOPIN, MOSIPIN> ::embedded_hal::blocking::spi::transfer::Default<u8>
207-
for Spi<SPI1, SCKPIN, MISOPIN, MOSIPIN>
236+
impl<SPI, SCKPIN, MISOPIN, MOSIPIN> ::embedded_hal::blocking::spi::transfer::Default<u8>
237+
for Spi<SPI, SCKPIN, MISOPIN, MOSIPIN>
238+
where
239+
SPI: Deref<Target = SpiRegisterBlock>,
208240
{
209241
}
210-
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
211-
impl<SCKPIN, MISOPIN, MOSIPIN> ::embedded_hal::blocking::spi::write::Default<u8>
212-
for Spi<SPI1, SCKPIN, MISOPIN, MOSIPIN>
242+
impl<SPI, SCKPIN, MISOPIN, MOSIPIN> ::embedded_hal::blocking::spi::write::Default<u8>
243+
for Spi<SPI, SCKPIN, MISOPIN, MOSIPIN>
244+
where
245+
SPI: Deref<Target = SpiRegisterBlock>,
213246
{
214247
}

0 commit comments

Comments
 (0)