Skip to content

Commit f30cb81

Browse files
bors[bot]burrbull
andauthored
Merge #404
404: timers rework r=therealprof a=burrbull Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents c508256 + 66fb0f8 commit f30cb81

28 files changed

+2200
-979
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ bxcan = "0.6"
2525
void = { default-features = false, version = "1.0.2" }
2626
embedded-hal = { features = ["unproven"], version = "0.2.6" }
2727
fugit = "0.3.5"
28+
fugit-timer = "0.1.3"
2829
rtic-monotonic = { version = "1.0", optional = true }
30+
bitflags = "1.3.2"
2931

3032
[dependencies.stm32-usbd]
3133
version = "0.6.0"

examples/blinky.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ fn main() -> ! {
3939
// in order to configure the port. For pins 0-7, crl should be passed instead.
4040
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
4141
// Configure the syst timer to trigger an update every second
42-
let mut timer = Timer::syst(cp.SYST, &clocks).start_count_down(1.Hz());
42+
let mut timer = Timer::syst(cp.SYST, &clocks).counter_hz();
43+
timer.start(1.Hz()).unwrap();
4344

4445
// Wait for the timer to trigger an update and change the state of the LED
4546
loop {

examples/blinky_generic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ fn main() -> ! {
2626
let mut gpioc = dp.GPIOC.split();
2727

2828
// Configure the syst timer to trigger an update every second
29-
let mut timer = Timer::syst(cp.SYST, &clocks).start_count_down(1.Hz());
29+
let mut timer = Timer::syst(cp.SYST, &clocks).counter_hz();
30+
timer.start(1.Hz()).unwrap();
3031

3132
// Create an array of LEDS to blink
3233
let mut leds = [

examples/blinky_timer_irq.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::hal::{
1818
gpio::{gpioc, Output, PushPull},
1919
pac::{interrupt, Interrupt, Peripherals, TIM2},
2020
prelude::*,
21-
timer::{CountDownTimer, Event, Timer},
21+
timer::{CounterMs, Event},
2222
};
2323

2424
use core::cell::RefCell;
@@ -38,14 +38,14 @@ type LedPin = gpioc::PC13<Output<PushPull>>;
3838
static G_LED: Mutex<RefCell<Option<LedPin>>> = Mutex::new(RefCell::new(None));
3939

4040
// Make timer interrupt registers globally available
41-
static G_TIM: Mutex<RefCell<Option<CountDownTimer<TIM2>>>> = Mutex::new(RefCell::new(None));
41+
static G_TIM: Mutex<RefCell<Option<CounterMs<TIM2>>>> = Mutex::new(RefCell::new(None));
4242

4343
// Define an interupt handler, i.e. function to call when interrupt occurs.
4444
// This specific interrupt will "trip" when the timer TIM2 times out
4545
#[interrupt]
4646
fn TIM2() {
4747
static mut LED: Option<LedPin> = None;
48-
static mut TIM: Option<CountDownTimer<TIM2>> = None;
48+
static mut TIM: Option<CounterMs<TIM2>> = None;
4949

5050
let led = LED.get_or_insert_with(|| {
5151
cortex_m::interrupt::free(|cs| {
@@ -86,7 +86,8 @@ fn main() -> ! {
8686
cortex_m::interrupt::free(|cs| *G_LED.borrow(cs).borrow_mut() = Some(led));
8787

8888
// Set up a timer expiring after 1s
89-
let mut timer = Timer::new(dp.TIM2, &clocks).start_count_down(1.Hz());
89+
let mut timer = dp.TIM2.counter_ms(&clocks);
90+
timer.start(1.secs()).unwrap();
9091

9192
// Generate an interrupt when the timer expires
9293
timer.listen(Event::Update);

examples/delay-timer-blinky.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//! Demonstrate the use of a blocking `Delay` using TIM2 general-purpose timer.
2+
3+
#![deny(unsafe_code)]
4+
#![allow(clippy::empty_loop)]
5+
#![no_main]
6+
#![no_std]
7+
8+
// Halt on panic
9+
use panic_halt as _; // panic handler
10+
11+
use cortex_m_rt::entry;
12+
use stm32f1xx_hal as hal;
13+
14+
use crate::hal::{pac, prelude::*};
15+
16+
#[entry]
17+
fn main() -> ! {
18+
if let (Some(dp), Some(_cp)) = (
19+
pac::Peripherals::take(),
20+
cortex_m::peripheral::Peripherals::take(),
21+
) {
22+
let mut flash = dp.FLASH.constrain();
23+
// Set up the LED. On the BluePill it's connected to pin PC13.
24+
let mut gpioc = dp.GPIOC.split();
25+
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
26+
27+
// Set up the system clock. We want to run at 48MHz for this one.
28+
let rcc = dp.RCC.constrain();
29+
let clocks = rcc
30+
.cfgr
31+
.use_hse(8.MHz())
32+
.sysclk(48.MHz())
33+
.freeze(&mut flash.acr);
34+
35+
// Create a delay abstraction based on general-pupose 32-bit timer TIM2
36+
37+
//let mut delay = hal::timer::FTimerUs::new(dp.TIM2, &clocks).delay();
38+
// or
39+
let mut delay = dp.TIM2.delay_us(&clocks);
40+
41+
loop {
42+
// On for 1s, off for 3s.
43+
led.set_high();
44+
// Use `embedded_hal::DelayMs` trait
45+
delay.delay_ms(1000_u32);
46+
led.set_low();
47+
// or use `fugit` duration units
48+
delay.delay(3.secs());
49+
}
50+
}
51+
52+
loop {}
53+
}

examples/delay.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use panic_halt as _;
88

99
use cortex_m_rt::entry;
10-
use stm32f1xx_hal::{delay::Delay, pac, prelude::*};
10+
use stm32f1xx_hal::{pac, prelude::*};
1111

1212
#[entry]
1313
fn main() -> ! {
@@ -30,12 +30,16 @@ fn main() -> ! {
3030
#[cfg(any(feature = "stm32f103", feature = "stm32f105", feature = "stm32f107"))]
3131
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
3232

33-
let mut delay = Delay::new(cp.SYST, &clocks);
33+
//let mut delay = hal::timer::Timer::syst(cp.SYST, &clocks).delay();
34+
// or
35+
let mut delay = cp.SYST.delay(&clocks);
3436

3537
loop {
3638
led.set_high();
39+
// Use `embedded_hal::DelayMs` trait
3740
delay.delay_ms(1_000_u16);
3841
led.set_low();
39-
delay.delay_ms(1_000_u16);
42+
// or use `fugit` duration units
43+
delay.delay(1.secs());
4044
}
4145
}

examples/dynamic_gpio.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use nb::block;
99
use cortex_m_rt::entry;
1010
use cortex_m_semihosting::hprintln;
1111
use embedded_hal::digital::v2::{InputPin, OutputPin};
12-
use stm32f1xx_hal::{pac, prelude::*, timer::Timer};
12+
use stm32f1xx_hal::{pac, prelude::*};
1313

1414
#[entry]
1515
fn main() -> ! {
@@ -32,7 +32,8 @@ fn main() -> ! {
3232

3333
let mut pin = gpioc.pc13.into_dynamic(&mut gpioc.crh);
3434
// Configure the syst timer to trigger an update every second
35-
let mut timer = Timer::syst(cp.SYST, &clocks).start_count_down(1.Hz());
35+
let mut timer = cp.SYST.counter_hz(&clocks);
36+
timer.start(1.Hz()).unwrap();
3637

3738
// Wait for the timer to trigger an update and change the state of the LED
3839
loop {

examples/gpio_input.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#![no_main]
1818
use cortex_m_rt::entry;
1919
use panic_halt as _;
20-
use stm32f1xx_hal::{delay::Delay, gpio::PinState, pac, prelude::*};
20+
use stm32f1xx_hal::{gpio::PinState, pac, prelude::*};
2121

2222
#[entry]
2323
fn main() -> ! {
@@ -53,7 +53,7 @@ fn main() -> ! {
5353
// The key_up for check buttons if long press.
5454
// if key_up is true, and buttons were not long press.
5555
let mut key_up: bool = true;
56-
let mut delay = Delay::new(cp.SYST, &clock);
56+
let mut delay = cp.SYST.delay(&clock);
5757
loop {
5858
let key_result = (key_0.is_low(), key_1.is_low());
5959
if key_up && (key_result.0 || key_result.1) {

examples/multi_mode_gpio.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ fn main() -> ! {
3131

3232
let mut pin = gpioc.pc13.into_floating_input(&mut gpioc.crh);
3333
// Configure the syst timer to trigger an update every second
34-
let mut timer = Timer::syst(cp.SYST, &clocks).start_count_down(1.Hz());
34+
let mut timer = Timer::syst(cp.SYST, &clocks).counter_hz();
35+
timer.start(1.Hz()).unwrap();
3536

3637
// Wait for the timer to trigger an update and change the state of the LED
3738
loop {

examples/pwm.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ use cortex_m_rt::entry;
1212
use stm32f1xx_hal::{
1313
pac,
1414
prelude::*,
15-
pwm::Channel,
1615
time::ms,
17-
timer::{Tim2NoRemap, Timer},
16+
timer::{Channel, Tim2NoRemap},
1817
};
1918

2019
#[entry]
@@ -51,8 +50,12 @@ fn main() -> ! {
5150
// let c3 = gpiob.pb8.into_alternate_push_pull(&mut gpiob.crh);
5251
// let c4 = gpiob.pb9.into_alternate_push_pull(&mut gpiob.crh);
5352

54-
let mut pwm =
55-
Timer::new(p.TIM2, &clocks).pwm::<Tim2NoRemap, _, _>(pins, &mut afio.mapr, 1.kHz());
53+
//let mut pwm =
54+
// Timer::new(p.TIM2, &clocks).pwm_hz::<Tim2NoRemap, _, _>(pins, &mut afio.mapr, 1.kHz());
55+
// or
56+
let mut pwm = p
57+
.TIM2
58+
.pwm_hz::<Tim2NoRemap, _, _>(pins, &mut afio.mapr, 1.kHz(), &clocks);
5659

5760
// Enable clock on each of the channels
5861
pwm.enable(Channel::C1);

examples/pwm_custom.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn main() -> ! {
3030
let p0 = pb4.into_alternate_push_pull(&mut gpiob.crl);
3131
let p1 = gpiob.pb5.into_alternate_push_pull(&mut gpiob.crl);
3232

33-
let pwm = Timer::new(p.TIM3, &clocks).pwm((p0, p1), &mut afio.mapr, 1.kHz());
33+
let pwm = Timer::new(p.TIM3, &clocks).pwm_hz((p0, p1), &mut afio.mapr, 1.kHz());
3434

3535
let max = pwm.get_max_duty();
3636

examples/pwm_input.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
use panic_halt as _;
88

99
use cortex_m_rt::entry;
10-
use stm32f1xx_hal::{pac, prelude::*, pwm_input::*, timer::Timer};
10+
use stm32f1xx_hal::{
11+
pac,
12+
prelude::*,
13+
timer::{pwm_input::*, Timer},
14+
};
1115

1216
#[entry]
1317
fn main() -> ! {

examples/qei.rs

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

1111
use cortex_m_rt::entry;
12-
use stm32f1xx_hal::{delay::Delay, pac, prelude::*, qei::QeiOptions, timer::Timer};
12+
use stm32f1xx_hal::{pac, prelude::*, qei::QeiOptions, timer::Timer};
1313

1414
#[entry]
1515
fn main() -> ! {
@@ -39,7 +39,7 @@ fn main() -> ! {
3939
let c2 = gpiob.pb7;
4040

4141
let qei = Timer::new(dp.TIM4, &clocks).qei((c1, c2), &mut afio.mapr, QeiOptions::default());
42-
let mut delay = Delay::new(cp.SYST, &clocks);
42+
let mut delay = cp.SYST.delay(&clocks);
4343

4444
loop {
4545
let before = qei.count();

examples/timer-interrupt-rtic.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mod app {
1717
gpio::{gpioc::PC13, Output, PinState, PushPull},
1818
pac,
1919
prelude::*,
20-
timer::{CountDownTimer, Event, Timer},
20+
timer::{CounterMs, Event},
2121
};
2222

2323
#[shared]
@@ -26,7 +26,7 @@ mod app {
2626
#[local]
2727
struct Local {
2828
led: PC13<Output<PushPull>>,
29-
timer_handler: CountDownTimer<pac::TIM1>,
29+
timer_handler: CounterMs<pac::TIM1>,
3030
}
3131

3232
#[init]
@@ -49,7 +49,8 @@ mod app {
4949
.pc13
5050
.into_push_pull_output_with_state(&mut gpioc.crh, PinState::High);
5151
// Configure the syst timer to trigger an update every second and enables interrupt
52-
let mut timer = Timer::new(cx.device.TIM1, &clocks).start_count_down(1.Hz());
52+
let mut timer = cx.device.TIM1.counter_ms(&clocks);
53+
timer.start(1.secs()).unwrap();
5354
timer.listen(Event::Update);
5455

5556
// Init the static resources to use them later through RTIC
@@ -93,13 +94,13 @@ mod app {
9394

9495
if *cx.local.count == 4 {
9596
// Changes timer update frequency
96-
cx.local.timer_handler.start(2.Hz());
97+
cx.local.timer_handler.start(500.millis()).unwrap();
9798
} else if *cx.local.count == 12 {
98-
cx.local.timer_handler.start(1.Hz());
99+
cx.local.timer_handler.start(1.secs()).unwrap();
99100
*cx.local.count = 0;
100101
}
101102

102103
// Clears the update flag
103-
cx.local.timer_handler.clear_update_interrupt_flag();
104+
cx.local.timer_handler.clear_interrupt(Event::Update);
104105
}
105106
}

0 commit comments

Comments
 (0)