LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
i2s_18xx_43xx.c
Go to the documentation of this file.
1 /*
2  * @brief LPC18xx/43xx I2S driver
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2012
6  * All rights reserved.
7  *
8  * @par
9  * Software that is described herein is for illustrative purposes only
10  * which provides customers with programming information regarding the
11  * LPC products. This software is supplied "AS IS" without any warranties of
12  * any kind, and NXP Semiconductors and its licensor disclaim any and
13  * all warranties, express or implied, including all implied warranties of
14  * merchantability, fitness for a particular purpose and non-infringement of
15  * intellectual property rights. NXP Semiconductors assumes no responsibility
16  * or liability for the use of the software, conveys no license or rights under any
17  * patent, copyright, mask work right, or any other intellectual property rights in
18  * or to any products. NXP Semiconductors reserves the right to make changes
19  * in the software without notification. NXP Semiconductors also makes no
20  * representation or warranty that such application will be suitable for the
21  * specified use without further testing or modification.
22  *
23  * @par
24  * Permission to use, copy, modify, and distribute this software and its
25  * documentation is hereby granted, under NXP Semiconductors' and its
26  * licensor's relevant copyrights in the software, without fee, provided that it
27  * is used in conjunction with NXP Semiconductors microcontrollers. This
28  * copyright, permission, and disclaimer notice must appear in all copies of
29  * this code.
30  */
31 
32 #include "chip.h"
33 
34 /*****************************************************************************
35  * Private types/enumerations/variables
36  ****************************************************************************/
37 
38 static uint8_t clksEnabled;
39 
40 /*****************************************************************************
41  * Public types/enumerations/variables
42  ****************************************************************************/
43 
44 /*****************************************************************************
45  * Private functions
46  ****************************************************************************/
47 
48 /*****************************************************************************
49  * Public functions
50  ****************************************************************************/
51 
52 /* Initialize the I2S interface */
54 {
55  if (clksEnabled == 0) {
57  }
58  clksEnabled++;
59 
60  IP_I2S_Init(pI2S);
61 }
62 
63 /* Shutdown I2S */
65 {
66  IP_I2S_DeInit(pI2S);
67 
68  clksEnabled--;
69  if (clksEnabled == 0) {
71  }
72 }
73 
74 /* Configure I2S for Audio Format input */
75 Status Chip_I2S_Config(LPC_I2S_T *pI2S, uint8_t TRMode, Chip_I2S_Audio_Format_T *audio_format)
76 {
77  uint32_t pClk;
78  uint32_t x, y;
79  uint64_t divider;
80  uint16_t dif;
81  uint16_t x_divide = 0, y_divide = 0;
82  uint32_t N;
83  uint16_t err, ErrorOptimal = 0xFFFF;
84 
85  pClk = (uint64_t) Chip_Clock_GetRate(CLK_APB1_I2S);
86 
87  /* divider is a fixed point number with 16 fractional bits */
88  divider = (((uint64_t) (audio_format->SampleRate) * 2 * (audio_format->WordWidth) * 2) << 16) / pClk;
89  /* find N that make x/y <= 1 -> divider <= 2^16 */
90  for (N = 64; N > 0; N--) {
91  if ((divider * N) < (1 << 16)) {
92  break;
93  }
94  }
95  if (N == 0) {
96  return ERROR;
97  }
98  divider *= N;
99  for (y = 255; y > 0; y--) {
100  x = y * divider;
101  if (x & (0xFF000000)) {
102  continue;
103  }
104  dif = x & 0xFFFF;
105  if (dif > 0x8000) {
106  err = 0x10000 - dif;
107  }
108  else {
109  err = dif;
110  }
111  if (err == 0) {
112  y_divide = y;
113  break;
114  }
115  else if (err < ErrorOptimal) {
116  ErrorOptimal = err;
117  y_divide = y;
118  }
119  }
120  x_divide = ((uint64_t) y_divide * (audio_format->SampleRate) * 2 * (audio_format->WordWidth) * N * 2) / pClk;
121  if (x_divide >= 256) {
122  x_divide = 0xFF;
123  }
124  if (x_divide == 0) {
125  x_divide = 1;
126  }
127  if (audio_format->WordWidth <= 8) {
128  IP_I2S_SetWordWidth(pI2S, TRMode, I2S_WORDWIDTH_8);
129  }
130  else if (audio_format->WordWidth <= 16) {
131  IP_I2S_SetWordWidth(pI2S, TRMode, I2S_WORDWIDTH_16);
132  }
133  else {
134  IP_I2S_SetWordWidth(pI2S, TRMode, I2S_WORDWIDTH_32);
135  }
136  IP_I2S_SetMono(pI2S, TRMode, (audio_format->ChannelNumber) == 1 ? I2S_MONO : I2S_STEREO);
138  IP_I2S_SetWS_Halfperiod(pI2S, TRMode, audio_format->WordWidth - 1);
140  IP_I2S_SetBitRate(pI2S, TRMode, N - 1);
141  IP_I2S_SetXYDivider(pI2S, TRMode, x_divide, y_divide);
142  return SUCCESS;
143 }
144 
145 /* Enable/Disable Interrupt with a specific FIFO depth */
146 void Chip_I2S_Int_Cmd(LPC_I2S_T *pI2S, uint8_t TRMode, FunctionalState NewState, uint8_t FIFO_Depth)
147 {
148  IP_I2S_InterruptCmd(pI2S, TRMode, NewState);
149  IP_I2S_SetFIFODepthIRQ(pI2S, TRMode, FIFO_Depth);
150 }
151 
152 /* Enable/Disable DMA with a specific FIFO depth */
154  uint8_t TRMode,
155  uint8_t DMANum,
156  FunctionalState NewState,
157  uint8_t FIFO_Depth)
158 {
159  IP_I2S_SetFIFODepthDMA(pI2S, TRMode, (IP_I2S_DMARequestNumber_T) DMANum, FIFO_Depth);
160  IP_I2S_DMACmd(pI2S, (IP_I2S_DMARequestNumber_T) DMANum, TRMode, NewState);
161 }