ch32_hal/embassy/mod.rs
1///! The time driver for Embassy framework.
2///
3/// This module provides the time driver for the Embassy framework.
4
5// CH32H4's SysTick is two 32-bit counters, so it has its own driver.
6// `time_driver_systick.rs` is the 64-bit counter used by the other
7// `qingke_v4` families.
8#[cfg(all(qingke_v4, not(time_driver_timer), not(ch32h4)))]
9#[path = "time_driver_systick.rs"]
10pub mod time_driver_impl;
11
12#[cfg(all(systick_v4x7, not(time_driver_timer)))]
13#[path = "time_driver_systick_32bit.rs"]
14pub mod time_driver_impl;
15
16// Hart 0 only. Hart 1's counter interrupts through `SysTick1` (vector 13),
17// which `qingke-rt` does not expose, so a V5F image still selects
18// `time-driver-timN`.
19#[cfg(all(systick_v3f_v5f, not(time_driver_timer), not(qingke_v5f)))]
20#[path = "time_driver_systick_h4.rs"]
21pub mod time_driver_impl;
22
23#[cfg(time_driver_timer)]
24#[path = "time_driver_tim.rs"]
25pub mod time_driver_impl;
26
27/// Initialize the Embassy time driver.
28///
29/// System global clocks must be initialized before calling this function.
30///
31/// # Safety
32///
33/// This function should be called only once.
34///
35/// # Implementation Notes
36///
37/// The WCH QingKe RISC-V core deviates from standard RISC-V specification:
38/// - `WFI` instruction will not wake up from disabled interrupts
39/// - Either `WFITOWFE` or `SEVONPEND` must be enabled for proper wake-up behavior
40pub unsafe fn init() {
41 #[cfg(feature = "rt-wfi")]
42 crate::pac::PFIC.sctlr().modify(|w| w.set_sevonpend(true));
43
44 #[cfg(any(
45 time_driver_timer,
46 systick_v4x7,
47 all(qingke_v4, not(ch32h4)),
48 all(systick_v3f_v5f, not(qingke_v5f)),
49 ))]
50 critical_section::with(|cs| time_driver_impl::init(cs));
51}