Converted device mode low-level demos to schedulerless.
[pub/USBasp.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 /* Setup audio stream endpoint */
140 Endpoint_ConfigureEndpoint(AUDIO_STREAM_EPNUM, EP_TYPE_ISOCHRONOUS,
141 ENDPOINT_DIR_OUT, AUDIO_STREAM_EPSIZE,
142 ENDPOINT_BANK_DOUBLE);
143
144 /* Indicate USB connected and ready */
145 LEDs_SetAllLEDs(LEDMASK_USB_READY);
146 }
147
148 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
149 * control requests that are not handled internally by the USB library (including the Audio class-specific
150 * requests) so that they can be handled appropriately for the application.
151 */
152 void EVENT_USB_UnhandledControlPacket(void)
153 {
154 /* Process General and Audio specific control requests */
155 switch (USB_ControlRequest.bRequest)
156 {
157 case REQ_SetInterface:
158 /* Set Interface is not handled by the library, as its function is application-specific */
159 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE))
160 {
161 Endpoint_ClearSETUP();
162
163 /* Check if the host is enabling the audio interface (setting AlternateSetting to 1) */
164 StreamingAudioInterfaceSelected = ((USB_ControlRequest.wValue) != 0);
165
166 /* Acknowledge status stage */
167 while (!(Endpoint_IsINReady()));
168 Endpoint_ClearIN();
169 }
170
171 break;
172 }
173 }
174
175 /** Task to manage the Audio interface, reading in audio samples from the host, and outputting them to the speakers/LEDs as
176 * desired.
177 */
178 void USB_Audio_Task(void)
179 {
180 /* Check to see if the streaming interface is selected, if not the host is not receiving audio */
181 if (!(StreamingAudioInterfaceSelected))
182 return;
183
184 /* Select the audio stream endpoint */
185 Endpoint_SelectEndpoint(AUDIO_STREAM_EPNUM);
186
187 /* Check if the current endpoint can be read from (contains a packet) and that the next sample should be read */
188 if (Endpoint_IsOUTReceived() && (TIFR0 & (1 << OCF0A)))
189 {
190 /* Clear the sample reload timer */
191 TIFR0 |= (1 << OCF0A);
192
193 /* Retrieve the signed 16-bit left and right audio samples */
194 int16_t LeftSample_16Bit = (int16_t)Endpoint_Read_Word_LE();
195 int16_t RightSample_16Bit = (int16_t)Endpoint_Read_Word_LE();
196
197 /* Check to see if the bank is now empty */
198 if (!(Endpoint_IsReadWriteAllowed()))
199 {
200 /* Acknowledge the packet, clear the bank ready for the next packet */
201 Endpoint_ClearOUT();
202 }
203
204 /* Massage signed 16-bit left and right audio samples into signed 8-bit */
205 int8_t LeftSample_8Bit = (LeftSample_16Bit >> 8);
206 int8_t RightSample_8Bit = (RightSample_16Bit >> 8);
207
208 #if defined(AUDIO_OUT_MONO)
209 /* Mix the two channels together to produce a mono, 8-bit sample */
210 int8_t MixedSample_8Bit = (((int16_t)LeftSample_8Bit + (int16_t)RightSample_8Bit) >> 1);
211
212 /* Load the sample into the PWM timer channel */
213 OCRxA = ((uint8_t)MixedSample_8Bit ^ (1 << 7));
214 #elif defined(AUDIO_OUT_STEREO)
215 /* Load the dual 8-bit samples into the PWM timer channels */
216 OCRxA = ((uint8_t)LeftSample_8Bit ^ (1 << 7));
217 OCRxB = ((uint8_t)RightSample_8Bit ^ (1 << 7));
218 #elif defined(AUDIO_OUT_PORTC)
219 /* Mix the two channels together to produce a mono, 8-bit sample */
220 int8_t MixedSample_8Bit = (((int16_t)LeftSample_8Bit + (int16_t)RightSample_8Bit) >> 1);
221
222 PORTC = MixedSample_8Bit;
223 #else
224 uint8_t LEDMask = LEDS_NO_LEDS;
225
226 /* Make left channel positive (absolute) */
227 if (LeftSample_8Bit < 0)
228 LeftSample_8Bit = -LeftSample_8Bit;
229
230 /* Make right channel positive (absolute) */
231 if (RightSample_8Bit < 0)
232 RightSample_8Bit = -RightSample_8Bit;
233
234 /* Set first LED based on sample value */
235 if (LeftSample_8Bit < ((128 / 8) * 1))
236 LEDMask |= LEDS_LED2;
237 else if (LeftSample_8Bit < ((128 / 8) * 3))
238 LEDMask |= (LEDS_LED1 | LEDS_LED2);
239 else
240 LEDMask |= LEDS_LED1;
241
242 /* Set second LED based on sample value */
243 if (RightSample_8Bit < ((128 / 8) * 1))
244 LEDMask |= LEDS_LED4;
245 else if (RightSample_8Bit < ((128 / 8) * 3))
246 LEDMask |= (LEDS_LED3 | LEDS_LED4);
247 else
248 LEDMask |= LEDS_LED3;
249
250 LEDs_SetAllLEDs(LEDMask);
251 #endif
252 }
253 }