Skip to content

Commit a822b25

Browse files
authored
Merge pull request #8 from unresc/use_cortex_m_delay
Use Delay implementation from cortex-m crate.
2 parents 6f45bf6 + 2383283 commit a822b25

File tree

3 files changed

+19
-80
lines changed

3 files changed

+19
-80
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ repository = "https://github.com/stm32-rs/stm32g4xx-hal"
1212
version = "0.0.0"
1313

1414
[dependencies]
15-
cortex-m = "0.6.1"
15+
cortex-m = "0.7.1"
1616
nb = "0.1.1"
1717
stm32g4 = "0.9.0"
1818

src/delay.rs

Lines changed: 17 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,29 @@
1-
//! Delays
2-
use core::cmp;
3-
use cortex_m::peripheral::SYST;
4-
use hal::blocking::delay::{DelayMs, DelayUs};
5-
6-
use crate::prelude::*;
71
use crate::rcc::Clocks;
8-
use crate::time::{Hertz, MicroSecond};
9-
10-
/// System timer (SysTick) as a delay provider
11-
pub struct Delay {
12-
clk: Hertz,
13-
syst: SYST,
14-
}
15-
16-
impl Delay {
17-
/// Configures the system timer (SysTick) as a delay provider
18-
pub fn new(syst: SYST, clocks: &Clocks) -> Self {
19-
Delay {
20-
syst,
21-
clk: clocks.ahb_clk / 8,
22-
}
23-
}
24-
25-
pub fn delay<T>(&mut self, delay: T)
26-
where
27-
T: Into<MicroSecond>,
28-
{
29-
let mut cycles = delay.into().cycles(self.clk);
30-
while cycles > 0 {
31-
let reload = cmp::min(cycles, 0x00ff_ffff);
32-
cycles -= reload;
33-
self.syst.set_reload(reload);
34-
self.syst.clear_current();
35-
self.syst.enable_counter();
36-
while !self.syst.has_wrapped() {}
37-
self.syst.disable_counter();
38-
}
39-
}
40-
41-
/// Releases the system timer (SysTick) resource
42-
pub fn release(self) -> SYST {
43-
self.syst
44-
}
45-
}
46-
47-
impl DelayUs<u32> for Delay {
48-
fn delay_us(&mut self, us: u32) {
49-
self.delay(us.us())
50-
}
51-
}
52-
53-
impl DelayUs<u16> for Delay {
54-
fn delay_us(&mut self, us: u16) {
55-
self.delay_us(us as u32)
56-
}
57-
}
58-
59-
impl DelayUs<u8> for Delay {
60-
fn delay_us(&mut self, us: u8) {
61-
self.delay_us(us as u32)
62-
}
63-
}
2+
use crate::time::MicroSecond;
3+
pub use cortex_m::delay::*;
4+
use cortex_m::{peripheral::SYST, prelude::_embedded_hal_blocking_delay_DelayUs};
645

65-
impl DelayMs<u32> for Delay {
66-
fn delay_ms(&mut self, ms: u32) {
67-
self.delay_us(ms.saturating_mul(1_000));
68-
}
69-
}
70-
71-
impl DelayMs<u16> for Delay {
72-
fn delay_ms(&mut self, ms: u16) {
73-
self.delay_ms(ms as u32);
74-
}
6+
pub trait SYSTDelayExt {
7+
fn delay(self, clocks: &Clocks) -> Delay;
758
}
769

77-
impl DelayMs<u8> for Delay {
78-
fn delay_ms(&mut self, ms: u8) {
79-
self.delay_ms(ms as u32);
10+
impl SYSTDelayExt for SYST {
11+
fn delay(self, clocks: &Clocks) -> Delay {
12+
Delay::new(self, clocks.ahb_clk.0)
8013
}
8114
}
8215

8316
pub trait DelayExt {
84-
fn delay(self, clocks: &Clocks) -> Delay;
17+
fn delay<T>(&mut self, delay: T)
18+
where
19+
T: Into<MicroSecond>;
8520
}
8621

87-
impl DelayExt for SYST {
88-
fn delay(self, clocks: &Clocks) -> Delay {
89-
Delay::new(self, clocks)
22+
impl DelayExt for Delay {
23+
fn delay<T>(&mut self, delay: T)
24+
where
25+
T: Into<MicroSecond>,
26+
{
27+
self.delay_us(delay.into().0)
9028
}
9129
}

src/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub use hal::watchdog::WatchdogEnable as _;
1717
// pub use crate::comparator::ComparatorExt as _;
1818
// pub use crate::crc::CrcExt as _;
1919
pub use crate::delay::DelayExt as _;
20+
pub use crate::delay::SYSTDelayExt as _;
2021
// pub use crate::dma::CopyDma as _;
2122
// pub use crate::dma::ReadDma as _;
2223
// pub use crate::dma::WriteDma as _;

0 commit comments

Comments
 (0)