| LPCOpen Platform v1.03 LPCOpen Platform for NXP LPC Microcontrollers

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
i2s.c
Go to the documentation of this file.
1 /*
2 * @brief I2S example
3 * This example show how to use the I2S in 3 modes : Polling, Interrupt and DMA
4 *
5 * @note
6 * Copyright(C) NXP Semiconductors, 2012
7 * All rights reserved.
8 *
9 * @par
10 * Software that is described herein is for illustrative purposes only
11 * which provides customers with programming information regarding the
12 * LPC products. This software is supplied "AS IS" without any warranties of
13 * any kind, and NXP Semiconductors and its licensor disclaim any and
14 * all warranties, express or implied, including all implied warranties of
15 * merchantability, fitness for a particular purpose and non-infringement of
16 * intellectual property rights. NXP Semiconductors assumes no responsibility
17 * or liability for the use of the software, conveys no license or rights under any
18 * patent, copyright, mask work right, or any other intellectual property rights in
19 * or to any products. NXP Semiconductors reserves the right to make changes
20 * in the software without notification. NXP Semiconductors also makes no
21 * representation or warranty that such application will be suitable for the
22 * specified use without further testing or modification.
23 *
24 * @par
25 * Permission to use, copy, modify, and distribute this software and its
26 * documentation is hereby granted, under NXP Semiconductors' and its
27 * licensor's relevant copyrights in the software, without fee, provided that it
28 * is used in conjunction with NXP Semiconductors microcontrollers. This
29 * copyright, permission, and disclaimer notice must appear in all copies of
30 * this code.
31 */
32
33 #include "board.h"
34
65 /*****************************************************************************
66 * Private types/enumerations/variables
67 ****************************************************************************/
68
69 #define BUFFER_FULL 0
70 #define BUFFER_EMPTY 1
71 #define BUFFER_AVAILABLE 2
72
73 typedef struct ring_buff {
74 uint32_t buffer[256];
75 uint8_t read_index;
76 uint8_t write_index;
77 } Ring_Buffer_t;
78
79 static char WelcomeMenu[] = "\n\rHello NXP Semiconductors \n\r"
80 "I2S DEMO : Connect audio headphone out from computer to line-in on tested board to get audio signal\n\r"
81 "Please press \'1\' to test Polling mode\n\r"
82 "Please press \'2\' to test Interrupt mode\n\r"
83 "Please press \'3\' to test DMA mode\n\r"
84 "Please press \'x\' to exit test mode\n\r"
85 "Please press \'m\' to DISABLE/ENABLE mute\n\r";
86
87 static Ring_Buffer_t ring_buffer;
88
89 static uint8_t send_flag;
90 static uint8_t channelTC;
91 static uint8_t dmaChannelNum_I2S_Tx, dmaChannelNum_I2S_Rx;
92 static uint8_t dma_send_receive;
93
94 /*****************************************************************************
95 * Public types/enumerations/variables
96 ****************************************************************************/
97
98 /*****************************************************************************
99 * Private functions
100 ****************************************************************************/
101
102 uint8_t mute_status = 0;
103
104 static void mute_toggle()
105 {
106 mute_status = !mute_status;
107 if (mute_status) {
108 Chip_I2S_EnableMute(LPC_I2S);
109 DEBUGOUT("MUTE ENABLE\n\r");
110 }
111 else {
112 Chip_I2S_DisableMute(LPC_I2S);
113 DEBUGOUT("MUTE DISABLE\n\r");
114 }
115 }
116 /* Get status of the ring buffer */
117 static uint8_t ring_buff_get_status(Ring_Buffer_t *ring_buff)
118 {
119 if (ring_buff->read_index == ring_buff->write_index) {
120 return BUFFER_EMPTY;
121 }
122 else if (ring_buff->read_index == (ring_buff->write_index) + 1) {
123 return BUFFER_FULL;
124 }
125 else {return BUFFER_AVAILABLE; }
126 }
127
128 /* Interrupt routine for I2S example */
129 static void App_Interrupt_Test(void)
130 {
131 uint8_t bufferUART, continue_Flag = 1;
132 DEBUGOUT("I2S Interrupt mode\n\r");
133
134 Chip_I2S_Int_Cmd(LPC_I2S, I2S_RX_MODE, ENABLE, 4);
135 Chip_I2S_Int_Cmd(LPC_I2S, I2S_TX_MODE, ENABLE, 4);
136 NVIC_EnableIRQ(I2S_IRQn);
137 while (continue_Flag) {
138 bufferUART = 0xFF;
139 bufferUART = DEBUGIN();
140 switch (bufferUART) {
141 case 'x':
142 continue_Flag = 0;
143 Chip_I2S_Int_Cmd(LPC_I2S, I2S_RX_MODE, DISABLE, 4);
144 NVIC_DisableIRQ(I2S_IRQn);
145 DEBUGOUT(WelcomeMenu);
146 break;
147
148 case 'm':
149 mute_toggle();
150 break;
151
152 default:
153 break;
154 }
155 }
156 }
157
158 /* Polling routine for I2S example */
159 static void App_Polling_Test(void)
160 {
161 uint32_t polling_data = 0;
162 uint8_t bufferUART, continue_Flag = 1;
163 DEBUGOUT("I2S Polling mode\n\r");
164 while (continue_Flag) {
165 bufferUART = 0xFF;
166 bufferUART = DEBUGIN();
167 switch (bufferUART) {
168 case 'x':
169 continue_Flag = 0;
170 DEBUGOUT(WelcomeMenu);
171 break;
172
173 case 'm':
174 mute_toggle();
175 break;
176
177 default:
178 break;
179 }
180
181 if (Chip_I2S_GetLevel(LPC_I2S, I2S_RX_MODE) > 0) {
182 polling_data = Chip_I2S_Receive(LPC_I2S);
183 send_flag = 1;
184 }
185 if ((Chip_I2S_GetLevel(LPC_I2S, I2S_TX_MODE) < 4) && (send_flag == 1)) {
186 Chip_I2S_Send(LPC_I2S, polling_data);
187 send_flag = 0;
188 }
189 }
190 }
191
192 /* DMA routine for I2S example */
193 static void App_DMA_Test(void)
194 {
195 uint8_t continue_Flag = 1, bufferUART = 0xFF;
196
197 Chip_I2S_DMA_Cmd(LPC_I2S, I2S_TX_MODE, I2S_DMA_REQUEST_NUMBER_1, ENABLE, 4);
198 Chip_I2S_DMA_Cmd(LPC_I2S, I2S_RX_MODE, I2S_DMA_REQUEST_NUMBER_2, ENABLE, 4);
199
200 /* Initialize GPDMA controller */
201 Chip_GPDMA_Init(LPC_GPDMA);
202
203 /* Setting GPDMA interrupt */
204 NVIC_DisableIRQ(DMA_IRQn);
205 NVIC_SetPriority(DMA_IRQn, ((0x01 << 3) | 0x01));
206 NVIC_EnableIRQ(DMA_IRQn);
207
208 dmaChannelNum_I2S_Rx = Chip_DMA_GetFreeChannel(LPC_GPDMA, GPDMA_CONN_I2S_Channel_1);
209
210 Chip_DMA_Transfer(LPC_GPDMA, dmaChannelNum_I2S_Rx,
211 GPDMA_CONN_I2S_Channel_1,
212 GPDMA_CONN_I2S_Channel_0,
213 GPDMA_TRANSFERTYPE_P2P_CONTROLLER_SrcPERIPHERAL,
214 1);
215
216 DEBUGOUT("I2S DMA mode\n\r");
217 while (continue_Flag) {
218 bufferUART = 0xFF;
219 bufferUART = DEBUGIN();
220 switch (bufferUART) {
221 case 'x':
222 continue_Flag = 0;
223 Chip_I2S_DMA_Cmd(LPC_I2S, I2S_RX_MODE, 2, DISABLE, 1);
224 Chip_I2S_DMA_Cmd(LPC_I2S, I2S_TX_MODE, 1, DISABLE, 1);
225
226 Chip_DMA_Stop(LPC_GPDMA, dmaChannelNum_I2S_Rx);
227 NVIC_DisableIRQ(DMA_IRQn);
228 DEBUGOUT(WelcomeMenu);
229 break;
230
231 case 'm':
232 mute_toggle();
233 break;
234
235 default:
236 break;
237 }
238 }
239 }
240
241 /*****************************************************************************
242 * Public functions
243 ****************************************************************************/
244
249 void DMA_IRQHandler(void)
250 {
251 if (dma_send_receive == 1) {
252 if (Chip_DMA_Interrupt(LPC_GPDMA, dmaChannelNum_I2S_Rx) == SUCCESS) {
253 channelTC++;
254 }
255 else {
256 /* Process error here */
257 }
258 }
259 else {
260 if (Chip_DMA_Interrupt(LPC_GPDMA, dmaChannelNum_I2S_Tx) == SUCCESS) {
261 channelTC++;
262 }
263 else {
264 /* Process error here */
265 }
266 }
267 }
268
273 void I2S_IRQHandler(void)
274 {
275 while ((ring_buff_get_status(&ring_buffer) != BUFFER_FULL) && (Chip_I2S_GetLevel(LPC_I2S, I2S_RX_MODE) > 0)) {
276 ring_buffer.buffer[ring_buffer.write_index++] = Chip_I2S_Receive(LPC_I2S);
277 }
278 while ((ring_buff_get_status(&ring_buffer) != BUFFER_EMPTY) && (Chip_I2S_GetLevel(LPC_I2S, I2S_TX_MODE) < 8)) {
279 Chip_I2S_Send(LPC_I2S, ring_buffer.buffer[ring_buffer.read_index++]);
280 }
281 }
282
287 int main(void)
288 {
289
290 Chip_I2S_Audio_Format_T audio_Confg;
291 uint8_t bufferUART, continue_Flag = 1;
292 audio_Confg.SampleRate = 48000;
293 /* Select audio data is 2 channels (1 is mono, 2 is stereo) */
294 audio_Confg.ChannelNumber = 2;
295 /* Select audio data is 16 bits */
296 audio_Confg.WordWidth = 16;
297
298 Board_Init();
299
300 #if defined( __GNUC__ )
301 __sys_write(0, "", 0);
302 #endif
303
304 DEBUGOUT(WelcomeMenu);
305
306 Board_Audio_Init(LPC_I2S, UDA1380_LINE_IN);
307 Chip_I2S_Init(LPC_I2S);
308 Chip_I2S_Config(LPC_I2S, I2S_RX_MODE, &audio_Confg);
309 Chip_I2S_Config(LPC_I2S, I2S_TX_MODE, &audio_Confg);
310
311 Chip_I2S_Stop(LPC_I2S, I2S_TX_MODE);
312 Chip_I2S_DisableMute(LPC_I2S);
313 Chip_I2S_Start(LPC_I2S, I2S_TX_MODE);
314 send_flag = 0;
315 while (continue_Flag) {
316 bufferUART = 0xFF;
317 bufferUART = DEBUGIN();
318 switch (bufferUART) {
319 case '1':
320 App_Polling_Test();
321 break;
322
323 case '2':
324 App_Interrupt_Test();
325 break;
326
327 case '3':
328 App_DMA_Test();
329 break;
330
331 case 'x':
332 continue_Flag = 0;
333 DEBUGOUT("Thanks for using\n\r");
334 break;
335
336 default:
337 break;
338 }
339 }
340 return 0;
341 }
342