Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timers rework #404

Merged
merged 1 commit into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ bxcan = "0.6"
void = { default-features = false, version = "1.0.2" }
embedded-hal = { features = ["unproven"], version = "0.2.6" }
fugit = "0.3.5"
fugit-timer = "0.1.3"
rtic-monotonic = { version = "1.0", optional = true }
bitflags = "1.3.2"

[dependencies.stm32-usbd]
version = "0.6.0"
Expand Down
3 changes: 2 additions & 1 deletion examples/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ fn main() -> ! {
// in order to configure the port. For pins 0-7, crl should be passed instead.
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
// Configure the syst timer to trigger an update every second
let mut timer = Timer::syst(cp.SYST, &clocks).start_count_down(1.Hz());
let mut timer = Timer::syst(cp.SYST, &clocks).counter_hz();
timer.start(1.Hz()).unwrap();

// Wait for the timer to trigger an update and change the state of the LED
loop {
Expand Down
3 changes: 2 additions & 1 deletion examples/blinky_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ fn main() -> ! {
let mut gpioc = dp.GPIOC.split();

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

// Create an array of LEDS to blink
let mut leds = [
Expand Down
9 changes: 5 additions & 4 deletions examples/blinky_timer_irq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::hal::{
gpio::{gpioc, Output, PushPull},
pac::{interrupt, Interrupt, Peripherals, TIM2},
prelude::*,
timer::{CountDownTimer, Event, Timer},
timer::{CounterMs, Event},
};

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

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

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

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

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

// Generate an interrupt when the timer expires
timer.listen(Event::Update);
Expand Down
53 changes: 53 additions & 0 deletions examples/delay-timer-blinky.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//! Demonstrate the use of a blocking `Delay` using TIM2 general-purpose timer.

#![deny(unsafe_code)]
#![allow(clippy::empty_loop)]
#![no_main]
#![no_std]

// Halt on panic
use panic_halt as _; // panic handler

use cortex_m_rt::entry;
use stm32f1xx_hal as hal;

use crate::hal::{pac, prelude::*};

#[entry]
fn main() -> ! {
if let (Some(dp), Some(_cp)) = (
pac::Peripherals::take(),
cortex_m::peripheral::Peripherals::take(),
) {
let mut flash = dp.FLASH.constrain();
// Set up the LED. On the BluePill it's connected to pin PC13.
let mut gpioc = dp.GPIOC.split();
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);

// Set up the system clock. We want to run at 48MHz for this one.
let rcc = dp.RCC.constrain();
let clocks = rcc
.cfgr
.use_hse(8.MHz())
.sysclk(48.MHz())
.freeze(&mut flash.acr);

// Create a delay abstraction based on general-pupose 32-bit timer TIM2

//let mut delay = hal::timer::FTimerUs::new(dp.TIM2, &clocks).delay();
// or
let mut delay = dp.TIM2.delay_us(&clocks);

loop {
// On for 1s, off for 3s.
led.set_high();
// Use `embedded_hal::DelayMs` trait
delay.delay_ms(1000_u32);
led.set_low();
// or use `fugit` duration units
delay.delay(3.secs());
}
}

loop {}
}
10 changes: 7 additions & 3 deletions examples/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use panic_halt as _;

use cortex_m_rt::entry;
use stm32f1xx_hal::{delay::Delay, pac, prelude::*};
use stm32f1xx_hal::{pac, prelude::*};

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

let mut delay = Delay::new(cp.SYST, &clocks);
//let mut delay = hal::timer::Timer::syst(cp.SYST, &clocks).delay();
// or
let mut delay = cp.SYST.delay(&clocks);

loop {
led.set_high();
// Use `embedded_hal::DelayMs` trait
delay.delay_ms(1_000_u16);
led.set_low();
delay.delay_ms(1_000_u16);
// or use `fugit` duration units
delay.delay(1.secs());
}
}
5 changes: 3 additions & 2 deletions examples/dynamic_gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use nb::block;
use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;
use embedded_hal::digital::v2::{InputPin, OutputPin};
use stm32f1xx_hal::{pac, prelude::*, timer::Timer};
use stm32f1xx_hal::{pac, prelude::*};

#[entry]
fn main() -> ! {
Expand All @@ -32,7 +32,8 @@ fn main() -> ! {

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

// Wait for the timer to trigger an update and change the state of the LED
loop {
Expand Down
4 changes: 2 additions & 2 deletions examples/gpio_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#![no_main]
use cortex_m_rt::entry;
use panic_halt as _;
use stm32f1xx_hal::{delay::Delay, gpio::PinState, pac, prelude::*};
use stm32f1xx_hal::{gpio::PinState, pac, prelude::*};

#[entry]
fn main() -> ! {
Expand Down Expand Up @@ -53,7 +53,7 @@ fn main() -> ! {
// The key_up for check buttons if long press.
// if key_up is true, and buttons were not long press.
let mut key_up: bool = true;
let mut delay = Delay::new(cp.SYST, &clock);
let mut delay = cp.SYST.delay(&clock);
loop {
let key_result = (key_0.is_low(), key_1.is_low());
if key_up && (key_result.0 || key_result.1) {
Expand Down
3 changes: 2 additions & 1 deletion examples/multi_mode_gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ fn main() -> ! {

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

// Wait for the timer to trigger an update and change the state of the LED
loop {
Expand Down
11 changes: 7 additions & 4 deletions examples/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ use cortex_m_rt::entry;
use stm32f1xx_hal::{
pac,
prelude::*,
pwm::Channel,
time::ms,
timer::{Tim2NoRemap, Timer},
timer::{Channel, Tim2NoRemap},
};

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

let mut pwm =
Timer::new(p.TIM2, &clocks).pwm::<Tim2NoRemap, _, _>(pins, &mut afio.mapr, 1.kHz());
//let mut pwm =
// Timer::new(p.TIM2, &clocks).pwm_hz::<Tim2NoRemap, _, _>(pins, &mut afio.mapr, 1.kHz());
// or
let mut pwm = p
.TIM2
.pwm_hz::<Tim2NoRemap, _, _>(pins, &mut afio.mapr, 1.kHz(), &clocks);

// Enable clock on each of the channels
pwm.enable(Channel::C1);
Expand Down
2 changes: 1 addition & 1 deletion examples/pwm_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() -> ! {
let p0 = pb4.into_alternate_push_pull(&mut gpiob.crl);
let p1 = gpiob.pb5.into_alternate_push_pull(&mut gpiob.crl);

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

let max = pwm.get_max_duty();

Expand Down
6 changes: 5 additions & 1 deletion examples/pwm_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
use panic_halt as _;

use cortex_m_rt::entry;
use stm32f1xx_hal::{pac, prelude::*, pwm_input::*, timer::Timer};
use stm32f1xx_hal::{
pac,
prelude::*,
timer::{pwm_input::*, Timer},
};

#[entry]
fn main() -> ! {
Expand Down
4 changes: 2 additions & 2 deletions examples/qei.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use panic_semihosting as _;
use cortex_m_semihosting::hprintln;

use cortex_m_rt::entry;
use stm32f1xx_hal::{delay::Delay, pac, prelude::*, qei::QeiOptions, timer::Timer};
use stm32f1xx_hal::{pac, prelude::*, qei::QeiOptions, timer::Timer};

#[entry]
fn main() -> ! {
Expand Down Expand Up @@ -39,7 +39,7 @@ fn main() -> ! {
let c2 = gpiob.pb7;

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

loop {
let before = qei.count();
Expand Down
13 changes: 7 additions & 6 deletions examples/timer-interrupt-rtic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod app {
gpio::{gpioc::PC13, Output, PinState, PushPull},
pac,
prelude::*,
timer::{CountDownTimer, Event, Timer},
timer::{CounterMs, Event},
};

#[shared]
Expand All @@ -26,7 +26,7 @@ mod app {
#[local]
struct Local {
led: PC13<Output<PushPull>>,
timer_handler: CountDownTimer<pac::TIM1>,
timer_handler: CounterMs<pac::TIM1>,
}

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

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

if *cx.local.count == 4 {
// Changes timer update frequency
cx.local.timer_handler.start(2.Hz());
cx.local.timer_handler.start(500.millis()).unwrap();
} else if *cx.local.count == 12 {
cx.local.timer_handler.start(1.Hz());
cx.local.timer_handler.start(1.secs()).unwrap();
*cx.local.count = 0;
}

// Clears the update flag
cx.local.timer_handler.clear_update_interrupt_flag();
cx.local.timer_handler.clear_interrupt(Event::Update);
}
}
Loading