Skip to content

Commit 3c4dcd4

Browse files
authored
Merge pull request #192 from wbuck/feature/embedded-time
Feature/embedded time
2 parents dbadc66 + 5be98c1 commit 3c4dcd4

File tree

19 files changed

+217
-298
lines changed

19 files changed

+217
-298
lines changed

CHANGELOG.md

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

88
## [Unreleased]
99

10+
## [v0.7.0] - 2021-03-10
11+
12+
### Added
13+
14+
- Replace custom time based units with types defined in the [embedded-time][] crate ([#192])
15+
16+
### Breaking changes
17+
18+
- The `rcc` public API now expects time based units in `Megahertz`.
19+
If the supplied frequency cannot be converted to `Hertz` the code
20+
will `panic`. This will occur if the supplied `Megahertz` frequency
21+
cannot fit into `u32::MAX` when converting to `Hertz`
22+
23+
```rust
24+
// The supplied frequencies must be in `MHz`.
25+
let clocks = rcc
26+
.cfgr
27+
.use_hse(8u32.MHz())
28+
.hclk(48u32.MHz())
29+
.sysclk(48u32.MHz())
30+
.pclk1(12u32.MHz())
31+
.pclk2(12u32.MHz())
32+
```
33+
34+
[embedded-time]: https://github.com/FluenTech/embedded-time/
1035
### Changed
1136

1237
- Added support for more CAN bit rates and modes. ([#186])
@@ -273,6 +298,7 @@ let clocks = rcc
273298

274299
[#208]: https://github.com/stm32-rs/stm32f3xx-hal/pull/208
275300
[#203]: https://github.com/stm32-rs/stm32f3xx-hal/issues/203
301+
[#192]: https://github.com/stm32-rs/stm32f3xx-hal/pull/192
276302
[#186]: https://github.com/stm32-rs/stm32f3xx-hal/pull/186
277303
[#184]: https://github.com/stm32-rs/stm32f3xx-hal/pull/184
278304
[#172]: https://github.com/stm32-rs/stm32f3xx-hal/pull/172

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ nb = "0.1"
3333
paste = "1"
3434
rtcc = "0.2"
3535
stm32f3 = "0.12"
36+
embedded-time = "0.10"
3637

3738
[dependencies.embedded-hal-can]
3839
version = "0.1.0"

examples/can.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use cortex_m_rt::entry;
1111

1212
use hal::prelude::*;
1313
use hal::stm32;
14+
use hal::time::{duration::*, rate::*};
1415
use hal::watchdog::IndependentWatchDog;
1516

1617
use hal::can::{Can, CanFilter, CanFrame, CanId, Filter, Frame, Receiver, Transmitter};
@@ -31,10 +32,10 @@ fn main() -> ! {
3132

3233
let _clocks = rcc
3334
.cfgr
34-
.use_hse(32.mhz())
35-
.sysclk(32.mhz())
36-
.pclk1(16.mhz())
37-
.pclk2(16.mhz())
35+
.use_hse(32u32.MHz())
36+
.sysclk(32u32.MHz())
37+
.pclk1(16u32.MHz())
38+
.pclk2(16u32.MHz())
3839
.freeze(&mut flash.acr);
3940

4041
// Configure CAN RX and TX pins (AF9)
@@ -60,7 +61,7 @@ fn main() -> ! {
6061
// Watchdog makes sure this gets restarted periodically if nothing happens
6162
let mut iwdg = IndependentWatchDog::new(dp.IWDG);
6263
iwdg.stop_on_debug(&dp.DBGMCU, true);
63-
iwdg.start(100.ms());
64+
iwdg.start(100u32.milliseconds());
6465

6566
// Send an initial message!
6667
asm::delay(100_000);

examples/i2c_scanner.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
#![no_std]
77
#![no_main]
88

9-
use core::ops::Range;
9+
use core::{convert::TryInto, ops::Range};
1010

1111
use panic_semihosting as _;
1212

1313
use cortex_m::asm;
1414
use cortex_m_rt::entry;
1515
use cortex_m_semihosting::{hprint, hprintln};
1616

17+
use hal::time::rate::*;
1718
use stm32f3xx_hal::{self as hal, pac, prelude::*};
1819

1920
const VALID_ADDR_RANGE: Range<u8> = 0x08..0x78;
@@ -33,7 +34,14 @@ fn main() -> ! {
3334
gpiob.pb6.into_af4(&mut gpiob.moder, &mut gpiob.afrl), // SCL
3435
gpiob.pb7.into_af4(&mut gpiob.moder, &mut gpiob.afrl), // SDA
3536
);
36-
let mut i2c = hal::i2c::I2c::new(dp.I2C1, pins, 100.khz(), clocks, &mut rcc.apb1);
37+
38+
let mut i2c = hal::i2c::I2c::new(
39+
dp.I2C1,
40+
pins,
41+
100u32.kHz().try_into().unwrap(),
42+
clocks,
43+
&mut rcc.apb1,
44+
);
3745

3846
hprintln!("Start i2c scanning...").expect("Error using hprintln.");
3947
hprintln!().unwrap();

examples/pwm.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use hal::gpio::GpioExt;
1818
use hal::pac;
1919
use hal::pwm::{tim16, tim2, tim3, tim8};
2020
use hal::rcc::RccExt;
21-
use hal::time::U32Ext;
21+
use hal::time::rate::*;
2222

2323
#[entry]
2424
fn main() -> ! {
@@ -28,7 +28,7 @@ fn main() -> ! {
2828
// Configure our clocks
2929
let mut flash = dp.FLASH.constrain();
3030
let mut rcc = dp.RCC.constrain();
31-
let clocks = rcc.cfgr.sysclk(16.mhz()).freeze(&mut flash.acr);
31+
let clocks = rcc.cfgr.sysclk(16u32.MHz()).freeze(&mut flash.acr);
3232

3333
// Prep the pins we need in their correct alternate function
3434
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
@@ -52,9 +52,9 @@ fn main() -> ! {
5252
// A four channel general purpose timer that's broadly available
5353
let tim3_channels = tim3(
5454
dp.TIM3,
55-
1280, // resolution of duty cycle
56-
50.hz(), // frequency of period
57-
&clocks, // To get the timer's clock speed
55+
1280, // resolution of duty cycle
56+
50u32.Hz(), // frequency of period
57+
&clocks, // To get the timer's clock speed
5858
);
5959

6060
// Channels without pins cannot be enabled, so we can't forget to
@@ -101,9 +101,9 @@ fn main() -> ! {
101101
// A 32-bit timer, so we can set a larger resolution
102102
let tim2_channels = tim2(
103103
dp.TIM2,
104-
160000, // resolution of duty cycle
105-
50.hz(), // frequency of period
106-
&clocks, // To get the timer's clock speed
104+
160000, // resolution of duty cycle
105+
50u32.Hz(), // frequency of period
106+
&clocks, // To get the timer's clock speed
107107
);
108108

109109
let mut tim2_ch3 = tim2_channels.2.output_to_pb10(pb10);
@@ -116,9 +116,9 @@ fn main() -> ! {
116116
// just use it directly
117117
let mut tim16_ch1 = tim16(
118118
dp.TIM16,
119-
1280, // resolution of duty cycle
120-
50.hz(), // frequency of period
121-
&clocks, // To get the timer's clock speed
119+
1280, // resolution of duty cycle
120+
50u32.Hz(), // frequency of period
121+
&clocks, // To get the timer's clock speed
122122
)
123123
.output_to_pb8(pb8);
124124
tim16_ch1.set_duty(tim16_ch1.get_max_duty() / 20); // 5% duty cyle
@@ -130,9 +130,9 @@ fn main() -> ! {
130130
// to complementary pins (works just like standard pins)
131131
let tim8_channels = tim8(
132132
dp.TIM8,
133-
1280, // resolution of duty cycle
134-
50.hz(), // frequency of period
135-
&clocks, // To get the timer's clock speed
133+
1280, // resolution of duty cycle
134+
50u32.Hz(), // frequency of period
135+
&clocks, // To get the timer's clock speed
136136
);
137137

138138
let mut tim8_ch1 = tim8_channels.0.output_to_pc10(pc10);

examples/serial_dma.rs

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

1010
use cortex_m::{asm, singleton};
1111
use cortex_m_rt::entry;
12-
use stm32f3xx_hal::{pac, prelude::*, serial::Serial};
12+
use stm32f3xx_hal::{pac, prelude::*, serial::Serial, time::rate::*};
1313

1414
#[entry]
1515
fn main() -> ! {
@@ -25,7 +25,7 @@ fn main() -> ! {
2525
gpioa.pa9.into_af7(&mut gpioa.moder, &mut gpioa.afrh),
2626
gpioa.pa10.into_af7(&mut gpioa.moder, &mut gpioa.afrh),
2727
);
28-
let serial = Serial::usart1(dp.USART1, pins, 9600.bps(), clocks, &mut rcc.apb2);
28+
let serial = Serial::usart1(dp.USART1, pins, 9600.Bd(), clocks, &mut rcc.apb2);
2929
let (tx, rx) = serial.split();
3030

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

examples/spi.rs

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

6+
use core::convert::TryInto;
7+
68
use panic_semihosting as _;
79

810
use stm32f3xx_hal as hal;
@@ -13,6 +15,7 @@ use cortex_m_rt::entry;
1315
use hal::pac;
1416
use hal::prelude::*;
1517
use hal::spi::{Mode, Phase, Polarity, Spi};
18+
use hal::time::rate::*;
1619

1720
#[entry]
1821
fn main() -> ! {
@@ -24,9 +27,9 @@ fn main() -> ! {
2427

2528
let clocks = rcc
2629
.cfgr
27-
.use_hse(8.mhz())
28-
.sysclk(48.mhz())
29-
.pclk1(24.mhz())
30+
.use_hse(8u32.MHz())
31+
.sysclk(48u32.MHz())
32+
.pclk1(24u32.MHz())
3033
.freeze(&mut flash.acr);
3134

3235
// Configure pins for SPI
@@ -43,7 +46,7 @@ fn main() -> ! {
4346
dp.SPI1,
4447
(sck, miso, mosi),
4548
spi_mode,
46-
3.mhz(),
49+
3u32.MHz().try_into().unwrap(),
4750
clocks,
4851
&mut rcc.apb2,
4952
);

examples/usb_serial.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use cortex_m_rt::entry;
1212

1313
use hal::pac;
1414
use hal::prelude::*;
15+
use hal::time::rate::*;
1516
use hal::usb::{Peripheral, UsbBus};
1617

1718
use usb_device::prelude::*;
@@ -26,10 +27,10 @@ fn main() -> ! {
2627

2728
let clocks = rcc
2829
.cfgr
29-
.use_hse(8.mhz())
30-
.sysclk(48.mhz())
31-
.pclk1(24.mhz())
32-
.pclk2(24.mhz())
30+
.use_hse(8u32.MHz())
31+
.sysclk(48u32.MHz())
32+
.pclk1(24u32.MHz())
33+
.pclk2(24u32.MHz())
3334
.freeze(&mut flash.acr);
3435

3536
assert!(clocks.usbclk_valid());

src/i2c.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
hal::blocking::i2c::{Read, Write, WriteRead},
1313
pac::{i2c1::RegisterBlock, rcc::cfgr3::I2C1SW_A, I2C1, RCC},
1414
rcc::{Clocks, APB1},
15-
time::{Hertz, U32Ext},
15+
time::rate::*,
1616
};
1717

1818
#[cfg(not(feature = "gpio-f333"))]
@@ -111,16 +111,13 @@ 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(i2c: I2C, pins: (SCL, SDA), freq: Hertz, clocks: Clocks, apb1: &mut APB1) -> Self
115115
where
116116
I2C: Instance,
117117
SCL: SclPin<I2C>,
118118
SDA: SdaPin<I2C>,
119-
F: Into<Hertz>,
120119
{
121-
let freq = freq.into().0;
122-
123-
crate::assert!(freq <= 1_000_000);
120+
crate::assert!(*freq.integer() <= 1_000_000);
124121

125122
I2C::enable_clock(apb1);
126123

@@ -133,16 +130,16 @@ impl<I2C, SCL, SDA> I2c<I2C, (SCL, SDA)> {
133130
// t_SYNC1 + t_SYNC2 > 4 * t_I2CCLK
134131
// t_SCL ~= t_SYNC1 + t_SYNC2 + t_SCLL + t_SCLH
135132
let i2cclk = I2C::clock(&clocks).0;
136-
let ratio = i2cclk / freq - 4;
137-
let (presc, scll, sclh, sdadel, scldel) = if freq >= 100_000 {
133+
let ratio = i2cclk / *freq.integer() - 4;
134+
let (presc, scll, sclh, sdadel, scldel) = if *freq.integer() >= 100_000 {
138135
// fast-mode or fast-mode plus
139136
// here we pick SCLL + 1 = 2 * (SCLH + 1)
140137
let presc = ratio / 387;
141138

142139
let sclh = ((ratio / (presc + 1)) - 3) / 3;
143140
let scll = 2 * (sclh + 1) - 1;
144141

145-
let (sdadel, scldel) = if freq > 400_000 {
142+
let (sdadel, scldel) = if *freq.integer() > 400_000 {
146143
// fast-mode plus
147144
let sdadel = 0;
148145
let scldel = i2cclk / 4_000_000 / (presc + 1) - 1;
@@ -451,7 +448,7 @@ macro_rules! i2c {
451448
fn clock(clocks: &Clocks) -> Hertz {
452449
// NOTE(unsafe) atomic read with no side effects
453450
match unsafe { (*RCC::ptr()).cfgr3.read().$i2cXsw().variant() } {
454-
I2C1SW_A::HSI => 8.mhz().into(),
451+
I2C1SW_A::HSI => Hertz(8_000_000),
455452
I2C1SW_A::SYSCLK => clocks.sysclk(),
456453
}
457454
}

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ pub use embedded_hal as hal;
102102
pub use nb;
103103
pub use nb::block;
104104

105+
pub use embedded_time as time;
106+
105107
#[cfg(feature = "defmt")]
106108
pub(crate) use defmt::{assert, panic, unreachable, unwrap};
107109
#[cfg(feature = "defmt")]
@@ -188,7 +190,6 @@ cfg_if::cfg_if! {
188190
pub mod rtc;
189191
pub mod serial;
190192
pub mod spi;
191-
pub mod time;
192193
pub mod timer;
193194
}
194195
}

0 commit comments

Comments
 (0)