1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//! A simple HAL for the Cirrus Logic/Wolfson WM8731 audio codec
//!
//! This crate provides simple abstractions for the various control registers on the WM8731.
//!
//! Each function returns a [`Register`] struct, representing the address and value for the register.
//! With the exception of `active` and `reset`, registers accept a function for configuration.
//!
//! For example, to power on/off certain features, we can call `power_down`:
//!
//! ```
//! # use wm8731::WM8731;
//! WM8731::power_down(|c| {
//!     c.line_input().power_off();
//!     c.adc().power_off();
//!     c.dac().power_off();
//! });
//! ```

#![no_std]
#![allow(clippy::new_without_default)]

mod bitmask;

pub mod enable_disable;
pub use enable_disable::EnableDisable;

pub mod line_in;
use line_in::LineIn;

pub mod headphone_out;
use headphone_out::HeadphoneOut;

pub mod analog_audio_path;
use analog_audio_path::AnalogAudioPath;

pub mod digital_audio_path;
use digital_audio_path::DigitalAudioPath;

pub mod power_down;
use power_down::PowerDown;

pub mod digital_audio_interface_format;
use digital_audio_interface_format::DigitalAudioInterfaceFormat;

pub mod sampling;
use sampling::Sampling;

pub mod active;
use active::Active;

pub mod sampling_rate;
use sampling_rate::SamplingRate;

pub struct Register {
    pub address: u8,
    pub value: u16,
}

#[cfg(test)]
mod tests;

/// A simple HAL for the Cirrus Logic/ Wolfson WM8731 audio codec
pub struct WM8731 {}

impl WM8731 {
    /// Left line input control register
    pub fn left_line_in<F: FnOnce(&mut LineIn)>(c: F) -> Register {
        let mut li = LineIn::new();
        c(&mut li);

        Register {
            address: 0,
            value: li.data,
        }
    }

    /// Right line in control register
    pub fn right_line_in<F: FnOnce(&mut LineIn)>(c: F) -> Register {
        let mut li = LineIn::new();
        c(&mut li);

        Register {
            address: 1,
            value: li.data,
        }
    }

    /// Left headphone out control register
    pub fn left_headphone_out<F: FnOnce(&mut HeadphoneOut)>(c: F) -> Register {
        let mut lho = HeadphoneOut::new();
        c(&mut lho);

        Register {
            address: 2,
            value: lho.data,
        }
    }

    /// Right headphone out control register
    pub fn right_headphone_out<F: FnOnce(&mut HeadphoneOut)>(c: F) -> Register {
        let mut rho = HeadphoneOut::new();
        c(&mut rho);

        Register {
            address: 3,
            value: rho.data,
        }
    }

    /// Analog audio path control register
    pub fn analog_audio_path<F: FnOnce(&mut AnalogAudioPath)>(c: F) -> Register {
        let mut aap = AnalogAudioPath::new();
        c(&mut aap);

        Register {
            address: 4,
            value: aap.data,
        }
    }

    /// Digital audio path control register
    pub fn digital_audio_path<F: FnOnce(&mut DigitalAudioPath)>(c: F) -> Register {
        let mut dap = DigitalAudioPath::new();
        c(&mut dap);

        Register {
            address: 5,
            value: dap.data,
        }
    }

    /// Power down control register
    pub fn power_down<F: FnOnce(&mut PowerDown)>(c: F) -> Register {
        let mut pd = PowerDown::new();
        c(&mut pd);

        Register {
            address: 6,
            value: pd.data,
        }
    }

    /// Digital audio interface format control register
    pub fn digital_audio_interface_format<F: FnOnce(&mut DigitalAudioInterfaceFormat)>(
        c: F,
    ) -> Register {
        let mut daif = DigitalAudioInterfaceFormat::new();
        c(&mut daif);

        Register {
            address: 7,
            value: daif.data,
        }
    }

    /// Sampling control register
    pub fn sampling<F: FnOnce(&mut Sampling)>(c: F) -> Register {
        let mut s = Sampling::new();
        c(&mut s);

        Register {
            address: 8,
            value: s.data,
        }
    }

    /// Active control register
    pub fn active() -> Active {
        Active::new(9)
    }

    /// Reset register
    pub fn reset() -> Register {
        Register {
            address: 15,
            value: 0,
        }
    }
}