Skip to content

Commit a079511

Browse files
committed
DelayNs
1 parent 4f46907 commit a079511

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ stm32f1 = "0.15.1"
2525
embedded-dma = "0.2.0"
2626
bxcan = "0.7"
2727
void = { default-features = false, version = "1.0.2" }
28-
fugit = "0.3.6"
28+
fugit = "0.3.7"
2929
fugit-timer = "0.1.3"
3030
rtic-monotonic = { version = "1.0", optional = true }
3131
bitflags = "1.3.2"

src/timer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pub mod pwm;
7373
pub use pwm::*;
7474

7575
mod hal_02;
76+
mod hal_1;
7677

7778
/// Timer wrapper
7879
pub struct Timer<TIM> {

src/timer/hal_1.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//! Delay implementation based on general-purpose 32 bit timers and System timer (SysTick).
2+
//!
3+
//! TIM2 and TIM5 are a general purpose 32-bit auto-reload up/downcounter with
4+
//! a 16-bit prescaler.
5+
6+
use core::convert::Infallible;
7+
use embedded_hal::delay::DelayNs;
8+
9+
use super::{Delay, Instance, PwmChannel, SysDelay, WithPwm};
10+
use fugit::ExtU32Ceil;
11+
12+
impl DelayNs for SysDelay {
13+
fn delay_ns(&mut self, ns: u32) {
14+
self.delay(ns.nanos_at_least());
15+
}
16+
17+
fn delay_ms(&mut self, ms: u32) {
18+
self.delay(ms.millis_at_least());
19+
}
20+
}
21+
22+
impl<TIM: Instance, const FREQ: u32> DelayNs for Delay<TIM, FREQ> {
23+
fn delay_ns(&mut self, ns: u32) {
24+
self.delay(ns.micros_at_least());
25+
}
26+
27+
fn delay_us(&mut self, us: u32) {
28+
self.delay(us.micros_at_least());
29+
}
30+
31+
fn delay_ms(&mut self, ms: u32) {
32+
self.delay(ms.millis_at_least());
33+
}
34+
}
35+
36+
impl<TIM: Instance + WithPwm, const C: u8> embedded_hal::pwm::ErrorType for PwmChannel<TIM, C> {
37+
type Error = Infallible;
38+
}
39+
40+
impl<TIM: Instance + WithPwm, const C: u8> embedded_hal::pwm::SetDutyCycle for PwmChannel<TIM, C> {
41+
fn max_duty_cycle(&self) -> u16 {
42+
self.get_max_duty()
43+
}
44+
fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> {
45+
self.set_duty(duty);
46+
Ok(())
47+
}
48+
}

0 commit comments

Comments
 (0)