sifli_hal/rcc/
mod.rs

1use critical_section::CriticalSection;
2
3mod clock;
4pub use clock::*;
5
6mod clock_configure;
7pub use clock_configure::*;
8
9use crate::time::Hertz;
10
11// TODO: should we split this into `RccEnable` and `RccReset` ?
12pub(crate) trait SealedRccEnableReset {
13    fn rcc_enable() {}
14
15    fn rcc_disable() {}
16
17    fn rcc_reset() {}
18}
19#[allow(private_bounds)]
20pub trait RccEnableReset: SealedRccEnableReset + 'static {}
21
22pub(crate) trait SealedRccGetFreq {
23    /// Get peripheral frequency
24    /// Returns `None` if clock is disabled
25    fn get_freq() -> Option<Hertz>;
26}
27
28#[allow(private_bounds)]
29pub trait RccGetFreq: SealedRccGetFreq + 'static {
30    /// Get peripheral frequency
31    /// Returns `None` if clock is disabled
32    fn frequency() -> Option<Hertz> {
33        Self::get_freq()
34    }
35}
36
37/// Enables peripheral `T`.
38///
39/// # Safety
40///
41/// Peripheral must not be in use.
42// TODO: should this be `unsafe`?
43pub fn enable_with_cs<T: RccEnableReset>(_cs: CriticalSection) {
44    T::rcc_enable();
45}
46
47/// Enables peripheral `T`.
48///
49/// # Safety
50///
51/// Peripheral must not be in use.
52// TODO: should this be `unsafe`?
53pub fn enable<T: RccEnableReset>() {
54    critical_section::with(|cs| enable_with_cs::<T>(cs));
55}
56
57/// Enables and resets peripheral `T`.
58///
59/// # Safety
60///
61/// Peripheral must not be in use.
62// TODO: should this be `unsafe`?
63pub fn enable_and_reset_with_cs<T: RccEnableReset>(_cs: CriticalSection) {
64    T::rcc_enable();
65    T::rcc_reset();
66}
67
68
69/// Enables and resets peripheral `T`.
70///
71/// # Safety
72///
73/// Peripheral must not be in use.
74// TODO: should this be `unsafe`?
75pub fn enable_and_reset<T: RccEnableReset>() {
76    critical_section::with(|cs| enable_and_reset_with_cs::<T>(cs));
77}
78
79/// Disables peripheral `T`.
80///
81/// # Safety
82///
83/// Peripheral must not be in use.
84// TODO: should this be `unsafe`?
85pub fn disable_with_cs<T: RccEnableReset>(_cs: CriticalSection) {
86    T::rcc_disable();
87}
88
89/// Disables peripheral `T`.
90///
91/// # Safety
92///
93/// Peripheral must not be in use.
94// TODO: should this be `unsafe`?
95pub fn disable<T: RccEnableReset>() {
96    critical_section::with(|cs| disable_with_cs::<T>(cs));
97}