Skip to content

Commit a2bc3c7

Browse files
committed
Dummy RCC
1 parent dcb1590 commit a2bc3c7

File tree

2 files changed

+117
-7
lines changed

2 files changed

+117
-7
lines changed

examples/stm32f4/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ license = "MIT OR Apache-2.0"
66

77
[dependencies]
88
# Change stm32f429zi to your chip name, if necessary.
9-
embassy-stm32 = { version = "0.2.0", path = "../../embassy-stm32", features = ["stm32f401re", "unstable-pac", "memory-x", "time-driver-tim4", "exti", "chrono", "rt"], default-features = false }
9+
embassy-stm32 = { version = "0.2.0", path = "../../embassy-stm32", features = ["stm32f401re", "unstable-pac", "memory-x", "exti", "chrono", "rt"], default-features = false }
1010
embassy-sync = { version = "0.7.0", path = "../../embassy-sync", features = [] }
1111
embassy-executor = { version = "0.7.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "executor-interrupt"] }
12-
embassy-time = { version = "0.4.0", path = "../../embassy-time", features = ["defmt-timestamp-uptime", "tick-hz-32_768"] }
12+
embassy-time = { version = "0.4.0", path = "../../embassy-time", features = ["defmt-timestamp-uptime", "tick-hz-1_000"] }
1313
embassy-usb = { version = "0.4.0", path = "../../embassy-usb", features = [] }
1414
embassy-net = { version = "0.7.0", path = "../../embassy-net", features = ["tcp", "dhcpv4", "medium-ethernet", ] }
1515
embassy-net-wiznet = { version = "0.2.0", path = "../../embassy-net-wiznet", features = ["defmt"] }
1616
embassy-futures = { version = "0.1.0", path = "../../embassy-futures" }
17-
17+
embassy-time-queue-utils = { version = "0.1.0", path = "../../embassy-time-queue-utils" }
18+
embassy-time-driver = { version = "0.2.0", path = "../../embassy-time-driver" }
1819
panic-halt = "1.0"
1920

2021
cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] }

examples/stm32f4/src/bin/blinky.rs

Lines changed: 113 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,131 @@
11
#![no_std]
22
#![no_main]
33

4+
use core::cell::RefCell;
5+
use core::sync::atomic::{AtomicU32, Ordering};
6+
use core::task::Waker;
7+
use cortex_m::peripheral::{syst::SystClkSource, SYST};
48
use embassy_executor::Spawner;
59
use embassy_stm32::gpio::{Level, Output, Speed};
6-
use embassy_time::Timer;
10+
use embassy_stm32::pac::{self};
11+
use embassy_stm32::peripherals::PB7;
12+
use embassy_time::{Timer};
713
use panic_halt as _;
814

15+
use critical_section::{CriticalSection, Mutex};
16+
use embassy_time_driver::Driver;
17+
use embassy_time_queue_utils::Queue;
18+
19+
/*
20+
use stm32f4xx_hal::{
21+
prelude::*,
22+
};
23+
24+
*/
25+
26+
struct MyDriver {
27+
ticks: AtomicU32,
28+
queue: Mutex<RefCell<Queue>>,
29+
next: AtomicU32,
30+
}
31+
32+
impl MyDriver {
33+
const fn new() -> Self {
34+
Self {
35+
ticks: AtomicU32::new(0),
36+
queue: Mutex::new(RefCell::new(Queue::new())),
37+
next: AtomicU32::new(0),
38+
}
39+
}
40+
41+
/// Called from SysTick interrupt to increment ticks and process wakeups
42+
fn on_tick(&self) {
43+
let ticks = self.ticks.fetch_add(1, Ordering::Relaxed) + 1;
44+
let next = self.next.load(Ordering::Relaxed);
45+
if ticks >= next {
46+
critical_section::with(|cs| {
47+
let now = self.now();
48+
let mut queue = self.queue.borrow(cs).borrow_mut();
49+
self.next.store(queue.next_expiration(now) as u32, Ordering::Relaxed);
50+
});
51+
}
52+
}
53+
54+
fn set_alarm(&self, cs: &CriticalSection, at: u64) -> bool {
55+
self.next.store(at as u32, Ordering::Relaxed);
56+
true
57+
}
58+
}
59+
60+
impl Driver for MyDriver {
61+
fn now(&self) -> u64 {
62+
self.ticks.load(Ordering::Relaxed) as u64
63+
}
64+
65+
fn schedule_wake(&self, at: u64, waker: &Waker) {
66+
critical_section::with(|cs| {
67+
let mut queue = self.queue.borrow(cs).borrow_mut();
68+
if queue.schedule_wake(at, waker) {
69+
let mut next = queue.next_expiration(self.now());
70+
while !self.set_alarm(&cs, next) {
71+
next = queue.next_expiration(self.now());
72+
}
73+
}
74+
});
75+
}
76+
}
77+
78+
embassy_time_driver::time_driver_impl!(static DRIVER: MyDriver = MyDriver::new());
79+
980
#[embassy_executor::main]
1081
async fn main(_spawner: Spawner) {
11-
let p = embassy_stm32::init(Default::default());
82+
//let dp = stm32f4xx_hal::pac::Peripherals::take().unwrap();
83+
84+
let cp = cortex_m::Peripherals::take().unwrap();
85+
86+
let clocks = 48_000_000; // setup_clocks(dp.RCC);
87+
88+
// Set PB7 to push-pull output
89+
pac::GPIOB
90+
.moder()
91+
.modify(|w| w.set_moder(7, pac::gpio::vals::Moder::OUTPUT));
92+
pac::GPIOB
93+
.otyper()
94+
.modify(|w| w.set_ot(7, pac::gpio::vals::Ot::PUSH_PULL));
95+
pac::GPIOB
96+
.ospeedr()
97+
.modify(|w| w.set_ospeedr(7, pac::gpio::vals::Ospeedr::HIGH_SPEED));
98+
pac::GPIOB
99+
.pupdr()
100+
.modify(|w| w.set_pupdr(7, pac::gpio::vals::Pupdr::FLOATING));
12101

13-
let mut led = Output::new(p.PB7, Level::High, Speed::Low);
102+
// SAFETY: PB7 is not used anywhere else
103+
let led = unsafe { Output::new(PB7::steal(), Level::High, Speed::Low) };
14104

105+
// Setup SysTick for 1 kHz ticks (1ms)
106+
let mut syst = cp.SYST;
107+
syst.set_clock_source(SystClkSource::Core);
108+
syst.set_reload(clocks / 1000 - 1);
109+
syst.clear_current();
110+
syst.enable_counter();
111+
syst.enable_interrupt();
112+
113+
blink_loop(led).await;
114+
}
115+
116+
async fn blink_loop(mut led: Output<'static>) {
15117
loop {
16118
led.set_high();
17119
Timer::after_millis(300).await;
18-
19120
led.set_low();
20121
Timer::after_millis(300).await;
21122
}
22123
}
124+
125+
#[cortex_m_rt::exception]
126+
fn SysTick() {
127+
critical_section::with(|cs| {
128+
// Call the driver's tick handler
129+
DRIVER.on_tick();
130+
});
131+
}

0 commit comments

Comments
 (0)