Skip to content

Commit d1b24a4

Browse files
committed
split master/slave spi structures
1 parent 100d6c8 commit d1b24a4

File tree

8 files changed

+434
-212
lines changed

8 files changed

+434
-212
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10-
- Simplify `gpio::Outport`
11-
- Split USART and UART implementations
10+
- Simplify `gpio::Outport` [#611]
11+
- Split SPI master and slave implementations [#609]
12+
- Split USART and UART implementations [#608]
1213
- Add `lapce` editor settings [#601]
1314
- Use `enum`s for alternate peripheral pins [#594] [#610]
1415
- Added missing U(S)ART DMA traits for HAL serial types [#593]
@@ -27,6 +28,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2728
[#601]: https://github.com/stm32-rs/stm32f4xx-hal/pull/601
2829
[#603]: https://github.com/stm32-rs/stm32f4xx-hal/pull/603
2930
[#610]: https://github.com/stm32-rs/stm32f4xx-hal/pull/610
31+
[#608]: https://github.com/stm32-rs/stm32f4xx-hal/pull/608
32+
[#609]: https://github.com/stm32-rs/stm32f4xx-hal/pull/609
33+
[#611]: https://github.com/stm32-rs/stm32f4xx-hal/pull/611
3034

3135
## [v0.15.0] - 2023-03-13
3236

examples/rtic-spi-slave-dma.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod app {
1616
pac::{DMA1, SPI3},
1717
prelude::*,
1818
rcc::RccExt,
19-
spi::{Rx, Spi, Tx},
19+
spi::{Rx, SpiSlave, Tx},
2020
};
2121
use panic_semihosting as _;
2222
use systick_monotonic::*;
@@ -56,7 +56,7 @@ mod app {
5656

5757
let rcc = device_peripherals.RCC;
5858
let rcc = rcc.constrain();
59-
let clocks = rcc.cfgr.sysclk(100.MHz()).pclk1(36.MHz()).freeze();
59+
let _clocks = rcc.cfgr.sysclk(100.MHz()).pclk1(36.MHz()).freeze();
6060

6161
let mono = Systick::new(core.SYST, 100_000_000);
6262

@@ -77,7 +77,8 @@ mod app {
7777
phase: Phase::CaptureOnFirstTransition,
7878
};
7979

80-
let spi3 = Spi::new_slave(spi, (sck, miso, mosi), mode, 8_000_000.Hz(), &clocks);
80+
let mut spi3 = SpiSlave::new(spi, (sck, miso, mosi, None), mode);
81+
spi3.set_internal_nss(false);
8182

8283
let (tx, rx) = spi3.use_dma().txrx();
8384

examples/spi_slave.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
use panic_halt as _;
5+
6+
use crate::hal::spi::{Mode, Phase, Polarity};
7+
use crate::hal::{gpio::Pull, pac, prelude::*};
8+
use cortex_m::asm;
9+
use cortex_m_rt::entry;
10+
use stm32f4xx_hal as hal;
11+
12+
/// SPI mode
13+
pub const MODE: Mode = Mode {
14+
phase: Phase::CaptureOnFirstTransition,
15+
polarity: Polarity::IdleLow,
16+
};
17+
18+
#[entry]
19+
fn main() -> ! {
20+
let p = pac::Peripherals::take().unwrap();
21+
22+
let rcc = p.RCC.constrain();
23+
let _clocks = rcc.cfgr.freeze();
24+
25+
let gpioa = p.GPIOA.split();
26+
27+
let sck = gpioa.pa5.internal_resistor(Pull::Up);
28+
let miso = gpioa.pa6.internal_resistor(Pull::Down);
29+
let mosi = gpioa.pa7.internal_resistor(Pull::Down);
30+
31+
// clock speed is determined by the master
32+
let nss = gpioa.pa4.internal_resistor(Pull::Up).into();
33+
let mut spi = p.SPI1.spi_slave((sck, miso, mosi, Some(nss)), MODE);
34+
// alternativelly you could use software `chip select`
35+
// let mut spi = SpiSlave::new(p.SPI1, (sck, miso, mosi, None), MODE);
36+
// spi.set_internal_nss(false);
37+
38+
let mut data = [0x1];
39+
// this will block until the master starts the clock
40+
spi.transfer_in_place(&mut data).unwrap();
41+
42+
// when you reach this breakpoint you'll be able to inspect the variable `data` which contains the
43+
// data sent by the master
44+
asm::bkpt();
45+
46+
loop {
47+
continue;
48+
}
49+
}

src/gpio/alt.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -427,21 +427,7 @@ pub mod spi1 {
427427
],
428428

429429
<Nss> for [
430-
#[cfg(any(
431-
feature = "gpio-f410",
432-
feature = "gpio-f411",
433-
feature = "gpio-f412",
434-
feature = "gpio-f413",
435-
feature = "gpio-f446"
436-
))]
437430
PA4<5>,
438-
#[cfg(any(
439-
feature = "gpio-f410",
440-
feature = "gpio-f411",
441-
feature = "gpio-f412",
442-
feature = "gpio-f413",
443-
feature = "gpio-f446"
444-
))]
445431
PA15<5>,
446432
],
447433
}

src/prelude.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,8 @@ pub use embedded_hal::adc::OneShot as _embedded_hal_adc_OneShot;
4040
pub use embedded_hal::blocking::delay::DelayMs as _embedded_hal_blocking_delay_DelayMs;
4141
pub use embedded_hal::blocking::delay::DelayUs as _embedded_hal_blocking_delay_DelayUs;
4242
pub use embedded_hal::blocking::serial::Write as _embedded_hal_blocking_serial_Write;
43-
pub use embedded_hal::blocking::spi::{
44-
Transfer as _embedded_hal_blocking_spi_Transfer, Write as _embedded_hal_blocking_spi_Write,
45-
};
4643
pub use embedded_hal::serial::Read as _embedded_hal_serial_Read;
4744
pub use embedded_hal::serial::Write as _embedded_hal_serial_Write;
48-
pub use embedded_hal::spi::FullDuplex as _embedded_hal_spi_FullDuplex;
4945
pub use embedded_hal::Capture as _embedded_hal_Capture;
5046
pub use embedded_hal::Pwm as _embedded_hal_Pwm;
5147
pub use embedded_hal::Qei as _embedded_hal_Qei;

0 commit comments

Comments
 (0)