]> begriffs open source - cmsis/blob - CMSIS/DoxyGen/Driver/src/Driver_SAI.c
CMSIS-Driver: minor enhancements in headers (added helper macros for driver name...
[cmsis] / CMSIS / DoxyGen / Driver / src / Driver_SAI.c
1 /* -----------------------------------------------------------------------------
2  * Copyright (c) 2013-2014 ARM Ltd.
3  *
4  * This software is provided 'as-is', without any express or implied warranty.
5  * In no event will the authors be held liable for any damages arising from
6  * the use of this software. Permission is granted to anyone to use this
7  * software for any purpose, including commercial applications, and to alter
8  * it and redistribute it freely, subject to the following restrictions:
9  *
10  * 1. The origin of this software must not be misrepresented; you must not
11  *    claim that you wrote the original software. If you use this software in
12  *    a product, an acknowledgment in the product documentation would be
13  *    appreciated but is not required.
14  *
15  * 2. Altered source versions must be plainly marked as such, and must not be
16  *    misrepresented as being the original software.
17  *
18  * 3. This notice may not be removed or altered from any source distribution.
19  *
20  *
21  * $Date:        9. Dec 2014
22  * $Revision:    V1.00
23  *
24  * Project:      SAI (Serial Audio Interface) Driver definitions
25  * -------------------------------------------------------------------------- */
26
27 /**
28 \defgroup sai_interface_gr SAI Interface
29 \brief   Driver API for Serial Audio Interface (%Driver_SAI.h)
30 \details 
31 The <b>Serial Audio Interface</b> (SAI) implements a synchronous serial bus interface for connecting digital audio devices. 
32 It is by far the most common mechanism used to transfer two channels of audio data between devices within a system. \b SAI
33 can transfer digital audio using various protocols:
34 - \ref Driver_SAI_I2S
35 - \ref Driver_SAI_MSB
36 - \ref Driver_SAI_LSB
37 - \ref Driver_SAI_PCM
38 - \ref Driver_SAI_AC97
39 - \ref Driver_SAI_User
40
41
42 <b>Block Diagram</b>
43
44 <p>&nbsp;</p>
45 \image html SAI_Schematics.png "Simplified SAI Schematic"
46 <p>&nbsp;</p>
47
48
49 <b>SAI API</b>
50
51 The following header files define the Application Programming Interface (API) for the SAI interface:
52   - \b %Driver_SAI.h : Driver API for Serial Audio Interface
53
54 The driver implementation is a typical part of the
55 <a class="el" href="../../Pack/html/index.html" target="_blank">Device Family Pack (DFP)</a> that supports the peripherals of
56 the microcontroller family.
57
58
59 <b>Driver Functions</b>
60
61 The driver functions are published in the access struct as explained in \ref DriverFunctions
62   - \ref ARM_DRIVER_SAI : access struct for SAI driver functions
63   
64 \section Driver_SAI_I2S I2S
65 <a href="https://en.wikipedia.org/wiki/I%C2%B2S" target="_blank">Integrated Interchip Sound</a> (\b I2S) is a serial bus
66 interface that connects digital audio devices together. It was introduced by Philips (now
67 <a href="http://www.nxp.com" target="_blank">NXP</a>) in the late 80's and last revised 1996. It uses pulse code modulation
68 to exchange the audio data between the devices. The following timing diagram explains the operation:
69  
70 \image html driver_sai_i2s.png
71
72 I2S separates the clock (\b SCK) from the serial data (\b SD), resulting in a lower jitter. A complete audio data frame
73 consists of two slots, one for the left channel and one for the right. The slot size equals the data size.
74 The word select (\b WS) line lets the device know whether the left channel (WS is low) or the right channel (WS is high) is
75 currently being transmitted. WS has a 50% duty-cycle signal that has the same frequency as the sample frequency. It is an
76 early signal, meaning that the WS line changes one clock cycle before the actual data (SD) is transmitted (left or right).
77 The data on SD is always transmitted MSB first and can have a data size of 8 up to 32 bits.
78
79 In terms of the CMSIS-Driver for SAI, the I2S protocol can be described as follows:
80 - Data Size: 8..32 (MSB first)
81 - Clock Polarity: Drive on falling edge, Capture on rising edge
82 - Frame Length: 2 * Data Size = 2 * Slot Size
83 - Frame Sync Width: Frame Length / 2
84 - Frame Sync Polarity: Active Low
85 - Frame Sync Early
86 - Slot Count: 2 (L R)
87 - Slot Size: Data Size
88 - Slot Offset: 0
89
90
91 \section Driver_SAI_MSB MSB Justified
92 \b MSB \b Justified is much like \ref Driver_SAI_I2S, with a few differences:
93
94 \image html driver_sai_msb.png
95
96 Unlike I2S, in MSB Justified the word select (\b WS) signals the left channel when it is active high and the right channel,
97 when it is active low. The signal changes when the first actual \b SD data is available. It might happen that a frame (left
98 or right) is not fully filled with data. In this case, all data after the LSB is forced to zero.
99
100 In terms of the CMSIS-Driver for SAI, the MSB Justified protocol can be described as follows:
101 - Data Size: 8..32 (MSB first)
102 - Clock Polarity: Drive on falling edge, Capture on rising edge
103 - Frame Length: 2 * Slot Size
104 - Frame Sync Width: Frame Length / 2
105 - Frame Sync Polarity: Active High
106 - Slot Count: 2 (L R)
107 - Slot Size: Data Size or higher (16/32)
108 - Slot Offset: 0 (Zero padding after Data: Slot Size - Data Size)
109
110 \section Driver_SAI_LSB LSB Justified
111 \b LSB \b Justified is much like \ref Driver_SAI_MSB, with the single difference that the padding 0's are sent before the
112 first actual data (MSB on \b SD):
113
114 \image html driver_sai_lsb.png
115
116 In terms of the CMSIS-Driver for SAI, the LSB Justified protocol can be described as follows:
117 - Data Size: 8..32 (MSB first)
118 - Clock Polarity: Drive on falling edge, Capture on rising edge
119 - Frame Length: 2*Slot Size
120 - Frame Sync Width: Frame Length / 2
121 - Frame Sync Polarity: Active High
122 - Slot Count: 2
123 - Slot Size: Data Size or higher (16/32)
124 - Slot Offset: Slot Size - Data Size (Zero padding before Data: Slot Size - Data Size)
125
126 \section Driver_SAI_PCM PCM
127 <a href="https://en.wikipedia.org/wiki/Pulse-code_modulation" target="_blank">Pulse Code Modulation</a> (\b PCM) differs to
128 the previous protocols in a few ways:
129
130 \image html driver_sai_pcm.png
131
132 - Only one channel is transferred.
133
134 - There are two types of synchronization modes available:
135   - In \b short \b frame sync mode, the falling edge of \b Frame \b Sync indicates the start of the serial data \b SD. \b Frame \b Sync is always
136     one clock cycle long.
137   - In \b long \b frame sync mode, the rising edge of \b Frame \b Sync indicates the start of the serial data \b SD. \b Frame \b Sync stays active
138     high for 13 clock cycles.
139
140 In terms of the CMSIS-Driver for SAI, the PCM protocol can be described as follows:\n
141 \b PCM \b Short \b Frame
142 - Data Size: 8..32 (MSB first)
143 - Clock Polarity: Drive on falling edge, Capture on rising edge
144 - Frame Length: Slot Size
145 - Frame Sync Width: 1
146 - Frame Sync Polarity: Active High
147 - Frame Sync Early
148 - Slot Count: 1
149 - Slot Size: Data Size or higher (16/32)
150 - Slot Offset: 0
151
152 \b PCM \b Long \b Frame
153 - Data Size: 16..32 (MSB first)
154 - Clock Polarity: Drive on falling edge, Capture on rising edge
155 - Frame Length: Slot Size
156 - Frame Sync Width: 13
157 - Frame Sync Polarity: Active High
158 - Slot Count: 1
159 - Slot Size: Data Size or higher (32)
160 - Slot Offset: 0
161
162 \section Driver_SAI_AC97 AC'97
163 <a href="https://en.wikipedia.org/wiki/AC'97" target="_blank">Audio Codec '97</a> was developed by
164 <a href="http://www.intel.com" target="_blank">Intel</a>. It is composed of five wires: the clock (12.288 MHz), a sync
165 signal, a reset signal, and two data wires: sdata_out (contains the AC97 output) and sdata_in (contains the CODEC output).
166 For more information, consult the
167 <a href="http://www-inst.eecs.berkeley.edu/~cs150/Documents/ac97_r23.pdf" target="_blank">standard documentation</a>.
168
169 \section Driver_SAI_User User Defined Protocol
170 Using the control structs of the CMSIS-Driver SAI, it is possible to create support for nearly all serial audio protocols
171 that are available today. 
172
173 \image html driver_sai_user.png
174
175 The following properties can be configured for a user protocol:
176 - Data Size in bits (8..32)
177 - Data Bit Order: MSB first (default) or LSB first
178 - Clock Polarity:
179   - Driver on falling edge, Capture on rising edge (default)
180   - Driver on rising edge, Capture on falling edge
181 - Frame Length in bits
182 - Frame Sync Width in bits (default=1)
183 - Frame Sync Polarity: active high (default) or low
184 - Frame Sync Early: Sync signal one bit before the first bit of frame
185 - Slot Count: number of slots in frame (default=1)
186 - Slot Size: equal to data size (default) or 16 or 32-bit
187 - Slot Offset: offset of first data bit in slot (default=0)
188
189 For more information, refer to \ref ARM_SAI_Control that explains the different configuration options in more detail.
190
191 @{
192 */
193
194
195 /** 
196 \struct     ARM_DRIVER_SAI
197 \details
198 The functions of the SAI driver are accessed by function pointers exposed by this structure.
199 Refer to \ref DriverFunctions for overview information.
200
201 Each instance of an SAI interface provides such an access structure. 
202 The instance is identified by a postfix number in the symbol name of the access structure, for example:
203  - \b Driver_SAI0 is the name of the access struct of the first instance (no. 0).
204  - \b Driver_SAI1 is the name of the access struct of the second instance (no. 1).
205
206 A middleware configuration setting allows connecting the middleware to a specific driver instance \b %Driver_SAI<i>n</i>.
207 The default is \token{0}, which connects a middleware to the first instance of a driver.
208 *****************************************************************************************************************/
209
210 /**
211 \struct     ARM_SAI_CAPABILITIES 
212 \details
213 An SAI driver can be implemented with different capabilities (for example protocol support). The data fields of this
214 structure encode the capabilities implemented by this driver. If a certain hardware peripheral is not able to handle one
215 of the protocols directly (not advertised using ARM_SAI_CAPABILITIES), then it might be possible to implement it using
216 the \ref Driver_SAI_User (if supported).
217
218 <b>Returned by:</b>
219   - \ref ARM_SAI_GetCapabilities
220 *****************************************************************************************************************/
221
222 /**
223 \struct ARM_SAI_STATUS 
224 \details
225 Structure with information about the status of the SAI. The data fields encode busy flags and error flags.
226
227 <b>Returned by:</b>
228   - \ref ARM_SAI_GetStatus
229 *****************************************************************************************************************/
230
231 /**
232 \typedef    ARM_SAI_SignalEvent_t
233 \details
234 Provides the typedef for the callback function \ref ARM_SAI_SignalEvent.
235
236 <b>Parameter for:</b>
237   - \ref ARM_SAI_Initialize
238 *******************************************************************************************************************/
239
240
241 /****** SAI specific error codes *****/
242 /**
243 \defgroup sai_execution_status Status Error Codes
244 \ingroup common_drv_gr
245 \brief Negative values indicate errors (SAI has specific codes in addition to common \ref execution_status). 
246 \details 
247 The SAI driver has additional status error codes that are listed below.
248 \note
249 - In case multiple errors exist, only the first encountered error will be reported.
250 - errors ARM_SAI_ERROR_BIT_ORDER, ARM_SAI_ERROR_FRAME_SYNC_xxx, ARM_SAI_ERROR_SLOT_xxx will only be reported in \ref Driver_SAI_User mode.
251 - The SAI driver also returns the common \ref execution_status.
252   
253 @{
254 \def ARM_SAI_ERROR_SYNCHRONIZATION       
255 The \b synchronization requested with the function \ref ARM_SAI_Control is not supported.
256
257 \def ARM_SAI_ERROR_PROTOCOL
258 The \b protocol requested with the function \ref ARM_SAI_Control is not supported.
259
260 \def ARM_SAI_ERROR_DATA_SIZE     
261 The <b>data size</b> requested with the function \ref ARM_SAI_Control is not supported.
262
263 \def ARM_SAI_ERROR_BIT_ORDER
264 The <b>bit order</b> requested with the function \ref ARM_SAI_Control is not supported.
265
266 \def ARM_SAI_ERROR_MONO_MODE            
267 The <b>mono mode</b> requested with the function \ref ARM_SAI_Control is not supported.
268
269 \def ARM_SAI_ERROR_COMPANDING   
270 The <b>companding</b> requested with the function \ref ARM_SAI_Control is not supported.
271
272 \def ARM_SAI_ERROR_CLOCK_POLARITY 
273 The <b>clock polarity</b> requested with the function \ref ARM_SAI_Control is not supported.
274
275 \def ARM_SAI_ERROR_AUDIO_FREQ
276 The <b>audio frequency</b> requested with the function \ref ARM_SAI_Control is not supported.
277
278 \def ARM_SAI_ERROR_MCLK_PIN
279 The <b>MCLK pin</b> setting requested with the function \ref ARM_SAI_Control is not supported.
280
281 \def ARM_SAI_ERROR_MCLK_PRESCALER       
282 The <b>MCLK prescaler</b> requested with the function \ref ARM_SAI_Control is not supported.
283
284 \def ARM_SAI_ERROR_FRAME_LENGTH
285 The <b>frame length</b> requested with the function \ref ARM_SAI_Control is not supported.
286
287 \def ARM_SAI_ERROR_FRAME_SYNC_WIDTH 
288 The <b>frame sync width</b> requested with the function \ref ARM_SAI_Control is not supported.
289
290 \def ARM_SAI_ERROR_FRAME_SYNC_POLARITY
291 The <b>frame sync polarity</b> requested with the function \ref ARM_SAI_Control is not supported.
292
293 \def ARM_SAI_ERROR_FRAME_SYNC_EARLY      
294 The <b>frame sync early</b> requested with the function \ref ARM_SAI_Control is not supported.
295
296 \def ARM_SAI_ERROR_SLOT_COUNT 
297 The <b>slot count</b> requested with the function \ref ARM_SAI_Control is not supported.
298
299 \def ARM_SAI_ERROR_SLOT_SIZE
300 The <b>slot size</b> requested with the function \ref ARM_SAI_Control is not supported.
301
302 \def ARM_SAI_ERROR_SLOT_OFFESET
303 The <b>slot offset</b> requested with the function \ref ARM_SAI_Control is not supported.
304 @}
305 */
306
307
308 /****** SAI Event *****/
309 /**
310 \defgroup SAI_events SAI Events
311 \ingroup sai_interface_gr
312 \brief The SAI driver generates call back events that are notified via the function \ref ARM_SAI_SignalEvent.
313 \details 
314 This section provides the event values for the \ref ARM_SAI_SignalEvent callback function.
315
316 The following call back notification events are generated:
317 @{
318 \def ARM_SAI_EVENT_SEND_COMPLETE    
319 \def ARM_SAI_EVENT_RECEIVE_COMPLETE 
320 \def ARM_SAI_EVENT_TX_UNDERFLOW     
321 \def ARM_SAI_EVENT_RX_OVERFLOW      
322 \def ARM_SAI_EVENT_FRAME_ERROR      
323 @}
324 */
325
326
327 /**
328 \defgroup sai_control SAI Control Codes
329 \ingroup sai_interface_gr
330 \brief Many parameters of the SAI driver are configured using the \ref ARM_SAI_Control function.
331 \details
332 @{
333 The various SAI control codes define:
334   - \ref sai_configure_control specifies SAI configuration
335   - \ref sai_controls specifies SAI controls
336
337 Refer to the \ref ARM_SAI_Control function for further details.
338 */
339
340
341 /**
342 \defgroup sai_configure_control SAI Configuration
343 \ingroup sai_control
344 \brief Specify Transmitter/Receiver configuration.
345 \details
346 @{
347 Configuration is specified by ORing \b ARM_SAI_CONFIGURE_<i>x</i> with the following parameters:
348  - \ref sai_mode_control
349  - \ref sai_sync_control
350  - \ref sai_protocol_control
351  - \ref sai_data_bits_control
352  - \ref sai_bit_order_control
353  - \ref sai_mono_control
354  - \ref sai_clock_pol_control
355  - \ref sai_companding_control
356  - \ref sai_mclk_pin_control
357
358 Additional configuration specified by \em arg1:
359  - \ref sai_frame_control
360  - \ref sai_slot_control
361
362 Additional configuration specified by \em arg2:
363  - <b>Audio Frequency</b> (Master only)
364  - \ref sai_mclk_pres_control
365
366 \defgroup sai_mode_control SAI Mode
367 \ingroup sai_configure_control
368 \brief Defines Transmitter/Receiver mode.
369 \details
370 @{
371 \def ARM_SAI_MODE_MASTER
372 \def ARM_SAI_MODE_SLAVE
373 @}
374
375 \defgroup sai_sync_control SAI Synchronization
376 \ingroup sai_configure_control
377 \brief Defines Transmitter/Receiver synchronization.
378 \details
379 @{
380 \def ARM_SAI_ASYNCHRONOUS
381 \def ARM_SAI_SYNCHRONOUS
382 @}
383
384 \defgroup sai_protocol_control SAI Protocol
385 \ingroup sai_configure_control
386 \brief Defines Transmitter/Receiver protocol.
387 \details
388 @{
389 \def ARM_SAI_PROTOCOL_USER
390 \def ARM_SAI_PROTOCOL_I2S
391 \def ARM_SAI_PROTOCOL_MSB_JUSTIFIED
392 \def ARM_SAI_PROTOCOL_LSB_JUSTIFIED
393 \def ARM_SAI_PROTOCOL_PCM_SHORT
394 \def ARM_SAI_PROTOCOL_PCM_LONG
395 \def ARM_SAI_PROTOCOL_AC97
396 @}
397
398 \defgroup sai_data_bits_control SAI Data Size
399 \ingroup sai_configure_control
400 \brief Defines data size in bits (per channel/slot).
401 \details
402 @{
403 \def ARM_SAI_DATA_SIZE(n)
404 @}
405
406 \defgroup sai_bit_order_control SAI Bit Order
407 \ingroup sai_configure_control
408 \brief Defines the bit order.
409 \details
410 @{
411 \def ARM_SAI_MSB_FIRST
412 \def ARM_SAI_LSB_FIRST
413 @}
414
415 \defgroup sai_mono_control SAI Mono Mode
416 \ingroup sai_configure_control
417 \brief Defines mono mode.
418 \details
419 @{
420 \def ARM_SAI_MONO_MODE
421 @}
422
423 \defgroup sai_companding_control SAI Companding
424 \ingroup sai_configure_control
425 \brief Defines companding.
426 \details
427 @{
428 \def ARM_SAI_COMPANDING_NONE
429 \def ARM_SAI_COMPANDING_A_LAW
430 \def ARM_SAI_COMPANDING_U_LAW
431 @}
432
433 \defgroup sai_clock_pol_control SAI Clock Polarity
434 \ingroup sai_configure_control
435 \brief Defines clock polarity.
436 \details
437 @{
438 \def ARM_SAI_CLOCK_POLARITY_0
439 \def ARM_SAI_CLOCK_POLARITY_1
440 @}
441
442 \defgroup sai_frame_control SAI Frame
443 \ingroup sai_configure_control
444 \brief Defines frame.
445 \details
446 @{
447 \def ARM_SAI_FRAME_LENGTH(n)
448 \def ARM_SAI_FRAME_SYNC_WIDTH(n)
449 \def ARM_SAI_FRAME_SYNC_POLARITY_HIGH
450 \def ARM_SAI_FRAME_SYNC_POLARITY_LOW
451 \def ARM_SAI_FRAME_SYNC_EARLY
452 @}
453
454 \defgroup sai_slot_control SAI Slot
455 \ingroup sai_configure_control
456 \brief Defines data slots.
457 \details
458 @{
459 \def ARM_SAI_SLOT_COUNT(n)
460 \def ARM_SAI_SLOT_SIZE_DEFAULT
461 \def ARM_SAI_SLOT_SIZE_16
462 \def ARM_SAI_SLOT_SIZE_32
463 \def ARM_SAI_SLOT_OFFSET(n)
464 @}
465
466 \defgroup sai_mclk_pin_control SAI Master Clock Pin
467 \ingroup sai_configure_control
468 \brief Defines MCLK pin.
469 \details
470 @{
471 \def ARM_SAI_MCLK_PIN_INACTIVE
472 \def ARM_SAI_MCLK_PIN_OUTPUT
473 \def ARM_SAI_MCLK_PIN_INPUT
474 @}
475
476 \defgroup sai_mclk_pres_control SAI Master Clock Prescaler
477 \ingroup sai_configure_control
478 \brief Defines MCLK prescaler.
479 \details
480 @{
481 \def ARM_SAI_MCLK_PRESCALER(n)
482 @}
483
484 @}
485 */
486
487
488 /**
489 \defgroup sai_controls SAI Controls
490 \ingroup sai_control
491 \brief Specifies controls.
492 \details
493 @{
494 \def ARM_SAI_CONFIGURE_TX
495 \sa ARM_SAI_Control
496 \def ARM_SAI_CONFIGURE_RX
497 \sa ARM_SAI_Control
498 \def ARM_SAI_CONTROL_TX
499 \sa ARM_SAI_Control; ARM_SAI_Send
500 \def ARM_SAI_CONTROL_RX
501 \sa ARM_SAI_Control; ARM_SAI_Receive
502 \def ARM_SAI_MASK_SLOTS_TX
503 \sa ARM_SAI_Control; ARM_SAI_Send
504 \def ARM_SAI_MASK_SLOTS_RX
505 \sa ARM_SAI_Control; ARM_SAI_Receive
506 \def ARM_SAI_ABORT_SEND
507 \sa ARM_SAI_Control; ARM_SAI_Send
508 \def ARM_SAI_ABORT_RECEIVE
509 \sa ARM_SAI_Control; ARM_SAI_Receive
510 @}
511 */
512
513
514 /**
515 @}
516 */
517 // end group SAI_control
518
519
520 //
521 // Function documentation
522 //
523
524 ARM_DRIVER_VERSION ARM_SAI_GetVersion (void)  {
525   return { 0, 0 };
526 }
527 /**
528 \fn ARM_DRIVER_VERSION ARM_SAI_GetVersion (void)
529 \details
530 The function \b ARM_SAI_GetVersion returns version information of the driver implementation in \ref ARM_DRIVER_VERSION
531  - API version is the version of the CMSIS-Driver specification used to implement this driver.
532  - Driver version is source code version of the actual driver implementation.
533
534 \b Example:
535 \code
536 extern ARM_DRIVER_SAI Driver_SAI0;
537 ARM_DRIVER_SAI *drv_info;
538  
539 void setup_sai (void)  {
540   ARM_DRIVER_VERSION  version;
541  
542   drv_info = &Driver_SAI0;  
543   version = drv_info->GetVersion ();
544   if (version.api < 0x10A)  {      // requires at minimum API version 1.10 or higher
545     // error handling
546     return;
547   }
548 }
549 \endcode
550 *****************************************************************************************************************/
551
552 ARM_SAI_CAPABILITIES ARM_SAI_GetCapabilities (void)  {
553   return {0};
554 }
555 /**
556 \details
557 \fn ARM_SAI_CAPABILITIES ARM_SAI_GetCapabilities (void)
558 The function \b  ARM_SAI_GetCapabilities retrieves information about the capabilities in this driver implementation.
559 The data fields of the struct \ref ARM_SAI_CAPABILITIES encode various capabilities, for example
560 supported protocols, or if a hardware is capable to create signal events using the \ref ARM_SAI_SignalEvent 
561 callback function.
562  
563 \b Example:
564 \code
565 extern ARM_DRIVER_SAI Driver_SAI0;
566 ARM_DRIVER_SAI *drv_info;
567   
568 void read_capabilities (void)  {
569   ARM_SAI_CAPABILITIES drv_capabilities;
570  
571   drv_info = &Driver_SAI0;  
572   drv_capabilities = drv_info->GetCapabilities ();
573   // interrogate capabilities
574  
575 }
576 \endcode
577 *****************************************************************************************************************/
578
579 int32_t ARM_SAI_Initialize (ARM_SAI_SignalEvent_t cb_event)  {
580   return ARM_DRIVER_OK;
581 }
582 /**
583 \fn int32_t ARM_SAI_Initialize (ARM_SAI_SignalEvent_t cb_event)
584 \details
585 The function \b ARM_SAI_Initialize initializes the SAI interface. It is called when the middleware component starts
586 operation.
587
588 The function performs the following operations:
589   - Initializes the required resources of the SAI interface.
590   - Registers the \ref ARM_SAI_SignalEvent callback function.
591
592 The parameter \em cb_event is a pointer to the \ref ARM_SAI_SignalEvent callback function; use a NULL pointer 
593 when no callback signals are required.
594 *****************************************************************************************************************/
595
596 int32_t ARM_SAI_Uninitialize (void)  {
597   return ARM_DRIVER_OK;
598 }
599 /**
600 \fn int32_t ARM_SAI_Uninitialize (void)
601 \details
602 The function \b ARM_SAI_Uninitialize de-initializes the resources of SAI interface.
603
604 It is called when the middleware component stops operation and releases the software resources used by the interface.
605 *****************************************************************************************************************/
606   
607 int32_t ARM_SAI_PowerControl (ARM_POWER_STATE state)  {
608   return ARM_DRIVER_OK;
609 }
610 /**
611 \fn int32_t ARM_SAI_PowerControl (ARM_POWER_STATE state)
612 \details
613 The function \b ARM_SAI_PowerControl allows you to control the power modes of the SAI interface.  
614
615 The parameter \em state sets the operation and can have the following values:
616   - \ref ARM_POWER_FULL : set-up peripheral for data transfers, enable interrupts (NVIC) and optionally DMA. 
617                           Can be called multiple times. If the peripheral is already in this mode the function performs 
618                                                   no operation and returns with \ref ARM_DRIVER_OK.
619   - \ref ARM_POWER_LOW : may use power saving. Returns \ref ARM_DRIVER_ERROR_UNSUPPORTED when not implemented.
620   - \ref ARM_POWER_OFF : terminates any pending data transfers, disables peripheral, disables related interrupts and DMA.
621       
622 Refer to \ref CallSequence for more information.
623 *****************************************************************************************************************/
624
625 int32_t ARM_SAI_Send (const void *data, uint32_t num)  {
626   return ARM_DRIVER_OK;
627 }
628 /**
629 \fn int32_t ARM_SAI_Send (const void *data, uint32_t num) 
630 \details
631 The function \b ARM_SAI_Send sends data to the SAI transmitter.
632
633 The function parameters specify the buffer with \a data and the number \a num of items to send.
634 The item size is defined by the data type which depends on the configured number of data bits.
635
636 Data type is:
637  - \em uint8_t when configured for \token{8} data bits
638  - \em uint16_t when configured for \token{9..16} data bits
639  - \em uint32_t when configured for \token{17..32} data bits
640
641 Transmitter is enabled by calling \ref ARM_SAI_Control with \ref ARM_SAI_CONTROL_TX as the control parameter and \token{1} as
642 an argument. This starts the transmit engine which, generates a clock and frame sync signal in master mode and transmits the
643 data. In slave mode, clock and frame sync are generated by the external master. When mute is active, data is discarded and
644 zero values are transmitted. 
645  
646 Calling the function <b>ARM_SAI_Send</b> only starts the send operation. The function is non-blocking and returns as soon as
647 the driver has started the operation (the driver typically configures DMA or the interrupt system for continuous transfer).
648 During the operation it is not allowed to call this function again. Also, the data buffer must stay allocated and the
649 contents of unsent data must not be modified. When the send operation is completed (requested number of items have been
650 sent), the event \ref ARM_SAI_EVENT_SEND_COMPLETE is generated. Progress of the send operation can be monitored by reading
651 the number of already sent items by calling the function \ref ARM_SAI_GetTxCount. 
652
653 The status of the transmitter can also be monitored by calling the function \ref ARM_SAI_GetStatus and checking the \em tx_busy flag,
654 which indicates if a transmission is still in progress.
655
656 If the transmitter is enabled and data is to be sent but the send operation has not been started yet, then the event
657 \ref ARM_SAI_EVENT_TX_UNDERFLOW is generated.
658
659 If an invalid synchronization frame is detected in slave mode, then the event \ref ARM_SAI_EVENT_FRAME_ERROR is generated (if
660 supported and reported by \em event_frame_error in \ref ARM_SAI_CAPABILITIES). 
661
662 The send operation can be aborted by calling the function \ref ARM_SAI_Control with the control parameter \ref ARM_SAI_ABORT_SEND.
663 *****************************************************************************************************************/
664
665 int32_t ARM_SAI_Receive (void *data, uint32_t num)  {
666   return ARM_DRIVER_OK;
667 }
668 /**
669 \fn int32_t ARM_SAI_Receive (void *data, uint32_t num)
670 \details
671 The function \b ARM_SAI_Receive is used to receive data from the SAI receiver. The function parameters specify the buffer for
672 \a data and the number \a num of items to receive. The item size is defined by the data type, which depends on the configured
673 number of data bits.
674
675 Data type is:
676  - \em uint8_t when configured for \token{8} data bits
677  - \em uint16_t when configured for \token{9..16} data bits
678  - \em uint32_t when configured for \token{17..32} data bits
679
680 The receiver is enabled by calling the function \ref ARM_SAI_Control with the control parameter \ref ARM_SAI_CONTROL_RX and the value \token{1}
681 for the parameter \em arg1. This starts the receive engine, which generates a clock and frame sync signal in master mode and receives
682 data. In slave mode, clock and frame sync are generated by the external master. 
683  
684 Calling the function <b>ARM_SAI_Receive</b> only starts the receive operation. The function is non-blocking and returns as
685 soon as the driver has started the operation (the driver typically configures DMA or the interrupt system for continuous
686 transfer). During the operation, it is not allowed to call this function again. The data buffer must also stay allocated.
687 When receive operation is completed (the requested number of items have been received), the
688 \ref ARM_SAI_EVENT_RECEIVE_COMPLETE event is generated. Progress of the receive operation can also be monitored by reading
689 the number of items already received by calling the function \ref ARM_SAI_GetRxCount. 
690
691 The status of the receiver can also be monitored by calling the function \ref ARM_SAI_GetStatus and checking the \em rx_busy flag, which
692 indicates whether a reception is still in progress. 
693
694 When the receiver is enabled and data is received but the receive operation has not been started yet, then the event
695 \ref ARM_SAI_EVENT_RX_OVERFLOW is generated.
696
697 If an invalid synchronization frame is detected in slave mode, then the event \ref ARM_SAI_EVENT_FRAME_ERROR is generated (if
698 supported and reported by \em event_frame_error in \ref ARM_SAI_CAPABILITIES). 
699
700 The receive operation can be aborted by calling the function \ref ARM_SAI_Control with the control parameter \ref ARM_SAI_ABORT_RECEIVE.
701 *****************************************************************************************************************/
702
703 uint32_t ARM_SAI_GetTxCount (void)  {
704   return 0;
705 }
706 /**
707 \fn uint32_t ARM_SAI_GetTxCount (void)
708 \details
709 The function \b ARM_SAI_GetTxCount returns the number of the currently transmitted data items during an \ref ARM_SAI_Send
710 operation.
711 *****************************************************************************************************************/
712
713 uint32_t ARM_SAI_GetRxCount (void)  {
714   return 0;
715 }
716 /**
717 \fn uint32_t ARM_SAI_GetRxCount (void)
718 \details
719 The function \b ARM_SAI_GetRxCount returns the number of the currently received data items during an \ref ARM_SAI_Receive
720 operation.
721 *****************************************************************************************************************/
722
723 int32_t ARM_SAI_Control (uint32_t control, uint32_t arg1, uint32_t arg2)  {
724   return ARM_DRIVER_OK;
725 }
726
727 /**
728 \fn int32_t ARM_SAI_Control (uint32_t control, uint32_t arg1, uint32_t arg2)
729 \details
730 The function \b ARM_SAI_Control controls the SAI interface and executes various operations.
731
732 The parameter \em control specifies the operation. Values are listed in the table <a href="#sai_contrl_tab"><b>Parameter <i>control</i></b></a>.\n
733 The parameter \em arg1 provides,  depending on the operation,  additional information or sets values. 
734 Refer to table <a href="#sai_arg1_tab"><b>Parameter <i>arg1</i></b></a>. \n
735 The parameter \em arg2 provides,  depending on the operation and/or \em arg1,  additional information or sets values. 
736
737 The driver provides a receiver/transmitter pair of signals. 
738 In asynchronous operation mode, they operate completely independent from each other. 
739 In synchronous operation mode, the synchronous channel uses the Clock (SCK) and Frame Sync (WS) signal from the asynchronous one
740 (control category <a href="#sai_sync"><b>Synchronization</b></a>).
741
742 The clock polarity can be set for every protocol, regardless whether it is already predefined for I2S, MSB/LSB Jusitified
743 (control category <a href="#sai_clk_polarity"><b>Clock Polarity</b></a>).
744
745 A master clock provides a faster clock from which the frame can be derived (usually 256 x faster than the normal frame clock). 
746 You can use a master clock only in master mode. A slave will always have only one clock 
747 (control category <a href="#master_clock"><b>Master Clock pin (MCLK)</b></a>).
748
749 \anchor sai_contrl_tab          
750 The table lists the operation values for \em control. Values from different categories can be ORed.
751 <table class="cmtable" summary="">
752 <tr><th> Parameter \em control              </th><th>                                       Bit </th><th>             Category      </th>
753     <th> Description                        </th></tr>
754 <tr><td> \ref ARM_SAI_CONFIGURE_TX          </td><td rowspan="9" style="text-align:right"> 0..7 </td><td rowspan="9"> Operation    </td>
755     <td> Configure transmitter. \em arg1 (see  <a href="#sai_arg1_tab"><b>Parameter <i>arg1</i></b></a>) and \em arg2 provide additional configuration.  </td></tr>
756 <tr><td> \ref ARM_SAI_CONFIGURE_TX          </td>    
757     <td> Configure transmitter. \em arg1 (see  <a href="#sai_arg1_tab"><b>Parameter <i>arg1</i></b></a>) and \em arg2 provide additional configuration.  </td></tr>
758 <tr><td> \ref ARM_SAI_CONFIGURE_RX          </td>
759     <td> Configure transmitter. \em arg1 and \em arg2 provide additional configuration.  </td></tr>
760 <tr><td> \ref ARM_SAI_CONTROL_TX                </td>
761     <td> Enable or disable transmitter and control mute; 
762         \em arg1.0 : \token{0=disable (default); 1=enable;} \em arg1.1 : \token{mute} (see \ref ARM_SAI_Send) </td></tr>
763 <tr><td> \ref ARM_SAI_CONTROL_RX            </td>
764     <td> Enable or disable receiver; \em arg1.0 : \token{0=disable (default); 1=enable} (see \ref ARM_SAI_Receive)  </td></tr>
765 <tr><td> \ref ARM_SAI_MASK_SLOTS_TX         </td>
766     <td> Mask transmitter slots; \em arg1 = \token{mask} (bit: 0=active, 1=inactive); all configured slots are active by default.  </td></tr>
767 <tr><td> \ref ARM_SAI_MASK_SLOTS_RX         </td>
768     <td> Mask receiver slots; \em arg1 = \token{mask} (bit: 0=active, 1=inactive); all configured slots are active by default.  </td></tr>
769 <tr><td> \ref ARM_SAI_ABORT_SEND            </td>
770     <td> Abort send operation (see \ref ARM_SAI_Send).  </td></tr>
771 <tr><td> \ref ARM_SAI_ABORT_RECEIVE         </td>
772     <td> Abort receive operation (see \ref ARM_SAI_Receive).  </td></tr>
773 <tr><td> \ref ARM_SAI_MODE_MASTER           </td><td rowspan="2" style="text-align:right"> 8    </td><td rowspan="2"> Mode             </td>
774     <td> Master mode. \em arg2 specifies the audio frequency in [Hz].  You can also set the <a href="#master_clock"><b>Master Clock pin</b></a>.</td></tr>
775 <tr><td> \ref ARM_SAI_MODE_SLAVE (default)  </td>
776     <td> Slave mode.  </td></tr>
777 <tr><td> \ref ARM_SAI_ASYNCHRONOUS (default)    \anchor sai_sync </td><td rowspan="2" style="text-align:right">  9  </td><td rowspan="2"> Synchronization </td>
778     <td> Asynchronous operation using own clock and sync signal.  </td></tr>
779 <tr><td> \ref ARM_SAI_SYNCHRONOUS               </td> 
780     <td> Synchronous operation using clock and sync signal from other transmitter/receiver.  </td></tr>
781 <tr><td> \ref ARM_SAI_PROTOCOL_USER  (default)  </td><td rowspan="7" style="text-align:right"> 10..12 </td><td rowspan="7"> Protocol     </td>
782     <td> User defined                           </td></tr>
783 <tr><td> \ref ARM_SAI_PROTOCOL_I2S              </td>
784     <td> I2C                                    </td></tr>
785 <tr><td> \ref ARM_SAI_PROTOCOL_MSB_JUSTIFIED    </td> 
786     <td> MSB (left) justified                   </td></tr>
787 <tr><td> \ref ARM_SAI_PROTOCOL_LSB_JUSTIFIED    </td> 
788     <td> LSB (right) justified                  </td></tr>
789 <tr><td> \ref ARM_SAI_PROTOCOL_PCM_SHORT        </td> 
790     <td> PCM with short frame                   </td></tr>
791 <tr><td> \ref ARM_SAI_PROTOCOL_PCM_LONG         </td> 
792     <td> PCM with long frame                    </td></tr>
793 <tr><td> \ref ARM_SAI_PROTOCOL_AC97             </td> 
794     <td> AC'97                                  </td></tr>
795 <tr><td> \ref ARM_SAI_DATA_SIZE(n)              </td><td style="text-align:right"> 13..17 </td><td> Data Size    </td>     
796     <td> Data size in bits; the range for \em n is \token{8..32}. See also: <a href="#frame_slot_size"><b>Frame Slot Size</b></a>.    </td></tr>
797 <tr><td> \ref ARM_SAI_MSB_FIRST                 </td><td rowspan="2" style="text-align:right"> 18     </td><td rowspan="2"> Bit Order    </td>     
798     <td> Data is transferred with MSB first.    </td></tr>
799 <tr><td> \ref ARM_SAI_LSB_FIRST                 </td>
800     <td> Data is transferred with LSB first (User protocol only, ignored otherwise).     </td></tr>
801 <tr><td> \ref ARM_SAI_MONO_MODE                 </td><td style="text-align:right"> 19     </td><td> Mono Mode</td>      
802     <td> Only for I2S, MSB/LSB justified. 
803              When using \ref Driver_SAI_I2S in mono mode, only data for a single channel is sent to and received from the driver.
804          Hardware will duplicate the data for the second channel on transmit and ignore the second channel on receive.    </td></tr>
805 <tr><td> \ref ARM_SAI_COMPANDING_NONE (default) </td><td rowspan="3" style="text-align:right"> 20..22 </td><td rowspan="3"> Companding    </td>
806     <td> No companding  </td></tr>
807 <tr><td> \ref ARM_SAI_COMPANDING_A_LAW          </td>   
808     <td> A-Law companding (8-bit data)          </td></tr>
809 <tr><td> \ref ARM_SAI_COMPANDING_U_LAW          </td>   
810     <td> u-Law companding (8-bit data)          </td></tr>
811 <tr><td> \ref ARM_SAI_CLOCK_POLARITY_0&nbsp;(default)  \anchor sai_clk_polarity > </td><td rowspan="2" style="text-align:right">    23   </td><td rowspan="2"> Clock Polarity </td>
812     <td> Drive on falling edge, capture on rising  edge. </td></tr>
813 <tr><td> \ref ARM_SAI_CLOCK_POLARITY_1         \anchor master_clock </td>
814     <td> Drive on rising  edge, capture on falling edge. </td></tr>
815 <tr><td> \ref ARM_SAI_MCLK_PIN_INACTIVE&nbsp;(default)  </td><td rowspan="3" style="text-align:right"> 24..26 </td><td rowspan="3"> Master Clock pin (MCLK) </td>
816     <td> MCLK not used.                         </td></tr>
817 <tr><td> \ref ARM_SAI_MCLK_PIN_OUTPUT           </td>   
818     <td> MCLK is output (Master mode only).     </td></tr>
819 <tr><td> \ref ARM_SAI_MCLK_PIN_INPUT            </td>   
820     <td> MCLK is input (Master mode only).      </td></tr>
821 </table>
822
823 \anchor sai_arg1_tab
824 The parameter \em arg1 provides frame-specific values depending on the \em control operation. Values from different categories can be ORed.
825 <table class="cmtable" summary="">
826 <tr><th nowrap> Parameter \em arg1       </th>
827     <th style="text-align:right">   Bit  </th>
828         <th> Category                        </th>
829     <th> Description                     </th></tr>
830 <tr><td> \ref ARM_SAI_FRAME_LENGTH(n)    </td>
831     <td style="text-align:right">  0..9  </td>
832         <td> Frame Length                    </td>
833     <td> Frame length in bits; the possible range for \em n is \token{8..1024}; default depends on protocol and data.    </td></tr>
834 <tr><td> \ref ARM_SAI_FRAME_SYNC_WIDTH(n)</td>
835     <td style="text-align:right"> 10..17 </td>
836         <td> Frame Sync Width                </td>
837     <td> Frame Sync width in bits; the possible range for \em n is \token{1..256}; \token{default=1}; User protocol only, ignored otherwise.  </td></tr>
838 <tr><td> \ref ARM_SAI_FRAME_SYNC_POLARITY_HIGH   </td>
839     <td rowspan="2" style="text-align:right"> 18 </td>
840         <td rowspan="2" style="white-spaces:nowrap"> Frame Sync Polarity  </td>
841     <td> Frame Sync is active high (default).    </td></tr>
842 <tr><td> \ref ARM_SAI_FRAME_SYNC_POLARITY_LOW    </td>
843     <td> Frame Sync is active low (User protocol only, ignored otherwise).   </td></tr>
844 <tr><td> \ref ARM_SAI_FRAME_SYNC_EARLY           </td>
845     <td style="text-align:right">  19  </td>
846         <td> Frame Sync Early              </td>
847     <td> Frame Sync one bit before the first bit of the frame (User protocol only, ignored otherwise).                   </td></tr>
848 <tr><td> \ref ARM_SAI_SLOT_COUNT(n)              </td>
849     <td style="text-align:right"> 20..24         </td>
850         <td> Frame Sync Count                        </td>
851     <td> Number of slots in frame; the possible range for \em n is \token{1..32}; default=\token{1};  User protocol only, ignored otherwise.  </td></tr>
852 <tr><td> \ref ARM_SAI_SLOT_SIZE_DEFAULT   \anchor frame_slot_size  </td>
853     <td rowspan="3" style="text-align:right"> 25..26                </td>
854         <td rowspan="3">  Frame Slot Size                               </td>
855     <td> Slot size is equal to data size (default).                 </td></tr>
856 <tr><td> \ref ARM_SAI_SLOT_SIZE_16                                  </td>
857     <td> Slot size is \token{16 bits} (User protocol only, ignored otherwise).     </td></tr>
858 <tr><td> \ref ARM_SAI_SLOT_SIZE_32                                  </td>
859     <td> Slot size is \token{32 bits} (User protocol only, ignored otherwise).     </td></tr>
860 <tr><td> \ref ARM_SAI_SLOT_OFFSET(n)     </td>
861     <td style="text-align:right"> 27..31 </td>
862         <td> Frame Slot Offset               </td>
863     <td> Offset of first data bit in slot; The range for \em n is \token{0..31}; default=\token{0};  User protocol only, ignored otherwise.   </td></tr>
864 </table>
865
866
867 \anchor mckl_prescaler
868 Depending on the \em control operation, the parameter \em arg2 specifies the Master Clock (MCLK) prescaler and calculates the audio frequency automatically.
869
870 Parameter \em arg2                       | MCLK Prescaler
871 :----------------------------------------|:--------------------------------------------
872 \ref ARM_SAI_MCLK_PRESCALER(n)           | MCLK prescaler; Audio frequency = MCLK/n; the range for \em n is \token{1..4096}; default=\token{1}.
873
874
875 \b Example
876
877 \code
878 extern ARM_DRIVER_SAI Driver_SAI0;
879  
880 // configure Transmitter to Asynchronous Master: I2S Protocol, 16-bit data, 16kHz Audio frequency
881 status = Driver_SAI0.Control(ARM_SAI_CONFIGURE_TX | 
882                              ARM_SAI_MODE_MASTER  | 
883                              ARM_SAI_ASYNCHRONOUS | 
884                              ARM_SAI_PROTOCOL_I2S | 
885                              ARM_SAI_DATA_SIZE(16), 0, 16000);
886  
887 // configure Receiver to Asynchronous Master: I2S Protocol, 16-bit data, 16kHz Audio frequency
888 status = Driver_SAI0.Control(ARM_SAI_CONFIGURE_RX | 
889                              ARM_SAI_MODE_MASTER  | 
890                              ARM_SAI_ASYNCHRONOUS | 
891                              ARM_SAI_PROTOCOL_I2S | 
892                              ARM_SAI_DATA_SIZE(16), 0, 16000);
893  
894 // enable Transmitter
895 status = Driver_SAI0.Control(ARM_SAI_CONTROL_TX, 1, 0);
896  
897 // enable Receiver
898 status = Driver_SAI0.Control(ARM_SAI_CONTROL_RX, 1, 0);
899 \endcode
900
901 *****************************************************************************************************************/
902
903 ARM_SAI_STATUS ARM_SAI_GetStatus (void)  {
904   return { 0 };
905 }
906 /**
907 \fn ARM_SAI_STATUS ARM_SAI_GetStatus (void)
908 \details
909 The function \b ARM_SAI_GetStatus retrieves the current SAI interface status.
910 *****************************************************************************************************************/
911
912 void ARM_SAI_SignalEvent (uint32_t event)  {
913   // function body
914 }
915 /**
916 \fn void ARM_SAI_SignalEvent (uint32_t event)
917 \details
918 The function \b ARM_SAI_SignalEvent is a callback function registered by the function \ref ARM_SAI_Initialize. 
919
920 The parameter \em event indicates one or more events that occurred during driver operation.
921 Each event is encoded in a separate bit and therefore it is possible to signal multiple events within the same call. 
922
923 The following events can be generated:
924
925 Parameter \em event                        | Bit | Description 
926 ------------------------------------------ |:---:|:-----------
927 \ref ARM_SAI_EVENT_SEND_COMPLETE           |  0  | Occurs after call to \ref ARM_SAI_Send to indicate that all the data has been sent (or queued in transmit buffers). The driver is ready for the next call to \ref ARM_SAI_Send.
928 \ref ARM_SAI_EVENT_RECEIVE_COMPLETE        |  1  | Occurs after call to \ref ARM_SAI_Receive to indicate that all the data has been received. The driver is ready for the next call to \ref ARM_SAI_Receive.
929 \ref ARM_SAI_EVENT_TX_UNDERFLOW            |  2  | Occurs when data is to be sent but send operation has not been started. Data field \em tx_underflow = \token{1} of \ref ARM_SAI_STATUS.
930 \ref ARM_SAI_EVENT_RX_OVERFLOW             |  3  | Occurs when data is received but receive operation has not been started. Data field \em rx_underflow = \token{1} of \ref ARM_SAI_STATUS.
931 \ref ARM_SAI_EVENT_FRAME_ERROR             |  4  | Occurs in slave mode when invalid synchronization frame is detected. Data field \em  event_frame_error = \token{1} of \ref ARM_SAI_STATUS.
932   
933 *****************************************************************************************************************/
934
935 /**
936 @}
937 */ 
938 // End SAI Interface