LPCOpen Platform
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 
40  {
41  .Config =
42  {
44 
45  .DataINEndpointNumber = AUDIO_STREAM_EPNUM,
46  .DataINEndpointSize = AUDIO_STREAM_EPSIZE,
47  },
48  };
49 
51 #define AUDIO_MAX_SAMPLE_FREQ 48000
52 
54 /* Sample Buffer */
55 //uint16_t* sample_buffer = NULL;
57 uint16_t sample_buffer[512] ATTR_ALIGNED(4);
59 
64 
65  /* Check if this is audio stream endpoint */
66  *packet_size = sample_buffer_size;
67  if(EPNum == AUDIO_STREAM_EPNUM)
68  return (uint32_t)&sample_buffer[0];
69  else return 0;
70 }
71 
75 int main(void)
76 {
77  uint32_t Button_State = 0;
78  SetupHardware();
79 
80  //sample_buffer = (uint16_t*)Audio_Get_ISO_Buffer_Address(0);
81  for (;;)
82  {
83  /* Only generate audio if the board button is being pressed */
84  /* Generate Square Wave at 1kHz */
85  if((Buttons_GetStatus() & BUTTONS_BUTTON1)!= Button_State)
86  {
87  Button_State ^= BUTTONS_BUTTON1;
88  for(int i=0; i < sample_buffer_size/4; i++)
89  sample_buffer[i] = (Button_State<<15);
90  }
91  Audio_Device_USBTask(&Microphone_Audio_Interface);
92  USB_USBTask();
93  }
94 }
95 
97 void SetupHardware(void)
98 {
99  Board_Init();
101  USB_Init();
102 }
103 
104 //#define USE_TEST_TONE
105 //#if defined(__LPC17XX__)||defined(__LPC18XX__)||defined(__LPC177X_8X__)
106 //void TIMER0_IRQHandler (void)
107 //#endif
108 //{
109 // uint8_t PrevEndpoint = Endpoint_GetCurrentEndpoint();
110 //
111 // /* Check that the USB bus is ready for the next sample to write */
112 // if (Audio_Device_IsReadyForNextSample(&Microphone_Audio_Interface))
113 // {
114 // int16_t AudioSample;
115 //
116 // #if defined(USE_TEST_TONE)
117 // static uint8_t SquareWaveSampleCount;
118 // static int16_t CurrentWaveValue;
119 //
120 // /* In test tone mode, generate a square wave at 1/256 of the sample rate */
121 // if (SquareWaveSampleCount++ == 0xFF)
122 // CurrentWaveValue ^= 0x8000;
123 //
124 // /* Only generate audio if the board button is being pressed */
125 // AudioSample = (Buttons_GetStatus() & BUTTONS_BUTTON1) ? CurrentWaveValue : 0;
126 // #else
127 // /* Audio sample is ADC value scaled to fit the entire range */
128 // AudioSample = ((SAMPLE_MAX_RANGE / ADC_MAX_RANGE) * ADC_GetResult());
129 // #if defined(MICROPHONE_BIASED_TO_HALF_RAIL)
130 // /* Microphone is biased to half rail voltage, subtract the bias from the sample value */
131 // AudioSample -= (SAMPLE_MAX_RANGE / 2);
132 // #endif
133 // #endif
134 //
135 // Audio_Device_WriteSample16(&Microphone_Audio_Interface, AudioSample);
136 // }
137 //
138 // Endpoint_SelectEndpoint(PrevEndpoint);
139 // LPC_TIMER0->IR = 1;
140 //}
141 
144 {
145 }
146 
149 {
150 }
151 
154 {
155  bool ConfigSuccess = true;
156 
157  ConfigSuccess &= Audio_Device_ConfigureEndpoints(&Microphone_Audio_Interface);
158 
159 // LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
160 }
161 
164 {
165  Audio_Device_ProcessControlRequest(&Microphone_Audio_Interface);
166 }
167 
172  const uint8_t EndpointProperty,
173  const uint8_t EndpointAddress,
174  const uint8_t EndpointControl,
175  uint16_t* const DataLength,
176  uint8_t* Data)
177 {
178  /* Check the requested endpoint to see if a supported endpoint is being manipulated */
179  if (EndpointAddress == (ENDPOINT_DIR_IN | Microphone_Audio_Interface.Config.DataINEndpointNumber))
180  {
181  /* Check the requested control to see if a supported control is being manipulated */
182  if (EndpointControl == AUDIO_EPCONTROL_SamplingFreq)
183  {
184  switch (EndpointProperty)
185  {
187  /* Check if we are just testing for a valid property, or actually adjusting it */
188  if (DataLength != NULL)
189  {
190  /* Set the new sampling frequency to the value given by the host */
191  CurrentAudioSampleFrequency = (((uint32_t)Data[2] << 16) | ((uint32_t)Data[1] << 8) | (uint32_t)Data[0]);
193  sample_buffer_size = CurrentAudioSampleFrequency * sizeof(uint16_t) / 1000;
194  }
195 
196  return true;
198  /* Check if we are just testing for a valid property, or actually reading it */
199  if (DataLength != NULL)
200  {
201  *DataLength = 3;
202 
203  Data[2] = (CurrentAudioSampleFrequency >> 16);
204  Data[1] = (CurrentAudioSampleFrequency >> 8);
205  Data[0] = (CurrentAudioSampleFrequency & 0xFF);
206  }
207 
208  return true;
209  }
210  }
211  }
212 
213  return false;
214 }
215 
221  const uint8_t Property,
222  const uint8_t EntityAddress,
223  const uint16_t Parameter,
224  uint16_t* const DataLength,
225  uint8_t* Data)
226 {
227  /* No audio interface entities in the device descriptor, thus no properties to get or set. */
228  return false;
229 }
230