LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
AudioInput.c
Go to the documentation of this file.
1 /*
2  * @brief Make your board becomes a USB microphone
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2012
6  * Copyright(C) Dean Camera, 2011, 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 "AudioInput.h"
34 
35 /*****************************************************************************
36  * Private types/enumerations/variables
37  ****************************************************************************/
38 
39 /*****************************************************************************
40  * Public types/enumerations/variables
41  ****************************************************************************/
42 
48  .Config = {
50 
51  .DataINEndpointNumber = AUDIO_STREAM_EPNUM,
52  .DataINEndpointSize = AUDIO_STREAM_EPSIZE,
53  .PortNumber = 0,
54  },
55 };
56 
58 #define AUDIO_MAX_SAMPLE_FREQ 48000
59 
61 /* Sample Buffer */
62 // uint16_t* sample_buffer = NULL;
64 uint16_t sample_buffer[512] ATTR_ALIGNED(4) __BSS(USBRAM_SECTION);
65 uint32_t sample_buffer_size = 0;
66 
67 /*****************************************************************************
68  * Private functions
69  ****************************************************************************/
70 
71 /* Configures the board hardware and chip peripherals for the demo's
72  functionality. */
73 static void SetupHardware(void)
74 {
75  Board_Init();
77  USB_Init(Microphone_Audio_Interface.Config.PortNumber, USB_MODE_Device);
78 }
79 
80 /*****************************************************************************
81  * Public functions
82  ****************************************************************************/
83 
85 {}
86 
87 void Audio_Init(uint32_t samplefreq)
88 {
89  CurrentAudioSampleFrequency = samplefreq;
90  sample_buffer_size = samplefreq * sizeof(uint16_t) / 1000;
91 }
92 
97 
98  /* Check if this is audio stream endpoint */
99  *packet_size = sample_buffer_size;
100  if ((EPNum & 0x7F) == AUDIO_STREAM_EPNUM) {
101  return (uint32_t) &sample_buffer[0];
102  }
103  else {return 0; }
104 }
105 
109 int main(void)
110 {
111  uint32_t Button_State = 0;
112  SetupHardware();
113 
114  // sample_buffer = (uint16_t*)Audio_Get_ISO_Buffer_Address(0);
115 #if defined(USB_DEVICE_ROM_DRIVER)
116  UsbdAdc_Init(&Microphone_Audio_Interface);
117 #endif
118 
119  for (;; ) {
120  /* Only generate audio if the board button is being pressed */
121  /* Generate Square Wave at 1kHz */
122  if ((Buttons_GetStatus() & BUTTONS_BUTTON1) != Button_State) {
123  int i;
124  Button_State ^= BUTTONS_BUTTON1;
125  for (i = 0; i < sample_buffer_size / 4; i++) {
126  sample_buffer[i] = (Button_State << 15);
127  }
128  }
129 #if !defined(USB_DEVICE_ROM_DRIVER)
130  Audio_Device_USBTask(&Microphone_Audio_Interface);
131  USB_USBTask(Microphone_Audio_Interface.Config.PortNumber, USB_MODE_Device);
132 #endif
133  }
134 }
135 
138 {}
139 
142 {}
143 
146 {
147  bool ConfigSuccess = true;
148 
149  ConfigSuccess &= Audio_Device_ConfigureEndpoints(&Microphone_Audio_Interface);
150 
151  // LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
152 }
153 
156 {
157  Audio_Device_ProcessControlRequest(&Microphone_Audio_Interface);
158 }
159 
164  const uint8_t EndpointProperty,
165  const uint8_t EndpointAddress,
166  const uint8_t EndpointControl,
167  uint16_t *const DataLength,
168  uint8_t *Data)
169 {
170  /* Check the requested endpoint to see if a supported endpoint is being manipulated */
171  if (EndpointAddress == (ENDPOINT_DIR_IN | Microphone_Audio_Interface.Config.DataINEndpointNumber)) {
172  /* Check the requested control to see if a supported control is being manipulated */
173  if (EndpointControl == AUDIO_EPCONTROL_SamplingFreq) {
174  switch (EndpointProperty) {
176  /* Check if we are just testing for a valid property, or actually adjusting it */
177  if (DataLength != NULL) {
178  /* Set the new sampling frequency to the value given by the host */
180  (((uint32_t) Data[2] << 16) | ((uint32_t) Data[1] << 8) | (uint32_t) Data[0]);
182  return false;
183  }
184  sample_buffer_size = CurrentAudioSampleFrequency * sizeof(uint16_t) / 1000;
185  }
186 
187  return true;
188 
190  /* Check if we are just testing for a valid property, or actually reading it */
191  if (DataLength != NULL) {
192  *DataLength = 3;
193 
194  Data[2] = (CurrentAudioSampleFrequency >> 16);
195  Data[1] = (CurrentAudioSampleFrequency >> 8);
196  Data[0] = (CurrentAudioSampleFrequency & 0xFF);
197  }
198 
199  return true;
200  }
201  }
202  }
203 
204  return false;
205 }
206 
212  const uint8_t Property,
213  const uint8_t EntityAddress,
214  const uint16_t Parameter,
215  uint16_t *const DataLength,
216  uint8_t *Data)
217 {
218  /* No audio interface entities in the device descriptor, thus no properties to get or set. */
219  return false;
220 }