Skip to content

Commit 94a5b4b

Browse files
authored
Merge pull request stm32-rs#238 from Sh3Rm4n/uart-refactor
Allow Serial::new instead of Serial::usartX
2 parents 74fd875 + b7308b4 commit 94a5b4b

File tree

10 files changed

+563
-321
lines changed

10 files changed

+563
-321
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1919
- `ld` feature, which enables the memory.x generation ([#216])
2020
- Implement `DelayMs` for `Milliseconds` and `DelayUs` for `Microseconds` ([#234])
2121
- ADC can now be `free()`'d ([#212])
22+
- Serial does now implement `embedded_hal::serial::{Read, Write}`.
23+
No `split()` necessary. ([#232])
24+
- Serial can now listen for the "Transmission Complete" `Tc` interrupt event ([#232])
25+
- Serial can now listen for the `Idle` interrupt event ([#238])
2226

2327
### Changed
2428

@@ -70,6 +74,7 @@ let clocks = rcc
7074
- Remove `stm32` module. Use `use stm32f3xx_hal::pac` instead.
7175
This module was a deprecated in [v0.5.0][] and is now subject for
7276
removal. ([#220])
77+
- `Serial::uart1` ... functions are renamed to `Serial::new`. ([#212])
7378

7479
## [v0.6.1] - 2020-12-10
7580

@@ -325,6 +330,7 @@ let clocks = rcc
325330
[filter]: https://defmt.ferrous-systems.com/filtering.html
326331

327332
[#234]: https://github.com/stm32-rs/stm32f3xx-hal/pull/234
333+
[#232]: https://github.com/stm32-rs/stm32f3xx-hal/pull/232
328334
[#229]: https://github.com/stm32-rs/stm32f3xx-hal/pull/229
329335
[#227]: https://github.com/stm32-rs/stm32f3xx-hal/pull/227
330336
[#220]: https://github.com/stm32-rs/stm32f3xx-hal/pull/220

examples/serial_dma.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn main() -> ! {
2929
.pa10
3030
.into_af7_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
3131
);
32-
let serial = Serial::usart1(dp.USART1, pins, 9600.Bd(), clocks, &mut rcc.apb2);
32+
let serial = Serial::new(dp.USART1, pins, 9600.Bd(), clocks, &mut rcc.apb2);
3333
let (tx, rx) = serial.split();
3434

3535
let dma1 = dp.DMA1.split(&mut rcc.ahb);

examples/serial_echo_rtic.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,8 @@ mod app {
6464
.into_af7_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
6565
);
6666
pins.1.internal_pull_up(&mut gpioa.pupdr, true);
67-
let mut serial: SerialType = Serial::usart1(
68-
cx.device.USART1,
69-
pins,
70-
19200_u32.Bd(),
71-
clocks,
72-
&mut rcc.apb2,
73-
);
67+
let mut serial: SerialType =
68+
Serial::new(cx.device.USART1, pins, 19200.Bd(), clocks, &mut rcc.apb2);
7469
serial.listen(Event::Rxne);
7570

7671
rprintln!("post init");

src/dma.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,10 @@ pub trait Channel: private::Channel {
333333
1 => BITS8,
334334
2 => BITS16,
335335
4 => BITS32,
336-
s => crate::panic!("unsupported word size: {:?}", s),
336+
#[cfg(not(feature = "defmt"))]
337+
s => core::panic!("unsupported word size: {:?}", s),
338+
#[cfg(feature = "defmt")]
339+
_ => defmt::panic!("unsupported word size"),
337340
};
338341

339342
self.ch().cr.modify(|_, w| {

src/gpio.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ pub struct Pin<Gpio, Index, Mode> {
275275
_mode: PhantomData<Mode>,
276276
}
277277

278+
// Make all GPIO peripheral trait extensions sealable.
279+
impl<Gpio, Index, Mode> crate::private::Sealed for Pin<Gpio, Index, Mode> {}
280+
278281
/// Fully erased pin
279282
///
280283
/// This moves the pin type information to be known

src/i2c.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ impl<I2C, SCL, SDA> I2c<I2C, (SCL, SDA)> {
132132
// t_SCL ~= t_SYNC1 + t_SYNC2 + t_SCLL + t_SCLH
133133
let i2cclk = I2C::clock(&clocks).0;
134134
let ratio = i2cclk / freq.integer() - 4;
135-
let (presc, scll, sclh, sdadel, scldel) = if freq.integer() >= 100_000 {
135+
let (presc, scll, sclh, sdadel, scldel) = if freq >= 100.kHz() {
136136
// fast-mode or fast-mode plus
137137
// here we pick SCLL + 1 = 2 * (SCLH + 1)
138138
let presc = ratio / 387;
139139

140140
let sclh = ((ratio / (presc + 1)) - 3) / 3;
141141
let scll = 2 * (sclh + 1) - 1;
142142

143-
let (sdadel, scldel) = if freq.integer() > 400_000 {
143+
let (sdadel, scldel) = if freq > 400.kHz() {
144144
// fast-mode plus
145145
let sdadel = 0;
146146
let scldel = i2cclk / 4_000_000 / (presc + 1) - 1;
@@ -449,7 +449,7 @@ macro_rules! i2c {
449449
fn clock(clocks: &Clocks) -> Hertz {
450450
// NOTE(unsafe) atomic read with no side effects
451451
match unsafe { (*RCC::ptr()).cfgr3.read().$i2cXsw().variant() } {
452-
I2C1SW_A::HSI => Hertz(8_000_000),
452+
I2C1SW_A::HSI => crate::rcc::HSI,
453453
I2C1SW_A::SYSCLK => clocks.sysclk(),
454454
}
455455
}

src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ pub mod watchdog;
161161

162162
cfg_if! {
163163
if #[cfg(feature = "defmt")] {
164+
#[allow(unused_imports)]
164165
pub(crate) use defmt::{assert, panic, unreachable, unwrap};
166+
#[allow(unused_imports)]
165167
pub(crate) use macros::expect;
166168

167169
mod macros {
@@ -177,7 +179,9 @@ cfg_if! {
177179
pub(crate) use expect_wrapper as expect;
178180
}
179181
} else {
182+
#[allow(unused_imports)]
180183
pub(crate) use core::{assert, panic, unreachable};
184+
#[allow(unused_imports)]
181185
pub(crate) use macros::{unwrap, expect};
182186

183187
mod macros {
@@ -203,3 +207,9 @@ cfg_if! {
203207
}
204208
}
205209
}
210+
211+
mod private {
212+
/// Private sealed trait to seal all GPIO implementations
213+
/// which do implement peripheral functionalities.
214+
pub trait Sealed {}
215+
}

src/rcc.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,10 @@ impl APB2 {
182182
}
183183
}
184184

185-
const HSI: u32 = 8_000_000; // Hz
185+
/// Frequency of interal hardware RC oscillator (HSI OSC)
186+
pub const HSI: Hertz = Hertz(8_000_000);
187+
/// Frequency of external 32.768 kHz oscillator (LSE OSC)
188+
pub const LSE: Hertz = Hertz(32_768);
186189

187190
// some microcontrollers do not have USB
188191
#[cfg(any(feature = "stm32f301", feature = "stm32f318", feature = "stm32f334",))]
@@ -469,7 +472,7 @@ impl CFGR {
469472
feature = "stm32f398"
470473
)))]
471474
fn calc_pll(&self, sysclk: u32) -> (u32, PllConfig) {
472-
let pllsrcclk = self.hse.unwrap_or(HSI / 2);
475+
let pllsrcclk = self.hse.unwrap_or(HSI.integer() / 2);
473476
// Get the optimal value for the pll divisor (PLL_DIV) and multiplier (PLL_MUL)
474477
// Only for HSE PLL_DIV can be changed
475478
let (pll_mul, pll_div): (u32, Option<u32>) = if self.hse.is_some() {
@@ -545,7 +548,7 @@ impl CFGR {
545548
feature = "stm32f398",
546549
))]
547550
fn calc_pll(&self, sysclk: u32) -> (u32, PllConfig) {
548-
let pllsrcclk = self.hse.unwrap_or(HSI);
551+
let pllsrcclk = self.hse.unwrap_or(HSI.integer());
549552

550553
let (pll_mul, pll_div) = {
551554
// Get the optimal value for the pll divisor (PLL_DIV) and multiplcator (PLL_MUL)
@@ -613,15 +616,17 @@ impl CFGR {
613616
// Oscillator (max 32 Mhz), without using the PLL.
614617
(Some(sysclk), Some(hse)) if sysclk == hse => (hse, cfgr::SW_A::HSE, None),
615618
// No need to use the PLL
616-
(Some(sysclk), None) if sysclk == HSI => (HSI, cfgr::SW_A::HSI, None),
619+
(Some(sysclk), None) if sysclk == HSI.integer() => {
620+
(HSI.integer(), cfgr::SW_A::HSI, None)
621+
}
617622
(Some(sysclk), _) => {
618623
let (sysclk, pll_config) = self.calc_pll(sysclk);
619624
(sysclk, cfgr::SW_A::PLL, Some(pll_config))
620625
}
621626
// Use HSE as system clock
622627
(None, Some(hse)) => (hse, cfgr::SW_A::HSE, None),
623628
// Use HSI as system clock
624-
(None, None) => (HSI, cfgr::SW_A::HSI, None),
629+
(None, None) => (HSI.integer(), cfgr::SW_A::HSI, None),
625630
}
626631
}
627632

0 commit comments

Comments
 (0)