1 //*****************************************************************************
2 //
3 // i2s.c - Driver for the I2S controller.
4 //
5 // Copyright (c) 2008-2012 Texas Instruments Incorporated.  All rights reserved.
6 // Software License Agreement
7 //
8 //   Redistribution and use in source and binary forms, with or without
9 //   modification, are permitted provided that the following conditions
10 //   are met:
11 //
12 //   Redistributions of source code must retain the above copyright
13 //   notice, this list of conditions and the following disclaimer.
14 //
15 //   Redistributions in binary form must reproduce the above copyright
16 //   notice, this list of conditions and the following disclaimer in the
17 //   documentation and/or other materials provided with the
18 //   distribution.
19 //
20 //   Neither the name of Texas Instruments Incorporated nor the names of
21 //   its contributors may be used to endorse or promote products derived
22 //   from this software without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 //
36 // This is part of revision 9453 of the Stellaris Peripheral Driver Library.
37 //
38 //*****************************************************************************
39 
40 //*****************************************************************************
41 //
42 //! \addtogroup i2s_api
43 //! @{
44 //
45 //*****************************************************************************
46 
47 #include "inc/hw_i2s.h"
48 #include "inc/hw_ints.h"
49 #include "inc/hw_memmap.h"
50 #include "inc/hw_types.h"
51 #include "driverlib/debug.h"
52 #include "driverlib/i2s.h"
53 #include "driverlib/interrupt.h"
54 
55 //*****************************************************************************
56 //
57 //! Enables the I2S transmit module for operation.
58 //!
59 //! \param ulBase is the I2S module base address.
60 //!
61 //! This function enables the transmit module for operation.  The module
62 //! should be enabled after configuration.  When the module is disabled,
63 //! no data or clocks are generated on the I2S signals.
64 //!
65 //! \return None.
66 //
67 //*****************************************************************************
68 void
I2STxEnable(unsigned long ulBase)69 I2STxEnable(unsigned long ulBase)
70 {
71     //
72     // Check the arguments.
73     //
74     ASSERT(ulBase == I2S0_BASE);
75 
76     //
77     // Enable the tx FIFO service request.
78     //
79     HWREG(ulBase + I2S_O_TXISM) = I2S_TXISM_FFM;
80 
81     //
82     // Read-modify-write the enable bit.
83     //
84     HWREG(ulBase + I2S_O_CFG) |= I2S_CFG_TXEN;
85 }
86 
87 //*****************************************************************************
88 //
89 //! Disables the I2S transmit module for operation.
90 //!
91 //! \param ulBase is the I2S module base address.
92 //!
93 //! This function disables the transmit module for operation.  The module
94 //! should be disabled before configuration.  When the module is disabled,
95 //! no data or clocks are generated on the I2S signals.
96 //!
97 //! \return None.
98 //
99 //*****************************************************************************
100 void
I2STxDisable(unsigned long ulBase)101 I2STxDisable(unsigned long ulBase)
102 {
103     //
104     // Check the arguments.
105     //
106     ASSERT(ulBase == I2S0_BASE);
107 
108     //
109     // Read-modify-write the enable bit.
110     //
111     HWREG(ulBase + I2S_O_CFG) &= ~I2S_CFG_TXEN;
112 }
113 
114 //*****************************************************************************
115 //
116 //! Writes data samples to the I2S transmit FIFO with blocking.
117 //!
118 //! \param ulBase is the I2S module base address.
119 //! \param ulData is the single- or dual-channel I2S data.
120 //!
121 //! This function writes a single-channel sample or combined left-right
122 //! samples to the I2S transmit FIFO.  The format of the sample is determined
123 //! by the configuration that was used with the function I2STxConfigSet().
124 //! If the transmit mode is \b I2S_MODE_DUAL_STEREO then the \e ulData
125 //! parameter contains either the left or right sample.  The left and right
126 //! sample alternate with each write to the FIFO, left sample first.  If the
127 //! transmit mode is \b I2S_MODE_COMPACT_STEREO_16 or
128 //! \b I2S_MODE_COMPACT_STEREO_8, then the \e ulData parameter contains both
129 //! the left and right samples.  If the transmit mode is
130 //! \b I2S_MODE_SINGLE_MONO then the \e ulData parameter contains the single
131 //! channel sample.
132 //!
133 //! For the compact modes, both the left and right samples are written at
134 //! the same time.  If 16-bit compact mode is used, then the least significant
135 //! 16 bits contain the left sample, and the most significant 16 bits contain
136 //! the right sample.  If 8-bit compact mode is used, then the lower 8 bits
137 //! contain the left sample, and the next 8 bits contain the right sample,
138 //! with the upper 16 bits unused.
139 //!
140 //! If there is no room in the transmit FIFO, then this function waits
141 //! in a polling loop until the data can be written.
142 //!
143 //! \return None.
144 //
145 //*****************************************************************************
146 void
I2STxDataPut(unsigned long ulBase,unsigned long ulData)147 I2STxDataPut(unsigned long ulBase, unsigned long ulData)
148 {
149     //
150     // Check the arguments.
151     //
152     ASSERT(ulBase == I2S0_BASE);
153 
154     //
155     // Wait until there is space.
156     //
157     while(HWREG(ulBase + I2S_O_TXLEV) >= 16)
158     {
159     }
160 
161     //
162     // Write the data to the I2S.
163     //
164     HWREG(ulBase + I2S_O_TXFIFO) = ulData;
165 }
166 
167 //*****************************************************************************
168 //
169 //! Writes data samples to the I2S transmit FIFO without blocking.
170 //!
171 //! \param ulBase is the I2S module base address.
172 //! \param ulData is the single- or dual-channel I2S data.
173 //!
174 //! This function writes a single-channel sample or combined left-right
175 //! samples to the I2S transmit FIFO.  The format of the sample is determined
176 //! by the configuration that was used with the function I2STxConfigSet().
177 //! If the transmit mode is \b I2S_MODE_DUAL_STEREO then the \e ulData
178 //! parameter contains either the left or right sample.  The left and right
179 //! sample alternate with each write to the FIFO, left sample first.  If the
180 //! transmit mode is \b I2S_MODE_COMPACT_STEREO_16 or
181 //! \b I2S_MODE_COMPACT_STEREO_8, then the \e ulData parameter contains both
182 //! the left and right samples.  If the transmit mode is
183 //! \b I2S_MODE_SINGLE_MONO then the \e ulData parameter contains the single-
184 //! channel sample.
185 //!
186 //! For the compact modes, both the left and right samples are written at
187 //! the same time.  If 16-bit compact mode is used, then the least significant
188 //! 16 bits contain the left sample, and the most significant 16 bits contain
189 //! the right sample.  If 8-bit compact mode is used, then the lower 8 bits
190 //! contain the left sample, and the next 8 bits contain the right sample,
191 //! with the upper 16 bits unused.
192 //!
193 //! If there is no room in the transmit FIFO, then this function returns
194 //! immediately without writing any data to the FIFO.
195 //!
196 //! \return The number of elements written to the I2S transmit FIFO (1 or 0).
197 //
198 //*****************************************************************************
199 long
I2STxDataPutNonBlocking(unsigned long ulBase,unsigned long ulData)200 I2STxDataPutNonBlocking(unsigned long ulBase, unsigned long ulData)
201 {
202     //
203     // Check the arguments.
204     //
205     ASSERT(ulBase == I2S0_BASE);
206 
207     //
208     // Check for space to write.
209     //
210     if(HWREG(ulBase + I2S_O_TXLEV) < 16)
211     {
212         HWREG(ulBase + I2S_O_TXFIFO) = ulData;
213         return(1);
214     }
215     else
216     {
217         return(0);
218     }
219 }
220 
221 //*****************************************************************************
222 //
223 //! Configures the I2S transmit module.
224 //!
225 //! \param ulBase is the I2S module base address.
226 //! \param ulConfig is the logical OR of the configuration options.
227 //!
228 //! This function is used to configure the options for the I2S transmit
229 //! channel.  The parameter \e ulConfig is the logical OR of the following
230 //! options:
231 //!
232 //! - \b I2S_CONFIG_FORMAT_I2S for standard I2S format,
233 //!   \b I2S_CONFIG_FORMAT_LEFT_JUST for left justified format, or
234 //!   \b I2S_CONFIG_FORMAT_RIGHT_JUST for right justified format.
235 //! - \b I2S_CONFIG_SCLK_INVERT to invert the polarity of the serial bit clock.
236 //! - \b I2S_CONFIG_MODE_DUAL for dual channel stereo,
237 //!   \b I2S_CONFIG_MODE_COMPACT_16 for 16-bit compact stereo mode,
238 //!   \b I2S_CONFIG_MODE_COMPACT_8 for 8-bit compact stereo mode, or
239 //!   \b I2S_CONFIG_MODE_MONO for single channel mono format.
240 //! - \b I2S_CONFIG_CLK_MASTER or \b I2S_CONFIG_CLK_SLAVE to select whether
241 //!   the I2S transmitter is the clock master or slave.
242 //! - \b I2S_CONFIG_SAMPLE_SIZE_32, \b _24, \b _20, \b _16, or \b _8
243 //!   to select the number of bits per sample.
244 //! - \b I2S_CONFIG_WIRE_SIZE_32, \b _24, \b _20, \b _16, or \b _8
245 //!   to select the number of bits per word that are transferred on the data
246 //!   line.
247 //! - \b I2S_CONFIG_EMPTY_ZERO or \b I2S_CONFIG_EMPTY_REPEAT to select whether
248 //!   the module transmits zeroes or repeats the last sample when the FIFO is
249 //!   empty.
250 //!
251 //! \return None.
252 //
253 //*****************************************************************************
254 void
I2STxConfigSet(unsigned long ulBase,unsigned long ulConfig)255 I2STxConfigSet(unsigned long ulBase, unsigned long ulConfig)
256 {
257     //
258     // Check the arguments.
259     //
260     ASSERT(ulBase == I2S0_BASE);
261     ASSERT((ulConfig & (I2S_CONFIG_FORMAT_MASK | I2S_CONFIG_MODE_MASK |
262                         I2S_CONFIG_EMPTY_MASK | I2S_CONFIG_CLK_MASK |
263                         I2S_CONFIG_SAMPLE_SIZE_MASK |
264                         I2S_CONFIG_WIRE_SIZE_MASK)) == ulConfig);
265 
266     //
267     // Check to see if a compact mode is used.
268     //
269     if((ulConfig & I2S_CONFIG_MODE_MASK) == I2S_CONFIG_MODE_COMPACT_8)
270     {
271         //
272         // If compact 8 mode is used, then need to adjust some bits
273         // before writing the config register.  Also set the FIFO
274         // config register for 8-bit compact samples.
275         //
276         ulConfig &= ~I2S_CONFIG_MODE_MONO;
277         HWREG(ulBase + I2S_O_TXFIFOCFG) = I2S_TXFIFOCFG_CSS;
278     }
279     else
280     {
281         //
282         // If compact 8 mode is not used, then set the FIFO config
283         // register for 16 bit.  This setting is okay if a compact
284         // mode is not used.
285         //
286         HWREG(ulBase + I2S_O_TXFIFOCFG) = 0;
287     }
288 
289     //
290     // Write the configuration register.  Because all the fields are
291     // specified by the configuration parameter, it is not necessary
292     // to do a read-modify-write.
293     //
294     HWREG(ulBase + I2S_O_TXCFG) = ulConfig;
295 }
296 
297 //*****************************************************************************
298 //
299 //! Sets the FIFO level at which a service request is generated.
300 //!
301 //! \param ulBase is the I2S module base address.
302 //! \param ulLevel is the FIFO service request limit.
303 //!
304 //! This function is used to set the transmit FIFO fullness level at which a
305 //! service request occurs.  The service request is used to generate an
306 //! interrupt or a DMA transfer request.  The transmit FIFO generates a
307 //! service request when the number of items in the FIFO is less than the level
308 //! specified in the \e ulLevel parameter.  For example, if \e ulLevel is 8,
309 //! then a service request is generated when there are less than 8 samples
310 //! remaining in the transmit FIFO.
311 //!
312 //! For the purposes of counting the FIFO level, a left-right sample pair
313 //! counts as 2, whether the mode is dual or compact stereo.  When mono mode is
314 //! used, internally the mono sample is still treated as a sample pair, so a
315 //! single mono sample counts as 2.  Because the FIFO always deals with sample
316 //! pairs, the level must be an even number from 0 to 16.  The maximum value is
317 //! 16, which causes a service request when there is any room in the FIFO.
318 //! The minimum value is 0, which disables the service request.
319 //!
320 //! \return None.
321 //
322 //*****************************************************************************
323 void
I2STxFIFOLimitSet(unsigned long ulBase,unsigned long ulLevel)324 I2STxFIFOLimitSet(unsigned long ulBase, unsigned long ulLevel)
325 {
326     //
327     // Check the arguments.
328     //
329     ASSERT(ulBase == I2S0_BASE);
330     ASSERT(ulLevel <= 16);
331 
332     //
333     // Write the FIFO limit
334     //
335     HWREG(ulBase + I2S_O_TXLIMIT) = ulLevel;
336 }
337 
338 //*****************************************************************************
339 //
340 //! Gets the current setting of the FIFO service request level.
341 //!
342 //! \param ulBase is the I2S module base address.
343 //!
344 //! This function is used to get the value of the transmit FIFO service
345 //! request level.  This value is set using the I2STxFIFOLimitSet()
346 //! function.
347 //!
348 //! \return Returns the current value of the FIFO service request limit.
349 //
350 //*****************************************************************************
351 unsigned long
I2STxFIFOLimitGet(unsigned long ulBase)352 I2STxFIFOLimitGet(unsigned long ulBase)
353 {
354     //
355     // Check the arguments.
356     //
357     ASSERT(ulBase == I2S0_BASE);
358 
359     //
360     // Read and return the FIFO limit
361     //
362     return(HWREG(ulBase + I2S_O_TXLIMIT));
363 }
364 
365 //*****************************************************************************
366 //
367 //! Gets the number of samples in the transmit FIFO.
368 //!
369 //! \param ulBase is the I2S module base address.
370 //!
371 //! This function is used to get the number of samples in the transmit FIFO.
372 //! For the purposes of measuring the FIFO level, a left-right sample pair
373 //! counts as 2, whether the mode is dual or compact stereo.  When mono mode is
374 //! used, internally the mono sample is still treated as a sample pair, so a
375 //! single mono sample counts as 2.  Because the FIFO always deals with sample
376 //! pairs, normally the level is an even number from 0 to 16.  If dual stereo
377 //! mode is used and only the left sample has been written without the matching
378 //! right sample, then the FIFO level is an odd value.  If the FIFO level is
379 //! odd, it indicates a left-right sample mismatch.
380 //!
381 //! \return Returns the number of samples in the transmit FIFO, which is
382 //! normally an even number.
383 //
384 //*****************************************************************************
385 unsigned long
I2STxFIFOLevelGet(unsigned long ulBase)386 I2STxFIFOLevelGet(unsigned long ulBase)
387 {
388     //
389     // Check the arguments.
390     //
391     ASSERT(ulBase == I2S0_BASE);
392 
393     //
394     // Read and return the transmit FIFO level.
395     //
396     return(HWREG(ulBase + I2S_O_TXLEV));
397 }
398 
399 //*****************************************************************************
400 //
401 //! Enables the I2S receive module for operation.
402 //!
403 //! \param ulBase is the I2S module base address.
404 //!
405 //! This function enables the receive module for operation.  The module should
406 //! be enabled after configuration.  When the module is disabled, no data is
407 //! clocked in regardless of the signals on the I2S interface.
408 //!
409 //! \return None.
410 //
411 //*****************************************************************************
412 void
I2SRxEnable(unsigned long ulBase)413 I2SRxEnable(unsigned long ulBase)
414 {
415     //
416     // Check the arguments.
417     //
418     ASSERT(ulBase == I2S0_BASE);
419 
420     //
421     // Enable the tx FIFO service request.
422     //
423     HWREG(ulBase + I2S_O_RXISM) = I2S_RXISM_FFM;
424 
425     //
426     // Read-modify-write the enable bit.
427     //
428     HWREG(ulBase + I2S_O_CFG) |= I2S_CFG_RXEN;
429 }
430 
431 //*****************************************************************************
432 //
433 //! Disables the I2S receive module for operation.
434 //!
435 //! \param ulBase is the I2S module base address.
436 //!
437 //! This function disables the receive module for operation.  The module should
438 //! be disabled before configuration.  When the module is disabled, no data is
439 //! clocked in regardless of the signals on the I2S interface.
440 //!
441 //! \return None.
442 //
443 //*****************************************************************************
444 void
I2SRxDisable(unsigned long ulBase)445 I2SRxDisable(unsigned long ulBase)
446 {
447     //
448     // Check the arguments.
449     //
450     ASSERT(ulBase == I2S0_BASE);
451 
452     //
453     // Read-modify-write the enable bit.
454     //
455     HWREG(ulBase + I2S_O_CFG) &= ~I2S_CFG_RXEN;
456 }
457 
458 //*****************************************************************************
459 //
460 //! Reads data samples from the I2S receive FIFO with blocking.
461 //!
462 //! \param ulBase is the I2S module base address.
463 //! \param pulData points to storage for the returned I2S sample data.
464 //!
465 //! This function reads a single channel sample or combined left-right
466 //! samples from the I2S receive FIFO.  The format of the sample is determined
467 //! by the configuration that was used with the function I2SRxConfigSet().
468 //! If the receive mode is \b I2S_MODE_DUAL_STEREO then the returned value
469 //! contains either the left or right sample.  The left and right sample
470 //! alternate with each read from the FIFO, left sample first.  If the receive
471 //! mode is \b I2S_MODE_COMPACT_STEREO_16 or \b I2S_MODE_COMPACT_STEREO_8, then
472 //! the returned data contains both the left and right samples.  If the
473 //! receive mode is \b I2S_MODE_SINGLE_MONO then the returned data
474 //! contains the single channel sample.
475 //!
476 //! For the compact modes, both the left and right samples are read at
477 //! the same time.  If 16-bit compact mode is used, then the least significant
478 //! 16 bits contain the left sample, and the most significant 16 bits contain
479 //! the right sample.  If 8-bit compact mode is used, then the lower 8 bits
480 //! contain the left sample, and the next 8 bits contain the right sample,
481 //! with the upper 16 bits unused.
482 //!
483 //! If there is no data in the receive FIFO, then this function waits
484 //! in a polling loop until data is available.
485 //!
486 //! \return None.
487 //
488 //*****************************************************************************
489 void
I2SRxDataGet(unsigned long ulBase,unsigned long * pulData)490 I2SRxDataGet(unsigned long ulBase, unsigned long *pulData)
491 {
492     //
493     // Check the arguments.
494     //
495     ASSERT(ulBase == I2S0_BASE);
496 
497     //
498     // Wait until there is data available.
499     //
500     while(HWREG(ulBase + I2S_O_RXLEV) == 0)
501     {
502     }
503 
504     //
505     // Read data from the I2S receive FIFO.
506     //
507     *pulData = HWREG(ulBase + I2S_O_RXFIFO);
508 }
509 
510 //*****************************************************************************
511 //
512 //! Reads data samples from the I2S receive FIFO without blocking.
513 //!
514 //! \param ulBase is the I2S module base address.
515 //! \param pulData points to storage for the returned I2S sample data.
516 //!
517 //! This function reads a single channel sample or combined left-right
518 //! samples from the I2S receive FIFO.  The format of the sample is determined
519 //! by the configuration that was used with the function I2SRxConfigSet().
520 //! If the receive mode is \b I2S_MODE_DUAL_STEREO then the received data
521 //! contains either the left or right sample.  The left and right sample
522 //! alternate with each read from the FIFO, left sample first.  If the receive
523 //! mode is \b I2S_MODE_COMPACT_STEREO_16 or \b I2S_MODE_COMPACT_STEREO_8, then
524 //! the received data contains both the left and right samples.  If the
525 //! receive mode is \b I2S_MODE_SINGLE_MONO then the received data
526 //! contains the single channel sample.
527 //!
528 //! For the compact modes, both the left and right samples are read at
529 //! the same time.  If 16-bit compact mode is used, then the least significant
530 //! 16 bits contain the left sample, and the most significant 16 bits contain
531 //! the right sample.  If 8-bit compact mode is used, then the lower 8 bits
532 //! contain the left sample, and the next 8 bits contain the right sample,
533 //! with the upper 16 bits unused.
534 //!
535 //! If there is no data in the receive FIFO, then this function returns
536 //! immediately without reading any data from the FIFO.
537 //!
538 //! \return The number of elements read from the I2S receive FIFO (1 or 0).
539 //
540 //*****************************************************************************
541 long
I2SRxDataGetNonBlocking(unsigned long ulBase,unsigned long * pulData)542 I2SRxDataGetNonBlocking(unsigned long ulBase, unsigned long *pulData)
543 {
544     //
545     // Check the arguments.
546     //
547     ASSERT(ulBase == I2S0_BASE);
548 
549     //
550     // Check for available samples.
551     //
552     if(HWREG(ulBase + I2S_O_RXLEV) != 0)
553     {
554         *pulData = HWREG(ulBase + I2S_O_RXFIFO);
555         return(1);
556     }
557     else
558     {
559         return(0);
560     }
561 }
562 
563 //*****************************************************************************
564 //
565 //! Configures the I2S receive module.
566 //!
567 //! \param ulBase is the I2S module base address.
568 //! \param ulConfig is the logical OR of the configuration options.
569 //!
570 //! This function is used to configure the options for the I2S receive
571 //! channel.  The parameter \e ulConfig is the logical OR of the following
572 //! options:
573 //!
574 //! - \b I2S_CONFIG_FORMAT_I2S for standard I2S format,
575 //!   \b I2S_CONFIG_FORMAT_LEFT_JUST for left justified format, or
576 //!   \b I2S_CONFIG_FORMAT_RIGHT_JUST for right justified format.
577 //! - \b I2S_CONFIG_SCLK_INVERT to invert the polarity of the serial bit clock.
578 //! - \b I2S_CONFIG_MODE_DUAL for dual channel stereo,
579 //!   \b I2S_CONFIG_MODE_COMPACT_16 for 16-bit compact stereo mode,
580 //!   \b I2S_CONFIG_MODE_COMPACT_8 for 8-bit compact stereo mode, or
581 //!   \b I2S_CONFIG_MODE_MONO for single channel mono format.
582 //! - \b I2S_CONFIG_CLK_MASTER or \b I2S_CONFIG_CLK_SLAVE to select whether
583 //!   the I2S receiver is the clock master or slave.
584 //! - \b I2S_CONFIG_SAMPLE_SIZE_32, \b _24, \b _20, \b _16, or \b _8
585 //!   to select the number of bits per sample.
586 //! - \b I2S_CONFIG_WIRE_SIZE_32, \b _24, \b _20, \b _16, or \b _8
587 //!   to select the number of bits per word that are transferred on the data
588 //!   line.
589 //!
590 //! \return None.
591 //
592 //*****************************************************************************
593 void
I2SRxConfigSet(unsigned long ulBase,unsigned long ulConfig)594 I2SRxConfigSet(unsigned long ulBase, unsigned long ulConfig)
595 {
596     //
597     // Check the arguments.
598     //
599     ASSERT(ulBase == I2S0_BASE);
600     ASSERT((ulConfig & (I2S_CONFIG_FORMAT_MASK | I2S_CONFIG_MODE_MASK |
601                         I2S_CONFIG_CLK_MASK | I2S_CONFIG_SAMPLE_SIZE_MASK |
602                         I2S_CONFIG_WIRE_SIZE_MASK)) == ulConfig);
603 
604     //
605     // Clear out any prior config of the RX FIFO config register.
606     //
607     HWREG(ulBase + I2S_O_RXFIFOCFG) = 0;
608 
609     //
610     // If mono mode is used, then the FMM bit needs to be set.
611     //
612     if((ulConfig & I2S_CONFIG_MODE_MASK) == I2S_CONFIG_MODE_MONO)
613     {
614         HWREG(ulBase + I2S_O_RXFIFOCFG) |= I2S_RXFIFOCFG_FMM;
615     }
616 
617     //
618     // If a compact mode is used, then the CSS bit needs to be set.
619     //
620     else if((ulConfig & I2S_CONFIG_MODE_MASK) == I2S_CONFIG_MODE_COMPACT_8)
621     {
622         HWREG(ulBase + I2S_O_RXFIFOCFG) |= I2S_RXFIFOCFG_CSS;
623     }
624 
625     //
626     // The "mono" bits must be removed from the configuration word
627     // prior to writing to hardware, because the RX configuration register
628     // does not actually use these bits.
629     //
630     ulConfig &= ~I2S_CONFIG_MODE_MONO;
631 
632     //
633     // Write the configuration register.  Because all the fields are
634     // specified by the configuration parameter, it is not necessary
635     // to do a read-modify-write.
636     //
637     HWREG(ulBase + I2S_O_RXCFG) = ulConfig;
638 }
639 
640 //*****************************************************************************
641 //
642 //! Sets the FIFO level at which a service request is generated.
643 //!
644 //! \param ulBase is the I2S module base address.
645 //! \param ulLevel is the FIFO service request limit.
646 //!
647 //! This function is used to set the receive FIFO fullness level at which a
648 //! service request occurs.  The service request is used to generate an
649 //! interrupt or a DMA transfer request.  The receive FIFO generates a
650 //! service request when the number of items in the FIFO is greater than the
651 //! level specified in the \e ulLevel parameter.  For example, if \e ulLevel is
652 //! 4, then a service request is generated when there are more than 4 samples
653 //! available in the receive FIFO.
654 //!
655 //! For the purposes of counting the FIFO level, a left-right sample pair
656 //! counts as 2, whether the mode is dual or compact stereo.  When mono mode is
657 //! used, internally the mono sample is still treated as a sample pair, so a
658 //! single mono sample counts as 2.  Because the FIFO always deals with sample
659 //! pairs, the level must be an even number from 0 to 16.  The minimum value is
660 //! 0, which causes a service request when there is any data available in
661 //! the FIFO.  The maximum value is 16, which disables the service request
662 //! (because there cannot be more than 16 items in the FIFO).
663 //!
664 //! \return None.
665 //
666 //*****************************************************************************
667 void
I2SRxFIFOLimitSet(unsigned long ulBase,unsigned long ulLevel)668 I2SRxFIFOLimitSet(unsigned long ulBase, unsigned long ulLevel)
669 {
670     //
671     // Check the arguments.
672     //
673     ASSERT(ulBase == I2S0_BASE);
674     ASSERT(ulLevel <= 16);
675 
676     //
677     // Write the FIFO limit
678     //
679     HWREG(ulBase + I2S_O_RXLIMIT) = ulLevel;
680 }
681 
682 //*****************************************************************************
683 //
684 //! Gets the current setting of the FIFO service request level.
685 //!
686 //! \param ulBase is the I2S module base address.
687 //!
688 //! This function is used to get the value of the receive FIFO service
689 //! request level.  This value is set using the I2SRxFIFOLimitSet()
690 //! function.
691 //!
692 //! \return Returns the current value of the FIFO service request limit.
693 //
694 //*****************************************************************************
695 unsigned long
I2SRxFIFOLimitGet(unsigned long ulBase)696 I2SRxFIFOLimitGet(unsigned long ulBase)
697 {
698     //
699     // Check the arguments.
700     //
701     ASSERT(ulBase == I2S0_BASE);
702 
703     //
704     // Read and return the FIFO limit.  The lower bit is masked
705     // because it always reads as 1 and has no meaning.
706     //
707     return(HWREG(ulBase + I2S_O_RXLIMIT) & 0xFFFE);
708 }
709 
710 //*****************************************************************************
711 //
712 //! Gets the number of samples in the receive FIFO.
713 //!
714 //! \param ulBase is the I2S module base address.
715 //!
716 //! This function is used to get the number of samples in the receive FIFO.
717 //! For the purposes of measuring the FIFO level, a left-right sample pair
718 //! counts as 2, whether the mode is dual or compact stereo.  When mono mode is
719 //! used, internally the mono sample is still treated as a sample pair, so a
720 //! single mono sample counts as 2.  Because the FIFO always deals with sample
721 //! pairs, normally the level is an even number from 0 to 16.  If dual stereo
722 //! mode is used and only the left sample has been read without reading the
723 //! matching right sample, then the FIFO level is an odd value.  If the FIFO
724 //! level is odd, it indicates a left-right sample mismatch.
725 //!
726 //! \return Returns the number of samples in the transmit FIFO, which is
727 //! normally an even number.
728 //
729 //*****************************************************************************
730 unsigned long
I2SRxFIFOLevelGet(unsigned long ulBase)731 I2SRxFIFOLevelGet(unsigned long ulBase)
732 {
733     //
734     // Check the arguments.
735     //
736     ASSERT(ulBase == I2S0_BASE);
737 
738     //
739     // Read and return the receive FIFO level.
740     //
741     return(HWREG(ulBase + I2S_O_RXLEV));
742 }
743 
744 //*****************************************************************************
745 //
746 //! Enables the I2S transmit and receive modules for operation.
747 //!
748 //! \param ulBase is the I2S module base address.
749 //!
750 //! This function simultaneously enables the transmit and receive modules for
751 //! operation, providing a synchronized SCLK and LRCLK.  The module should be
752 //! enabled after configuration.  When the module is disabled, no data or
753 //! clocks are generated on the I2S signals.
754 //!
755 //! \return None.
756 //
757 //*****************************************************************************
758 void
I2STxRxEnable(unsigned long ulBase)759 I2STxRxEnable(unsigned long ulBase)
760 {
761     //
762     // Check the arguments.
763     //
764     ASSERT(ulBase == I2S0_BASE);
765 
766     //
767     // Enable the Tx FIFO service request.
768     //
769     HWREG(ulBase + I2S_O_TXISM) = I2S_TXISM_FFM;
770 
771     //
772     // Enable the Rx FIFO service request.
773     //
774     HWREG(ulBase + I2S_O_RXISM) = I2S_RXISM_FFM;
775 
776     //
777     // Enable the transmit and receive modules.
778     //
779     HWREG(ulBase + I2S_O_CFG) |= I2S_CFG_TXEN | I2S_CFG_RXEN;
780 }
781 
782 //*****************************************************************************
783 //
784 //! Disables the I2S transmit and receive modules.
785 //!
786 //! \param ulBase is the I2S module base address.
787 //!
788 //! This function simultaneously disables the transmit and receive modules.
789 //! When the module is disabled, no data or clocks are generated on the I2S
790 //! signals.
791 //!
792 //! \return None.
793 //
794 //*****************************************************************************
795 void
I2STxRxDisable(unsigned long ulBase)796 I2STxRxDisable(unsigned long ulBase)
797 {
798     //
799     // Check the arguments.
800     //
801     ASSERT(ulBase == I2S0_BASE);
802 
803     //
804     // Disable the transmit and receive modules.
805     //
806     HWREG(ulBase + I2S_O_CFG) &= ~(I2S_CFG_TXEN | I2S_CFG_RXEN);
807 }
808 
809 //*****************************************************************************
810 //
811 //! Configures the I2S transmit and receive modules.
812 //!
813 //! \param ulBase is the I2S module base address.
814 //! \param ulConfig is the logical OR of the configuration options.
815 //!
816 //! This function is used to configure the options for the I2S transmit and
817 //! receive channels with identical parameters.  The parameter \e ulConfig is
818 //! the logical OR of the following options:
819 //!
820 //! - \b I2S_CONFIG_FORMAT_I2S for standard I2S format,
821 //!   \b I2S_CONFIG_FORMAT_LEFT_JUST for left justified format, or
822 //!   \b I2S_CONFIG_FORMAT_RIGHT_JUST for right justified format.
823 //! - \b I2S_CONFIG_SCLK_INVERT to invert the polarity of the serial bit clock.
824 //! - \b I2S_CONFIG_MODE_DUAL for dual-channel stereo,
825 //!   \b I2S_CONFIG_MODE_COMPACT_16 for 16-bit compact stereo mode,
826 //!   \b I2S_CONFIG_MODE_COMPACT_8 for 8-bit compact stereo mode, or
827 //!   \b I2S_CONFIG_MODE_MONO for single-channel mono format.
828 //! - \b I2S_CONFIG_CLK_MASTER or \b I2S_CONFIG_CLK_SLAVE to select whether
829 //!   the I2S transmitter is the clock master or slave.
830 //! - \b I2S_CONFIG_SAMPLE_SIZE_32, \b _24, \b _20, \b _16, or \b _8
831 //!   to select the number of bits per sample.
832 //! - \b I2S_CONFIG_WIRE_SIZE_32, \b _24, \b _20, \b _16, or \b _8
833 //!   to select the number of bits per word that are transferred on the data
834 //!   line.
835 //! - \b I2S_CONFIG_EMPTY_ZERO or \b I2S_CONFIG_EMPTY_REPEAT to select whether
836 //!   the module transmits zeroes or repeats the last sample when the FIFO is
837 //!   empty.
838 //!
839 //! \return None.
840 //
841 //*****************************************************************************
842 void
I2STxRxConfigSet(unsigned long ulBase,unsigned long ulConfig)843 I2STxRxConfigSet(unsigned long ulBase, unsigned long ulConfig)
844 {
845     //
846     // Check the arguments.
847     //
848     ASSERT(ulBase == I2S0_BASE);
849     ASSERT((ulConfig & (I2S_CONFIG_FORMAT_MASK | I2S_CONFIG_MODE_MASK |
850                         I2S_CONFIG_EMPTY_MASK | I2S_CONFIG_CLK_MASK |
851                         I2S_CONFIG_SAMPLE_SIZE_MASK |
852                         I2S_CONFIG_WIRE_SIZE_MASK)) == ulConfig);
853 
854     //
855     // Clear out any prior configuration of the FIFO config registers.
856     //
857     HWREG(ulBase + I2S_O_TXFIFOCFG) = 0;
858     HWREG(ulBase + I2S_O_RXFIFOCFG) = 0;
859 
860     //
861     // If mono mode is used, then the FMM bit needs to be set.
862     //
863     if((ulConfig & I2S_CONFIG_MODE_MASK) == I2S_CONFIG_MODE_MONO)
864     {
865         HWREG(ulBase + I2S_O_RXFIFOCFG) |= I2S_RXFIFOCFG_FMM;
866         ulConfig &= ~(I2S_CONFIG_MODE_MONO);
867     }
868 
869     //
870     // If a compact mode is used, then the CSS bit needs to be set.
871     //
872     if((ulConfig & I2S_CONFIG_MODE_MASK) == I2S_CONFIG_MODE_COMPACT_8)
873     {
874         HWREG(ulBase + I2S_O_TXFIFOCFG) |= I2S_TXFIFOCFG_CSS;
875         HWREG(ulBase + I2S_O_RXFIFOCFG) |= I2S_RXFIFOCFG_CSS;
876     }
877 
878     //
879     // Write the configuration register.  Because all the fields are specified
880     // by the configuration parameter, it is not necessary to do a
881     // read-modify-write.
882     //
883     HWREG(ulBase + I2S_O_TXCFG) = ulConfig;
884     HWREG(ulBase + I2S_O_RXCFG) = ulConfig;
885 }
886 
887 //*****************************************************************************
888 //
889 //! Selects the source of the master clock, internal or external.
890 //!
891 //! \param ulBase is the I2S module base address.
892 //! \param ulMClock is the logical OR of the master clock configuration
893 //! choices.
894 //!
895 //! This function selects whether the master clock is sourced from the device
896 //! internal PLL or comes from an external pin.  The I2S serial bit clock
897 //! (SCLK) and left-right word clock (LRCLK) are derived from the I2S master
898 //! clock.  The transmit and receive modules can be configured independently.
899 //!  The \e ulMClock parameter is chosen from the following:
900 //!
901 //! - one of \b I2S_TX_MCLK_EXT or \b I2S_TX_MCLK_INT
902 //! - one of \b I2S_RX_MCLK_EXT or \b I2S_RX_MCLK_INT
903 //!
904 //! \return None.
905 //
906 //*****************************************************************************
907 void
I2SMasterClockSelect(unsigned long ulBase,unsigned long ulMClock)908 I2SMasterClockSelect(unsigned long ulBase, unsigned long ulMClock)
909 {
910     unsigned long ulConfig;
911 
912     //
913     // Check the arguments.
914     //
915     ASSERT(ulBase == I2S0_BASE);
916     ASSERT((ulMClock & (I2S_TX_MCLK_EXT | I2S_RX_MCLK_EXT)) == ulMClock);
917 
918     //
919     // Set the clock selection bits in the configuation word.
920     //
921     ulConfig = HWREG(ulBase + I2S_O_CFG) &
922                    ~(I2S_TX_MCLK_EXT | I2S_RX_MCLK_EXT);
923     HWREG(ulBase + I2S_O_CFG) = ulConfig | ulMClock;
924 }
925 
926 //*****************************************************************************
927 //
928 //! Enables I2S interrupt sources.
929 //!
930 //! \param ulBase is the I2S module base address.
931 //! \param ulIntFlags is a bit mask of the interrupt sources to be enabled.
932 //!
933 //! This function enables the specified I2S sources to generate interrupts.
934 //! The \e ulIntFlags parameter can be the logical OR of any of the following
935 //! values:
936 //!
937 //! - \b I2S_INT_RXERR for receive errors
938 //! - \b I2S_INT_RXREQ for receive FIFO service requests
939 //! - \b I2S_INT_TXERR for transmit errors
940 //! - \b I2S_INT_TXREQ for transmit FIFO service requests
941 //!
942 //! \return None.
943 //
944 //*****************************************************************************
945 void
I2SIntEnable(unsigned long ulBase,unsigned long ulIntFlags)946 I2SIntEnable(unsigned long ulBase, unsigned long ulIntFlags)
947 {
948     //
949     // Check the arguments.
950     //
951     ASSERT(ulBase == I2S0_BASE);
952     ASSERT((ulIntFlags & (I2S_INT_RXERR | I2S_INT_RXREQ |
953                           I2S_INT_TXERR | I2S_INT_TXREQ)) == ulIntFlags);
954 
955     //
956     // Enable the specified interrupts.
957     //
958     HWREG(ulBase + I2S_O_IM) |= ulIntFlags;
959 }
960 
961 //*****************************************************************************
962 //
963 //! Disables I2S interrupt sources.
964 //!
965 //! \param ulBase is the I2S module base address.
966 //! \param ulIntFlags is a bit mask of the interrupt sources to be disabled.
967 //!
968 //! This function disables the specified I2S sources for interrupt
969 //! generation.  The \e ulIntFlags parameter can be the logical OR
970 //! of any of the following values: \b I2S_INT_RXERR, \b I2S_INT_RXREQ,
971 //! \b I2S_INT_TXERR, or \b I2S_INT_TXREQ.
972 //!
973 //! \return None.
974 //
975 //*****************************************************************************
976 void
I2SIntDisable(unsigned long ulBase,unsigned long ulIntFlags)977 I2SIntDisable(unsigned long ulBase, unsigned long ulIntFlags)
978 {
979     //
980     // Check the arguments.
981     //
982     ASSERT(ulBase == I2S0_BASE);
983     ASSERT((ulIntFlags & (I2S_INT_RXERR | I2S_INT_RXREQ |
984                           I2S_INT_TXERR | I2S_INT_TXREQ)) == ulIntFlags);
985 
986     //
987     // Enable the specified interrupts.
988     //
989     HWREG(ulBase + I2S_O_IM) &= ~ulIntFlags;
990 }
991 
992 //*****************************************************************************
993 //
994 //! Gets the I2S interrupt status.
995 //!
996 //! \param ulBase is the I2S module base address.
997 //! \param bMasked is set \b true to get the masked interrupt status, or
998 //! \b false to get the raw interrupt status.
999 //!
1000 //! This function returns the I2S interrupt status.  It can return either
1001 //! the raw or masked interrupt status.
1002 //!
1003 //! \return Returns the masked or raw I2S interrupt status, as a bit field
1004 //! of any of the following values: \b I2S_INT_RXERR, \b I2S_INT_RXREQ,
1005 //! \b I2S_INT_TXERR, or \b I2S_INT_TXREQ
1006 //
1007 //*****************************************************************************
1008 unsigned long
I2SIntStatus(unsigned long ulBase,tBoolean bMasked)1009 I2SIntStatus(unsigned long ulBase, tBoolean bMasked)
1010 {
1011     //
1012     // Check the arguments.
1013     //
1014     ASSERT(ulBase == I2S0_BASE);
1015 
1016     //
1017     // Return either the interrupt status or the raw interrupt status as
1018     // requested.
1019     //
1020     if(bMasked)
1021     {
1022         return(HWREG(ulBase + I2S_O_MIS));
1023     }
1024     else
1025     {
1026         return(HWREG(ulBase + I2S_O_RIS));
1027     }
1028 }
1029 
1030 //*****************************************************************************
1031 //
1032 //! Clears pending I2S interrupt sources.
1033 //!
1034 //! \param ulBase is the I2S module base address.
1035 //! \param ulIntFlags is a bit mask of the interrupt sources to be cleared.
1036 //!
1037 //! This function clears the specified pending I2S interrupts.  This function
1038 //! must be called in the interrupt handler to keep the interrupt from being
1039 //! triggered again immediately upon exit.  The \e ulIntFlags parameter can be
1040 //! the logical OR of any of the following values: \b I2S_INT_RXERR,
1041 //! \b I2S_INT_RXREQ, \b I2S_INT_TXERR, or \b I2S_INT_TXREQ.
1042 //!
1043 //! \note Because there is a write buffer in the Cortex-M processor, it may
1044 //! take several clock cycles before the interrupt source is actually cleared.
1045 //! Therefore, it is recommended that the interrupt source be cleared early in
1046 //! the interrupt handler (as opposed to the very last action) to avoid
1047 //! returning from the interrupt handler before the interrupt source is
1048 //! actually cleared.  Failure to do so may result in the interrupt handler
1049 //! being immediately reentered (because the interrupt controller still sees
1050 //! the interrupt source asserted).
1051 //!
1052 //! \return None.
1053 //
1054 //*****************************************************************************
1055 void
I2SIntClear(unsigned long ulBase,unsigned long ulIntFlags)1056 I2SIntClear(unsigned long ulBase, unsigned long ulIntFlags)
1057 {
1058     //
1059     // Check the arguments.
1060     //
1061     ASSERT(ulBase == I2S0_BASE);
1062     ASSERT((ulIntFlags & (I2S_INT_RXERR | I2S_INT_RXREQ |
1063                           I2S_INT_TXERR | I2S_INT_TXREQ)) == ulIntFlags);
1064 
1065     //
1066     // Clear the requested interrupt sources.
1067     //
1068     HWREG(ulBase + I2S_O_IC) = ulIntFlags;
1069 }
1070 
1071 //*****************************************************************************
1072 //
1073 //! Registers an interrupt handler for the I2S controller.
1074 //!
1075 //! \param ulBase is the I2S module base address.
1076 //! \param pfnHandler is a pointer to the function to be called when the
1077 //! interrupt is activated.
1078 //!
1079 //! This function sets and enables the handler to be called when the I2S
1080 //! controller generates an interrupt.  Specific I2S interrupts must still be
1081 //! enabled with the I2SIntEnable() function.  It is the responsibility of the
1082 //! interrupt handler to clear any pending interrupts with I2SIntClear().
1083 //!
1084 //! \sa IntRegister() for important information about registering interrupt
1085 //! handlers.
1086 //!
1087 //! \return None.
1088 //
1089 //*****************************************************************************
1090 void
I2SIntRegister(unsigned long ulBase,void (* pfnHandler)(void))1091 I2SIntRegister(unsigned long ulBase, void (*pfnHandler)(void))
1092 {
1093     //
1094     // Check the arguments.
1095     //
1096     ASSERT(ulBase == I2S0_BASE);
1097     ASSERT(pfnHandler);
1098 
1099     //
1100     // Register the interrupt handler.
1101     //
1102     IntRegister(INT_I2S0, pfnHandler);
1103 
1104     //
1105     // Enable the I2S interface interrupt.
1106     //
1107     IntEnable(INT_I2S0);
1108 }
1109 
1110 //*****************************************************************************
1111 //
1112 //! Unregisters an interrupt handler for the I2S controller.
1113 //!
1114 //! \param ulBase is the I2S module base address.
1115 //!
1116 //! This function disables and clears the handler to be called when the
1117 //! I2S interrupt occurs.
1118 //!
1119 //! \sa IntRegister() for important information about registering interrupt
1120 //! handlers.
1121 //!
1122 //! \return None.
1123 //
1124 //*****************************************************************************
1125 void
I2SIntUnregister(unsigned long ulBase)1126 I2SIntUnregister(unsigned long ulBase)
1127 {
1128     //
1129     // Check the arguments.
1130     //
1131     ASSERT(ulBase == I2S0_BASE);
1132 
1133     //
1134     // Disable the I2S interface interrupt.
1135     //
1136     IntDisable(INT_I2S0);
1137 
1138     //
1139     // Unregister the interrupt handler.
1140     //
1141     IntUnregister(INT_I2S0);
1142 }
1143 
1144 //*****************************************************************************
1145 //
1146 // Close the Doxygen group.
1147 //! @}
1148 //
1149 //*****************************************************************************
1150