Skip to content

Commit 77c2265

Browse files
bors[bot]burrbull
andauthored
Merge #381
381: try to use fugit::Duraction for CountDown::Time r=therealprof a=burrbull ~~Not for SysTick for now~~ Use 1 microsecond as minimal timeout for SysTick `Counter` This PR lets to use `fugit` units for timer including values bigger then 1 second. Sampling of timer (time of 1 tick) now can be set in by FREQ generic constant. Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents 4efd928 + 3e9f915 commit 77c2265

12 files changed

+338
-29
lines changed

CHANGELOG.md

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

1010
### Added
1111

12+
- Added `Counter` with `CountDown<Time=fugit::TimerDuration>` and `atat::Clock` implementations [#381]
1213
- `Into<serial::Config>` for `Bps` [#387]
1314
- Added the missing DMA implementations for USART3 [#373]
1415
- `count_down` constructor for `Timer` -> `CountDownTimer` without start [#382]
@@ -23,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2324
[#382]: https://github.com/stm32-rs/stm32f4xx-hal/pull/382
2425
[#380]: https://github.com/stm32-rs/stm32f4xx-hal/pull/380
2526
[#374]: https://github.com/stm32-rs/stm32f4xx-hal/pull/374
27+
[#381]: https://github.com/stm32-rs/stm32f4xx-hal/pull/381
2628

2729
### Changed
2830

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ cast = { default-features = false, version = "0.3.0" }
4141
void = { default-features = false, version = "1.0.2" }
4242
embedded-hal = { features = ["unproven"], version = "0.2.6" }
4343
display-interface = { version = "0.4.1", optional = true }
44-
fugit = "0.3.0"
44+
fugit = "0.3.3"
4545
rtic-monotonic = { version = "1.0", optional = true }
46+
atat = { version = "0.14.0", optional = true }
4647

4748
[dependencies.stm32_i2s_v12x]
4849
version = "0.2.0"

examples/adc_dma_rtic.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use panic_semihosting as _;
77
mod app {
88
use cortex_m_semihosting::hprintln;
99
use dwt_systick_monotonic::DwtSystick;
10-
pub use fugit::ExtU32;
1110

1211
use stm32f4xx_hal::{
1312
adc::{

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::hal::{
1616
prelude::*,
1717
rcc::{Clocks, Rcc},
1818
spi::Spi,
19-
timer::{CountDownTimer, Event, Timer},
19+
timer::{CounterUs, Event, Timer},
2020
};
2121

2222
use core::cell::{Cell, RefCell};
@@ -43,8 +43,7 @@ use ssd1306::{prelude::*, Ssd1306};
4343
// Set up global state. It's all mutexed up for concurrency safety.
4444
static ELAPSED_MS: Mutex<Cell<u32>> = Mutex::new(Cell::new(0u32));
4545
static ELAPSED_RESET_MS: Mutex<Cell<u32>> = Mutex::new(Cell::new(0u32));
46-
static TIMER_TIM2: Mutex<RefCell<Option<CountDownTimer<pac::TIM2>>>> =
47-
Mutex::new(RefCell::new(None));
46+
static TIMER_TIM2: Mutex<RefCell<Option<CounterUs<pac::TIM2>>>> = Mutex::new(RefCell::new(None));
4847
static STATE: Mutex<Cell<StopwatchState>> = Mutex::new(Cell::new(StopwatchState::Ready));
4948
static BUTTON: Mutex<RefCell<Option<PA0<Input<PullDown>>>>> = Mutex::new(RefCell::new(None));
5049

@@ -127,8 +126,8 @@ fn main() -> ! {
127126
disp.flush().unwrap();
128127

129128
// Create a 1ms periodic interrupt from TIM2
130-
let mut timer = Timer::new(dp.TIM2, &clocks).count_down();
131-
timer.start(1.hz());
129+
let mut timer = Timer::new(dp.TIM2, &clocks).counter();
130+
timer.start(1.secs()).unwrap();
132131
timer.listen(Event::TimeOut);
133132

134133
free(|cs| {

examples/blinky-timer-irq.rs

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

2020
use core::cell::RefCell;
2121
use cortex_m::{asm::wfi, interrupt::Mutex};
2222
use cortex_m_rt::entry;
23-
use embedded_hal::timer::CountDown;
2423

2524
// NOTE You can uncomment 'hprintln' here and in the code below for a bit more
2625
// verbosity at runtime, at the cost of throwing off the timing of the blink
@@ -36,14 +35,14 @@ type LedPin = gpioa::PA5<Output<PushPull>>;
3635
static G_LED: Mutex<RefCell<Option<LedPin>>> = Mutex::new(RefCell::new(None));
3736

3837
// Make timer interrupt registers globally available
39-
static G_TIM: Mutex<RefCell<Option<CountDownTimer<TIM2>>>> = Mutex::new(RefCell::new(None));
38+
static G_TIM: Mutex<RefCell<Option<CounterUs<TIM2>>>> = Mutex::new(RefCell::new(None));
4039

4140
// Define an interupt handler, i.e. function to call when interrupt occurs.
4241
// This specific interrupt will "trip" when the timer TIM2 times out
4342
#[interrupt]
4443
fn TIM2() {
4544
static mut LED: Option<LedPin> = None;
46-
static mut TIM: Option<CountDownTimer<TIM2>> = None;
45+
static mut TIM: Option<CounterUs<TIM2>> = None;
4746

4847
let led = LED.get_or_insert_with(|| {
4948
cortex_m::interrupt::free(|cs| {
@@ -79,8 +78,8 @@ fn main() -> ! {
7978
cortex_m::interrupt::free(|cs| *G_LED.borrow(cs).borrow_mut() = Some(led));
8079

8180
// Set up a timer expiring after 1s
82-
let mut timer = Timer::new(dp.TIM2, &clocks).count_down();
83-
timer.start(1.hz());
81+
let mut timer = Timer::new(dp.TIM2, &clocks).counter();
82+
timer.start(1.secs()).unwrap();
8483

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

examples/rtic-tick.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use panic_halt as _;
55

66
#[rtic::app(device = stm32f4xx_hal::pac, dispatchers = [USART1])]
77
mod app {
8-
use fugit::ExtU32;
98
use stm32f4xx_hal::{
109
gpio::{gpioc::PC13, Output, PushPull},
1110
pac,

examples/stopwatch-with-ssd1306-and-interrupts.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::hal::{
2828
interrupt, pac,
2929
prelude::*,
3030
rcc::{Clocks, Rcc},
31-
timer::{CountDownTimer, Event, Timer},
31+
timer::{CounterUs, Event, Timer},
3232
};
3333
use core::cell::{Cell, RefCell};
3434
use core::fmt::Write;
@@ -49,8 +49,7 @@ use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
4949

5050
// Set up global state. It's all mutexed up for concurrency safety.
5151
static ELAPSED_MS: Mutex<Cell<u32>> = Mutex::new(Cell::new(0u32));
52-
static TIMER_TIM2: Mutex<RefCell<Option<CountDownTimer<pac::TIM2>>>> =
53-
Mutex::new(RefCell::new(None));
52+
static TIMER_TIM2: Mutex<RefCell<Option<CounterUs<pac::TIM2>>>> = Mutex::new(RefCell::new(None));
5453
static STATE: Mutex<Cell<StopwatchState>> = Mutex::new(Cell::new(StopwatchState::Ready));
5554
static BUTTON: Mutex<RefCell<Option<PC13<Input<PullUp>>>>> = Mutex::new(RefCell::new(None));
5655

@@ -93,8 +92,8 @@ fn main() -> ! {
9392
disp.flush().unwrap();
9493

9594
// Create a 1ms periodic interrupt from TIM2
96-
let mut timer = Timer::new(dp.TIM2, &clocks).count_down();
97-
timer.start(1.hz());
95+
let mut timer = Timer::new(dp.TIM2, &clocks).counter();
96+
timer.start(1.secs()).unwrap();
9897
timer.listen(Event::TimeOut);
9998

10099
free(|cs| {

examples/timer-periph.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use panic_halt as _;
1515
use cortex_m_rt::entry;
1616
use cortex_m_semihosting::hprintln;
1717

18-
use embedded_hal::timer::Cancel;
1918
use hal::timer;
2019
use hal::timer::Timer;
2120
use stm32f4xx_hal as hal;
@@ -29,8 +28,8 @@ fn main() -> ! {
2928
let clocks = rcc.cfgr.sysclk(24.mhz()).freeze();
3029

3130
// Create a timer based on SysTick
32-
let mut timer = Timer::new(dp.TIM1, &clocks).count_down();
33-
timer.start(1.hz());
31+
let mut timer = Timer::new(dp.TIM1, &clocks).counter_ms();
32+
timer.start(1.secs()).unwrap();
3433

3534
hprintln!("hello!").unwrap();
3635
// wait until timer expires
@@ -46,8 +45,7 @@ fn main() -> ! {
4645
timer.cancel().unwrap();
4746

4847
// start it again
49-
timer.start(1.hz());
50-
nb::block!(timer.wait()).unwrap();
48+
timer.delay(1.secs()).unwrap();
5149
hprintln!("timer expired 3").unwrap();
5250

5351
timer.cancel().unwrap();

examples/timer-syst.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use panic_halt as _;
1515
use cortex_m_rt::entry;
1616
use cortex_m_semihosting::hprintln;
1717

18-
use embedded_hal::timer::Cancel;
1918
use hal::timer;
2019
use hal::timer::Timer;
2120
use stm32f4xx_hal as hal;
@@ -30,8 +29,8 @@ fn main() -> ! {
3029
let clocks = rcc.cfgr.sysclk(24.mhz()).freeze();
3130

3231
// Create a timer based on SysTick
33-
let mut timer = Timer::syst(cp.SYST, &clocks).count_down();
34-
timer.start(24.hz());
32+
let mut timer = Timer::syst(cp.SYST, &clocks).counter();
33+
timer.start(42.millis()).unwrap();
3534

3635
hprintln!("hello!").unwrap();
3736
// wait until timer expires
@@ -47,8 +46,7 @@ fn main() -> ! {
4746
timer.cancel().unwrap();
4847

4948
// start it again
50-
timer.start(24.hz());
51-
nb::block!(timer.wait()).unwrap();
49+
timer.delay(42.millis()).unwrap();
5250
hprintln!("timer expired 3").unwrap();
5351

5452
timer.cancel().unwrap();

src/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub use embedded_hal::watchdog::WatchdogEnable as _embedded_hal_watchdog_Watchdo
5757
pub use embedded_hal::Capture as _embedded_hal_Capture;
5858
pub use embedded_hal::Pwm as _embedded_hal_Pwm;
5959
pub use embedded_hal::Qei as _embedded_hal_Qei;
60+
pub use fugit::ExtU32 as _fugit_ExtU32;
6061

6162
#[cfg(all(feature = "device-selected", feature = "dac"))]
6263
pub use crate::dac::DacExt as _stm32f4xx_hal_dac_DacExt;

0 commit comments

Comments
 (0)