Skip to content

Commit f05b81e

Browse files
bors[bot]burrbull
andauthored
Merge #337
337: Timer rework r=therealprof a=burrbull Co-authored-by: Andrey Zgarbul <zgarbul.andrey@gmail.com> Co-authored-by: Zgarbul Andrey <zgarbul.andrey@gmail.com>
2 parents 3669202 + cc6019e commit f05b81e

23 files changed

+327
-399
lines changed

CHANGELOG.md

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

4343
### Changed
4444

45+
- [breaking-change] `Timer::new` now just initializes peripheral.
46+
Use `.start_count_down` to start count, `pwm` or `delay` on `Timer` struct.
4547
- Add `Spi::new`, `I2s::new, `spi::Instance` and deprecate `Spi:spix`,
4648
deprecate `Serial::usartx`, remove deprecated `I2c::i2cx`
4749
- Deprecate `free` in favour of `release`

examples/analog-stopwatch-with-spi-ssd1306.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::hal::{
1717
rcc::{Clocks, Rcc},
1818
spi::Spi,
1919
stm32,
20-
timer::{Event, Timer},
20+
timer::{CountDownTimer, Event, Timer},
2121
};
2222

2323
use arrayvec::ArrayString;
@@ -47,7 +47,8 @@ use ssd1306::{prelude::*, Builder};
4747
// Set up global state. It's all mutexed up for concurrency safety.
4848
static ELAPSED_MS: Mutex<Cell<u32>> = Mutex::new(Cell::new(0u32));
4949
static ELAPSED_RESET_MS: Mutex<Cell<u32>> = Mutex::new(Cell::new(0u32));
50-
static TIMER_TIM2: Mutex<RefCell<Option<Timer<stm32::TIM2>>>> = Mutex::new(RefCell::new(None));
50+
static TIMER_TIM2: Mutex<RefCell<Option<CountDownTimer<stm32::TIM2>>>> =
51+
Mutex::new(RefCell::new(None));
5152
static STATE: Mutex<Cell<StopwatchState>> = Mutex::new(Cell::new(StopwatchState::Ready));
5253
static BUTTON: Mutex<RefCell<Option<PA0<Input<PullDown>>>>> = Mutex::new(RefCell::new(None));
5354

@@ -116,7 +117,7 @@ fn main() -> ! {
116117

117118
let dc = gpioe.pe3.into_push_pull_output();
118119
let mut ss = gpioe.pe4.into_push_pull_output();
119-
let mut delay = hal::delay::Delay::new(cp.SYST, clocks);
120+
let mut delay = hal::delay::Delay::new(cp.SYST, &clocks);
120121

121122
ss.set_high();
122123
delay.delay_ms(100_u32);
@@ -129,7 +130,7 @@ fn main() -> ! {
129130
disp.flush().unwrap();
130131

131132
// Create a 1ms periodic interrupt from TIM2
132-
let mut timer = Timer::tim2(dp.TIM2, 1.khz(), clocks);
133+
let mut timer = Timer::new(dp.TIM2, &clocks).start_count_down(1.hz());
133134
timer.listen(Event::TimeOut);
134135

135136
free(|cs| {

examples/blinky-timer-irq.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::hal::{
1414
gpio::{gpioa, Output, PushPull},
1515
prelude::*,
1616
stm32::{interrupt, Interrupt, Peripherals, TIM2},
17-
timer::{Event, Timer},
17+
timer::{CountDownTimer, Event, Timer},
1818
};
1919

2020
use core::cell::RefCell;
@@ -36,14 +36,14 @@ type LEDPIN = gpioa::PA5<Output<PushPull>>;
3636
static G_LED: Mutex<RefCell<Option<LEDPIN>>> = Mutex::new(RefCell::new(None));
3737

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

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

4848
let led = LED.get_or_insert_with(|| {
4949
cortex_m::interrupt::free(|cs| {
@@ -79,7 +79,7 @@ fn main() -> ! {
7979
cortex_m::interrupt::free(|cs| *G_LED.borrow(cs).borrow_mut() = Some(led));
8080

8181
// Set up a timer expiring after 1s
82-
let mut timer = Timer::tim2(dp.TIM2, 1.hz(), clocks);
82+
let mut timer = Timer::new(dp.TIM2, &clocks).start_count_down(1.hz());
8383

8484
// Generate an interrupt when the timer expires
8585
timer.listen(Event::TimeOut);

examples/delay-syst-blinky.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn main() -> ! {
2828
let clocks = rcc.cfgr.sysclk(48.mhz()).freeze();
2929

3030
// Create a delay abstraction based on SysTick
31-
let mut delay = hal::delay::Delay::new(cp.SYST, clocks);
31+
let mut delay = hal::delay::Delay::new(cp.SYST, &clocks);
3232

3333
loop {
3434
// On for 1s, off for 1s.

examples/delay-timer-blinky.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn main() -> ! {
2828
let clocks = rcc.cfgr.sysclk(48.mhz()).freeze();
2929

3030
// Create a delay abstraction based on general-pupose 32-bit timer TIM5
31-
let mut delay = hal::delay::Delay::tim5(dp.TIM5, clocks);
31+
let mut delay = hal::delay::Delay::tim5(dp.TIM5, &clocks);
3232

3333
loop {
3434
// On for 1s, off for 1s.

examples/f413disco_lcd_ferris.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ fn main() -> ! {
746746
gpioe.pe5.into_push_pull_output().set_high();
747747

748748
// Get delay provider
749-
let mut delay = Delay::new(cp.SYST, clocks);
749+
let mut delay = Delay::new(cp.SYST, &clocks);
750750

751751
// Set up timing
752752
let write_timing = Timing::default().data(3).address_setup(3).bus_turnaround(0);

examples/i2s-audio-out-dma.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ fn main() -> ! {
103103
let gpioc = dp.GPIOC.split();
104104
let gpiod = dp.GPIOD.split();
105105

106-
let mut delay = Delay::new(cp.SYST, clocks);
106+
let mut delay = Delay::new(cp.SYST, &clocks);
107107

108108
let i2c = I2c::new(
109109
dp.I2C1,

examples/i2s-audio-out.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fn main() -> ! {
100100
// The 86 MHz frequency can be divided to get a sample rate very close to 48 kHz.
101101
let clocks = rcc.cfgr.use_hse(8.mhz()).i2s_clk(86.mhz()).freeze();
102102

103-
let mut delay = Delay::new(cp.SYST, clocks);
103+
let mut delay = Delay::new(cp.SYST, &clocks);
104104

105105
let i2c = I2c::new(
106106
dp.I2C1,

examples/pwm.rs

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

88
use cortex_m_rt::entry;
9-
use stm32f4xx_hal::{prelude::*, pwm, stm32};
9+
use stm32f4xx_hal::{pac, prelude::*, timer::Timer};
1010

1111
#[entry]
1212
fn main() -> ! {
13-
if let Some(dp) = stm32::Peripherals::take() {
13+
if let Some(dp) = pac::Peripherals::take() {
1414
// Set up the system clock.
1515
let rcc = dp.RCC.constrain();
1616
let clocks = rcc.cfgr.freeze();
1717

1818
let gpioa = dp.GPIOA.split();
1919
let channels = (gpioa.pa8.into_alternate(), gpioa.pa9.into_alternate());
2020

21-
let pwm = pwm::tim1(dp.TIM1, channels, clocks, 20u32.khz());
21+
let pwm = Timer::new(dp.TIM1, &clocks).pwm(channels, 20u32.khz());
2222
let (mut ch1, _ch2) = pwm;
2323
let max_duty = ch1.get_max_duty();
2424
ch1.set_duty(max_duty / 2);

examples/qei.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn main() -> ! {
3232
let clocks = rcc.cfgr.freeze();
3333

3434
// Create a delay abstraction based on SysTick.
35-
let mut delay = Delay::new(cp.SYST, clocks);
35+
let mut delay = Delay::new(cp.SYST, &clocks);
3636

3737
let gpioa = dp.GPIOA.split();
3838

0 commit comments

Comments
 (0)