Skip to content

Commit 423bb0e

Browse files
committed
Use waiter-trait crate
1 parent 79bb4fd commit 423bb0e

File tree

15 files changed

+230
-289
lines changed

15 files changed

+230
-289
lines changed

.github/workflows/release.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ jobs:
2323
python-version: "3.13"
2424
- run: python scripts/sync_code.py --check
2525
- run: cargo build --release --features=stm32f100,xC
26-
- run: cargo build --release --features=stm32f103,xG,rtic
27-
- run: cargo clippy --features=stm32f103,xG,rtic
28-
- run: cargo test --target=x86_64-unknown-linux-gnu --features=std
29-
- run: python scripts/build.py --example=f103c8
26+
- run: python scripts/build.py build --release
27+
- run: python scripts/build.py clippy
28+
- run: python scripts/build.py test
29+
- run: python scripts/build.py build --example=f103c8
3030

3131
publish:
3232
needs: test

.vscode/tasks.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,45 @@
1010
"command": "python",
1111
"args": [
1212
"scripts/build.py",
13+
"check"
1314
],
1415
"group": {
1516
"kind": "build",
1617
"isDefault": true
1718
},
1819
},
1920
{
20-
"label": "build",
21+
"label": "build_example_f103c8",
2122
"type": "process",
2223
"command": "python",
2324
"args": [
2425
"scripts/build.py",
25-
"--release"
26+
"build",
27+
"-e=f103c8",
2628
],
2729
"group": "build",
2830
},
2931
{
30-
"label": "build_example_f103c8",
32+
"label": "test",
3133
"type": "process",
3234
"command": "python",
3335
"args": [
3436
"scripts/build.py",
35-
"-e",
36-
"f103c8"
37-
],
38-
"group": "build",
39-
},
40-
{
41-
"label": "Test Windows",
42-
"type": "cargo",
43-
"command": "test",
44-
"args": [
45-
"--features=std",
46-
"--target=x86_64-pc-windows-msvc",
37+
"test"
4738
],
4839
"group": {
4940
"kind": "test",
5041
"isDefault": true
5142
},
52-
}
43+
},
44+
{
45+
"label": "clippy",
46+
"type": "process",
47+
"command": "python",
48+
"args": [
49+
"scripts/build.py",
50+
"clippy"
51+
],
52+
},
5353
]
5454
}

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
name = "stm32f1-hal"
1010
readme = "README.md"
1111
repository = "https://github.com/jw-mcu-rust/stm32f1-hal"
12-
version = "0.0.5"
12+
version = "0.1.0"
1313

1414
[package.metadata.docs.rs]
1515
features = ["stm32f103", "xG"]
@@ -65,6 +65,7 @@
6565
rtic-monotonic = { version = "1.0", optional = true }
6666
rtrb = { version = "0.3", default-features = false }
6767
stm32f1 = { version = "0.16", optional = true }
68+
waiter-trait = "0.4"
6869

6970
[profile.release]
7071
codegen-units = 1

examples/f103c8/src/led_task.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
use crate::embedded_hal::digital::StatefulOutputPin;
2-
use crate::hal::os::TimeoutInstance;
1+
use crate::{embedded_hal::digital::StatefulOutputPin, waiter_trait::WaiterTime};
32

43
pub struct LedTask<P, T> {
54
led: P,
65
timeout: T,
76
}
87

