Skip to main content

ch32_hal/rcc/
mod.rs

1//! Reset and clock control (RCC).
2//!
3//! # API shape (all chip families)
4//!
5//! | Item | Role |
6//! |------|------|
7//! | [`Config`] | Family-specific clock setup (presets + optional fields) |
8//! | [`init`] | Apply `Config` to hardware and refresh the global cache |
9//! | [`refresh`] | Read RCC registers and refresh the cache (bootloader / dual-stage init) |
10//! | [`clocks`] | Cached AHB/APB frequencies for drivers |
11//! | [`HSI_FREQ`] / [`LSI_FREQ`] | Nominal RC oscillator rates |
12//! | [`Hse`] | External clock description when HSE is used |
13//!
14//! On CH32H4, [`clocks`] additionally reports the V3F/V5F core frequencies
15//! ([`Clocks::v3f`], [`Clocks::v5f`]); there is no separate `core_clocks()`.
16//!
17//! Pass the board HSE crystal frequency to [`refresh`] whenever SYSCLK can be sourced from HSE
18//! or PLL fed by HSE (same requirement as `HSE_VALUE` in the WCH C SDK).
19
20use crate::time::Hertz;
21
22mod shared;
23
24/// 时钟树与 CSDK 命名对照(阅读用,见文件内文档)。
25#[doc(hidden)]
26pub mod clock_tree_reference;
27
28pub mod mco;
29pub use mco::{Mco, McoPin, McoSource};
30
31#[cfg(ch32v003)]
32#[path = "v003.rs"]
33mod rcc_impl;
34
35#[cfg(all(any(ch32v0, ch32m0), not(ch32v003)))]
36#[path = "v00x.rs"]
37mod rcc_impl;
38
39#[cfg(any(ch32v1, ch32l1))]
40#[path = "v1.rs"]
41mod rcc_impl;
42
43#[cfg(any(ch32v2, ch32v3, ch32f2))]
44#[path = "v3.rs"]
45mod rcc_impl;
46
47#[cfg(any(ch32x0, ch643))]
48#[path = "x0.rs"]
49mod rcc_impl;
50
51#[cfg(ch641)]
52#[path = "ch641.rs"]
53mod rcc_impl;
54
55#[cfg(rcc_h4)]
56#[path = "h4/mod.rs"]
57mod rcc_impl;
58
59#[cfg(rcc_v4x7)]
60#[path = "v4x7.rs"]
61mod rcc_impl;
62
63pub use rcc_impl::*;
64
65/// Nominal HSI frequency for the selected chip.
66pub const HSI_FREQ: Hertz = HSI_FREQUENCY;
67
68#[cfg(rcc_h4)]
69const DEFAULT_FREQUENCY: Hertz = Hertz(25_000_000);
70#[cfg(rcc_v4x7)]
71const DEFAULT_FREQUENCY: Hertz = Hertz(20_000_000);
72#[cfg(all(any(ch32x0, ch643), not(rcc_h4)))]
73const DEFAULT_FREQUENCY: Hertz = Hertz(48_000_000);
74#[cfg(all(not(rcc_h4), not(any(ch32x0, ch643)), any(ch32v003, ch32v0, ch32m0, ch641)))]
75const DEFAULT_FREQUENCY: Hertz = Hertz(24_000_000);
76#[cfg(all(not(any(rcc_h4, rcc_v4x7)), not(any(ch32x0, ch643, ch32v003, ch32v0, ch32m0, ch641))))]
77const DEFAULT_FREQUENCY: Hertz = Hertz(8_000_000);
78
79static mut CLOCKS: Clocks = Clocks {
80    sysclk: DEFAULT_FREQUENCY,
81    hclk: DEFAULT_FREQUENCY,
82    pclk1: DEFAULT_FREQUENCY,
83    pclk2: DEFAULT_FREQUENCY,
84    pclk1_tim: DEFAULT_FREQUENCY,
85    pclk2_tim: DEFAULT_FREQUENCY,
86    #[cfg(rcc_h4)]
87    v3f: DEFAULT_FREQUENCY,
88    #[cfg(rcc_h4)]
89    v5f: DEFAULT_FREQUENCY,
90};
91
92#[derive(Copy, Clone, Eq, PartialEq, Debug)]
93pub struct Clocks {
94    pub sysclk: Hertz,
95    /// AHB clock (on H4: V3F core / HCLK domain after `FPRE`).
96    pub hclk: Hertz,
97    pub pclk1: Hertz,
98    pub pclk2: Hertz,
99    pub(crate) pclk1_tim: Hertz,
100    pub(crate) pclk2_tim: Hertz,
101    /// V3F (boot core, hart 0) core frequency. H4 only.
102    #[cfg(rcc_h4)]
103    pub v3f: Hertz,
104    /// V5F (second core, hart 1) core frequency. H4 only.
105    #[cfg(rcc_h4)]
106    pub v5f: Hertz,
107}
108
109#[inline]
110pub fn clocks() -> &'static Clocks {
111    unsafe { &CLOCKS }
112}
113
114/// External high-speed clock (HSE).
115#[derive(Clone, Copy, Debug, PartialEq, Eq)]
116pub struct Hse {
117    pub freq: Hertz,
118    pub mode: HseMode,
119}
120
121#[derive(Clone, Copy, Debug, PartialEq, Eq)]
122pub enum HseMode {
123    Oscillator,
124    Bypass,
125}
126
127#[cfg(not(ch32v208))]
128pub const LSI_FREQ: Hertz = Hertz(40_000);
129#[cfg(ch32v208)]
130pub const LSI_FREQ: Hertz = Hertz(32_768);
131
132#[allow(dead_code)]
133#[derive(Clone, Copy, PartialEq, Eq)]
134pub enum LseMode {
135    Oscillator,
136    Bypass,
137}
138
139pub struct LseConfig {
140    pub frequency: Hertz,
141    pub mode: LseMode,
142}
143
144pub enum RtcClockSource {
145    LSE,
146    LSI,
147    HSE,
148    DISABLE,
149}
150
151pub struct LsConfig {
152    pub rtc: RtcClockSource,
153    pub lsi: bool,
154    pub lse: Option<LseConfig>,
155}
156
157impl LsConfig {
158    pub const fn default_lse() -> Self {
159        Self {
160            rtc: RtcClockSource::LSE,
161            lse: Some(LseConfig {
162                frequency: Hertz(32_768),
163                mode: LseMode::Oscillator,
164            }),
165            lsi: false,
166        }
167    }
168
169    pub const fn default_lsi() -> Self {
170        Self {
171            rtc: RtcClockSource::LSI,
172            lsi: true,
173            lse: None,
174        }
175    }
176
177    pub const fn off() -> Self {
178        Self {
179            rtc: RtcClockSource::DISABLE,
180            lsi: false,
181            lse: None,
182        }
183    }
184}
185
186impl Default for LsConfig {
187    fn default() -> Self {
188        Self::default_lsi()
189    }
190}
191
192#[allow(unused)]
193impl LsConfig {
194    pub(crate) fn init(&self) -> Option<Hertz> {
195        todo!()
196    }
197}
198
199pub unsafe fn init(config: Config) {
200    rcc_impl::init(config);
201}
202
203/// Re-measure RCC and update [`clocks`] (on H4 that includes `v3f` / `v5f`).
204///
205/// Use after C `SystemInit`, a bootloader, or the other core has configured clocks.
206pub unsafe fn refresh(hse: Option<Hertz>) {
207    rcc_impl::refresh_clocks(hse);
208}
209
210pub(crate) use shared::{apb_timer_clk, set_clocks};