Some minor whitespace corrections.
[pub/lufa.git] / Demos / Device / LowLevel / AudioOutput / AudioOutput.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 Audio Output demo. This file contains the main tasks of the demo and
34 * is responsible for the initial application hardware configuration.
35 */
36
37 #include "AudioOutput.h"
38
39 /** Flag to indicate if the streaming audio alternative interface has been selected by the host. */
40 bool StreamingAudioInterfaceSelected = false;
41
42 /** Main program entry point. This routine contains the overall program flow, including initial
43 * setup of all components and the main program loop.
44 */
45 int main(void)
46 {
47 SetupHardware();
48
49 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
50
51 for (;;)
52 {
53 USB_Audio_Task();
54 USB_USBTask();
55 }
56 }
57
58 /** Configures the board hardware and chip peripherals for the demo's functionality. */
59 void SetupHardware(void)
60 {
61 /* Disable watchdog if enabled by bootloader/fuses */
62 MCUSR &= ~(1 << WDRF);
63 wdt_disable();
64
65 /* Disable clock division */
66 clock_prescale_set(clock_div_1);
67
68 /* Hardware Initialization */
69 LEDs_Init();
70 USB_Init();
71 }
72
73 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs, and
74 * configures the sample update and PWM timers.
75 */
76 void EVENT_USB_Connect(void)
77 {
78 /* Indicate USB enumerating */
79 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
80
81 /* Sample reload timer initialization */
82 OCR0A = (F_CPU / AUDIO_SAMPLE_FREQUENCY) - 1;
83 TCCR0A = (1 << WGM01); // CTC mode
84 TCCR0B = (1 << CS00); // Fcpu speed
85
86 #if defined(AUDIO_OUT_MONO)
87 /* Set speaker as output */
88 DDRC |= (1 << 6);
89 #elif defined(AUDIO_OUT_STEREO)
90 /* Set speakers as outputs */
91 DDRC |= ((1 << 6) | (1 << 5));
92 #elif defined(AUDIO_OUT_PORTC)
93 /* Set PORTC as outputs */
94 DDRC |= 0xFF;
95 #endif
96
97 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
98 /* PWM speaker timer initialization */
99 TCCRxA = ((1 << WGMx0) | (1 << COMxA1) | (1 << COMxA0)
100 | (1 << COMxB1) | (1 << COMxB0)); // Set on match, clear on TOP
101 TCCRxB = ((1 << WGMx2) | (1 << CSx0)); // Fast 8-Bit PWM, Fcpu speed
102 #endif
103 }
104
105 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
106 * the status LEDs, disables the sample update and PWM output timers and stops the USB and Audio management tasks.
107 */
108 void EVENT_USB_Disconnect(void)
109 {
110 /* Stop the timers */
111 TCCR0B = 0;
112 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
113 TCCRxB = 0;
114 #endif
115
116 #if defined(AUDIO_OUT_MONO)
117 /* Set speaker as input to reduce current draw */
118 DDRC &= ~(1 << 6);
119 #elif defined(AUDIO_OUT_STEREO)
120 /* Set speakers as inputs to reduce current draw */
121 DDRC &= ~((1 << 6) | (1 << 5));
122 #elif defined(AUDIO_OUT_PORTC)
123 /* Set PORTC low */
124 PORTC = 0x00;
125 #endif
126
127 /* Indicate streaming audio interface not selected */
128 StreamingAudioInterfaceSelected = false;
129
130 /* Indicate USB not ready */
131 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
132 }
133
134 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
135 * of the USB device after enumeration - the device endpoints are configured.
136 */
137 void EVENT_USB_ConfigurationChanged(void)
138 {
139 /* Indicate USB connected and ready */
140 LEDs_SetAllLEDs(LEDMASK_USB_READY);
141
142 /* Setup audio stream endpoint */
143 if (!(Endpoint_ConfigureEndpoint(AUDIO_STREAM_EPNUM, EP_TYPE_ISOCHRONOUS,
144 ENDPOINT_DIR_OUT, AUDIO_STREAM_EPSIZE,
145 ENDPOINT_BANK_DOUBLE)))
146 {
147 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
148 }
149 }
150
151 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
152 * control requests that are not handled internally by the USB library (including the Audio class-specific
153 * requests) so that they can be handled appropriately for the application.
154 */
155 void EVENT_USB_UnhandledControlPacket(void)
156 {
157 /* Process General and Audio specific control requests */
158 switch (USB_ControlRequest.bRequest)
159 {
160 case REQ_SetInterface:
161 /* Set Interface is not handled by the library, as its function is application-specific */
162 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE))
163 {
164 Endpoint_ClearSETUP();
165
166 /* Check if the host is enabling the audio interface (setting AlternateSetting to 1) */
167 StreamingAudioInterfaceSelected = ((USB_ControlRequest.wValue) != 0);
168
169 /* Acknowledge status stage */
170 while (!(Endpoint_IsINReady()));
171 Endpoint_ClearIN();
172 }
173
174 break;
175 }
176 }
177
178 /** Task to manage the Audio interface, reading in audio samples from the host, and outputting them to the speakers/LEDs as
179 * desired.
180 */
181 void USB_Audio_Task(void)
182 {
183 /* Check to see if the streaming interface is selected, if not the host is not receiving audio */
184 if (!(StreamingAudioInterfaceSelected))
185 return;
186
187 /* Select the audio stream endpoint */
188 Endpoint_SelectEndpoint(AUDIO_STREAM_EPNUM);
189
190 /* Check if the current endpoint can be read from (contains a packet) and that the next sample should be read */
191 if (Endpoint_IsOUTReceived() && (TIFR0 & (1 << OCF0A)))
192 {
193 /* Clear the sample reload timer */
194 TIFR0 |= (1 << OCF0A);
195
196 /* Retrieve the signed 16-bit left and right audio samples */
197 int16_t LeftSample_16Bit = (int16_t)Endpoint_Read_Word_LE();
198 int16_t RightSample_16Bit = (int16_t)Endpoint_Read_Word_LE();
199
200 /* Check to see if the bank is now empty */
201 if (!(Endpoint_IsReadWriteAllowed()))
202 {
203 /* Acknowledge the packet, clear the bank ready for the next packet */
204 Endpoint_ClearOUT();
205 }
206
207 /* Massage signed 16-bit left and right audio samples into signed 8-bit */
208 int8_t LeftSample_8Bit = (LeftSample_16Bit >> 8);
209 int8_t RightSample_8Bit = (RightSample_16Bit >> 8);
210
211 #if defined(AUDIO_OUT_MONO)
212 /* Mix the two channels together to produce a mono, 8-bit sample */
213 int8_t MixedSample_8Bit = (((int16_t)LeftSample_8Bit + (int16_t)RightSample_8Bit) >> 1);
214
215 /* Load the sample into the PWM timer channel */
216 OCRxA = ((uint8_t)MixedSample_8Bit ^ (1 << 7));
217 #elif defined(AUDIO_OUT_STEREO)
218 /* Load the dual 8-bit samples into the PWM timer channels */
219 OCRxA = ((uint8_t)LeftSample_8Bit ^ (1 << 7));
220 OCRxB = ((uint8_t)RightSample_8Bit ^ (1 << 7));
221 #elif defined(AUDIO_OUT_PORTC)
222 /* Mix the two channels together to produce a mono, 8-bit sample */
223 int8_t MixedSample_8Bit = (((int16_t)LeftSample_8Bit + (int16_t)RightSample_8Bit) >> 1);
224
225 PORTC = MixedSample_8Bit;
226 #else
227 uint8_t LEDMask = LEDS_NO_LEDS;
228
229 /* Make left channel positive (absolute) */
230 if (LeftSample_8Bit < 0)
231 LeftSample_8Bit = -LeftSample_8Bit;
232
233 /* Make right channel positive (absolute) */
234 if (RightSample_8Bit < 0)
235 RightSample_8Bit = -RightSample_8Bit;
236
237 /* Set first LED based on sample value */
238 if (LeftSample_8Bit < ((128 / 8) * 1))
239 LEDMask |= LEDS_LED2;
240 else if (LeftSample_8Bit < ((128 / 8) * 3))
241 LEDMask |= (LEDS_LED1 | LEDS_LED2);
242 else
243 LEDMask |= LEDS_LED1;
244
245 /* Set second LED based on sample value */
246 if (RightSample_8Bit < ((128 / 8) * 1))
247 LEDMask |= LEDS_LED4;
248 else if (RightSample_8Bit < ((128 / 8) * 3))
249 LEDMask |= (LEDS_LED3 | LEDS_LED4);
250 else
251 LEDMask |= LEDS_LED3;
252
253 LEDs_SetAllLEDs(LEDMask);
254 #endif
255 }
256 }