Skip to content

Commit 547f975

Browse files
committed
Return Result where Rate conversion may fail
1 parent 71a5cc0 commit 547f975

File tree

9 files changed

+110
-88
lines changed

9 files changed

+110
-88
lines changed

examples/can.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use panic_semihosting as _;
66

77
use stm32f3xx_hal as hal;
88

9-
use core::convert::TryFrom;
10-
119
use cortex_m::asm;
1210
use cortex_m_rt::entry;
1311

@@ -34,10 +32,14 @@ fn main() -> ! {
3432

3533
let _clocks = rcc
3634
.cfgr
37-
.use_hse(Hertz::try_from(32u32.MHz()).unwrap())
38-
.sysclk(Hertz::try_from(32u32.MHz()).unwrap())
39-
.pclk1(Hertz::try_from(16u32.MHz()).unwrap())
40-
.pclk2(Hertz::try_from(16u32.MHz()).unwrap())
35+
.use_hse(32u32.MHz())
36+
.unwrap()
37+
.sysclk(32u32.MHz())
38+
.unwrap()
39+
.pclk1(16u32.MHz())
40+
.unwrap()
41+
.pclk2(16u32.MHz())
42+
.unwrap()
4143
.freeze(&mut flash.acr);
4244

4345
// Configure CAN RX and TX pins (AF9)

examples/i2c_scanner.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ use core::ops::Range;
1010

1111
use panic_semihosting as _;
1212

13-
use core::convert::TryFrom;
14-
1513
use cortex_m::asm;
1614
use cortex_m_rt::entry;
1715
use cortex_m_semihosting::{hprint, hprintln};
@@ -37,13 +35,7 @@ fn main() -> ! {
3735
gpiob.pb7.into_af4(&mut gpiob.moder, &mut gpiob.afrl), // SDA
3836
);
3937

40-
let mut i2c = hal::i2c::I2c::new(
41-
dp.I2C1,
42-
pins,
43-
Hertz::try_from(100u32.kHz()).unwrap(),
44-
clocks,
45-
&mut rcc.apb1,
46-
);
38+
let mut i2c = hal::i2c::I2c::new(dp.I2C1, pins, 100u32.kHz(), clocks, &mut rcc.apb1).unwrap();
4739

4840
hprintln!("Start i2c scanning...").expect("Error using hprintln.");
4941
hprintln!().unwrap();

examples/pwm.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use panic_semihosting as _;
77

88
use stm32f3xx_hal as hal;
99

10-
use core::convert::TryFrom;
11-
1210
use cortex_m::asm;
1311
use cortex_m_rt::entry;
1412

@@ -30,10 +28,7 @@ fn main() -> ! {
3028
// Configure our clocks
3129
let mut flash = dp.FLASH.constrain();
3230
let mut rcc = dp.RCC.constrain();
33-
let clocks = rcc
34-
.cfgr
35-
.sysclk(Hertz::try_from(16u32.MHz()).unwrap())
36-
.freeze(&mut flash.acr);
31+
let clocks = rcc.cfgr.sysclk(16u32.MHz()).unwrap().freeze(&mut flash.acr);
3732

3833
// Prep the pins we need in their correct alternate function
3934
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);

examples/spi.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use panic_semihosting as _;
77

88
use stm32f3xx_hal as hal;
99

10-
use core::convert::TryFrom;
11-
1210
use cortex_m::asm;
1311
use cortex_m_rt::entry;
1412

@@ -27,9 +25,12 @@ fn main() -> ! {
2725

2826
let clocks = rcc
2927
.cfgr
30-
.use_hse(Hertz::try_from(8u32.MHz()).unwrap())
31-
.sysclk(Hertz::try_from(48u32.MHz()).unwrap())
32-
.pclk1(Hertz::try_from(24u32.MHz()).unwrap())
28+
.use_hse(8u32.MHz())
29+
.unwrap()
30+
.sysclk(48u32.MHz())
31+
.unwrap()
32+
.pclk1(24u32.MHz())
33+
.unwrap()
3334
.freeze(&mut flash.acr);
3435

3536
// Configure pins for SPI
@@ -46,10 +47,11 @@ fn main() -> ! {
4647
dp.SPI1,
4748
(sck, miso, mosi),
4849
spi_mode,
49-
Hertz::try_from(3u32.MHz()).unwrap(),
50+
3u32.MHz(),
5051
clocks,
5152
&mut rcc.apb2,
52-
);
53+
)
54+
.unwrap();
5355

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

examples/usb_serial.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ use stm32f3xx_hal as hal;
1010
use cortex_m::asm::delay;
1111
use cortex_m_rt::entry;
1212

13-
use core::convert::TryFrom;
14-
1513
use hal::pac;
1614
use hal::prelude::*;
1715
use hal::time::rate::*;
@@ -29,10 +27,14 @@ fn main() -> ! {
2927

3028
let clocks = rcc
3129
.cfgr
32-
.use_hse(Hertz::try_from(8u32.MHz()).unwrap())
33-
.sysclk(Hertz::try_from(48u32.MHz()).unwrap())
34-
.pclk1(Hertz::try_from(24u32.MHz()).unwrap())
35-
.pclk2(Hertz::try_from(24u32.MHz()).unwrap())
30+
.use_hse(8u32.MHz())
31+
.unwrap()
32+
.sysclk(48u32.MHz())
33+
.unwrap()
34+
.pclk1(24u32.MHz())
35+
.unwrap()
36+
.pclk2(24u32.MHz())
37+
.unwrap()
3638
.freeze(&mut flash.acr);
3739

3840
assert!(clocks.usbclk_valid());

src/i2c.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
//!
55
//! [examples/i2c_scanner.rs]: https://github.com/stm32-rs/stm32f3xx-hal/blob/v0.6.0/examples/i2c_scanner.rs
66
7-
use core::convert::TryFrom;
7+
use core::convert::{TryFrom, TryFrom};
88
use core::ops::Deref;
99

1010
use crate::{
1111
gpio::{gpioa, gpiob, AF4},
1212
hal::blocking::i2c::{Read, Write, WriteRead},
1313
pac::{i2c1::RegisterBlock, rcc::cfgr3::I2C1SW_A, I2C1, RCC},
1414
rcc::{Clocks, APB1},
15-
time::rate::Hertz,
15+
time::rate::{Hertz, Rate},
1616
};
1717

1818
#[cfg(not(feature = "gpio-f333"))]
@@ -111,14 +111,20 @@ macro_rules! busy_wait {
111111

112112
impl<I2C, SCL, SDA> I2c<I2C, (SCL, SDA)> {
113113
/// Configures the I2C peripheral to work in master mode
114-
pub fn new<F>(i2c: I2C, pins: (SCL, SDA), freq: F, clocks: Clocks, apb1: &mut APB1) -> Self
114+
pub fn new<F>(
115+
i2c: I2C,
116+
pins: (SCL, SDA),
117+
freq: F,
118+
clocks: Clocks,
119+
apb1: &mut APB1,
120+
) -> Result<Self, <F as TryInto<Hertz<u32>>>::Error>
115121
where
116122
I2C: Instance,
117123
SCL: SclPin<I2C>,
118124
SDA: SdaPin<I2C>,
119-
F: Into<Hertz>,
125+
F: Rate + TryInto<Hertz<u32>>,
120126
{
121-
let freq = freq.into().0;
127+
let freq = (freq.try_into()? as Hertz).0;
122128

123129
crate::assert!(freq <= 1_000_000);
124130

@@ -195,7 +201,7 @@ impl<I2C, SCL, SDA> I2c<I2C, (SCL, SDA)> {
195201
// Enable the peripheral
196202
i2c.cr1.modify(|_, w| w.pe().set_bit());
197203

198-
Self { i2c, pins }
204+
Ok(Self { i2c, pins })
199205
}
200206

201207
/// Releases the I2C peripheral and associated pins

src/rcc.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@
5454
//! For more details read the documentation of the [`CFGR`] methods to
5555
//! find out how to setup the clock.
5656
57+
use core::convert::TryInto;
58+
5759
use crate::pac::{
5860
rcc::{self, cfgr, cfgr2},
5961
RCC,
6062
};
6163

6264
use crate::flash::ACR;
63-
use crate::time::rate::Hertz;
65+
use crate::time::rate::{Hertz, Rate};
6466

6567
/// Extension trait that constrains the `RCC` peripheral
6668
pub trait RccExt {
@@ -333,12 +335,12 @@ impl CFGR {
333335
///
334336
/// Will result in a hang if an external oscillator is not connected or it fails to start,
335337
/// unless [css](CFGR::enable_css) is enabled.
336-
pub fn use_hse<F>(mut self, freq: F) -> Self
338+
pub fn use_hse<F>(mut self, freq: F) -> Result<Self, <F as TryInto<Hertz<u32>>>::Error>
337339
where
338-
F: Into<Hertz>,
340+
F: Rate + TryInto<Hertz<u32>>,
339341
{
340-
self.hse = Some(freq.into().0);
341-
self
342+
self.hse = Some((freq.try_into()? as Hertz).0);
343+
Ok(self)
342344
}
343345

344346
/// Enable `HSE` bypass.
@@ -364,12 +366,12 @@ impl CFGR {
364366
}
365367

366368
/// Sets a frequency for the AHB bus
367-
pub fn hclk<F>(mut self, freq: F) -> Self
369+
pub fn hclk<F>(mut self, freq: F) -> Result<Self, <F as TryInto<Hertz<u32>>>::Error>
368370
where
369-
F: Into<Hertz>,
371+
F: Rate + TryInto<Hertz<u32>>,
370372
{
371-
self.hclk = Some(freq.into().0);
372-
self
373+
self.hclk = Some((freq.try_into()? as Hertz).0);
374+
Ok(self)
373375
}
374376

375377
/// Sets a frequency for the `APB1` bus
@@ -378,12 +380,12 @@ impl CFGR {
378380
///
379381
/// If not manually set, it will be set to [`CFGR::sysclk`] frequency
380382
/// or [`CFGR::sysclk`] frequency / 2, if [`CFGR::sysclk`] > 36 Mhz
381-
pub fn pclk1<F>(mut self, freq: F) -> Self
383+
pub fn pclk1<F>(mut self, freq: F) -> Result<Self, <F as TryInto<Hertz<u32>>>::Error>
382384
where
383-
F: Into<Hertz>,
385+
F: Rate + TryInto<Hertz<u32>>,
384386
{
385-
self.pclk1 = Some(freq.into().0);
386-
self
387+
self.pclk1 = Some((freq.try_into()? as Hertz).0);
388+
Ok(self)
387389
}
388390

389391
/// Sets a frequency for the `APB2` bus
@@ -399,12 +401,12 @@ impl CFGR {
399401
///
400402
/// [stm32f302xd,stm32f302xe,stm32f303xd,stm32f303xe,stm32f398]
401403
///
402-
pub fn pclk2<F>(mut self, freq: F) -> Self
404+
pub fn pclk2<F>(mut self, freq: F) -> Result<Self, <F as TryInto<Hertz<u32>>>::Error>
403405
where
404-
F: Into<Hertz>,
406+
F: Rate + TryInto<Hertz<u32>>,
405407
{
406-
self.pclk2 = Some(freq.into().0);
407-
self
408+
self.pclk2 = Some((freq.try_into()? as Hertz).0);
409+
Ok(self)
408410
}
409411

410412
/// Sets the system (core) frequency
@@ -422,12 +424,12 @@ impl CFGR {
422424
/// even when using the internal oscillator:
423425
///
424426
/// [stm32f302xd,stm32f302xe,stm32f303xd,stm32f303xe,stm32f398]
425-
pub fn sysclk<F>(mut self, freq: F) -> Self
427+
pub fn sysclk<F>(mut self, freq: F) -> Result<Self, <F as TryInto<Hertz<u32>>>::Error>
426428
where
427-
F: Into<Hertz>,
429+
F: Rate + TryInto<Hertz<u32>>,
428430
{
429-
self.sysclk = Some(freq.into().0);
430-
self
431+
self.sysclk = Some((freq.try_into()? as Hertz).0);
432+
Ok(self)
431433
}
432434

433435
/// Calculate the values for the pll multiplier (`PLLMUL`) and the pll divisior (`PLLDIV`).

src/spi.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//!
55
//! [examples/spi.rs]: https://github.com/stm32-rs/stm32f3xx-hal/blob/v0.6.0/examples/spi.rs
66
7-
use core::ptr;
7+
use core::{convert::TryInto, ptr};
88

99
use crate::hal::spi::FullDuplex;
1010
pub use crate::hal::spi::{Mode, Phase, Polarity};
@@ -131,7 +131,7 @@ use crate::rcc::APB1;
131131
feature = "stm32f398"
132132
))]
133133
use crate::rcc::APB2;
134-
use crate::time::rate::Hertz;
134+
use crate::time::rate::{Hertz, Rate};
135135
use core::marker::PhantomData;
136136

137137
/// SPI error
@@ -424,9 +424,9 @@ macro_rules! hal {
424424
freq: F,
425425
clocks: Clocks,
426426
apb2: &mut $APBX,
427-
) -> Self
427+
) -> Result<Self, <F as TryInto<Hertz<u32>>>::Error>
428428
where
429-
F: Into<Hertz>,
429+
F: Rate + TryInto<Hertz<u32>>,
430430
SCK: SckPin<$SPIX>,
431431
MISO: MisoPin<$SPIX>,
432432
MOSI: MosiPin<$SPIX>,
@@ -445,6 +445,7 @@ macro_rules! hal {
445445
w.ssoe().disabled()
446446
});
447447

448+
let freq: Hertz = freq.try_into()?;
448449
// CPHA: phase
449450
// CPOL: polarity
450451
// MSTR: master mode
@@ -468,7 +469,7 @@ macro_rules! hal {
468469
Polarity::IdleHigh => w.cpol().idle_high(),
469470
};
470471

471-
w.br().variant(Self::compute_baud_rate(clocks.$pclkX(), freq.into()));
472+
w.br().variant(Self::compute_baud_rate(clocks.$pclkX(), freq));
472473

473474
w.spe()
474475
.enabled()
@@ -484,7 +485,7 @@ macro_rules! hal {
484485
.unidirectional()
485486
});
486487

487-
Spi { spi, pins, _word: PhantomData }
488+
Ok(Spi { spi, pins, _word: PhantomData })
488489
}
489490

490491
/// Releases the SPI peripheral and associated pins
@@ -494,13 +495,18 @@ macro_rules! hal {
494495

495496
/// Change the baud rate of the SPI
496497
pub fn reclock<F>(&mut self, freq: F, clocks: Clocks)
497-
where F: Into<Hertz>
498+
-> Result<(), <F as TryInto<Hertz<u32>>>::Error>
499+
where
500+
F: Rate + TryInto<Hertz<u32>>,
498501
{
499502
self.spi.cr1.modify(|_, w| w.spe().disabled());
503+
504+
let freq: Hertz = freq.try_into()?;
500505
self.spi.cr1.modify(|_, w| {
501-
w.br().variant(Self::compute_baud_rate(clocks.$pclkX(), freq.into()));
506+
w.br().variant(Self::compute_baud_rate(clocks.$pclkX(), freq));
502507
w.spe().enabled()
503508
});
509+
Ok(())
504510
}
505511

506512
fn compute_baud_rate(clocks: Hertz, freq: Hertz) -> spi1::cr1::BR_A {

0 commit comments

Comments
 (0)