Skip to main content

ch32_hal/rcc/
mco.rs

1//! Microcontroller clock output (MCO).
2//!
3//! The MCO output is configured after [`crate::rcc::init`], because it borrows
4//! the pin that carries the signal. The driver writes `RCC_CFGR0.MCO` and
5//! switches the pin to alternate-function push-pull; it does not change the
6//! clock tree.
7//!
8//! Pin assignment differs across the series:
9//!
10//! | Family | Pin |
11//! | --- | --- |
12//! | CH32V1/V2/V3/V4, CH32L1 | PA8 |
13//! | CH32V0 | PC4 |
14//! | CH32X0, CH643 | PB9 |
15//! | CH641 | PB7 |
16//! | CH32H4 | PB0 (AF0) |
17//!
18//! The valid pins come from the ch32-data pin tables; the `McoPin` impls are
19//! generated by `build.rs`.
20
21use crate::gpio::{AfType, AnyPin, OutputType, Pin, Speed};
22// `McoSource` is the PAC `vals::Mco` enum once the pinned ch32-metapac carries
23// it for the active RCC version. rcc_v1 and rcc_v4x7 got the enum later than
24// the others, so until then build.rs leaves `ch32_mco_enum` unset and we use a
25// local `#[repr(u8)]` enum with the same variant names instead.
26#[cfg(ch32_mco_enum)]
27pub use crate::pac::rcc::vals::Mco as McoSource;
28use crate::pac::RCC;
29use crate::Peri;
30
31/// CH32V1/RCC v1 selects the source with a three-bit field.
32#[cfg(all(not(ch32_mco_enum), rcc_v1))]
33#[allow(non_camel_case_types)]
34#[derive(Clone, Copy, Debug, PartialEq, Eq)]
35#[repr(u8)]
36pub enum McoSource {
37    NO_CLK = 0b000,
38    SYSCLK = 0b100,
39    HSI = 0b101,
40    HSE = 0b110,
41    PLL_DIV2 = 0b111,
42}
43
44/// CH32V407/CH32V467 selects the source with a four-bit field.
45///
46/// RM ยง3.3.5.5: values `00xx` produce no clock; the remaining listed values
47/// select SYSCLK, HSI, HSE, UTMI, ETH_PLL/8 and XT1.
48#[cfg(all(not(ch32_mco_enum), rcc_v4x7))]
49#[allow(non_camel_case_types)]
50#[derive(Clone, Copy, Debug, PartialEq, Eq)]
51#[repr(u8)]
52pub enum McoSource {
53    NO_CLK = 0b0000,
54    SYSCLK = 0b0100,
55    HSI = 0b0101,
56    HSE = 0b0110,
57    UTMI = 0b0111,
58    ETH_PLL_DIV8 = 0b1000,
59    XT1 = 0b1010,
60}
61
62/// Pin that can carry the MCO signal.
63#[cfg(not(afio_h4))]
64pub trait McoPin: Pin {}
65
66/// Pin that can carry the MCO signal, including its CH32H4 AF number.
67#[cfg(afio_h4)]
68pub trait McoPin: Pin {
69    fn af_num(&self) -> u8;
70}
71
72// H4's MCO pin (PB0/AF0) is the one data entry that arrived after the pinned
73// PAC. `build.rs` emits `ch32_data_h4_mco_pin` and generates the impl from the
74// pin table once the data carries it; until then this fallback keeps PB0
75// usable.
76#[cfg(all(rcc_h4, not(ch32_data_h4_mco_pin)))]
77impl McoPin for crate::peripherals::PB0 {
78    fn af_num(&self) -> u8 {
79        0
80    }
81}
82
83/// MCO output driver.
84///
85/// Holds the pin for the lifetime of the output. Dropping it releases the pin
86/// but leaves `RCC_CFGR0.MCO` and the pin mode unchanged, matching the other
87/// clock outputs.
88pub struct Mco<'d> {
89    _pin: Peri<'d, AnyPin>,
90}
91
92impl<'d> Mco<'d> {
93    /// Select `source` on MCO and configure `pin` as alternate-function
94    /// push-pull.
95    pub fn new(pin: Peri<'d, impl McoPin>, source: McoSource) -> Self {
96        critical_section::with(|_| {
97            #[cfg(ch32_mco_enum)]
98            RCC.cfgr0().modify(|w| w.set_mco(source));
99            #[cfg(not(ch32_mco_enum))]
100            RCC.cfgr0().modify(|w| w.set_mco(source as u8));
101        });
102
103        pin.set_as_af(
104            // CH32H4 selects the function with the pin's AF number
105            // (`AFIO.GPIO_AFR`); every other family uses the legacy AF
106            // push-pull CNF and needs no number.
107            #[cfg(afio_h4)]
108            pin.af_num(),
109            AfType::output(OutputType::PushPull, Speed::High),
110        );
111        let pin: Peri<'d, AnyPin> = pin.into();
112
113        Self { _pin: pin }
114    }
115}