stm32_rust_template/driver/sai/
mod.rs

1#![allow(dead_code)]
2
3use bitflags::bitflags;
4use core::ops::FnMut;
5
6/// Defines the audio protocols supported by the SAI peripheral.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum Protocol {
9    UserDefined,
10    I2S,
11    MsbJustified,
12    LsbJustified,
13    PcmShort,
14    PcmLong,
15    Ac97,
16}
17
18/// Defines the data companding options.
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum Companding {
21    None,
22    ALaw,
23    ULaw,
24}
25
26/// Defines the bit order for audio data transmission.
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub enum BitOrder {
29    MsbFirst,
30    LsbFirst,
31}
32
33/// Defines the polarity of the clock signal.
34#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35pub enum ClockPolarity {
36    FallingEdgeDrive,
37    RisingEdgeDrive,
38}
39
40/// Defines the direction/mode of the SAI block.
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub enum Mode {
43    Master,
44    Slave,
45}
46
47/// Defines synchronization mode between transmitter and receiver.
48#[derive(Debug, Clone, Copy, PartialEq, Eq)]
49pub enum Synchronization {
50    Asynchronous,
51    Synchronous,
52}
53
54/// Defines power states for the SAI peripheral.
55#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56pub enum PowerState {
57    Off,
58    Low,
59    Full,
60}
61
62/// Represents the status of the SAI peripheral.
63#[derive(Debug, Clone, Copy, PartialEq, Eq)]
64pub struct Status {
65    pub tx_busy: bool,
66    pub rx_busy: bool,
67    pub tx_underflow: bool,
68    pub rx_overflow: bool,
69    pub frame_error: bool,
70}
71
72/// Represents the driver capabilities.
73#[derive(Debug, Clone, Copy, PartialEq, Eq)]
74pub struct Capabilities {
75    pub asynchronous: bool,
76    pub synchronous: bool,
77    pub protocol_user: bool,
78    pub protocol_i2s: bool,
79    pub protocol_justified: bool,
80    pub protocol_pcm: bool,
81    pub protocol_ac97: bool,
82    pub mono_mode: bool,
83    pub companding: bool,
84    pub mclk_pin: bool,
85    pub event_frame_error: bool,
86}
87
88bitflags! {
89    /// Represents SAI communication events.
90    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
91    pub struct Event: u32 {
92        const SEND_COMPLETE       = 1 << 0;
93        const RECEIVE_COMPLETE    = 1 << 1;
94        const TX_UNDERFLOW        = 1 << 2;
95        const RX_OVERFLOW         = 1 << 3;
96        const FRAME_ERROR         = 1 << 4;
97    }
98}
99
100/// A generic error type for the SAI driver, using i32 for compatibility.
101pub type Error = i32;
102
103/// A specialized Result type for SAI operations.
104pub type Result<T> = core::result::Result<T, Error>;
105
106/// Trait defining the standard interface for a SAI driver.
107pub trait Sai<'a> {
108    /// Initializes the SAI peripheral with a callback for event signaling.
109    fn initialize(&mut self, callback: impl FnMut(Event) + 'a) -> Result<()>;
110
111    /// De-initializes the SAI peripheral.
112    fn uninitialize(&mut self) -> Result<()>;
113
114    /// Controls the power state of the SAI block.
115    fn power_control(&mut self, state: PowerState) -> Result<()>;
116
117    /// Sends audio data over SAI.
118    fn send(&mut self, data: &[u8]) -> Result<()>;
119
120    /// Receives audio data over SAI.
121    fn receive(&mut self, buffer: &mut [u8]) -> Result<()>;
122
123    /// Gets the number of bytes sent so far.
124    fn get_tx_count(&self) -> u32;
125
126    /// Gets the number of bytes received so far.
127    fn get_rx_count(&self) -> u32;
128
129    /// Configures the SAI peripheral with user-defined parameters.
130    ///
131    /// Arguments are typically encoded bitfields (frame length, slot count, etc.).
132    fn control(&mut self, control: u32, arg1: u32, arg2: u32) -> Result<()>;
133
134    /// Returns the current status of the SAI block.
135    fn get_status(&self) -> Status;
136}