I2S.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2016, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!*****************************************************************************
33  * @file I2S.h
34  *
35  * @brief I2S driver interface
36  *
37  * The I2S header file should be included in an application as follows:
38  * @code
39  * #include <ti/drivers/I2S.h>
40  * @endcode
41  *
42  * # Overview #
43  * The I2S driver facilitates the use of Inter-IC Sound (I2S), which is
44  * used to connect digital audio devices so that audio signals can be
45  * communicated between devices. The I2S driver simplifies reading and
46  * writing to any of the Multichannel Audio Serial Port (McASP) peripherals
47  * on the board with Receive and Transmit support. These include blocking,
48  * non-blocking, read and write characters on the McASP peripheral.
49  *
50  * The APIs in this driver serve as an interface to a typical RTOS
51  * application. Its purpose is to redirect the I2S APIs to specific
52  * driver implementations which are specified using a pointer to an
53  * #I2S_FxnTable.
54  * The specific peripheral implementations are responsible
55  * for creating all the RTOS specific primitives to allow for thread-safe
56  * operation.
57  *
58  * # Usage #
59  *
60  * To use the I2S driver for reaading and writing data to the I2S peripheral,
61  * the application calls the following APIs:
62  * - I2S_init(): Initialize the I2S driver.
63  * - I2S_Params_init(): Initialize a #I2S_Params structure with default
64  * vaules. Then change the parameters from non-default values as
65  * needed.
66  * - I2S_open(): Open an instance of the I2S driver, passing the
67  * initialized parameters, or NULL, and an index (described later).
68  * - If using callback mode, I2S_read() and I2S_write().
69  * - If using issue/reclaim mode, I2S_readIssue(), I2S_readReclaim(),
70  * I2S_writeIssue() and I2S_writeReclaim().
71  * - I2S_close(): De-initialize the I2S instance.
72  *
73  * ### I2S Driver Configuration #
74  *
75  * In order to use the I2S APIs, the application is required
76  * to provide device-specific I2S configuration in the Board.c file.
77  * The I2S driver interface defines a configuration data structure:
78  *
79  * @code
80  * typedef struct I2S_Config_ {
81  * // Pointer to driver-specific implementation of I2S functions
82  * I2S_FxnTable const *fxnTablePtr;
83  * void *object; // Driver specific data object
84  * void const *hwAttrs; // Driver specific hardware attributes
85  * } I2S_Config;
86  * @endcode
87  *
88  * The application must declare an array of I2S_Config elements, named
89  * I2S_config[]. Each element of I2S_config[] must be populated with
90  * pointers to a device specific I2S driver implementation's function
91  * table, driver object, and hardware attributes. The hardware attributes
92  * define properties such as the I2S peripheral's base address and pins.
93  * Each element in I2S_config[] corresponds to an I2S instance, and
94  * and none of the elements should have NULL pointers.
95  * There is no correlation between the index and the peripheral
96  * designation (such as I2S0 or I2S1). For example, it is possible
97  * to use I2S_config[0] for I2S1.
98  *
99  * Because I2S configuration is very device dependent, you will need to
100  * check the doxygen for the device specific I2S implementation. There you
101  * will find a description of the I2S hardware attributes. Please also
102  * refer to the board.c file of any of your examples to see the I2S
103  * configuration.
104  *
105  * ### Initializing the I2S Driver #
106  *
107  * I2S_init() must be called before any other I2S APIs. This function
108  * iterates through the elements of the I2S_config[] array, calling
109  * the element's device implementation I2S initialization function.
110  *
111  * ### I2S Parameters
112  *
113  * The #I2S_Params structure is passed to the I2S_open() call. If NULL
114  * is passed for the parameters, I2S_open() uses default parameters.
115  * An #I2S_Params structure is initialized with default values by passing
116  * it to I2S_Params_init().
117  * Some of the I2S parameters are described below. To see brief descriptions
118  * of all the parameters, see #I2S_Params.
119  *
120  * #### I2S Operation Mode
121  * The I2S operation mode determines whether transmit and/or receive modes
122  * are enabled. The mode is specified with one of the following constants:
123  * - #I2S_OPMODE_TX_ONLY: Enable transmit only.
124  * - #I2S_OPMODE_RX_ONLY: Enable receive only.
125  * - #I2S_OPMODE_TX_RX_SYNC: Enable both receive and transmit.
126  *
127  * #### I2S Data Mode
128  * A separate data mode may be specified for read calls and write calls.
129  * The available modes are:
130  * - #I2S_MODE_CALLBACK: This mode is non-blocking. Calls to I2S_read() or
131  * I2S_write() return immediately. When the transfer is finished, the
132  * user configured callback function is called.
133  * - #I2S_MODE_ISSUERECLAIM: Call I2S_readIssue() and I2S_writeIssue() to
134  * queue buffers to the I2S. I2S_readReclaim() blocks until a buffer
135  * of data is available. I2S_writeReclaim() blocks until a buffer of
136  * data has been issued and the descriptor can be returned back to the
137  * caller.
138  *
139  * ### Opening the I2S Driver #
140  * After initializing the I2S driver by calling I2S_init(), the application
141  * can open an I2S instance by calling I2S_open(). This function
142  * takes an index into the I2S_config[] array, and an I2S parameters data
143  * structure. The I2S instance is specified by the index of the I2S in
144  * I2S_config[]. Only one I2S index can be used at a time;
145  * calling I2S_open() a second time with the same index previosly
146  * passed to I2S_open() will result in an error. You can,
147  * though, re-use the index if the instance is closed via I2S_close().
148  *
149  * If NULL is passed for the I2S_Params structure to I2S_open(), default values
150  * are used. If the open call is successful, it returns a non-NULL value.
151  *
152  * Example opening an I2S driver instance:
153  * @code
154  * I2S_Handle handle;
155  * I2S_Params params;
156  *
157  * I2S_Params_init(&params);
158  * params.operationMode = I2S_MODE_TX_RX_SYNC;
159  * < Change other params as required >
160  *
161  * handle = I2S_open(Board_I2S0, &params);
162  * if (!handle) {
163  * // Error opening I2S, handle accordingly
164  * }
165  * @endcode
166  *
167  * ### Writing Data #
168  * The following example calls I2S_writeIssue() to write to an I2S driver
169  * instance that has been opened. It first queues up two buffers of text.
170  * Within an infinite loop, it calls I2S_writeReclaim() to retrieve a
171  * buffer and then re-queues the buffer.
172  *
173  * @code
174  * const unsigned char hello[] = "Hello World\n";
175  * const unsigned char hello1[] = "Hello World1\n";
176  * I2S_BufDesc writeBuffer1;
177  * I2S_BufDesc writeBuffer2;
178  * I2S_BufDesc *pDesc = NULL;
179  *
180  * writeBuffer1.bufPtr = &hello;
181  * writeBuffer1.bufSize = sizeof(hello);
182  * writeBuffer2.bufPtr = &hello1;
183  * writeBuffer2.bufSize = sizeof(hello1);
184  *
185  * ret = I2S_writeIssue(handle, &writeBuffer1);
186  * ret = I2S_writeIssue(handle, &writeBuffer2);
187  *
188  * while(1) {
189  * ret = I2S_writeReclaim(handle, &pDesc);
190  * pDesc->bufPtr = &hello;;
191  * pDesc->bufSize = sizeof(hello);
192  * ret = I2S_writeIssue(handle, pDesc);
193  * }
194  *
195  * @endcode
196  *
197  * ### Reading Data #
198  * The following example calls I2S_readIssue() to queue a buffer for
199  * reading from an I2S driver instance. It first queues up two buffers of
200  * text. Within an infinite loop, it then calls I2S_readReclaim() to retrieve
201  * a full buffer of data.
202  *
203  * @code
204  * unsigned char rxBuffer[20];
205  * unsigned char rxBuffer1[20];
206  * I2S_BufDesc readBuffer1;
207  * I2S_BufDesc readBuffer2;
208  * I2S_BufDesc *pDesc = NULL;
209  *
210  * readBuffer1.bufPtr = &rxBuffer;
211  * readBuffer1.bufSize = 20;
212  * readBuffer2.bufPtr = &rxBuffer1;
213  * readBuffer2.bufSize = 20;
214  *
215  * ret = I2S_readIssue(handle, &readBuffer1);
216  * ret = I2S_readIssue(handle, &readBuffer2);
217  *
218  * while(1)
219  * {
220  * ret = I2S_readReclaim(handle, &pDesc);
221  * pDesc->bufPtr = &rxBuffer;
222  * pDesc->bufSize = 20;
223  * ret = I2S_readIssue(handle, pDesc);
224  * }
225  * @endcode
226  *
227  * # Implementation #
228  *
229  * The I2S driver interface module is joined (at link time) to an
230  * array of I2S_Config data structures named *I2S_config*.
231  * *I2S_config* is implemented in the application with each entry being an
232  * instance of a I2S peripheral. Each entry in *I2S_config* contains a:
233  * - (I2S_FxnTable *) to a set of functions that implement a I2S peripheral
234  * - (void *) data object that is associated with the I2S_FxnTable
235  * - (void *) hardware attributes that are associated to the I2S_FxnTable
236  *
237  *******************************************************************************
238  */
239 
240 #ifndef ti_drivers_I2S__include
241 #define ti_drivers_I2S__include
242 
243 #ifdef __cplusplus
244 extern "C" {
245 #endif
246 
247 #include <stdint.h>
248 #include <stddef.h>
249 
250 #include <ti/drivers/utils/List.h>
251 
269 #define I2S_CMD_RESERVED (32)
270 
283 #define I2S_STATUS_RESERVED (-32)
284 
298 #define I2S_STATUS_SUCCESS (0)
299 
306 #define I2S_STATUS_ERROR (-1)
307 
315 #define I2S_STATUS_UNDEFINEDCMD (-2)
316 
326 /* Add I2S_CMD_<commands> here */
327 
332 #define I2S_ERROR (I2S_STATUS_ERROR)
333 
337 #define I2S_WAIT_FOREVER (~(0U))
338 
342 typedef struct I2S_Config_ *I2S_Handle;
343 
347 typedef struct I2S_BufDesc_ {
348 
351 
353  void *bufPtr;
354 
356  size_t bufSize;
357 
359  uintptr_t descArg;
360 } I2S_BufDesc;
361 
372 typedef void (*I2S_Callback)(I2S_Handle handle, I2S_BufDesc *desc);
373 
380 typedef enum I2S_DataMode_ {
386 
394 } I2S_DataMode;
395 
401 typedef enum I2S_OpMode_ {
405 } I2S_OpMode;
406 
418 
424 typedef enum I2S_PinMode_ {
428 } I2S_PinMode;
429 
438 typedef struct I2S_Params_ {
440  I2S_OpMode operationMode;
441 
444 
446  uint8_t slotLength;
447 
449  uint8_t bitsPerSample;
450 
452  uint8_t numChannels;
453 
455  I2S_DataMode readMode;
456 
459 
461  uint32_t readTimeout;
462 
464  I2S_DataMode writeMode;
465 
468 
470  uint32_t writeTimeout;
471 
474 } I2S_Params;
475 
480 typedef void (*I2S_CloseFxn) (I2S_Handle handle);
481 
486 typedef int_fast16_t (*I2S_ControlFxn)(I2S_Handle handle,
487  uint_fast16_t cmd,
488  void *arg);
489 
494 typedef void (*I2S_InitFxn)(I2S_Handle handle);
495 
500 typedef I2S_Handle (*I2S_OpenFxn)(I2S_Handle handle, I2S_Params *params);
501 
506 typedef int_fast16_t (*I2S_IssueFxn)(I2S_Handle handle, I2S_BufDesc *desc);
507 
512 typedef size_t (*I2S_ReclaimFxn)(I2S_Handle handle, I2S_BufDesc **desc);
513 
519 typedef struct I2S_FxnTable_ {
522 
525 
528 
531 
534 
537 
540 
543 
544 } I2S_FxnTable;
545 
556 typedef struct I2S_Config_ {
560 
562  void *object;
563 
565  void const *hwAttrs;
566 } I2S_Config;
567 
578 extern void I2S_close(I2S_Handle handle);
579 
617 extern int_fast16_t I2S_control(I2S_Handle handle,
618  uint_fast16_t cmd,
619  void *arg);
620 
629 extern void I2S_init(void);
630 
651 extern I2S_Handle I2S_open(uint_least8_t index, I2S_Params *params);
652 
677 extern void I2S_Params_init(I2S_Params *params);
678 
691 extern int_fast16_t I2S_read(I2S_Handle handle, I2S_BufDesc *desc);
692 
707 extern int_fast16_t I2S_readIssue(I2S_Handle handle, I2S_BufDesc *desc);
708 
718 extern size_t I2S_readReclaim(I2S_Handle handle, I2S_BufDesc **pDesc);
719 
732 extern int_fast16_t I2S_write(I2S_Handle handle, I2S_BufDesc *desc);
733 
746 extern int_fast16_t I2S_writeIssue(I2S_Handle handle, I2S_BufDesc *desc);
747 
758 extern size_t I2S_writeReclaim(I2S_Handle handle, I2S_BufDesc **pDesc);
759 
760 #ifdef __cplusplus
761 }
762 #endif
763 
764 #endif /* ti_drivers_I2S__include */
I2S_DataMode_
I2S mode settings.
Definition: I2S.h:380
I2S_Callback writeCallback
Definition: I2S.h:467
struct I2S_Params_ I2S_Params
Basic I2S Parameters.
I2S_Handle I2S_open(uint_least8_t index, I2S_Params *params)
Function to initialize a given I2S peripheral specified by the particular index value. The parameter specifies which mode the I2S will operate.
int_fast16_t(* I2S_ControlFxn)(I2S_Handle handle, uint_fast16_t cmd, void *arg)
A function pointer to a driver specific implementation of I2S_control().
Definition: I2S.h:486
int_fast16_t I2S_read(I2S_Handle handle, I2S_BufDesc *desc)
Function to queue a buffer of data to the I2S in callback mode for reading.
void(* I2S_CloseFxn)(I2S_Handle handle)
A function pointer to a driver specific implementation of I2S_CloseFxn().
Definition: I2S.h:480
size_t I2S_readReclaim(I2S_Handle handle, I2S_BufDesc **pDesc)
Function to retrieve a full buffer of data read by the I2S.
void * bufPtr
Definition: I2S.h:353
I2S_IssueFxn readIssueFxn
Definition: I2S.h:533
int_fast16_t(* I2S_IssueFxn)(I2S_Handle handle, I2S_BufDesc *desc)
A function pointer to a driver specific implementation of I2S_IssueFxn().
Definition: I2S.h:506
Definition: I2S.h:393
Definition: I2S.h:402
I2S_InitFxn initFxn
Definition: I2S.h:527
Definition: I2S.h:426
void(* I2S_Callback)(I2S_Handle handle, I2S_BufDesc *desc)
The definition of a callback function used by the I2S driver when used in I2S_MODE_CALLBACK.
Definition: I2S.h:372
I2S_OpMode operationMode
Definition: I2S.h:440
Basic I2S Parameters.
Definition: I2S.h:438
int_fast16_t I2S_writeIssue(I2S_Handle handle, I2S_BufDesc *desc)
Function to queue a buffer of data to the I2S in Issue/Reclaim mode for writing.
I2S_Handle(* I2S_OpenFxn)(I2S_Handle handle, I2S_Params *params)
A function pointer to a driver specific implementation of I2S_OpenFxn().
Definition: I2S.h:500
uintptr_t descArg
Definition: I2S.h:359
int_fast16_t I2S_control(I2S_Handle handle, uint_fast16_t cmd, void *arg)
Function performs implementation specific features on a given I2S_Handle.
I2S_ControlFxn controlFxn
Definition: I2S.h:524
Definition: I2S.h:414
uint8_t slotLength
Definition: I2S.h:446
size_t I2S_writeReclaim(I2S_Handle handle, I2S_BufDesc **pDesc)
Function to retrieve a buffer that the I2S has finished writing.
uint8_t numChannels
Definition: I2S.h:452
I2S_OpMode_
I2S mode settings.
Definition: I2S.h:401
I2S_OpenFxn openFxn
Definition: I2S.h:530
enum I2S_PinMode_ I2S_PinMode
I2S serial pin mode.
size_t bufSize
Definition: I2S.h:356
Definition: I2S.h:425
void(* I2S_InitFxn)(I2S_Handle handle)
A function pointer to a driver specific implementation of I2S_init().
Definition: I2S.h:494
I2S_PinMode_
I2S serial pin mode.
Definition: I2S.h:424
Definition: I2S.h:385
I2S_IssueFxn writeIssueFxn
Definition: I2S.h:539
uint8_t bitsPerSample
Definition: I2S.h:449
void I2S_init(void)
Function to initializes the I2S module.
Definition: List.h:126
uint32_t writeTimeout
Definition: I2S.h:470
void const * hwAttrs
Definition: I2S.h:565
Definition: I2S.h:416
Definition: I2S.h:403
void * customParams
Definition: I2S.h:473
Definition: I2S.h:415
I2S_ReclaimFxn writeReclaimFxn
Definition: I2S.h:542
List_Elem qElem
Definition: I2S.h:350
uint32_t readTimeout
Definition: I2S.h:461
The definition of a I2S function table that contains the required set of functions to control a speci...
Definition: I2S.h:519
Definition: I2S.h:427
enum I2S_DataMode_ I2S_DataMode
I2S mode settings.
Definition: I2S.h:404
I2S_SerInActiveConfig_
I2S Serializer InActive state settings.
Definition: I2S.h:413
I2S Global configuration.
Definition: I2S.h:556
enum I2S_SerInActiveConfig_ I2S_SerInActiveConfig
I2S Serializer InActive state settings.
void I2S_Params_init(I2S_Params *params)
Function to initialize the I2S_Params struct to its defaults.
I2S_DataMode readMode
Definition: I2S.h:455
struct I2S_BufDesc_ I2S_BufDesc
I2S buffer descriptor for issue/reclaim mode.
uint32_t samplingFrequency
Definition: I2S.h:443
I2S_Callback readCallback
Definition: I2S.h:458
I2S_CloseFxn closeFxn
Definition: I2S.h:521
I2S_FxnTable const * fxnTablePtr
Definition: I2S.h:559
void * object
Definition: I2S.h:562
struct I2S_FxnTable_ I2S_FxnTable
The definition of a I2S function table that contains the required set of functions to control a speci...
I2S_ReclaimFxn readReclaimFxn
Definition: I2S.h:536
int_fast16_t I2S_readIssue(I2S_Handle handle, I2S_BufDesc *desc)
Function to queue a buffer of data to the I2S in Issue/Reclaim mode for reading.
I2S buffer descriptor for issue/reclaim mode.
Definition: I2S.h:347
enum I2S_OpMode_ I2S_OpMode
I2S mode settings.
void I2S_close(I2S_Handle handle)
Function to close a given I2S peripheral specified by the I2S handle.
struct I2S_Config_ * I2S_Handle
A handle that is returned from a I2S_open() call.
Definition: I2S.h:342
struct I2S_Config_ I2S_Config
I2S Global configuration.
I2S_DataMode writeMode
Definition: I2S.h:464
int_fast16_t I2S_write(I2S_Handle handle, I2S_BufDesc *desc)
Function to queue a buffer of data to the I2S in callback mode for writing.
size_t(* I2S_ReclaimFxn)(I2S_Handle handle, I2S_BufDesc **desc)
A function pointer to a driver specific implementation of I2S_ReclaimFxn().
Definition: I2S.h:512
Linked List interface for use in drivers.
Copyright 2017, Texas Instruments Incorporated