]> git.sur5r.net Git - freertos/blob
3bb7a544059874cd9c96995d5de0080eb226d812
[freertos] /
1 /**********************************************************************\r
2 * $Id$          lpc18xx_i2s.c           2011-06-02\r
3 *//**\r
4 * @file         lpc18xx_i2s.c\r
5 * @brief        Contains all functions support for I2S firmware library\r
6 *                       on LPC18xx\r
7 * @version      1.0\r
8 * @date         02. June. 2011\r
9 * @author       NXP MCU SW Application Team\r
10 *\r
11 * Copyright(C) 2011, NXP Semiconductor\r
12 * All rights reserved.\r
13 *\r
14 ***********************************************************************\r
15 * Software that is described herein is for illustrative purposes only\r
16 * which provides customers with programming information regarding the\r
17 * products. This software is supplied "AS IS" without any warranties.\r
18 * NXP Semiconductors assumes no responsibility or liability for the\r
19 * use of the software, conveys no license or title under any patent,\r
20 * copyright, or mask work right to the product. NXP Semiconductors\r
21 * reserves the right to make changes in the software without\r
22 * notification. NXP Semiconductors also make no representation or\r
23 * warranty that such application will be suitable for the specified\r
24 * use without further testing or modification.\r
25 **********************************************************************/\r
26 \r
27 /* Peripheral group ----------------------------------------------------------- */\r
28 /** @addtogroup I2S\r
29  * @{\r
30  */\r
31 \r
32 /* Includes ------------------------------------------------------------------- */\r
33 #include "lpc18xx_i2s.h"\r
34 #include "lpc18xx_cgu.h"\r
35 \r
36 \r
37 /* If this source file built with example, the LPC18xx FW library configuration\r
38  * file in each example directory ("lpc18xx_libcfg.h") must be included,\r
39  * otherwise the default FW library configuration file must be included instead\r
40  */\r
41 #ifdef __BUILD_WITH_EXAMPLE__\r
42 #include "lpc18xx_libcfg.h"\r
43 #else\r
44 #include "lpc18xx_libcfg_default.h"\r
45 #endif /* __BUILD_WITH_EXAMPLE__ */\r
46 \r
47 \r
48 #ifdef _I2S\r
49 \r
50 /* Private Functions ---------------------------------------------------------- */\r
51 \r
52 static uint8_t i2s_GetWordWidth(LPC_I2Sn_Type *I2Sx, uint8_t TRMode);\r
53 static uint8_t i2s_GetChannel(LPC_I2Sn_Type *I2Sx, uint8_t TRMode);\r
54 \r
55 /********************************************************************//**\r
56  * @brief               Get I2S wordwidth value\r
57  * @param[in]   I2Sx I2S peripheral selected, should be: LPC_I2S\r
58  * @param[in]   TRMode is the I2S mode, should be:\r
59  *                                      - I2S_TX_MODE = 0       :transmit mode\r
60  *                                      - I2S_RX_MODE = 1       :receive mode\r
61  * @return              The wordwidth value, should be: 8,16 or 32\r
62  *********************************************************************/\r
63 static uint8_t i2s_GetWordWidth(LPC_I2Sn_Type *I2Sx, uint8_t TRMode) {\r
64         uint8_t value;\r
65 \r
66         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
67         CHECK_PARAM(PARAM_I2S_TRX(TRMode));\r
68 \r
69         if (TRMode == I2S_TX_MODE) {\r
70                 value = (I2Sx->DAO) & 0x03; /* get wordwidth bit */\r
71         } else {\r
72                 value = (I2Sx->DAI) & 0x03; /* get wordwidth bit */\r
73         }\r
74         switch (value) {\r
75         case I2S_WORDWIDTH_8:\r
76                 return 8;\r
77         case I2S_WORDWIDTH_16:\r
78                 return 16;\r
79         default:\r
80                 return 32;\r
81         }\r
82 }\r
83 \r
84 /********************************************************************//**\r
85  * @brief               Get I2S channel value\r
86  * @param[in]   I2Sx I2S peripheral selected, should be: LPC_I2S\r
87  * @param[in]   TRMode is the I2S mode, should be:\r
88  *                                      - I2S_TX_MODE = 0       :transmit mode\r
89  *                                      - I2S_RX_MODE = 1       :receive mode\r
90  * @return              The channel value, should be: 1(mono) or 2(stereo)\r
91  *********************************************************************/\r
92 static uint8_t i2s_GetChannel(LPC_I2Sn_Type *I2Sx, uint8_t TRMode) {\r
93         uint8_t value;\r
94 \r
95         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
96         CHECK_PARAM(PARAM_I2S_TRX(TRMode));\r
97 \r
98         if (TRMode == I2S_TX_MODE) {\r
99                 value = (I2Sx->DAO) & 0x04; /* get bit[2] */\r
100         } else {\r
101                 value = (I2Sx->DAI) & 0x04; /* get bit[2] */\r
102         }\r
103         value >>= 2;\r
104     if(value == I2S_MONO) return 1;\r
105       return 2;\r
106 }\r
107 \r
108 /* End of Private Functions --------------------------------------------------- */\r
109 \r
110 \r
111 /* Public Functions ----------------------------------------------------------- */\r
112 /** @addtogroup I2S_Public_Functions\r
113  * @{\r
114  */\r
115 \r
116 /********************************************************************//**\r
117  * @brief               Initialize I2S\r
118  *                                      - Turn on power and clock\r
119  * @param[in]   I2Sx I2S peripheral selected, should be: LPC_I2S\r
120  * @return              none\r
121  *********************************************************************/\r
122 void I2S_Init(LPC_I2Sn_Type *I2Sx) {\r
123         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
124 \r
125         // Turn on power and clock\r
126         //CGU_ConfigPPWR(CGU_PCONP_PCI2S, ENABLE);\r
127         I2Sx->DAI = I2Sx->DAO = 0x00;\r
128 }\r
129 \r
130 /********************************************************************//**\r
131  * @brief               Configuration I2S, setting:\r
132  *                                      - master/slave mode\r
133  *                                      - wordwidth value\r
134  *                                      - channel mode\r
135  * @param[in]   I2Sx I2S peripheral selected, should be: LPC_I2S\r
136  * @param[in]   TRMode transmit/receive mode, should be:\r
137  *                                      - I2S_TX_MODE = 0       :transmit mode\r
138  *                                      - I2S_RX_MODE = 1       :receive mode\r
139  * @param[in]   ConfigStruct pointer to I2S_CFG_Type structure\r
140  *              which will be initialized.\r
141  * @return              none\r
142  *********************************************************************/\r
143 void I2S_Config(LPC_I2Sn_Type *I2Sx, uint8_t TRMode, I2S_CFG_Type* ConfigStruct)\r
144 {\r
145         uint32_t bps, config;\r
146 \r
147         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
148 \r
149         CHECK_PARAM(PARAM_I2S_WORDWIDTH(ConfigStruct->wordwidth));\r
150         CHECK_PARAM(PARAM_I2S_CHANNEL(ConfigStruct->mono));\r
151         CHECK_PARAM(PARAM_I2S_STOP(ConfigStruct->stop));\r
152         CHECK_PARAM(PARAM_I2S_RESET(ConfigStruct->reset));\r
153         CHECK_PARAM(PARAM_I2S_WS_SEL(ConfigStruct->ws_sel));\r
154         CHECK_PARAM(PARAM_I2S_MUTE(ConfigStruct->mute));\r
155 \r
156         /* Setup clock */\r
157         bps = (ConfigStruct->wordwidth +1)*8;\r
158 \r
159         /* Calculate audio config */\r
160         config = (bps - 1)<<6 | (ConfigStruct->ws_sel)<<5 | (ConfigStruct->reset)<<4 |\r
161                 (ConfigStruct->stop)<<3 | (ConfigStruct->mono)<<2 | (ConfigStruct->wordwidth);\r
162 \r
163         if(TRMode == I2S_RX_MODE){\r
164                 I2Sx->DAI = config;\r
165         }else{\r
166                 I2Sx->DAO = config;\r
167         }\r
168 }\r
169 \r
170 /********************************************************************//**\r
171  * @brief               DeInitial both I2S transmit or receive\r
172  * @param[in]   I2Sx I2S peripheral selected, should be: LPC_I2S\r
173  * @return              none\r
174  *********************************************************************/\r
175 void I2S_DeInit(LPC_I2Sn_Type *I2Sx) {\r
176         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
177 \r
178         // Turn off power and clock\r
179         //CGU_ConfigPPWR(CGU_PCONP_PCI2S, DISABLE);\r
180 }\r
181 \r
182 /********************************************************************//**\r
183  * @brief               Get I2S Buffer Level\r
184  * @param[in]   I2Sx I2S peripheral selected, should be: LPC_I2S\r
185  * @param[in]   TRMode Transmit/receive mode, should be:\r
186  *                                      - I2S_TX_MODE = 0       :transmit mode\r
187  *                                      - I2S_RX_MODE = 1       :receive mode\r
188  * @return              current level of Transmit/Receive Buffer\r
189  *********************************************************************/\r
190 uint8_t I2S_GetLevel(LPC_I2Sn_Type *I2Sx, uint8_t TRMode)\r
191 {\r
192         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
193         CHECK_PARAM(PARAM_I2S_TRX(TRMode));\r
194 \r
195         if(TRMode == I2S_TX_MODE)\r
196         {\r
197                 return ((I2Sx->STATE >> 16) & 0xFF);\r
198         }\r
199         else\r
200         {\r
201                 return ((I2Sx->STATE >> 8) & 0xFF);\r
202         }\r
203 }\r
204 \r
205 /********************************************************************//**\r
206  * @brief               I2S Start: clear all STOP,RESET and MUTE bit, ready to operate\r
207  * @param[in]   I2Sx I2S peripheral selected, should be: LPC_I2S\r
208  * @return              none\r
209  *********************************************************************/\r
210 void I2S_Start(LPC_I2Sn_Type *I2Sx)\r
211 {\r
212         //Clear STOP,RESET and MUTE bit\r
213         I2Sx->DAO &= ~I2S_DAI_RESET;\r
214         I2Sx->DAI &= ~I2S_DAI_RESET;\r
215         I2Sx->DAO &= ~I2S_DAI_STOP;\r
216         I2Sx->DAI &= ~I2S_DAI_STOP;\r
217         I2Sx->DAO &= ~I2S_DAI_MUTE;\r
218 }\r
219 \r
220 /********************************************************************//**\r
221  * @brief               I2S Send data\r
222  * @param[in]   I2Sx I2S peripheral selected, should be: LPC_I2S\r
223  * @param[in]   BufferData pointer to uint32_t is the data will be send\r
224  * @return              none\r
225  *********************************************************************/\r
226 void I2S_Send(LPC_I2Sn_Type *I2Sx, uint32_t BufferData) {\r
227         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
228 \r
229         I2Sx->TXFIFO = BufferData;\r
230 }\r
231 \r
232 /********************************************************************//**\r
233  * @brief               I2S Receive Data\r
234  * @param[in]   I2Sx pointer to LPC_I2Sn_Type, should be: LPC_I2S\r
235  * @return              received value\r
236  *********************************************************************/\r
237 uint32_t I2S_Receive(LPC_I2Sn_Type* I2Sx) {\r
238         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
239 \r
240         return (I2Sx->RXFIFO);\r
241 \r
242 }\r
243 \r
244 /********************************************************************//**\r
245  * @brief               I2S Pause\r
246  * @param[in]   I2Sx I2S peripheral selected, should be: LPC_I2S\r
247  * @param[in]   TRMode is transmit/receive mode, should be:\r
248  *                                      - I2S_TX_MODE = 0       :transmit mode\r
249  *                                      - I2S_RX_MODE = 1       :receive mode\r
250  * @return              none\r
251  *********************************************************************/\r
252 void I2S_Pause(LPC_I2Sn_Type *I2Sx, uint8_t TRMode) {\r
253         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
254         CHECK_PARAM(PARAM_I2S_TRX(TRMode));\r
255 \r
256         if (TRMode == I2S_TX_MODE) //Transmit mode\r
257         {\r
258                 I2Sx->DAO |= I2S_DAO_STOP;\r
259         } else //Receive mode\r
260         {\r
261                 I2Sx->DAI |= I2S_DAI_STOP;\r
262         }\r
263 }\r
264 \r
265 /********************************************************************//**\r
266  * @brief               I2S Mute\r
267  * @param[in]   I2Sx I2S peripheral selected, should be: LPC_I2S\r
268  * @param[in]   TRMode is transmit/receive mode, should be:\r
269  *                                      - I2S_TX_MODE = 0       :transmit mode\r
270  *                                      - I2S_RX_MODE = 1       :receive mode\r
271  * @return              none\r
272  *********************************************************************/\r
273 void I2S_Mute(LPC_I2Sn_Type *I2Sx, uint8_t TRMode) {\r
274         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
275         CHECK_PARAM(PARAM_I2S_TRX(TRMode));\r
276 \r
277         if (TRMode == I2S_TX_MODE) //Transmit mode\r
278         {\r
279                 I2Sx->DAO |= I2S_DAO_MUTE;\r
280         } else //Receive mode\r
281         {\r
282                 I2Sx->DAI |= I2S_DAI_MUTE;\r
283         }\r
284 }\r
285 \r
286 /********************************************************************//**\r
287  * @brief               I2S Stop\r
288  * @param[in]   I2Sx I2S peripheral selected, should be: LPC_I2S\r
289  * @param[in]   TRMode is transmit/receive mode, should be:\r
290  *                                      - I2S_TX_MODE = 0       :transmit mode\r
291  *                                      - I2S_RX_MODE = 1       :receive mode\r
292  * @return              none\r
293  *********************************************************************/\r
294 void I2S_Stop(LPC_I2Sn_Type *I2Sx, uint8_t TRMode) {\r
295         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
296         CHECK_PARAM(PARAM_I2S_TRX(TRMode));\r
297 \r
298         if (TRMode == I2S_TX_MODE) //Transmit mode\r
299         {\r
300                 I2Sx->DAO &= ~I2S_DAO_MUTE;\r
301                 I2Sx->DAO |= I2S_DAO_STOP;\r
302                 I2Sx->DAO |= I2S_DAO_RESET;\r
303         } else //Receive mode\r
304         {\r
305                 I2Sx->DAI |= I2S_DAI_STOP;\r
306                 I2Sx->DAI |= I2S_DAI_RESET;\r
307         }\r
308 }\r
309 \r
310 /********************************************************************//**\r
311  * @brief               Set frequency for I2S\r
312  * @param[in]   I2Sx I2S peripheral selected, should be: LPC_I2S\r
313  * @param[in]   Freq is the frequency for I2S will be set. It can range\r
314  *                              from 16-96 kHz(16, 22.05, 32, 44.1, 48, 96kHz)\r
315  * @param[in]   TRMode is transmit/receive mode, should be:\r
316  *                                      - I2S_TX_MODE = 0       :transmit mode\r
317  *                                      - I2S_RX_MODE = 1       :receive mode\r
318  * @return              Status: ERROR or SUCCESS\r
319  *********************************************************************/\r
320 Status I2S_FreqConfig(LPC_I2Sn_Type *I2Sx, uint32_t Freq, uint8_t TRMode) {\r
321 \r
322         /* Calculate bit rate\r
323          * The formula is:\r
324          *      bit_rate = channel*wordwidth - 1\r
325          * 48kHz sample rate for 16 bit stereo date requires\r
326          * a bit rate of 48000*16*2=1536MHz (MCLK)\r
327          */\r
328         uint32_t i2sPclk;\r
329         uint64_t divider;\r
330         uint8_t bitrate, channel, wordwidth;\r
331         uint32_t x, y;\r
332         uint16_t dif;\r
333         uint16_t error;\r
334         uint16_t x_divide, y_divide;\r
335         uint16_t ErrorOptimal = 0xFFFF;\r
336         uint32_t N;\r
337 \r
338         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
339         CHECK_PARAM(PRAM_I2S_FREQ(Freq));\r
340         CHECK_PARAM(PARAM_I2S_TRX(TRMode));\r
341 \r
342         //LPC_CGU->BASE_VPB1_CLK = 0x08<<24 | AUTO_BLOCK;\r
343         CGU_EntityConnect(CGU_CLKSRC_PLL1, CGU_BASE_APB1);\r
344         i2sPclk = CGU_GetPCLKFrequency(CGU_PERIPHERAL_I2S);\r
345         if(TRMode == I2S_TX_MODE)\r
346         {\r
347                 channel = i2s_GetChannel(I2Sx,I2S_TX_MODE);\r
348                 wordwidth = i2s_GetWordWidth(I2Sx,I2S_TX_MODE);\r
349         }\r
350         else\r
351         {\r
352                 channel = i2s_GetChannel(I2Sx,I2S_RX_MODE);\r
353                 wordwidth = i2s_GetWordWidth(I2Sx,I2S_RX_MODE);\r
354         }\r
355         bitrate = 2 * wordwidth - 1;\r
356 \r
357         /* Calculate X and Y divider\r
358          * The MCLK rate for the I2S transmitter is determined by the value\r
359          * in the I2STXRATE/I2SRXRATE register. The required I2STXRATE/I2SRXRATE\r
360          * setting depends on the desired audio sample rate desired, the format\r
361          * (stereo/mono) used, and the data size.\r
362          * The formula is:\r
363          *              I2S_MCLK = PCLK * (X/Y) / 2\r
364          * We have:\r
365          *              I2S_MCLK = Freq * bit_rate * I2Sx->TXBITRATE;\r
366          * So: (X/Y) = (Freq * bit_rate * I2Sx->TXBITRATE)/PCLK*2\r
367          * We use a loop function to chose the most suitable X,Y value\r
368          */\r
369 \r
370         /* divider is a fixed point number with 16 fractional bits */\r
371         divider = ((uint64_t)(Freq *( bitrate+1) * 2)<<16) / i2sPclk;\r
372 \r
373         /* find N that make x/y <= 1 -> divider <= 2^16 */\r
374         for(N=64;N>=0;N--){\r
375                 if((divider*N) < (1<<16)) break;\r
376         }\r
377 \r
378         if(N == 0) return ERROR;\r
379 \r
380         divider *= N;\r
381 \r
382         for (y = 255; y > 0; y--) {\r
383                 x = y * divider;\r
384                 if(x & (0xFF000000)) continue;\r
385                 dif = x & 0xFFFF;\r
386                 if(dif>0x8000) error = 0x10000-dif;\r
387                 else error = dif;\r
388                 if (error == 0)\r
389                 {\r
390                         y_divide = y;\r
391                         break;\r
392                 }\r
393                 else if (error < ErrorOptimal)\r
394                 {\r
395                         ErrorOptimal = error;\r
396                         y_divide = y;\r
397                 }\r
398         }\r
399         x_divide = ((uint64_t)y_divide * Freq *( bitrate+1)* N * 2)/i2sPclk;\r
400         if(x_divide >= 256) x_divide = 0xFF;\r
401         if(x_divide == 0) x_divide = 1;\r
402         if (TRMode == I2S_TX_MODE)// Transmitter\r
403         {\r
404                 I2Sx->TXBITRATE = N;\r
405                 I2Sx->TXRATE = y_divide | (x_divide << 8);\r
406         } else //Receiver\r
407         {\r
408                 I2Sx->RXBITRATE = N;\r
409                 I2Sx->RXRATE = y_divide | (x_divide << 8);\r
410         }\r
411         return SUCCESS;\r
412 }\r
413 \r
414 /********************************************************************//**\r
415  * @brief               I2S set bitrate\r
416  * @param[in]   I2Sx I2S peripheral selected, should be: LPC_I2S\r
417  * @param[in]   bitrate value will be set, it can be calculate as follows:\r
418  *                                      bitrate = channel * wordwidth - 1\r
419  *                              bitrate value should be in range: 0 .. 63\r
420  * @param[in]   TRMode is transmit/receive mode, should be:\r
421  *                                      - I2S_TX_MODE = 0       :transmit mode\r
422  *                                      - I2S_RX_MODE = 1       :receive mode\r
423  * @return              none\r
424  *********************************************************************/\r
425 void I2S_SetBitRate(LPC_I2Sn_Type *I2Sx, uint8_t bitrate, uint8_t TRMode)\r
426 {\r
427         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
428         CHECK_PARAM(PARAM_I2S_BITRATE(bitrate));\r
429         CHECK_PARAM(PARAM_I2S_TRX(TRMode));\r
430 \r
431         if(TRMode == I2S_TX_MODE)\r
432         {\r
433                 I2Sx->TXBITRATE = bitrate;\r
434         }\r
435         else\r
436         {\r
437                 I2Sx->RXBITRATE = bitrate;\r
438         }\r
439 }\r
440 \r
441 /********************************************************************//**\r
442  * @brief               Configuration operating mode for I2S\r
443  * @param[in]   I2Sx I2S peripheral selected, should be: LPC_I2S\r
444  * @param[in]   ModeConfig pointer to I2S_MODEConf_Type will be used to\r
445  *                              configure\r
446  * @param[in]   TRMode is transmit/receive mode, should be:\r
447  *                                      - I2S_TX_MODE = 0       :transmit mode\r
448  *                                      - I2S_RX_MODE = 1       :receive mode\r
449  * @return              none\r
450  *********************************************************************/\r
451 void I2S_ModeConfig(LPC_I2Sn_Type *I2Sx, I2S_MODEConf_Type* ModeConfig,\r
452                 uint8_t TRMode)\r
453 {\r
454         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
455         CHECK_PARAM(PARAM_I2S_CLKSEL(ModeConfig->clksel));\r
456         CHECK_PARAM(PARAM_I2S_4PIN(ModeConfig->fpin));\r
457         CHECK_PARAM(PARAM_I2S_MCLK(ModeConfig->mcena));\r
458         CHECK_PARAM(PARAM_I2S_TRX(TRMode));\r
459 \r
460         if (TRMode == I2S_TX_MODE) {\r
461                 I2Sx->TXMODE &= ~0x0F; //clear bit 3:0 in I2STXMODE register\r
462                 if (ModeConfig->clksel == I2S_CLKSEL_MCLK) {\r
463                         I2Sx->TXMODE |= 0x02;\r
464                 }\r
465                 if (ModeConfig->fpin == I2S_4PIN_ENABLE) {\r
466                         I2Sx->TXMODE |= (1 << 2);\r
467                 }\r
468                 if (ModeConfig->mcena == I2S_MCLK_ENABLE) {\r
469                         I2Sx->TXMODE |= (1 << 3);\r
470                 }\r
471         } else {\r
472                 I2Sx->RXMODE &= ~0x0F; //clear bit 3:0 in I2STXMODE register\r
473                 if (ModeConfig->clksel == I2S_CLKSEL_MCLK) {\r
474                         I2Sx->RXMODE |= 0x02;\r
475                 }\r
476                 if (ModeConfig->fpin == I2S_4PIN_ENABLE) {\r
477                         I2Sx->RXMODE |= (1 << 2);\r
478                 }\r
479                 if (ModeConfig->mcena == I2S_MCLK_ENABLE) {\r
480                         I2Sx->RXMODE |= (1 << 3);\r
481                 }\r
482         }\r
483 }\r
484 \r
485 /********************************************************************//**\r
486  * @brief               Configure DMA operation for I2S\r
487  * @param[in]   I2Sx I2S peripheral selected, should be: LPC_I2S\r
488  * @param[in]   DMAConfig pointer to I2S_DMAConf_Type will be used to configure\r
489  * @param[in]   TRMode is transmit/receive mode, should be:\r
490  *                                      - I2S_TX_MODE = 0       :transmit mode\r
491  *                                      - I2S_RX_MODE = 1       :receive mode\r
492  * @return              none\r
493  *********************************************************************/\r
494 void I2S_DMAConfig(LPC_I2Sn_Type *I2Sx, I2S_DMAConf_Type* DMAConfig,\r
495                 uint8_t TRMode)\r
496 {\r
497         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
498         CHECK_PARAM(PARAM_I2S_DMA(DMAConfig->DMAIndex));\r
499         CHECK_PARAM(PARAM_I2S_DMA_DEPTH(DMAConfig->depth));\r
500         CHECK_PARAM(PARAM_I2S_TRX(TRMode));\r
501 \r
502         if (TRMode == I2S_RX_MODE) {\r
503                 if (DMAConfig->DMAIndex == I2S_DMA_1) {\r
504                         I2Sx->DMA1 = (DMAConfig->depth) << 8;\r
505                 } else {\r
506                         I2Sx->DMA2 = (DMAConfig->depth) << 8;\r
507                 }\r
508         } else {\r
509                 if (DMAConfig->DMAIndex == I2S_DMA_1) {\r
510                         I2Sx->DMA1 = (DMAConfig->depth) << 16;\r
511                 } else {\r
512                         I2Sx->DMA2 = (DMAConfig->depth) << 16;\r
513                 }\r
514         }\r
515 }\r
516 \r
517 /********************************************************************//**\r
518  * @brief               Enable/Disable DMA operation for I2S\r
519  * @param[in]   I2Sx: I2S peripheral selected, should be: LPC_I2S\r
520  * @param[in]   DMAIndex chose what DMA is used, should be:\r
521  *                                      - I2S_DMA_1 = 0         :DMA1\r
522  *                                      - I2S_DMA_2 = 1         :DMA2\r
523  * @param[in]   TRMode is transmit/receive mode, should be:\r
524  *                                      - I2S_TX_MODE = 0       :transmit mode\r
525  *                                      - I2S_RX_MODE = 1       :receive mode\r
526  * @param[in]   NewState is new state of DMA operation, should be:\r
527  *                              - ENABLE\r
528  *                              - DISABLE\r
529  * @return              none\r
530  *********************************************************************/\r
531 void I2S_DMACmd(LPC_I2Sn_Type *I2Sx, uint8_t DMAIndex, uint8_t TRMode,\r
532                 FunctionalState NewState)\r
533 {\r
534         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
535         CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));\r
536         CHECK_PARAM(PARAM_I2S_DMA(DMAIndex));\r
537         CHECK_PARAM(PARAM_I2S_TRX(TRMode));\r
538 \r
539         if (TRMode == I2S_RX_MODE) {\r
540                 if (DMAIndex == I2S_DMA_1) {\r
541                         if (NewState == ENABLE)\r
542                                 I2Sx->DMA1 |= 0x01;\r
543                         else\r
544                                 I2Sx->DMA1 &= ~0x01;\r
545                 } else {\r
546                         if (NewState == ENABLE)\r
547                                 I2Sx->DMA2 |= 0x01;\r
548                         else\r
549                                 I2Sx->DMA2 &= ~0x01;\r
550                 }\r
551         } else {\r
552                 if (DMAIndex == I2S_DMA_1) {\r
553                         if (NewState == ENABLE)\r
554                                 I2Sx->DMA1 |= 0x02;\r
555                         else\r
556                                 I2Sx->DMA1 &= ~0x02;\r
557                 } else {\r
558                         if (NewState == ENABLE)\r
559                                 I2Sx->DMA2 |= 0x02;\r
560                         else\r
561                                 I2Sx->DMA2 &= ~0x02;\r
562                 }\r
563         }\r
564 }\r
565 \r
566 /********************************************************************//**\r
567  * @brief               Configure IRQ for I2S\r
568  * @param[in]   I2Sx I2S peripheral selected, should be: LPC_I2S\r
569  * @param[in]   TRMode is transmit/receive mode, should be:\r
570  *                                      - I2S_TX_MODE = 0       :transmit mode\r
571  *                                      - I2S_RX_MODE = 1       :receive mode\r
572  * @param[in]   level is the FIFO level that triggers IRQ request\r
573  * @return              none\r
574  *********************************************************************/\r
575 void I2S_IRQConfig(LPC_I2Sn_Type *I2Sx, uint8_t TRMode, uint8_t level) {\r
576         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
577         CHECK_PARAM(PARAM_I2S_TRX(TRMode));\r
578         CHECK_PARAM(PARAM_I2S_IRQ_LEVEL(level));\r
579 \r
580         if (TRMode == I2S_RX_MODE) {\r
581                 I2Sx->IRQ |= (level << 8);\r
582         } else {\r
583                 I2Sx->IRQ |= (level << 16);\r
584         }\r
585 }\r
586 \r
587 /********************************************************************//**\r
588  * @brief               Enable/Disable IRQ for I2S\r
589  * @param[in]   I2Sx I2S peripheral selected, should be: LPC_I2S\r
590  * @param[in]   TRMode is transmit/receive mode, should be:\r
591  *                                      - I2S_TX_MODE = 0       :transmit mode\r
592  *                                      - I2S_RX_MODE = 1       :receive mode\r
593  * @param[in]   NewState is new state of DMA operation, should be:\r
594  *                                      - ENABLE\r
595  *                                      - DISABLE\r
596  * @return              none\r
597  *********************************************************************/\r
598 void I2S_IRQCmd(LPC_I2Sn_Type *I2Sx, uint8_t TRMode, FunctionalState NewState) {\r
599         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
600         CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));\r
601 \r
602         if (TRMode == I2S_RX_MODE) {\r
603                 if (NewState == ENABLE)\r
604                         I2Sx->IRQ |= 0x01;\r
605                 else\r
606                         I2Sx->IRQ &= ~0x01;\r
607                 //Enable DMA\r
608 \r
609         } else {\r
610                 if (NewState == ENABLE)\r
611                         I2Sx->IRQ |= 0x02;\r
612                 else\r
613                         I2Sx->IRQ &= ~0x02;\r
614         }\r
615 }\r
616 \r
617 /********************************************************************//**\r
618  * @brief               Get I2S interrupt status\r
619  * @param[in]   I2Sx I2S peripheral selected, should be: LPC_I2S\r
620  * @param[in]   TRMode is transmit/receive mode, should be:\r
621  *                                      - I2S_TX_MODE = 0       :transmit mode\r
622  *                                      - I2S_RX_MODE = 1       :receive mode\r
623  * @return              FunctionState   should be:\r
624  *                                      - ENABLE        :interrupt is enable\r
625  *                                      - DISABLE       :interrupt is disable\r
626  *********************************************************************/\r
627 FunctionalState I2S_GetIRQStatus(LPC_I2Sn_Type *I2Sx,uint8_t TRMode)\r
628 {\r
629         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
630         if(TRMode == I2S_TX_MODE)\r
631                 return (FunctionalState)((I2Sx->IRQ >> 1)&0x01);\r
632         else\r
633                 return (FunctionalState)((I2Sx->IRQ)&0x01);\r
634 }\r
635 \r
636 /********************************************************************//**\r
637  * @brief               Get I2S interrupt depth\r
638  * @param[in]   I2Sx I2S peripheral selected, should be: LPC_I2S\r
639  * @param[in]   TRMode is transmit/receive mode, should be:\r
640  *                                      - I2S_TX_MODE = 0       :transmit mode\r
641  *                                      - I2S_RX_MODE = 1       :receive mode\r
642  * @return              depth of FIFO level on which to create an irq request\r
643  *********************************************************************/\r
644 uint8_t I2S_GetIRQDepth(LPC_I2Sn_Type *I2Sx,uint8_t TRMode)\r
645 {\r
646         CHECK_PARAM(PARAM_I2Sx(I2Sx));\r
647         if(TRMode == I2S_TX_MODE)\r
648                 return (((I2Sx->IRQ)>>16)&0xFF);\r
649         else\r
650                 return (((I2Sx->IRQ)>>8)&0xFF);\r
651 }\r
652 /**\r
653  * @}\r
654  */\r
655 \r
656 #endif /* _I2S */\r
657 \r
658 /**\r
659  * @}\r
660  */\r
661 \r
662 /* --------------------------------- End Of File ------------------------------ */\r
663 \r