Skip to content

Commit aa5282b

Browse files
Merge #310
310: impl `Countdown` r=richardeoin a=andrewgazelka Implements a polling version of delay (`CountDown`) in accordance to `embedded_hal` Co-authored-by: Andrew Gazelka <andrew.gazelka@gmail.com>
2 parents f90cf59 + 91ebdbe commit aa5282b

File tree

5 files changed

+100
-16
lines changed

5 files changed

+100
-16
lines changed

.github/bors.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ block_labels = ["wip"]
22
delete_merged_branches = true
33
status = [
44
"Rustfmt",
5-
"ci (1.56.1, stm32h743)",
6-
"ci (1.56.1, stm32h753)",
7-
"ci (1.56.1, stm32h743v)",
8-
"ci (1.56.1, stm32h753v)",
9-
"ci (1.56.1, stm32h747cm7)",
10-
"ci (1.56.1, stm32h7b3)",
11-
"ci (1.56.1, stm32h7b0)",
12-
"ci (1.56.1, stm32h735)",
5+
"ci (1.57.0, stm32h743)",
6+
"ci (1.57.0, stm32h753)",
7+
"ci (1.57.0, stm32h743v)",
8+
"ci (1.57.0, stm32h753v)",
9+
"ci (1.57.0, stm32h747cm7)",
10+
"ci (1.57.0, stm32h7b3)",
11+
"ci (1.57.0, stm32h7b0)",
12+
"ci (1.57.0, stm32h735)",
1313
"ci (stable, stm32h743)",
1414
"ci (stable, stm32h753)",
1515
"ci (stable, stm32h743v)",

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
strategy:
1212
matrix: # All permutations of {rust, mcu}
1313
rust:
14-
- 1.56.1 # MSRV
14+
- 1.57.0 # MSRV
1515
- stable
1616
mcu:
1717
- stm32h743

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ features = ["stm32h743v", "rt", "xspi", "sdmmc", "fmc", "usb_hs", "rtc", "ethern
2525
targets = ["thumbv7em-none-eabihf"]
2626

2727
[dependencies]
28+
fugit = "0.3.3"
2829
embedded-hal = "0.2.6"
2930
embedded-dma = "0.1.2"
3031
cortex-m = "^0.7.4"

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ stm32h7xx-hal
77
[![Bors enabled](https://bors.tech/images/badge_small.svg)](https://app.bors.tech/repositories/12691)
88
[![CI](https://github.com/stm32-rs/stm32h7xx-hal/workflows/Continuous%20integration/badge.svg)](https://github.com/stm32-rs/stm32h7xx-hal/actions)
99
[![Crates.io](https://img.shields.io/crates/v/stm32h7xx-hal.svg)](https://crates.io/crates/stm32h7xx-hal)
10-
![Minimum rustc version](https://img.shields.io/badge/rustc-1.56.1+-yellow.svg)
10+
![Minimum rustc version](https://img.shields.io/badge/rustc-1.57.0+-yellow.svg)
1111

1212
[_stm32h7xx-hal_](https://github.com/stm32-rs/stm32h7xx-hal) contains
1313
a hardware abstraction layer on top of the peripheral access API for
@@ -111,7 +111,7 @@ programming interfaces are only available on the high density connectors.
111111
Minimum supported Rust version
112112
------------------------------
113113

114-
The Minimum Supported Rust Version (MSRV) at the moment is **1.56.1**. Older
114+
The Minimum Supported Rust Version (MSRV) at the moment is **1.57.0**. Older
115115
versions **may** compile, especially when some features are not used
116116
in your application.
117117

src/delay.rs

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@
3737
use cast::u32;
3838
use cortex_m::peripheral::syst::SystClkSource;
3939
use cortex_m::peripheral::SYST;
40-
41-
use crate::nb::block;
42-
use crate::rcc::CoreClocks;
43-
use crate::time::{Hertz, U32Ext};
4440
use embedded_hal::{
4541
blocking::delay::{DelayMs, DelayUs},
4642
timer::CountDown,
4743
};
44+
use void::Void;
45+
46+
use crate::nb::block;
47+
use crate::rcc::CoreClocks;
48+
use crate::time::{Hertz, U32Ext};
4849

4950
pub trait DelayExt {
5051
fn delay(self, clocks: CoreClocks) -> Delay;
@@ -62,6 +63,88 @@ pub struct Delay {
6263
syst: SYST,
6364
}
6465

66+
/// Implements [CountDown](embedded_hal::timer::CountDown) for the System timer (SysTick).
67+
pub struct Countdown<'a> {
68+
clocks: CoreClocks,
69+
syst: &'a mut SYST,
70+
total_rvr: u64,
71+
finished: bool,
72+
}
73+
74+
impl<'a> Countdown<'a> {
75+
/// Create a new [CountDown] measured in microseconds.
76+
pub fn new(syst: &'a mut SYST, clocks: CoreClocks) -> Self {
77+
Self {
78+
syst,
79+
clocks,
80+
total_rvr: 0,
81+
finished: true,
82+
}
83+
}
84+
85+
/// start a wait cycle and sets finished to true if [CountdownUs] is done waiting.
86+
fn start_wait(&mut self) {
87+
// The SysTick Reload Value register supports values between 1 and 0x00FFFFFF.
88+
const MAX_RVR: u32 = 0x00FF_FFFF;
89+
90+
if self.total_rvr != 0 {
91+
self.finished = false;
92+
let current_rvr = if self.total_rvr <= MAX_RVR.into() {
93+
self.total_rvr as u32
94+
} else {
95+
MAX_RVR
96+
};
97+
98+
self.syst.set_reload(current_rvr);
99+
self.syst.clear_current();
100+
self.syst.enable_counter();
101+
102+
self.total_rvr -= current_rvr as u64;
103+
} else {
104+
self.finished = true;
105+
}
106+
}
107+
}
108+
109+
impl<'a> CountDown for Countdown<'a> {
110+
type Time = fugit::MicrosDurationU32;
111+
112+
fn start<T>(&mut self, count: T)
113+
where
114+
T: Into<Self::Time>,
115+
{
116+
let us = count.into().ticks();
117+
118+
// With c_ck up to 480e6, we need u64 for delays > 8.9s
119+
120+
self.total_rvr = if cfg!(not(feature = "revision_v")) {
121+
// See errata ES0392 §2.2.3. Revision Y does not have the /8 divider
122+
u64::from(us) * u64::from(self.clocks.c_ck().0 / 1_000_000)
123+
} else if cfg!(feature = "cm4") {
124+
// CM4 dervived from HCLK
125+
u64::from(us) * u64::from(self.clocks.hclk().0 / 8_000_000)
126+
} else {
127+
// Normally divide by 8
128+
u64::from(us) * u64::from(self.clocks.c_ck().0 / 8_000_000)
129+
};
130+
131+
self.start_wait();
132+
}
133+
134+
fn wait(&mut self) -> nb::Result<(), Void> {
135+
if self.finished {
136+
return Ok(());
137+
}
138+
139+
if self.syst.has_wrapped() {
140+
self.syst.disable_counter();
141+
self.start_wait();
142+
}
143+
144+
Err(nb::Error::WouldBlock)
145+
}
146+
}
147+
65148
impl Delay {
66149
/// Configures the system timer (SysTick) as a delay provider
67150
pub fn new(mut syst: SYST, clocks: CoreClocks) -> Self {
@@ -105,7 +188,7 @@ impl DelayUs<u32> for Delay {
105188
// See errata ES0392 §2.2.3. Revision Y does not have the /8 divider
106189
u64::from(us) * u64::from(self.clocks.c_ck().0 / 1_000_000)
107190
} else if cfg!(feature = "cm4") {
108-
// CM4 dervived from HCLK
191+
// CM4 derived from HCLK
109192
u64::from(us) * u64::from(self.clocks.hclk().0 / 8_000_000)
110193
} else {
111194
// Normally divide by 8

0 commit comments

Comments
 (0)