9-
impl<P: StatefulOutputPin, T: TimeoutInstance> LedTask<P, T> {
8+
impl<P: StatefulOutputPin, T: WaiterTime> LedTask<P, T> {
109
pub fn new(led: P, timeout: T) -> Self {
1110
Self { led, timeout }
1211
}

examples/f103c8/src/main.rs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ use stm32f1_hal::{
1515
embedded_io,
1616
gpio::{Edge, ExtiPin, PinState},
1717
nvic_scb::PriorityGrouping,
18-
os::{RetryTimes, Timeout},
1918
pac::{self, Interrupt},
2019
prelude::*,
2120
rcc,
22-
timer::{syst::SysTickTimeout, *},
21+
time::MonoTimer,
22+
timer::*,
2323
uart::{self, UartPeriphExt},
24+
waiter_trait::{self, Counter, NonInterval, Waiter},
2425
};
2526

2627
mod led_task;
@@ -58,6 +59,10 @@ fn main() -> ! {
5859
exti: dp.EXTI,
5960
};
6061

62+
let mut sys_timer = cp.SYST.counter_hz(&mcu);
63+
sys_timer.start(20.Hz()).unwrap();
64+
let mono_timer = MonoTimer::new(cp.DWT, cp.DCB, &mcu.rcc.clocks);
65+
6166
// Keep them in one place for easier management
6267
mcu.nvic.enable(Interrupt::USART1, false); // Optional
6368
mcu.nvic.set_priority(Interrupt::USART1, 2);
@@ -88,7 +93,8 @@ fn main() -> ! {
8893
};
8994

9095
// let mut uart_task = uart_poll_init(uart_tx, uart_rx);
91-
// let mut uart_task = uart_interrupt_init(uart_tx, uart_rx, &all_it::USART1_CB, &mut mcu);
96+
// let mut uart_task =
97+
// uart_interrupt_init(uart_tx, uart_rx, &all_it::USART1_CB, &mut mcu, &sys_timer);
9298
dma1.4.set_priority(DmaPriority::Medium);
9399
dma1.5.set_priority(DmaPriority::Medium);
94100
let mut uart_task = uart_dma_init(
@@ -98,16 +104,17 @@ fn main() -> ! {
98104
uart_rx,
99105
dma1.5,
100106
&mut mcu,
107+
&sys_timer,
101108
);
102109

103110
// LED --------------------------------------
104111

105112
let mut led = gpiob
106113
.pb0
107114
.into_open_drain_output_with_state(&mut gpiob.crl, PinState::High);
108-
cp.SYST.counter_hz(&mcu).start(1000.Hz()).unwrap();
109-
let mut timeout = SysTickTimeout::new(1000_000);
110-
let mut led_task = LedTask::new(led, timeout.start());
115+
let mut water = sys_timer.waiter_us(1.secs(), NonInterval::default());
116+
// let water = mono_timer.waiter(1.secs());
117+
let mut led_task = LedTask::new(led, water.start());
111118

112119
// PWM --------------------------------------
113120

@@ -154,8 +161,8 @@ fn uart_poll_init<U: UartPeriphExt>(
154161
tx: uart::Tx<U>,
155162
rx: uart::Rx<U>,
156163
) -> UartPollTask<impl embedded_io::Write, impl embedded_io::Read> {
157-
let uart_rx = rx.into_poll(RetryTimes::new(0), RetryTimes::new(1_000));
158-
let uart_tx = tx.into_poll(RetryTimes::new(0), RetryTimes::new(10_000));
164+
let uart_rx = rx.into_poll(Counter::new(0), Counter::new(1_000));
165+
let uart_tx = tx.into_poll(Counter::new(0), Counter::new(10_000));
159166
UartPollTask::new(32, uart_tx, uart_rx)
160167
}
161168

@@ -164,10 +171,15 @@ fn uart_interrupt_init<U: UartPeriphExt + 'static>(
164171
rx: uart::Rx<U>,
165172
interrupt_callback: &hal::interrupt::Callback,
166173
mcu: &mut Mcu,
174+
timer: &SystemTimer,
167175
) -> UartPollTask<impl embedded_io::Write + 'static, impl embedded_io::Read + 'static> {
168-
let (rx, mut rx_it) = rx.into_interrupt(64, SysTickTimeout::new(100));
169-
let (tx, mut tx_it) =
170-
tx.into_interrupt(32, SysTickTimeout::new(0), SysTickTimeout::new(32 * 200));
176+
let (rx, mut rx_it) =
177+
rx.into_interrupt(64, timer.waiter_us(100.micros(), NonInterval::default()));
178+
let (tx, mut tx_it) = tx.into_interrupt(
179+
32,
180+
timer.waiter_us(0.micros(), NonInterval::default()),
181+
timer.waiter_us(32 * 200.micros(), NonInterval::default()),
182+
);
171183
interrupt_callback.set(mcu, move || {
172184
rx_it.handler();
173185
tx_it.handler();
@@ -182,14 +194,19 @@ fn uart_dma_init<'r, U: UartPeriphExt + 'static>(
182194
rx: uart::Rx<U>,
183195
dma_rx: impl DmaBindRx<U> + 'r,
184196
mcu: &mut Mcu,
197+
timer: &SystemTimer,
185198
) -> UartPollTask<impl embedded_io::Write + 'static, impl embedded_io::Read + 'r> {
186-
let uart_rx = rx.into_dma_circle(dma_rx, 64, SysTickTimeout::new(100));
199+
let uart_rx = rx.into_dma_circle(
200+
dma_rx,
201+
64,
202+
timer.waiter_us(100.micros(), NonInterval::default()),
203+
);
187204
dma_tx.set_interrupt(DmaEvent::TransferComplete, true);
188205
let (uart_tx, mut tx_it) = tx.into_dma_ringbuf(
189206
dma_tx,
190207
32,
191-
SysTickTimeout::new(0),
192-
SysTickTimeout::new(32 * 200),
208+
timer.waiter_us(0.micros(), NonInterval::default()),
209+
timer.waiter_us(32 * 200.micros(), NonInterval::default()),
193210
);
194211
interrupt_callback.set(mcu, move || {
195212
tx_it.interrupt_reload();

examples/f103c8/src/uart_task.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
use crate::embedded_io::{Read, Write};
2-
use crate::hal::ringbuf::*;
1+
use crate::{
2+
embedded_io::{Read, Write},
3+
hal::ringbuf::*,
4+
};
35

46
pub struct UartPollTask<W: Write, R: Read> {
57
tx: W,

scripts/build.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22
import os
3+
import platform
34
import subprocess
45

56
from base import green
@@ -13,26 +14,32 @@ def run_cmd(cmd: list[str]) -> None:
1314

1415
def main() -> int:
1516
parser = argparse.ArgumentParser()
17+
parser.add_argument("cmd", choices=["check", "test", "build", "clippy"])
1618
parser.add_argument("--release", action="store_true")
1719
parser.add_argument("--features", type=str, nargs="*")
1820
parser.add_argument("-e", "--examples", type=str, nargs="*")
1921
opts = parser.parse_args()
2022

2123
sync_all()
2224

23-
cmd = ["cargo"]
25+
cmd = ["cargo", opts.cmd]
2426

2527
if opts.examples:
2628
for e in opts.examples:
2729
os.chdir("examples/" + e)
28-
cmd.extend(["build", "--release"])
30+
cmd.append("--release")
2931
run_cmd(cmd)
3032
os.chdir("../../")
33+
elif opts.cmd == "test":
34+
cmd.append("--features=std")
35+
if platform.system().lower() == "windows":
36+
cmd.append("--target=x86_64-pc-windows-msvc")
37+
else:
38+
cmd.append("--target=x86_64-unknown-linux-gnu")
39+
run_cmd(cmd)
3140
else:
3241
if opts.release:
33-
cmd.extend(["build", "--release"])
34-
else:
35-
cmd.append("check")
42+
cmd.append("--release")
3643

3744
if opts.features is None:
3845
cmd.append(f"--features=stm32f103,xG,rtic")

src/common/os.rs

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -8,86 +8,4 @@ cfg_if::cfg_if! {
88
}
99
}
1010

11-
#[inline(always)]
12-
pub fn yield_cpu() {
13-
// TODO yield
14-
}
15-
16-
pub trait Timeout {
17-
fn start(&mut self) -> impl TimeoutInstance;
18-
}
19-
20-
pub trait TimeoutInstance {
21-
fn timeout(&mut self) -> bool;
22-
fn restart(&mut self);
23-
fn interval(&self);
24-
}
25-
26-
// Retry ----------------------------------
27-
28-
pub struct RetryTimes {
29-
retry_times: usize,
30-
}
31-
impl RetryTimes {
32-
pub fn new(retry_times: usize) -> Self {
33-
Self { retry_times }
34-
}
35-
}
36-
impl Timeout for RetryTimes {
37-
#[inline]
38-
fn start(&mut self) -> impl TimeoutInstance {
39-
RetryTimesInstance {
40-
count: 0,
41-
retry_times: self.retry_times,
42-
}
43-
}
44-
}
45-
46-
pub struct RetryTimesInstance {
47-
count: usize,
48-
retry_times: usize,
49-
}
50-
impl TimeoutInstance for RetryTimesInstance {
51-
#[inline]
52-
fn timeout(&mut self) -> bool {
53-
if self.count <= self.retry_times {
54-
self.count = self.count.wrapping_add(1);
55-
false
56-
} else {
57-
true
58-
}
59-
}
60-
61-
#[inline(always)]
62-
fn restart(&mut self) {
63-
self.count = 0;
64-
}
65-
66-
#[inline(always)]
67-
fn interval(&self) {}
68-
}
69-
70-
// Always ----------------------------------
71-
72-
#[derive(Default)]
73-
pub struct AlwaysTimeout {}
74-
impl Timeout for AlwaysTimeout {
75-
#[inline]
76-
fn start(&mut self) -> impl TimeoutInstance {
77-
AlwaysTimeoutInstance {}
78-
}
79-
}
80-
81-
pub struct AlwaysTimeoutInstance {}
82-
impl TimeoutInstance for AlwaysTimeoutInstance {
83-
#[inline(always)]
84-
fn timeout(&mut self) -> bool {
85-
true
86-
}
87-
88-
#[inline(always)]
89-
fn restart(&mut self) {}
90-
91-
#[inline(always)]
92-
fn interval(&self) {}
93-
}
11+
pub use waiter_trait::{Waiter, WaiterTime};

0 commit comments

Comments
 (0)