Skip to content

Commit

Permalink
Merge pull request #183 from jamwaffles/embedded-time
Browse files Browse the repository at this point in the history
Migrate crate over to embedded-time
  • Loading branch information
hannobraun committed Jul 26, 2021
2 parents 06eb9e4 + 2c9e21c commit 37cfc5e
Show file tree
Hide file tree
Showing 26 changed files with 69 additions and 144 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ the linker instead of having a default for the whole sub-family. ([#173])

### Breaking Changes

- Migrate from custom `Hertz` implementation to [`embedded-time`](https://crates.io/crates/embedded-time) ([#183])

- Add `enable` to `GeneralPurposeTimer`

- `Instance::clock_frequency` is now an associated function and doesn't take `&self` anymore.
Expand Down Expand Up @@ -119,6 +121,7 @@ _Not yet tracked in this changelog._

<!-- Links to pull requests and issues. Note that you can use "issues"
in the URL for both issues and pull requests. -->
[#183]: https://github.com/stm32-rs/stm32l0xx-hal/issues/183
[#148]: https://github.com/stm32-rs/stm32l0xx-hal/issues/148
[#145]: https://github.com/stm32-rs/stm32l0xx-hal/issues/145
[#144]: https://github.com/stm32-rs/stm32l0xx-hal/issues/144
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ cortex-m = "0.7.0"
cortex-m-rt = "0.6.8"
cortex-m-semihosting = "0.3.2"
nb = "1.0.0"
embedded-time = "0.12.0"

[dependencies.cast]
version = "0.2.2"
Expand Down
2 changes: 1 addition & 1 deletion examples/adc_pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() -> ! {
let gpioa = dp.GPIOA.split(&mut rcc);

// Configure the timer as PWM on PA1.
let pwm = pwm::Timer::new(dp.TIM2, 1.khz(), &mut rcc);
let pwm = pwm::Timer::new(dp.TIM2, 1_000.Hz(), &mut rcc);
let mut pwm = pwm.channel2.assign(gpioa.pa1);
let max_duty = pwm.get_max_duty() / 4095;
pwm.enable();
Expand Down
4 changes: 2 additions & 2 deletions examples/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ fn main() -> ! {

loop {
let wait = match button.is_high() {
Ok(true) => 300.ms(),
Ok(false) => 100.ms(),
Ok(true) => 300.milliseconds(),
Ok(false) => 100.milliseconds(),
_ => unreachable!(),
};
delay.delay(wait);
Expand Down
2 changes: 1 addition & 1 deletion examples/lptim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn main() -> ! {
blink(&mut led);

// 1 seconds of regular run mode
lptim.start(1.hz());
lptim.start(1.Hz());
block!(lptim.wait()).unwrap();

Exti::unpend(exti_line);
Expand Down
2 changes: 1 addition & 1 deletion examples/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() -> ! {
let gpioa = dp.GPIOA.split(&mut rcc);

// Initialize TIM2 for PWM
let pwm = pwm::Timer::new(dp.TIM2, 10.khz(), &mut rcc);
let pwm = pwm::Timer::new(dp.TIM2, 10_000.Hz(), &mut rcc);

#[cfg(feature = "stm32l0x1")]
let mut pwm = pwm.channel2.assign(gpioa.pa1);
Expand Down
2 changes: 1 addition & 1 deletion examples/serial_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() -> ! {
.usart(
gpioa.pa2,
gpioa.pa3,
serial::Config::default().baudrate(115_200.bps()),
serial::Config::default().baudrate(115_200.Bd()),
&mut rcc,
)
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion examples/serial_dma_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn main() -> ! {
.usart(
gpioa.pa2,
gpioa.pa3,
serial::Config::default().baudrate(115_200.bps()),
serial::Config::default().baudrate(115_200.Bd()),
&mut rcc,
)
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion examples/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn main() -> ! {
// Initialise the SPI peripheral.
let mut spi = dp
.SPI1
.spi((sck, miso, mosi), spi::MODE_0, 100_000.hz(), &mut rcc);
.spi((sck, miso, mosi), spi::MODE_0, 100_000.Hz(), &mut rcc);

loop {
nss.set_low().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/spi_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn main() -> ! {
// Initialise the SPI peripheral.
let mut spi = dp
.SPI1
.spi((sck, miso, mosi), spi::MODE_0, 100_000.hz(), &mut rcc);
.spi((sck, miso, mosi), spi::MODE_0, 100_000.Hz(), &mut rcc);

let mut scb = cp.SCB;
let mut dma = DMA::new(dp.DMA1, &mut rcc);
Expand Down
2 changes: 1 addition & 1 deletion examples/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn main() -> ! {
let led = gpioa.pa1.into_push_pull_output();

// Configure the timer.
let mut timer = dp.TIM2.timer(1.hz(), &mut rcc);
let mut timer = dp.TIM2.timer(1.Hz(), &mut rcc);
timer.listen();

// Store the LED and timer in mutex refcells to make them available from the
Expand Down
2 changes: 1 addition & 1 deletion examples/timer_interrupt_rtic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const APP: () = {
let led = gpioa.pa1.into_push_pull_output().downgrade();

// Configure the timer.
let mut timer = device.TIM2.timer(1.hz(), &mut rcc);
let mut timer = device.TIM2.timer(1.Hz(), &mut rcc);
timer.listen();

// Return the initialised resources.
Expand Down
2 changes: 1 addition & 1 deletion examples/watchdog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() -> ! {
let mut watchdog = dp.IWDG.watchdog();

// Start a watchdog with a 100ms period.
watchdog.start(100.ms());
watchdog.start(10.Hz());

let mut counter = 50;
loop {
Expand Down
14 changes: 11 additions & 3 deletions src/delay.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//! Delays

use crate::hal::blocking::delay::{DelayMs, DelayUs};
use crate::rcc::Clocks;
use crate::time::MicroSeconds;
use cast::u32;
use core::convert::TryInto;
use cortex_m::peripheral::syst::SystClkSource;
use cortex_m::peripheral::SYST;
use embedded_time::duration::{Extensions, Microseconds};

pub trait DelayExt {
fn delay(self, clocks: Clocks) -> Delay;
Expand All @@ -31,11 +33,17 @@ impl Delay {
let ticks_per_us = freq / 1_000_000_u32;
Delay { ticks_per_us, syst }
}

/// Wait for the given time.
///
/// Note that durations above `u32::MAX` microseconds will be clamped at `u32::MAX`.
pub fn delay<T>(&mut self, delay: T)
where
T: Into<MicroSeconds>,
T: TryInto<Microseconds>,
{
self.delay_us(delay.into().0)
let delay = delay.try_into().unwrap_or_else(|_| u32::MAX.microseconds());

self.delay_us(delay.0)
}

/// Releases the system timer (SysTick) resource
Expand Down
2 changes: 1 addition & 1 deletion src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::pac::i2c1::{
RegisterBlock,
};
use crate::rcc::Rcc;
use crate::time::Hertz;
use cast::u8;
use embedded_time::rate::Hertz;

// I²C traits
use crate::hal::blocking::i2c::{Read, Write, WriteRead};
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ pub mod serial;
pub mod signature;
pub mod spi;
pub mod syscfg;
pub mod time;
pub mod timer;
#[cfg(all(
feature = "stm32-usbd",
Expand Down
16 changes: 8 additions & 8 deletions src/lptim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use crate::hal;
use crate::pac::LPTIM;
use crate::pwr::PWR;
use crate::rcc::Rcc;
use crate::time::{Hertz, MicroSeconds};
use cast::{u32, u64};
use core::convert::TryFrom;
use core::marker::PhantomData;
use embedded_time::duration::Microseconds;
use embedded_time::rate::Hertz;
use void::Void;

mod sealed {
Expand Down Expand Up @@ -85,7 +86,7 @@ pub struct Interrupts {
/// The timer can be initialized either in one-shot mode or in periodic mode, using `init_oneshot`
/// or `init_periodic` respectively. In periodic mode, the embedded-hal `Periodic` marker trait is
/// implemented and the `CountDown` implementation uses `Hertz` as the time unit. In one-shot mode,
/// the `CountDown` implementation instead uses `MicroSeconds`, allowing for a multi-second timeout
/// the `CountDown` implementation instead uses `Microseconds`, allowing for a multi-second timeout
/// to be configured (with the tradeoff being a larger code size due to use of 64-bit arithmetic).
pub struct LpTimer<M: CountMode> {
lptim: LPTIM,
Expand Down Expand Up @@ -365,11 +366,11 @@ impl hal::timer::CountDown for LpTimer<Periodic> {
impl hal::timer::Periodic for LpTimer<Periodic> {}

impl hal::timer::CountDown for LpTimer<OneShot> {
type Time = MicroSeconds;
type Time = Microseconds;

fn start<T>(&mut self, period: T)
where
T: Into<MicroSeconds>,
T: Into<Microseconds>,
{
self.configure(TimeConf::calculate_period(self.input_freq, period.into()));

Expand Down Expand Up @@ -435,7 +436,7 @@ impl TimeConf {

/// Calculates prescaler and autoreload value for producing overflows after every
/// `output_period`.
fn calculate_period(input_freq: Hertz, output_period: MicroSeconds) -> Self {
fn calculate_period(input_freq: Hertz, output_period: Microseconds) -> Self {
// Here, the `output_period` can be very long, resulting in an output frequency of < 1 Hz.

// Fi = Frequency of input clock
Expand Down Expand Up @@ -484,7 +485,6 @@ impl TimeConf {
#[cfg(test)]
mod tests {
use super::*;
use crate::time::U32Ext;

/// Test-only methods.
impl TimeConf {
Expand All @@ -498,8 +498,8 @@ mod tests {
Hertz(input_freq.0 / u32(self.psc()) / u32(self.arr))
}

fn output_period(&self, input_freq: Hertz) -> MicroSeconds {
MicroSeconds(
fn output_period(&self, input_freq: Hertz) -> Microseconds {
Microseconds(
u32(u64(self.psc()) * u64(self.arr) * 1_000_000 / u64(input_freq.0)).unwrap(),
)
}
Expand Down
6 changes: 5 additions & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ pub use crate::{
pwr::PowerMode as _,
rcc::RccExt as _,
spi::SpiExt as _,
time::U32Ext as _,
timer::TimerExt as _,
watchdog::{IndependedWatchdogExt as _, WindowWatchdogExt as _},
};

// Make items like `.Hz()`, `.microseconds()` available from embedded-time
pub use embedded_time::{
duration::Extensions as DurationExtensions, rate::Extensions as RateExtensions,
};

#[cfg(any(
feature = "io-STM32L021",
feature = "io-STM32L031",
Expand Down
19 changes: 10 additions & 9 deletions src/pwm.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use core::marker::PhantomData;
use core::ops::Deref;

use cortex_m::interrupt;

use crate::gpio::gpioa::{PA0, PA1, PA2, PA3};
use crate::gpio::{AltMode, PinMode};
use crate::hal;
use crate::pac::{tim2, TIM2, TIM3};
use crate::rcc::Rcc;
use crate::time::Hertz;
use cast::{u16, u32};
use core::marker::PhantomData;
use core::ops::Deref;
use cortex_m::interrupt;
use embedded_time::rate::Hertz;

#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
use crate::gpio::{
Expand Down Expand Up @@ -43,7 +41,9 @@ where
I: Instance,
{
/// Create new timer instance that is automatically started with given frequency
pub fn new(timer: I, frequency: Hertz, rcc: &mut Rcc) -> Self {
pub fn new(timer: I, frequency: impl Into<Hertz>, rcc: &mut Rcc) -> Self {
let frequency = frequency.into();

timer.enable(rcc);

let mut tim = Self {
Expand Down Expand Up @@ -72,7 +72,8 @@ where
/// In order to do this operation properly the function stop the timer and then starts it again.
/// The duty cycle that was set before for given pin needs to adjusted according to the
/// frequency
pub fn set_frequency(&mut self, frequency: Hertz, rcc: &Rcc) {
pub fn set_frequency(&mut self, frequency: impl Into<Hertz>, rcc: &Rcc) {
let frequency = frequency.into();
self.stop();
let (psc, arr) = get_clock_config(frequency.0, I::clock_frequency(rcc));
self.instance.psc.write(|w| w.psc().bits(psc));
Expand Down Expand Up @@ -268,7 +269,7 @@ where
{
/// This allows to dynamically change the frequency of the underlying PWM timer.
///
/// **WARNING:**
/// **WARNING:**
/// This changes the frequency for all channels associated with the PWM timer.
pub fn set_frequency(&mut self, frequency: Hertz, rcc: &Rcc) {
let (psc, arr) = get_clock_config(frequency.0, I::clock_frequency(rcc));
Expand Down
16 changes: 8 additions & 8 deletions src/rcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::mco;
use crate::pac::rcc::cfgr::{MCOPRE_A, MCOSEL_A};
use crate::pac::RCC;
use crate::pwr::PWR;
use crate::time::{Hertz, U32Ext};
use embedded_time::rate::{Extensions, Hertz};

#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
use crate::{pac::CRS, syscfg::SYSCFG};
Expand Down Expand Up @@ -340,7 +340,7 @@ impl RccExt for RCC {
PLLDiv::Div3 => freq / 3,
PLLDiv::Div4 => freq / 4,
};
assert!(freq <= 32.mhz().0);
assert!(freq <= 32_u32.MHz().0);

self.cfgr.write(move |w| unsafe {
w.pllmul()
Expand Down Expand Up @@ -393,12 +393,12 @@ impl RccExt for RCC {

let clocks = Clocks {
source: cfgr.mux,
sys_clk: sys_clk.hz(),
ahb_clk: ahb_freq.hz(),
apb1_clk: apb1_freq.hz(),
apb2_clk: apb2_freq.hz(),
apb1_tim_clk: apb1_tim_freq.hz(),
apb2_tim_clk: apb2_tim_freq.hz(),
sys_clk: sys_clk.Hz(),
ahb_clk: ahb_freq.Hz(),
apb1_clk: apb1_freq.Hz(),
apb2_clk: apb2_freq.Hz(),
apb1_tim_clk: apb1_tim_freq.Hz(),
apb2_tim_clk: apb2_tim_freq.Hz(),
};

Rcc { rb: self, clocks }
Expand Down
4 changes: 2 additions & 2 deletions src/rtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
//!
//! See STM32L0x2 reference manual, chapter 26.

use embedded_time::rate::Extensions;
use void::Void;

use crate::{
hal::timer::{self, Cancel as _},
pac,
pwr::PWR,
rcc::Rcc,
time::U32Ext,
};

/// Entry point to the RTC API
Expand Down Expand Up @@ -57,7 +57,7 @@ impl RTC {
});

let apb1_clk = rcc.clocks.apb1_clk();
let rtc_clk = 32_768u32.hz(); // LSE crystal frequency
let rtc_clk = 32_768u32.Hz(); // LSE crystal frequency

// The APB1 clock must not be slower than the RTC clock.
if apb1_clk < rtc_clk {
Expand Down
Loading

0 comments on commit 37cfc5e

Please sign in to comment.