LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 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 
58 /*****************************************************************************
59  * Private types/enumerations/variables
60  ****************************************************************************/
61 
62 #define BUFFER_FULL 0
63 #define BUFFER_EMPTY 1
64 #define BUFFER_AVAILABLE 2
65 
66 typedef struct ring_buff {
67  uint32_t buffer[256];
68  uint8_t read_index;
69  uint8_t write_index;
71 
72 static char WelcomeMenu[] = "\n\rHello NXP Semiconductors \n\r"
73  "I2S DEMO : Connect audio headphone out from computer to line-in on tested board to get audio signal\n\r"
74  "Please press \'1\' to test Polling mode\n\r"
75  "Please press \'2\' to test Interrupt mode\n\r"
76  "Please press \'3\' to test DMA mode\n\r"
77  "Please press \'x\' to exit test mode\n\r"
78  "Please press \'m\' to DISABLE/ENABLE mute\n\r";
79 
81 
82 static uint8_t send_flag;
83 static uint8_t channelTC;
85 static uint8_t dma_send_receive;
86 
87 /*****************************************************************************
88  * Public types/enumerations/variables
89  ****************************************************************************/
90 
91 /*****************************************************************************
92  * Private functions
93  ****************************************************************************/
94 
95 /* Get status of the ring buffer */
96 static uint8_t ring_buff_get_status(Ring_Buffer_t *ring_buff)
97 {
98  if (ring_buff->read_index == ring_buff->write_index) {
99  return BUFFER_EMPTY;
100  }
101  else if (ring_buff->read_index == (ring_buff->write_index) + 1) {
102  return BUFFER_FULL;
103  }
104  else {return BUFFER_AVAILABLE; }
105 }
106 
107 /* Interrupt routine for I2S example */
108 static void App_Interrupt_Test(void)
109 {
110  uint8_t bufferUART, continue_Flag = 1;
111  FunctionalState mute_status = ENABLE;
112  DEBUGOUT("I2S Interrupt mode\n\r");
115  NVIC_EnableIRQ(I2S0_IRQn);
116  while (continue_Flag) {
117  bufferUART = 0xFF;
118  bufferUART = DEBUGIN();
119  switch (bufferUART) {
120  case 'x':
121  continue_Flag = 0;
123  NVIC_DisableIRQ(I2S0_IRQn);
125  break;
126 
127  case 'm':
128  Chip_I2S_Mute(LPC_I2S0, mute_status);
129  mute_status = (FunctionalState) !mute_status;
130  if (!mute_status) {
131  DEBUGOUT("MUTE ENABLE\n\r");
132  }
133  else {DEBUGOUT("MUTE DISABLE\n\r"); }
134  break;
135 
136  default:
137  break;
138  }
139  }
140 }
141 
142 /* Polling routine for I2S example */
143 static void App_Polling_Test(void)
144 {
145  uint32_t polling_data = 0;
146  uint8_t bufferUART, continue_Flag = 1;
147  FunctionalState mute_status = ENABLE;
148  DEBUGOUT("I2S Polling mode\n\r");
149  while (continue_Flag) {
150  bufferUART = 0xFF;
151  bufferUART = DEBUGIN();
152  switch (bufferUART) {
153  case 'x':
154  continue_Flag = 0;
156  break;
157 
158  case 'm':
159  Chip_I2S_Mute(LPC_I2S0, mute_status);
160  mute_status = (FunctionalState) !mute_status;
161  if (!mute_status) {
162  DEBUGOUT("MUTE ENABLE\n\r");
163  }
164  else {DEBUGOUT("MUTE DISABLE\n\r"); }
165  break;
166 
167  default:
168  break;
169  }
170 
172  polling_data = Chip_I2S_Receive(LPC_I2S0);
173  send_flag = 1;
174  }
175  if ((Chip_I2S_GetLevel(LPC_I2S0, I2S_TX_MODE) < 4) && (send_flag == 1)) {
176  Chip_I2S_Send(LPC_I2S0, polling_data);
177  send_flag = 0;
178  }
179  }
180 }
181 
182 /* DMA routine for I2S example */
183 static void App_DMA_Test(void)
184 {
185  uint8_t continue_Flag = 1, bufferUART = 0xFF;
186  FunctionalState mute_status = ENABLE;
189  /* Initialize GPDMA controller */
190  Chip_GPDMA_Init();
191  /* Setting GPDMA interrupt */
192  NVIC_DisableIRQ(DMA_IRQn);
193  NVIC_SetPriority(DMA_IRQn, ((0x01 << 3) | 0x01));
194  NVIC_EnableIRQ(DMA_IRQn);
195 
197 
202  1);
203 
204  DEBUGOUT("I2S DMA mode\n\r");
205  while (continue_Flag) {
206  bufferUART = 0xFF;
207  bufferUART = DEBUGIN();
208  switch (bufferUART) {
209  case 'x':
210  continue_Flag = 0;
213 
215  NVIC_DisableIRQ(DMA_IRQn);
217  break;
218 
219  case 'm':
220  Chip_I2S_Mute(LPC_I2S0, mute_status);
221  mute_status = (FunctionalState) !mute_status;
222  if (!mute_status) {
223  DEBUGOUT("MUTE ENABLE\n\r");
224  }
225  else {DEBUGOUT("MUTE DISABLE\n\r"); }
226  break;
227 
228  default:
229  break;
230  }
231  }
232 }
233 
234 /*****************************************************************************
235  * Public functions
236  ****************************************************************************/
237 
242 void DMA_IRQHandler(void)
243 {
244  if (dma_send_receive == 1) {
246  channelTC++;
247  }
248  else {
249  /* Process error here */
250  }
251  }
252  else {
254  channelTC++;
255  }
256  else {
257  /* Process error here */
258  }
259  }
260 }
261 
266 void I2S0_IRQHandler(void)
267 {
268  while ((ring_buff_get_status(&ring_buffer) != BUFFER_FULL) && (Chip_I2S_GetLevel(LPC_I2S0, I2S_RX_MODE) > 0)) {
269  ring_buffer.buffer[ring_buffer.write_index++] = Chip_I2S_Receive(LPC_I2S0);
270  }
271  while ((ring_buff_get_status(&ring_buffer) != BUFFER_EMPTY) && (Chip_I2S_GetLevel(LPC_I2S0, I2S_TX_MODE) < 8)) {
272  Chip_I2S_Send(LPC_I2S0, ring_buffer.buffer[ring_buffer.read_index++]);
273  }
274 }
275 
280 int main(void)
281 {
282 
283  Chip_I2S_Audio_Format_Type audio_Confg;
284  uint8_t bufferUART, continue_Flag = 1;
285  audio_Confg.SampleRate = 48000;
286  /* Select audio data is 2 channels (1 is mono, 2 is stereo) */
287  audio_Confg.ChannelNumber = 2;
288  /* Select audio data is 16 bits */
289  audio_Confg.WordWidth = 16;
290 
291  Board_Init();
292 #if defined( __GNUC__ )
293  __sys_write(0, "", 0);
294 #endif
298 
300  Chip_I2S_Config(LPC_I2S0, I2S_RX_MODE, &audio_Confg);
301  Chip_I2S_Config(LPC_I2S0, I2S_TX_MODE, &audio_Confg);
302 
306  send_flag = 0;
307  while (continue_Flag) {
308  bufferUART = 0xFF;
309  bufferUART = DEBUGIN();
310  switch (bufferUART) {
311  case '1':
313  break;
314 
315  case '2':
317  break;
318 
319  case '3':
320  App_DMA_Test();
321  break;
322 
323  case 'x':
324  continue_Flag = 0;
325  DEBUGOUT("Thanks for using\n\r");
326  break;
327 
328  default:
329  break;
330  }
331  }
332 }
333