Update file header copyrights for 2012.
[pub/USBasp.git] / Demos / Host / ClassDriver / AudioOutputHost / AudioOutputHost.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * Main source file for the AudioOutputHost demo. This file contains the main tasks of
34 * the demo and is responsible for the initial application hardware configuration.
35 */
36
37 #include "AudioOutputHost.h"
38
39 /** LUFA Audio Class driver interface configuration and state information. This structure is
40 * passed to all Audio Class driver functions, so that multiple instances of the same class
41 * within a device can be differentiated from one another.
42 */
43 USB_ClassInfo_Audio_Host_t Speaker_Audio_Interface =
44 {
45 .Config =
46 {
47 .DataOUTPipeNumber = 1,
48 },
49 };
50
51
52 /** Main program entry point. This routine configures the hardware required by the application, then
53 * enters a loop to run the application tasks in sequence.
54 */
55 int main(void)
56 {
57 SetupHardware();
58
59 puts_P(PSTR(ESC_FG_CYAN "Audio Output Host Demo running.\r\n" ESC_FG_WHITE));
60
61 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
62 sei();
63
64 for (;;)
65 {
66 Audio_Host_USBTask(&Speaker_Audio_Interface);
67 USB_USBTask();
68 }
69 }
70
71 /** ISR to handle the reloading of the PWM timer with the next sample. */
72 ISR(TIMER0_COMPA_vect, ISR_BLOCK)
73 {
74 uint8_t PrevPipe = Pipe_GetCurrentPipe();
75
76 /* Check that the USB bus is ready for the next sample to write */
77 if (Audio_Host_IsReadyForNextSample(&Speaker_Audio_Interface))
78 {
79 int16_t AudioSample;
80
81 #if defined(USE_TEST_TONE)
82 static uint8_t SquareWaveSampleCount;
83 static int16_t CurrentWaveValue;
84
85 /* In test tone mode, generate a square wave at 1/256 of the sample rate */
86 if (SquareWaveSampleCount++ == 0xFF)
87 CurrentWaveValue ^= 0x8000;
88
89 /* Only generate audio if the board button is being pressed */
90 AudioSample = (Buttons_GetStatus() & BUTTONS_BUTTON1) ? CurrentWaveValue : 0;
91 #else
92 /* Audio sample is ADC value scaled to fit the entire range */
93 AudioSample = ((SAMPLE_MAX_RANGE / ADC_MAX_RANGE) * ADC_GetResult());
94
95 #if defined(MICROPHONE_BIASED_TO_HALF_RAIL)
96 /* Microphone is biased to half rail voltage, subtract the bias from the sample value */
97 AudioSample -= (SAMPLE_MAX_RANGE / 2);
98 #endif
99 #endif
100
101 Audio_Host_WriteSample16(&Speaker_Audio_Interface, AudioSample);
102 Audio_Host_WriteSample16(&Speaker_Audio_Interface, AudioSample);
103 }
104
105 Pipe_SelectPipe(PrevPipe);
106 }
107
108 /** Configures the board hardware and chip peripherals for the demo's functionality. */
109 void SetupHardware(void)
110 {
111 /* Disable watchdog if enabled by bootloader/fuses */
112 MCUSR &= ~(1 << WDRF);
113 wdt_disable();
114
115 /* Disable clock division */
116 clock_prescale_set(clock_div_1);
117
118 /* Hardware Initialization */
119 Serial_Init(9600, false);
120 LEDs_Init();
121 Buttons_Init();
122 ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_32);
123 ADC_SetupChannel(MIC_IN_ADC_CHANNEL);
124 USB_Init();
125
126 /* Create a stdio stream for the serial port for stdin and stdout */
127 Serial_CreateStream(NULL);
128 }
129
130 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
131 * starts the library USB task to begin the enumeration and USB management process.
132 */
133 void EVENT_USB_Host_DeviceAttached(void)
134 {
135 puts_P(PSTR("Device Attached.\r\n"));
136 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
137 }
138
139 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
140 * stops the library USB task management process.
141 */
142 void EVENT_USB_Host_DeviceUnattached(void)
143 {
144 puts_P(PSTR("\r\nDevice Unattached.\r\n"));
145 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
146 }
147
148 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
149 * enumerated by the host and is now ready to be used by the application.
150 */
151 void EVENT_USB_Host_DeviceEnumerationComplete(void)
152 {
153 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
154
155 uint16_t ConfigDescriptorSize;
156 uint8_t ConfigDescriptorData[512];
157
158 if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
159 sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
160 {
161 puts_P(PSTR("Error Retrieving Configuration Descriptor.\r\n"));
162 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
163 return;
164 }
165
166 if (Audio_Host_ConfigurePipes(&Speaker_Audio_Interface,
167 ConfigDescriptorSize, ConfigDescriptorData) != AUDIO_ENUMERROR_NoError)
168 {
169 puts_P(PSTR("Attached Device Not a Valid Audio Output Device.\r\n"));
170 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
171 return;
172 }
173
174 if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
175 {
176 puts_P(PSTR("Error Setting Device Configuration.\r\n"));
177 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
178 return;
179 }
180
181 if (Audio_Host_StartStopStreaming(&Speaker_Audio_Interface, true) != HOST_SENDCONTROL_Successful)
182 {
183 puts_P(PSTR("Error Enabling Audio Stream.\r\n"));
184 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
185 USB_Host_SetDeviceConfiguration(0);
186 return;
187 }
188
189 USB_Audio_SampleFreq_t SampleRate = AUDIO_SAMPLE_FREQ(48000);
190 if (Audio_Host_GetSetEndpointProperty(&Speaker_Audio_Interface, Speaker_Audio_Interface.Config.DataOUTPipeNumber,
191 AUDIO_REQ_SetCurrent, AUDIO_EPCONTROL_SamplingFreq,
192 sizeof(SampleRate), &SampleRate) != HOST_SENDCONTROL_Successful)
193 {
194 puts_P(PSTR("Error Setting Audio Sampling Frequency.\r\n"));
195 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
196 USB_Host_SetDeviceConfiguration(0);
197 return;
198 }
199
200 /* Sample reload timer initialization */
201 TIMSK0 = (1 << OCIE0A);
202 OCR0A = ((F_CPU / 8 / 48000) - 1);
203 TCCR0A = (1 << WGM01); // CTC mode
204 TCCR0B = (1 << CS01); // Fcpu/8 speed
205
206 puts_P(PSTR("Audio Device Enumerated.\r\n"));
207 LEDs_SetAllLEDs(LEDMASK_USB_READY);
208 }
209
210 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
211 void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
212 {
213 USB_Disable();
214
215 printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
216 " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
217
218 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
219 for(;;);
220 }
221
222 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
223 * enumerating an attached USB device.
224 */
225 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
226 const uint8_t SubErrorCode)
227 {
228 printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
229 " -- Error Code %d\r\n"
230 " -- Sub Error Code %d\r\n"
231 " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
232
233 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
234 }
235