Driver API for Serial Audio Interface (Driver_SAI.h) More...
Driver API for Serial Audio Interface (Driver_SAI.h)
The Serial Audio Interface (SAI) implements a synchronous serial bus interface for connecting digital audio devices. It is by far the most common mechanism used to transfer two channels of audio data between devices within a system. SAI can transfer digital audio using various protocols:
Block Diagram
Simplified SAI Schematic
SAI API
The following header files define the Application Programming Interface (API) for the SAI interface:
The driver implementation is a typical part of the Device Family Pack (DFP) that supports the peripherals of the microcontroller family.
Driver Functions
The driver functions are published in the access struct as explained in Common Driver Functions
Integrated Interchip Sound (I2S) is a serial bus interface that connects digital audio devices together. It was introduced by Philips (now NXP) in the late 80's and last revised 1996. It uses pulse code modulation to exchange the audio data between the devices. The following timing diagram explains the operation:
I2S separates the clock (SCK) from the serial data (SD), resulting in a lower jitter. A complete audio data frame consists of two slots, one for the left channel and one for the right. The slot size equals the data size. The word select (WS) line lets the device know whether the left channel (WS is low) or the right channel (WS is high) is currently being transmitted. WS has a 50% duty-cycle signal that has the same frequency as the sample frequency. It is an early signal, meaning that the WS line changes one clock cycle before the actual data (SD) is transmitted (left or right). The data on SD is always transmitted MSB first and can have a data size of 8 up to 32 bits.
In terms of the CMSIS-Driver for SAI, the I2S protocol can be described as follows:
MSB Justified is much like I2S, with a few differences:
Unlike I2S, in MSB Justified the word select (WS) signals the left channel when it is active high and the right channel, when it is active low. The signal changes when the first actual SD data is available. It might happen that a frame (left or right) is not fully filled with data. In this case, all data after the LSB is forced to zero.
In terms of the CMSIS-Driver for SAI, the MSB Justified protocol can be described as follows:
LSB Justified is much like MSB Justified, with the single difference that the padding 0's are sent before the first actual data (MSB on SD):
In terms of the CMSIS-Driver for SAI, the LSB Justified protocol can be described as follows:
Pulse Code Modulation (PCM) differs to the previous protocols in a few ways:
In terms of the CMSIS-Driver for SAI, the PCM protocol can be described as follows:
PCM Short Frame
PCM Long Frame
Audio Codec '97 was developed by Intel. It is composed of five wires: the clock (12.288 MHz), a sync signal, a reset signal, and two data wires: sdata_out (contains the AC97 output) and sdata_in (contains the CODEC output). For more information, consult the standard documentation.
User Defined ProtocolUsing the control structs of the CMSIS-Driver SAI, it is possible to create support for nearly all serial audio protocols that are available today.
The following properties can be configured for a user protocol:
For more information, refer to ARM_SAI_Control that explains the different configuration options in more detail.
Access structure of the SAI Driver.
The functions of the SAI driver are accessed by function pointers exposed by this structure. Refer to Common Driver Functions for overview information.
Each instance of an SAI interface provides such an access structure. The instance is identified by a postfix number in the symbol name of the access structure, for example:
A middleware configuration setting allows connecting the middleware to a specific driver instance Driver_SAIn. The default is 0, which connects a middleware to the first instance of a driver.
int32_t(* Send)(const void *data, uint32_t num)Pointer to ARM_SAI_Send : Start sending data to SAI Interface.
int32_t(* Receive)(void *data, uint32_t num)Pointer to ARM_SAI_Receive : Start receiving data from SAI Interface.
int32_t(* Control)(uint32_t control, uint32_t arg1, uint32_t arg2)Pointer to ARM_SAI_Control : Control SAI Interface.
struct ARM_SAI_CAPABILITIESSAI Driver Capabilities.
An SAI driver can be implemented with different capabilities (for example protocol support). The data fields of this structure encode the capabilities implemented by this driver. If a certain hardware peripheral is not able to handle one of the protocols directly (not advertised using ARM_SAI_CAPABILITIES), then it might be possible to implement it using the User Defined Protocol (if supported).
Returned by:
Data Fields uint32_t asynchronous: 1 supports asynchronous Transmit/Receive uint32_t synchronous: 1 supports synchronous Transmit/Receive uint32_t protocol_user: 1 supports user defined Protocol uint32_t protocol_i2s: 1 supports I2S Protocol uint32_t protocol_justified: 1 supports MSB/LSB justified Protocol uint32_t protocol_pcm: 1 supports PCM short/long frame Protocol uint32_t protocol_ac97: 1 supports AC'97 Protocol uint32_t mono_mode: 1 supports Mono mode uint32_t companding: 1 supports Companding uint32_t mclk_pin: 1 supports MCLK (Master Clock) pin uint32_t event_frame_error: 1 supports Frame error event: ARM_SAI_EVENT_FRAME_ERROR uint32_t reserved: 21 Reserved (must be zero)SAI Status.
Structure with information about the status of the SAI. The data fields encode busy flags and error flags.
Returned by:
Data Fields uint32_t tx_busy: 1 Transmitter busy flag. uint32_t rx_busy: 1 Receiver busy flag. uint32_t tx_underflow: 1 Transmit data underflow detected (cleared on start of next send operation) uint32_t rx_overflow: 1 Receive data overflow detected (cleared on start of next receive operation) uint32_t frame_error: 1 Sync Frame error detected (cleared on start of next send/receive operation) uint32_t reserved: 27Get driver version.
The function ARM_SAI_GetVersion returns version information of the driver implementation in ARM_DRIVER_VERSION
Example:
void setup_sai (void) {
drv_info = &Driver_SAI0;
if(version.
api< 0x10A) {
return;
}
}
Get driver capabilities.
The function ARM_SAI_GetCapabilities retrieves information about the capabilities in this driver implementation. The data fields of the struct ARM_SAI_CAPABILITIES encode various capabilities, for example supported protocols, or if a hardware is capable to create signal events using the ARM_SAI_SignalEvent callback function.
Example:
void read_capabilities (void) {
drv_info = &Driver_SAI0;
}
Initialize SAI Interface.
The function ARM_SAI_Initialize initializes the SAI interface. It is called when the middleware component starts operation.
The function performs the following operations:
The parameter cb_event is a pointer to the ARM_SAI_SignalEvent callback function; use a NULL pointer when no callback signals are required.
int32_t ARM_SAI_Uninitialize ( void )De-initialize SAI Interface.
The function ARM_SAI_Uninitialize de-initializes the resources of SAI interface.
It is called when the middleware component stops operation and releases the software resources used by the interface.
Control SAI Interface Power.
The function ARM_SAI_PowerControl allows you to control the power modes of the SAI interface.
The parameter state sets the operation and can have the following values:
Refer to Function Call Sequence for more information.
int32_t ARM_SAI_Send ( const void * data, uint32_t num )Start sending data to SAI transmitter.
The function ARM_SAI_Send sends data to the SAI transmitter.
The function parameters specify the buffer with data and the number num of items to send. The item size is defined by the data type which depends on the configured number of data bits.
Data type is:
Transmitter is enabled by calling ARM_SAI_Control with ARM_SAI_CONTROL_TX as the control parameter and 1 as an argument. This starts the transmit engine which, generates a clock and frame sync signal in master mode and transmits the data. In slave mode, clock and frame sync are generated by the external master. When mute is active, data is discarded and zero values are transmitted.
Calling the function ARM_SAI_Send only starts the send operation. The function is non-blocking and returns as soon as the driver has started the operation (the driver typically configures DMA or the interrupt system for continuous transfer). During the operation it is not allowed to call this function again. Also, the data buffer must stay allocated and the contents of unsent data must not be modified. When the send operation is completed (requested number of items have been sent), the event ARM_SAI_EVENT_SEND_COMPLETE is generated. Progress of the send operation can be monitored by reading the number of already sent items by calling the function ARM_SAI_GetTxCount.
The status of the transmitter can also be monitored by calling the function ARM_SAI_GetStatus and checking the tx_busy flag, which indicates if a transmission is still in progress.
If the transmitter is enabled and data is to be sent but the send operation has not been started yet, then the event ARM_SAI_EVENT_TX_UNDERFLOW is generated.
If an invalid synchronization frame is detected in slave mode, then the event ARM_SAI_EVENT_FRAME_ERROR is generated (if supported and reported by event_frame_error in ARM_SAI_CAPABILITIES).
The send operation can be aborted by calling the function ARM_SAI_Control with the control parameter ARM_SAI_ABORT_SEND.
int32_t ARM_SAI_Receive ( void * data, uint32_t num )Start receiving data from SAI receiver.
The function ARM_SAI_Receive is used to receive data from the SAI receiver. The function parameters specify the buffer for data and the number num of items to receive. The item size is defined by the data type, which depends on the configured number of data bits.
Data type is:
The receiver is enabled by calling the function ARM_SAI_Control with the control parameter ARM_SAI_CONTROL_RX and the value 1 for the parameter arg1. This starts the receive engine, which generates a clock and frame sync signal in master mode and receives data. In slave mode, clock and frame sync are generated by the external master.
Calling the function ARM_SAI_Receive only starts the receive operation. The function is non-blocking and returns as soon as the driver has started the operation (the driver typically configures DMA or the interrupt system for continuous transfer). During the operation, it is not allowed to call this function again. The data buffer must also stay allocated. When receive operation is completed (the requested number of items have been received), the ARM_SAI_EVENT_RECEIVE_COMPLETE event is generated. Progress of the receive operation can also be monitored by reading the number of items already received by calling the function ARM_SAI_GetRxCount.
The status of the receiver can also be monitored by calling the function ARM_SAI_GetStatus and checking the rx_busy flag, which indicates whether a reception is still in progress.
When the receiver is enabled and data is received but the receive operation has not been started yet, then the event ARM_SAI_EVENT_RX_OVERFLOW is generated.
If an invalid synchronization frame is detected in slave mode, then the event ARM_SAI_EVENT_FRAME_ERROR is generated (if supported and reported by event_frame_error in ARM_SAI_CAPABILITIES).
The receive operation can be aborted by calling the function ARM_SAI_Control with the control parameter ARM_SAI_ABORT_RECEIVE.
uint32_t ARM_SAI_GetTxCount ( void )Get transmitted data count.
The function ARM_SAI_GetTxCount returns the number of the currently transmitted data items during an ARM_SAI_Send operation.
uint32_t ARM_SAI_GetRxCount ( void )Get received data count.
The function ARM_SAI_GetRxCount returns the number of the currently received data items during an ARM_SAI_Receive operation.
int32_t ARM_SAI_Control ( uint32_t control, uint32_t arg1, uint32_t arg2 )Control SAI Interface.
The function ARM_SAI_Control controls the SAI interface and executes various operations.
The parameter control specifies the operation. Values are listed in the table Parameter control.
The parameter arg1 provides, depending on the operation, additional information or sets values. Refer to table Parameter arg1.
The parameter arg2 provides, depending on the operation and/or arg1, additional information or sets values.
The driver provides a receiver/transmitter pair of signals. In asynchronous operation mode, they operate completely independent from each other. In synchronous operation mode, the synchronous channel uses the Clock (SCK) and Frame Sync (WS) signal from the asynchronous one (control category Synchronization).
The clock polarity can be set for every protocol, regardless whether it is already predefined for I2S, MSB/LSB Jusitified (control category Clock Polarity).
A master clock provides a faster clock from which the frame can be derived (usually 256 x faster than the normal frame clock). You can use a master clock only in master mode. A slave will always have only one clock (control category Master Clock pin (MCLK)).
The table lists the operation values for control. Values from different categories can be ORed.
The parameter arg1 provides frame-specific values depending on the control operation. Values from different categories can be ORed.
Parameter arg1 Bit Category Description ARM_SAI_FRAME_LENGTH(n) 0..9 Frame Length Frame length in bits; the possible range for n is 8..1024; default depends on protocol and data. ARM_SAI_FRAME_SYNC_WIDTH(n) 10..17 Frame Sync Width Frame Sync width in bits; the possible range for n is 1..256; default=1; User protocol only, ignored otherwise. ARM_SAI_FRAME_SYNC_POLARITY_HIGH 18 Frame Sync Polarity Frame Sync is active high (default). ARM_SAI_FRAME_SYNC_POLARITY_LOW Frame Sync is active low (User protocol only, ignored otherwise). ARM_SAI_FRAME_SYNC_EARLY 19 Frame Sync Early Frame Sync one bit before the first bit of the frame (User protocol only, ignored otherwise). ARM_SAI_SLOT_COUNT(n) 20..24 Frame Sync Count Number of slots in frame; the possible range for n is 1..32; default=1; User protocol only, ignored otherwise. ARM_SAI_SLOT_SIZE_DEFAULT 25..26 Frame Slot Size Slot size is equal to data size (default). ARM_SAI_SLOT_SIZE_16 Slot size is 16 bits (User protocol only, ignored otherwise). ARM_SAI_SLOT_SIZE_32 Slot size is 32 bits (User protocol only, ignored otherwise). ARM_SAI_SLOT_OFFSET(n) 27..31 Frame Slot Offset Offset of first data bit in slot; The range for n is 0..31; default=0; User protocol only, ignored otherwise.Depending on the control operation, the parameter arg2 specifies the Master Clock (MCLK) prescaler and calculates the audio frequency automatically.
Parameter arg2 MCLK Prescaler ARM_SAI_MCLK_PRESCALER(n) MCLK prescaler; Audio frequency = MCLK/n; the range for n is 1..4096; default=1.Example
Get SAI status.
The function ARM_SAI_GetStatus retrieves the current SAI interface status.
void ARM_SAI_SignalEvent ( uint32_t event )Signal SAI Events.
The function ARM_SAI_SignalEvent is a callback function registered by the function ARM_SAI_Initialize.
The parameter event indicates one or more events that occurred during driver operation. Each event is encoded in a separate bit and therefore it is possible to signal multiple events within the same call.
The following events can be generated:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4