Skip to content

Commit 44032b1

Browse files
authored
Merge pull request #273 from Sh3Rm4n/spi
Rework Spi
2 parents 5f4a53b + 6ebcfe8 commit 44032b1

File tree

15 files changed

+859
-512
lines changed

15 files changed

+859
-512
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
139139
- `Timer::new` now does not take a timeout value. This means, that the
140140
timer will now not be started automatically and one has to explicitly call
141141
`start()`.
142+
- Rework SPI implementation: ([#273])
143+
- A generic `Instance` trait now represents all Spi peripherals.
144+
- `Spi::spi1` and so on are renamed to `Spi::new`.
145+
- Add SPI configuration type to be passed into `Spi::new`
142146

143147
## [v0.7.0] - 2021-06-18
144148

@@ -464,6 +468,7 @@ let clocks = rcc
464468
[defmt]: https://github.com/knurling-rs/defmt
465469
[filter]: https://defmt.ferrous-systems.com/filtering.html
466470

471+
[#273]: https://github.com/stm32-rs/stm32f3xx-hal/pull/273
467472
[#271]: https://github.com/stm32-rs/stm32f3xx-hal/pull/271
468473
[#270]: https://github.com/stm32-rs/stm32f3xx-hal/pull/270
469474
[#267]: https://github.com/stm32-rs/stm32f3xx-hal/pull/267

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,15 @@ required-features = ["ld", "stm32f303xc", "usb"]
175175

176176
[[example]]
177177
name = "spi"
178-
required-features = ["ld", "stm32f303"]
178+
required-features = ["ld", "stm32f303xc"]
179179

180180
[[example]]
181181
name = "can"
182182
required-features = ["ld", "rt", "can", "stm32f302"]
183183

184184
[[example]]
185185
name = "serial_dma"
186-
required-features = ["ld", "rt", "stm32f303"]
186+
required-features = ["ld", "rt", "stm32f303xc"]
187187

188188
[[example]]
189189
name = "serial_echo_rtic"

examples/spi.rs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#![no_std]
44
#![no_main]
55

6-
use core::convert::TryInto;
7-
86
use panic_semihosting as _;
97

108
use stm32f3xx_hal as hal;
@@ -14,15 +12,15 @@ use cortex_m_rt::entry;
1412

1513
use hal::pac;
1614
use hal::prelude::*;
17-
use hal::spi::{Mode, Phase, Polarity, Spi};
15+
use hal::spi::Spi;
1816

1917
#[entry]
2018
fn main() -> ! {
2119
let dp = pac::Peripherals::take().unwrap();
2220

2321
let mut flash = dp.FLASH.constrain();
2422
let mut rcc = dp.RCC.constrain();
25-
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
23+
let mut gpioc = dp.GPIOC.split(&mut rcc.ahb);
2624

2725
let clocks = rcc
2826
.cfgr
@@ -32,29 +30,17 @@ fn main() -> ! {
3230
.freeze(&mut flash.acr);
3331

3432
// Configure pins for SPI
35-
let sck = gpioa
36-
.pa5
37-
.into_af5_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
38-
let miso = gpioa
39-
.pa6
40-
.into_af5_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
41-
let mosi = gpioa
42-
.pa7
43-
.into_af5_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
44-
45-
let spi_mode = Mode {
46-
polarity: Polarity::IdleLow,
47-
phase: Phase::CaptureOnFirstTransition,
48-
};
49-
50-
let mut spi = Spi::spi1(
51-
dp.SPI1,
52-
(sck, miso, mosi),
53-
spi_mode,
54-
3u32.MHz().try_into().unwrap(),
55-
clocks,
56-
&mut rcc.apb2,
57-
);
33+
let sck = gpioc
34+
.pc10
35+
.into_af6_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
36+
let miso = gpioc
37+
.pc11
38+
.into_af6_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
39+
let mosi = gpioc
40+
.pc12
41+
.into_af6_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
42+
43+
let mut spi = Spi::new(dp.SPI3, (sck, miso, mosi), 3.MHz(), clocks, &mut rcc.apb1);
5844

5945
// Create an `u8` array, which can be transfered via SPI.
6046
let msg_send: [u8; 8] = [0xD, 0xE, 0xA, 0xD, 0xB, 0xE, 0xE, 0xF];

release.toml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,48 @@ file = "src/i2c.rs"
5151
search = "https://github.com/stm32-rs/stm32f3xx-hal/blob/[a-z0-9\\.-]+/"
5252
replace = "https://github.com/stm32-rs/stm32f3xx-hal/blob/v{{version}}/"
5353

54+
[[pre-release-replacements]]
55+
min = 0
56+
file = "src/timer.rs"
57+
search = "https://github.com/stm32-rs/stm32f3xx-hal/blob/[a-z0-9\\.-]+/"
58+
replace = "https://github.com/stm32-rs/stm32f3xx-hal/blob/v{{version}}/"
59+
60+
[[pre-release-replacements]]
61+
min = 0
62+
file = "src/timer/interrupts.rs"
63+
search = "https://github.com/stm32-rs/stm32f3xx-hal/blob/[a-z0-9\\.-]+/"
64+
replace = "https://github.com/stm32-rs/stm32f3xx-hal/blob/v{{version}}/"
65+
66+
[[pre-release-replacements]]
67+
min = 0
68+
file = "src/spi.rs"
69+
search = "https://github.com/stm32-rs/stm32f3xx-hal/blob/[a-z0-9\\.-]+/"
70+
replace = "https://github.com/stm32-rs/stm32f3xx-hal/blob/v{{version}}/"
71+
72+
[[pre-release-replacements]]
73+
min = 0
74+
file = "src/spi/config.rs"
75+
search = "https://github.com/stm32-rs/stm32f3xx-hal/blob/[a-z0-9\\.-]+/"
76+
replace = "https://github.com/stm32-rs/stm32f3xx-hal/blob/v{{version}}/"
77+
5478
[[pre-release-replacements]]
5579
min = 0
5680
file = "src/serial.rs"
5781
search = "https://github.com/stm32-rs/stm32f3xx-hal/blob/[a-z0-9\\.-]+/"
5882
replace = "https://github.com/stm32-rs/stm32f3xx-hal/blob/v{{version}}/"
5983

84+
[[pre-release-replacements]]
85+
min = 0
86+
file = "src/serial/config.rs"
87+
search = "https://github.com/stm32-rs/stm32f3xx-hal/blob/[a-z0-9\\.-]+/"
88+
replace = "https://github.com/stm32-rs/stm32f3xx-hal/blob/v{{version}}/"
89+
90+
[[pre-release-replacements]]
91+
min = 0
92+
file = "src/interrupts.rs"
93+
search = "https://github.com/stm32-rs/stm32f3xx-hal/blob/[a-z0-9\\.-]+/"
94+
replace = "https://github.com/stm32-rs/stm32f3xx-hal/blob/v{{version}}/"
95+
6096
[[pre-release-replacements]]
6197
min = 0
6298
file = "src/watchdog.rs"

src/adc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub enum SampleTime {
7272
}
7373

7474
impl Default for SampleTime {
75-
/// [`SampelTime::Cycles1C5`] is also the reset value.
75+
/// [`SampleTime::Cycles1C5`] is also the reset value.
7676
fn default() -> Self {
7777
SampleTime::Cycles1C5
7878
}

src/serial.rs

Lines changed: 2 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -319,152 +319,7 @@ cfg_if! {
319319
}
320320
}
321321

322-
/// Types for configuring a serial interface.
323-
pub mod config {
324-
use crate::pac::usart1::cr2::STOP_A;
325-
use crate::time::rate::{Baud, Extensions};
326-
327-
/// Stop Bit configuration parameter for serial.
328-
///
329-
/// Wrapper around [`STOP_A`]
330-
#[derive(Clone, Copy, Debug, PartialEq)]
331-
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
332-
pub enum StopBits {
333-
/// 0.5 stop bit
334-
Stop0P5,
335-
/// 1 stop bit
336-
Stop1,
337-
/// 1.5 stop bit
338-
Stop1P5,
339-
/// 2 stop bit
340-
Stop2,
341-
}
342-
343-
impl From<StopBits> for STOP_A {
344-
fn from(stopbit: StopBits) -> Self {
345-
match stopbit {
346-
StopBits::Stop0P5 => STOP_A::STOP0P5,
347-
StopBits::Stop1 => STOP_A::STOP1,
348-
StopBits::Stop1P5 => STOP_A::STOP1P5,
349-
StopBits::Stop2 => STOP_A::STOP2,
350-
}
351-
}
352-
}
353-
354-
impl From<STOP_A> for StopBits {
355-
fn from(stopbit: STOP_A) -> Self {
356-
match stopbit {
357-
STOP_A::STOP0P5 => StopBits::Stop0P5,
358-
STOP_A::STOP1 => StopBits::Stop1,
359-
STOP_A::STOP1P5 => StopBits::Stop1P5,
360-
STOP_A::STOP2 => StopBits::Stop2,
361-
}
362-
}
363-
}
364-
365-
/// Parity generation and checking. If odd or even parity is selected, the
366-
/// underlying USART will be configured to send/receive the parity bit in
367-
/// addtion to the data bits.
368-
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
369-
#[derive(Debug, Clone, Copy, PartialEq)]
370-
pub enum Parity {
371-
/// No parity bit will be added/checked.
372-
None,
373-
/// The MSB transmitted/received will be generated/checked to have a
374-
/// even number of bits set.
375-
Even,
376-
/// The MSB transmitted/received will be generated/checked to have a
377-
/// odd number of bits set.
378-
Odd,
379-
}
380-
381-
/// Configuration struct for [`Serial`](super::Serial) providing all
382-
/// communication-related / parameters. `Serial` always uses eight data
383-
/// bits plus the parity bit - if selected.
384-
///
385-
/// Create a configuration by using `default` in combination with the
386-
/// builder methods. The following snippet shows creating a configuration
387-
/// for 19,200 Baud, 8N1 by deriving it from the default value:
388-
/// ```
389-
/// # use stm32f3xx_hal::serial::config::*;
390-
/// # use stm32f3xx_hal::time::rate::{Baud, Extensions};
391-
/// let config = Config::default().baudrate(19_200.Bd());
392-
///
393-
/// assert!(config.baudrate == 19_200.Bd());
394-
/// assert!(config.parity == Parity::None);
395-
/// assert!(config.stopbits == StopBits::STOP1);
396-
/// ```
397-
#[derive(Debug, Clone, Copy, PartialEq)]
398-
#[non_exhaustive]
399-
pub struct Config {
400-
/// Serial interface baud rate
401-
pub baudrate: Baud,
402-
/// Whether and how to generate/check a parity bit
403-
pub parity: Parity,
404-
/// The number of stop bits to follow the last data bit or the parity
405-
/// bit
406-
pub stopbits: StopBits,
407-
}
408-
409-
impl Config {
410-
/// Sets the given baudrate.
411-
pub fn baudrate(mut self, baudrate: impl Into<Baud>) -> Self {
412-
self.baudrate = baudrate.into();
413-
self
414-
}
415-
416-
/// Sets the given parity.
417-
pub fn parity(mut self, parity: Parity) -> Self {
418-
self.parity = parity;
419-
self
420-
}
421-
422-
/// Sets the stop bits to `stopbits`.
423-
pub fn stopbits(mut self, stopbits: StopBits) -> Self {
424-
self.stopbits = stopbits;
425-
self
426-
}
427-
}
428-
429-
impl Default for Config {
430-
/// Creates a new configuration with typically used parameters: 115,200
431-
/// Baud 8N1.
432-
fn default() -> Config {
433-
Config {
434-
baudrate: 115_200.Bd(),
435-
parity: Parity::None,
436-
stopbits: StopBits::Stop1,
437-
}
438-
}
439-
}
440-
441-
impl<T: Into<Baud>> From<T> for Config {
442-
fn from(b: T) -> Config {
443-
Config {
444-
baudrate: b.into(),
445-
..Default::default()
446-
}
447-
}
448-
}
449-
450-
#[cfg(feature = "defmt")]
451-
impl defmt::Format for Config {
452-
fn format(&self, f: defmt::Formatter) {
453-
// Omitting pins makes it:
454-
// 1. Easier.
455-
// 2. Not to specialized to use it ergonimically for users
456-
// even in a generic context.
457-
// 3. Not require specialization.
458-
defmt::write!(
459-
f,
460-
"Serial {{ baudrate: {} Bd , parity: {} , stopbits: {} }}",
461-
self.baudrate.0,
462-
self.parity,
463-
self.stopbits,
464-
);
465-
}
466-
}
467-
}
322+
pub mod config;
468323

469324
/// Serial abstraction
470325
///
@@ -615,7 +470,7 @@ where
615470
Rx: RxPin<Usart>,
616471
Config: Into<config::Config>,
617472
{
618-
use self::config::*;
473+
use config::*;
619474

620475
let config = config.into();
621476

@@ -724,7 +579,6 @@ where
724579

725580
/// Obtain the associated interrupt number for the serial peripheral.
726581
///
727-
///
728582
/// Used to unmask / enable the interrupt with [`cortex_m::peripheral::NVIC::unmask()`].
729583
/// This is useful for all `cortex_m::peripheral::INTERRUPT` functions.
730584
///

0 commit comments

Comments
 (0)