rtc_hal/
square_wave.rs

1//! Traits for Square Wave control
2
3use crate::rtc::Rtc;
4
5/// Square wave output frequencies
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum SquareWaveFreq {
8    /// 1 Hz
9    Hz1,
10    /// 1024 Hz (1.024 kHz)
11    Hz1024,
12    /// 4096 Hz (4.096 kHz)
13    Hz4096,
14    /// 8192 Hz (8.192 kHz)
15    Hz8192,
16    /// 32768 Hz (32.768 kHz)
17    Hz32768,
18    /// Custom frequency (if supported by device)
19    Custom(u32),
20}
21
22impl SquareWaveFreq {
23    /// Get frequency value in Hz
24    pub fn to_hz(&self) -> u32 {
25        match self {
26            Self::Hz1 => 1,
27            Self::Hz1024 => 1024,
28            Self::Hz4096 => 4096,
29            Self::Hz8192 => 8192,
30            Self::Hz32768 => 32768,
31            Self::Custom(freq) => *freq,
32        }
33    }
34
35    /// Create from Hz value
36    pub fn from_hz(hz: u32) -> Self {
37        match hz {
38            1 => Self::Hz1,
39            1024 => Self::Hz1024,
40            4096 => Self::Hz4096,
41            8192 => Self::Hz8192,
42            32768 => Self::Hz32768,
43            other => Self::Custom(other),
44        }
45    }
46}
47
48/// Square wave functionality trait
49pub trait SquareWave: Rtc {
50    /// Configure Frequency and enable square wave
51    fn start_square_wave(&mut self, freq: SquareWaveFreq) -> Result<(), Self::Error>;
52
53    /// Enable square wave output
54    fn enable_square_wave(&mut self) -> Result<(), Self::Error>;
55
56    /// Disable square wave output
57    fn disable_square_wave(&mut self) -> Result<(), Self::Error>;
58
59    /// Set the frequency (without enabling/disabling)
60    fn set_square_wave_frequency(&mut self, freq: SquareWaveFreq) -> Result<(), Self::Error>;
61}