3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
33 * Main source file for the AudioOutput demo. This file contains the main tasks of the demo and
34 * is responsible for the initial application hardware configuration.
37 #include "AudioOutput.h"
39 /** Flag to indicate if the streaming audio alternative interface has been selected by the host. */
40 bool StreamingAudioInterfaceSelected
= false;
42 /** Main program entry point. This routine contains the overall program flow, including initial
43 * setup of all components and the main program loop.
49 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
58 /** Configures the board hardware and chip peripherals for the demo's functionality. */
59 void SetupHardware(void)
61 /* Disable watchdog if enabled by bootloader/fuses */
62 MCUSR
&= ~(1 << WDRF
);
65 /* Disable clock division */
66 clock_prescale_set(clock_div_1
);
68 /* Hardware Initialization */
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.
76 void EVENT_USB_Device_Connect(void)
78 /* Indicate USB enumerating */
79 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
81 /* Sample reload timer initialization */
82 OCR0A
= (F_CPU
/ 8 / AUDIO_SAMPLE_FREQUENCY
) - 1;
83 TCCR0A
= (1 << WGM01
); // CTC mode
84 TCCR0B
= (1 << CS01
); // Fcpu/8 speed
86 #if defined(AUDIO_OUT_MONO)
87 /* Set speaker as output */
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 */
97 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
98 /* PWM speaker timer initialization */
99 TCCR3A
= ((1 << WGM30
) | (1 << COM3A1
) | (1 << COM3A0
)
100 | (1 << COM3B1
) | (1 << COM3B0
)); // Set on match, clear on TOP
101 TCCR3B
= ((1 << WGM32
) | (1 << CS30
)); // Fast 8-Bit PWM, Fcpu speed
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.
108 void EVENT_USB_Device_Disconnect(void)
110 /* Stop the timers */
112 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
116 #if defined(AUDIO_OUT_MONO)
117 /* Set speaker as input to reduce current draw */
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)
127 /* Indicate streaming audio interface not selected */
128 StreamingAudioInterfaceSelected
= false;
130 /* Indicate USB not ready */
131 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
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.
137 void EVENT_USB_Device_ConfigurationChanged(void)
139 /* Indicate USB connected and ready */
140 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
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
)))
147 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
151 /** Event handler for the USB_UnhandledControlRequest 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.
155 void EVENT_USB_Device_UnhandledControlRequest(void)
157 /* Process General and Audio specific control requests */
158 switch (USB_ControlRequest
.bRequest
)
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
))
164 Endpoint_ClearSETUP();
166 /* Check if the host is enabling the audio interface (setting AlternateSetting to 1) */
167 StreamingAudioInterfaceSelected
= ((USB_ControlRequest
.wValue
) != 0);
169 Endpoint_ClearStatusStage();
176 /** Task to manage the Audio interface, reading in audio samples from the host, and outputting them to the speakers/LEDs as
179 void USB_Audio_Task(void)
181 /* Device must be connected and configured for the task to run */
182 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
185 /* Check to see if the streaming interface is selected, if not the host is not receiving audio */
186 if (!(StreamingAudioInterfaceSelected
))
189 /* Select the audio stream endpoint */
190 Endpoint_SelectEndpoint(AUDIO_STREAM_EPNUM
);
192 /* Check if the current endpoint can be read from (contains a packet) and that the next sample should be read */
193 if (Endpoint_IsOUTReceived() && (TIFR0
& (1 << OCF0A
)))
195 /* Clear the sample reload timer */
196 TIFR0
|= (1 << OCF0A
);
198 /* Retrieve the signed 16-bit left and right audio samples, convert to 8-bit */
199 int8_t LeftSample_8Bit
= ((int16_t)Endpoint_Read_Word_LE() >> 8);
200 int8_t RightSample_8Bit
= ((int16_t)Endpoint_Read_Word_LE() >> 8);
202 /* Mix the two channels together to produce a mono, 8-bit sample */
203 int8_t MixedSample_8Bit
= (((int16_t)LeftSample_8Bit
+ (int16_t)RightSample_8Bit
) >> 1);
205 /* Check to see if the bank is now empty */
206 if (!(Endpoint_IsReadWriteAllowed()))
208 /* Acknowledge the packet, clear the bank ready for the next packet */
212 #if defined(AUDIO_OUT_MONO)
213 /* Load the sample into the PWM timer channel */
214 OCR3A
= (MixedSample_8Bit
^ (1 << 7));
215 #elif defined(AUDIO_OUT_STEREO)
216 /* Load the dual 8-bit samples into the PWM timer channels */
217 OCR3A
= (LeftSample_8Bit
^ (1 << 7));
218 OCR3B
= (RightSample_8Bit
^ (1 << 7));
219 #elif defined(AUDIO_OUT_PORTC)
220 /* Load the 8-bit mixed sample into PORTC */
221 PORTC
= MixedSample_8Bit
;
224 uint8_t LEDMask
= LEDS_NO_LEDS
;
226 /* Turn on LEDs as the sample amplitude increases */
227 if (MixedSample_8Bit
> 16)
228 LEDMask
= (LEDS_LED1
| LEDS_LED2
| LEDS_LED3
| LEDS_LED4
);
229 else if (MixedSample_8Bit
> 8)
230 LEDMask
= (LEDS_LED1
| LEDS_LED2
| LEDS_LED3
);
231 else if (MixedSample_8Bit
> 4)
232 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
233 else if (MixedSample_8Bit
> 2)
234 LEDMask
= (LEDS_LED1
);
236 LEDs_SetAllLEDs(LEDMask
);