LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
AudioOutputHost.c
Go to the documentation of this file.
1 /*
2  * @brief Audio Output Host Example
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 "AudioOutputHost.h"
34 
40  .Config = {
41  .DataOUTPipeNumber = 1,
42  },
43 };
44 
48 int main(void)
49 {
50  SetupHardware();
51  DEBUGOUT("Audio Output Host Demo running.\r\n");
52 
53  for (;; ) {
54  Audio_Host_USBTask(&Speaker_Audio_Interface);
55  USB_USBTask();
56  }
57 }
58 
60 #define USE_TEST_TONE
61 #if defined(__LPC17XX__) || defined(__LPC177X_8X__) || defined(__LPC18XX__) || defined(__LPC43XX__)
62 void TIMER1_IRQHandler(void)
63 {
64  uint8_t PrevPipe = Pipe_GetCurrentPipe(Speaker_Audio_Interface.Config.PortNumber);
65  /* Check that the USB bus is ready for the next sample to write */
66  if (Audio_Host_IsReadyForNextSample(&Speaker_Audio_Interface)) {
67  int16_t AudioSample;
68  #if defined(USE_TEST_TONE)
69  static uint8_t SquareWaveSampleCount;
70  static int16_t CurrentWaveValue;
71 
72  /* In test tone mode, generate a square wave at 1/256 of the sample rate */
73  if (SquareWaveSampleCount++ == 0xFF) {
74  CurrentWaveValue ^= 0x8000;
75  }
76 
77  /* Only generate audio if the board button is being pressed */
78  AudioSample = (Buttons_GetStatus() & BUTTONS_BUTTON1) ? CurrentWaveValue : 0;
79  #else
80  /* Audio sample is ADC value scaled to fit the entire range */
81  AudioSample = ((SAMPLE_MAX_RANGE / ADC_MAX_RANGE) * ADC_GetResult());
82 
83  #if defined(MICROPHONE_BIASED_TO_HALF_RAIL)
84  /* Microphone is biased to half rail voltage, subtract the bias from the sample value */
85  AudioSample -= (SAMPLE_MAX_RANGE / 2);
86  #endif
87  #endif
88 
89  Audio_Host_WriteSample16(&Speaker_Audio_Interface, AudioSample);
90  Audio_Host_WriteSample16(&Speaker_Audio_Interface, AudioSample);
91  }
92 
93  Pipe_SelectPipe(Speaker_Audio_Interface.Config.PortNumber, PrevPipe);
94 }
95 
96 #endif
97 
98 void Init_Timer(uint32_t freq)
99 {
100  /* Enable timer 1 clock and reset */
101  Chip_Clock_EnableOpts(CLK_MX_TIMER1, true, true, 1);
104 
105  /* Timer setup for match and interrupt at TICKRATE */
108  Chip_TIMER_SetMatch(LPC_TIMER1, 1, (1000000 / freq));
111 
112  NVIC_SetPriority(TIMER1_IRQn, ((0x01 << 3) | 0x01));
113  /* Enable timer interrupt */
114  NVIC_EnableIRQ(TIMER1_IRQn);
115 }
116 
117 void DeInitTimer(void)
118 {
120  NVIC_DisableIRQ(TIMER1_IRQn);
121 }
122 
124 void SetupHardware(void)
125 {
126  Board_Init();
127  /* Hardware Initialization */
130  // ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_32);
131  // ADC_SetupChannel(MIC_IN_ADC_CHANNEL);
132  USB_Init();
133 
134  /* Create a stdio stream for the serial port for stdin and stdout */
136 }
137 
141 void EVENT_USB_Host_DeviceAttached(const uint8_t corenum)
142 {
143  DEBUGOUT(("Device Attached on port %d\r\n"), corenum);
144  // LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
145 }
146 
150 void EVENT_USB_Host_DeviceUnattached(const uint8_t corenum)
151 {
152  DEBUGOUT(("\r\nDevice Unattached on port %d\r\n"), corenum);
153  // LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
154  DeInitTimer();
155 }
156 
160 void EVENT_USB_Host_DeviceEnumerationComplete(const uint8_t corenum)
161 {
162  // LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
163 
164  uint16_t ConfigDescriptorSize;
165  uint8_t ConfigDescriptorData[512];
166 
167  if (USB_Host_GetDeviceConfigDescriptor(corenum, 1, &ConfigDescriptorSize, ConfigDescriptorData,
168  sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful) {
169  DEBUGOUT("Error Retrieving Configuration Descriptor.\r\n");
170  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
171  return;
172  }
173 
174  Speaker_Audio_Interface.Config.PortNumber = corenum;
175  if (Audio_Host_ConfigurePipes(&Speaker_Audio_Interface,
176  ConfigDescriptorSize, ConfigDescriptorData) != AUDIO_ENUMERROR_NoError) {
177  DEBUGOUT("Attached Device Not a Valid Audio Output Device.\r\n");
178  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
179  return;
180  }
181 
183  DEBUGOUT("Error Setting Device Configuration.\r\n");
184  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
185  return;
186  }
187 
188  if (Audio_Host_StartStopStreaming(&Speaker_Audio_Interface, true) != HOST_SENDCONTROL_Successful) {
189  DEBUGOUT("Error Enabling Audio Stream.\r\n");
190  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
191  USB_Host_SetDeviceConfiguration(Speaker_Audio_Interface.Config.PortNumber, 0);
192  return;
193  }
194 
195  USB_Audio_SampleFreq_t SampleRate = AUDIO_SAMPLE_FREQ(48000);
196  if (Audio_Host_GetSetEndpointProperty(&Speaker_Audio_Interface, Speaker_Audio_Interface.Config.DataOUTPipeNumber,
198  sizeof(SampleRate), &SampleRate) != HOST_SENDCONTROL_Successful) {
199  DEBUGOUT("Error Setting Audio Sampling Frequency.\r\n");
200  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
201  USB_Host_SetDeviceConfiguration(Speaker_Audio_Interface.Config.PortNumber, 0);
202  return;
203  }
204  DEBUGOUT("Audio Device Enumerated.\r\n");
205  Init_Timer(48000);
206  // LEDs_SetAllLEDs(LEDMASK_USB_READY);
207 }
208 
210 void EVENT_USB_Host_HostError(const uint8_t corenum, const uint8_t ErrorCode)
211 {
212  USB_Disable();
213 
214  DEBUGOUT(("Host Mode Error\r\n"
215  " -- Error port %d\r\n"
216  " -- Error Code %d\r\n" ), corenum, ErrorCode);
217 
218  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
219  for (;; ) ;
220 }
221 
225 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t corenum,
226  const uint8_t ErrorCode,
227  const uint8_t SubErrorCode)
228 {
229  DEBUGOUT(("Dev Enum Error\r\n"
230  " -- Error port %d\r\n"
231  " -- Error Code %d\r\n"
232  " -- Sub Error Code %d\r\n"
233  " -- In State %d\r\n" ),
234  corenum, ErrorCode, SubErrorCode, USB_HostState[corenum]);
235 
236  // LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
237 }