3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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 Audio Output demo. This file contains the main tasks of the demo and
34 * is responsible for the initial application hardware configuration.
37 /* --- Project Configuration (Choose ONE) --- */
38 //#define AUDIO_OUT_MONO
39 #define AUDIO_OUT_STEREO
40 //#define AUDIO_OUT_LEDS
41 //#define AUDIO_OUT_PORTC
42 /* --- --- --- --- --- --- --- --- --- --- --- */
44 #include "AudioOutput.h"
46 /* Project Tags, for reading out using the ButtLoad project */
47 BUTTLOADTAG(ProjName
, "LUFA AudioOut App");
48 BUTTLOADTAG(BuildTime
, __TIME__
);
49 BUTTLOADTAG(BuildDate
, __DATE__
);
50 BUTTLOADTAG(LUFAVersion
, "LUFA V" LUFA_VERSION_STRING
);
52 /* Scheduler Task List */
55 { Task
: USB_USBTask
, TaskStatus
: TASK_STOP
},
56 { Task
: USB_Audio_Task
, TaskStatus
: TASK_STOP
},
60 /** Main program entry point. This routine configures the hardware required by the application, then
61 * starts the scheduler to run the application tasks.
65 /* Disable watchdog if enabled by bootloader/fuses */
66 MCUSR
&= ~(1 << WDRF
);
69 /* Disable Clock Division */
70 SetSystemClockPrescaler(0);
72 /* Hardware Initialization */
75 /* Indicate USB not ready */
76 UpdateStatus(Status_USBNotReady
);
78 /* Initialize Scheduler so that it can be used */
81 /* Initialize USB Subsystem */
84 /* Scheduling - routine never returns, so put this last in the main function */
88 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs, and
89 * configures the sample update and PWM timers.
91 EVENT_HANDLER(USB_Connect
)
93 /* Start USB management task */
94 Scheduler_SetTaskMode(USB_USBTask
, TASK_RUN
);
96 /* Indicate USB enumerating */
97 UpdateStatus(Status_USBEnumerating
);
99 /* Sample reload timer initialization */
100 OCR0A
= (F_CPU
/ AUDIO_SAMPLE_FREQUENCY
);
101 TCCR0A
= (1 << WGM01
); // CTC mode
102 TCCR0B
= (1 << CS00
); // Fcpu speed
104 #if defined(AUDIO_OUT_MONO)
105 /* Set speaker as output */
107 #elif defined(AUDIO_OUT_STEREO)
108 /* Set speakers as outputs */
109 DDRC
|= ((1 << 6) | (1 << 5));
110 #elif defined(AUDIO_OUT_PORTC)
111 /* Set PORTC as outputs */
115 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
116 /* PWM speaker timer initialization */
117 TCCRxA
= ((1 << WGMx0
) | (1 << COMxA1
) | (1 << COMxA0
)
118 | (1 << COMxB1
) | (1 << COMxB0
)); // Set on match, clear on TOP
119 TCCRxB
= ((1 << WGMx2
) | (1 << CSx0
)); // Fast 8-Bit PWM, Fcpu speed
123 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
124 * the status LEDs, disables the sample update and PWM output timers and stops the USB and Audio management tasks.
126 EVENT_HANDLER(USB_Disconnect
)
128 /* Stop the timers */
130 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
134 #if defined(AUDIO_OUT_MONO)
135 /* Set speaker as input to reduce current draw */
137 #elif defined(AUDIO_OUT_STEREO)
138 /* Set speakers as inputs to reduce current draw */
139 DDRC
&= ~((1 << 6) | (1 << 5));
140 #elif defined(AUDIO_OUT_PORTC)
145 /* Stop running audio and USB management tasks */
146 Scheduler_SetTaskMode(USB_Audio_Task
, TASK_STOP
);
147 Scheduler_SetTaskMode(USB_USBTask
, TASK_STOP
);
149 /* Indicate USB not ready */
150 UpdateStatus(Status_USBNotReady
);
153 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
154 * of the USB device after enumeration - the device endpoints are configured.
156 EVENT_HANDLER(USB_ConfigurationChanged
)
158 /* Setup audio stream endpoint */
159 Endpoint_ConfigureEndpoint(AUDIO_STREAM_EPNUM
, EP_TYPE_ISOCHRONOUS
,
160 ENDPOINT_DIR_OUT
, AUDIO_STREAM_EPSIZE
,
161 ENDPOINT_BANK_DOUBLE
);
163 /* Indicate USB connected and ready */
164 UpdateStatus(Status_USBReady
);
167 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
168 * control requests that are not handled internally by the USB library (including the Audio class-specific
169 * requests) so that they can be handled appropriately for the application.
171 EVENT_HANDLER(USB_UnhandledControlPacket
)
173 /* Process General and Audio specific control requests */
176 case REQ_SetInterface
:
177 /* Set Interface is not handled by the library, as its function is application-specific */
178 if (bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_INTERFACE
))
180 uint16_t wValue
= Endpoint_Read_Word_LE();
182 Endpoint_ClearSetupReceived();
184 /* Check if the host is enabling the audio interface (setting AlternateSetting to 1) */
187 /* Start audio task */
188 Scheduler_SetTaskMode(USB_Audio_Task
, TASK_RUN
);
192 /* Stop audio task */
193 Scheduler_SetTaskMode(USB_Audio_Task
, TASK_STOP
);
196 /* Handshake the request */
197 Endpoint_ClearSetupIN();
204 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
205 * log to a serial port, or anything else that is suitable for status updates.
207 * \param CurrentStatus Current status of the system, from the AudioOutput_StatusCodes_t enum
209 void UpdateStatus(uint8_t CurrentStatus
)
211 uint8_t LEDMask
= LEDS_NO_LEDS
;
213 /* Set the LED mask to the appropriate LED mask based on the given status code */
214 switch (CurrentStatus
)
216 case Status_USBNotReady
:
217 LEDMask
= (LEDS_LED1
);
219 case Status_USBEnumerating
:
220 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
222 case Status_USBReady
:
223 LEDMask
= (LEDS_LED2
| LEDS_LED4
);
227 /* Set the board LEDs to the new LED mask */
228 LEDs_SetAllLEDs(LEDMask
);
231 /** Task to manage the Audio interface, reading in audio samples from the host, and outputting them to the speakers/LEDs as
236 /* Select the audio stream endpoint */
237 Endpoint_SelectEndpoint(AUDIO_STREAM_EPNUM
);
239 /* Check if the current endpoint can be read from (contains a packet) and that the next sample should be read */
240 if (Endpoint_ReadWriteAllowed() && (TIFR0
& (1 << OCF0A
)))
242 /* Clear the sample reload timer */
243 TIFR0
|= (1 << OCF0A
);
245 /* Retreive the signed 16-bit left and right audio samples */
246 int16_t LeftSample_16Bit
= (int16_t)Endpoint_Read_Word_LE();
247 int16_t RightSample_16Bit
= (int16_t)Endpoint_Read_Word_LE();
249 /* Check to see if the bank is now empty */
250 if (!(Endpoint_ReadWriteAllowed()))
252 /* Acknowedge the packet, clear the bank ready for the next packet */
253 Endpoint_ClearCurrentBank();
256 /* Massage signed 16-bit left and right audio samples into signed 8-bit */
257 int8_t LeftSample_8Bit
= (LeftSample_16Bit
>> 8);
258 int8_t RightSample_8Bit
= (RightSample_16Bit
>> 8);
260 #if defined(AUDIO_OUT_MONO)
261 /* Mix the two channels together to produce a mono, 8-bit sample */
262 int8_t MixedSample_8Bit
= (((int16_t)LeftSample_8Bit
+ (int16_t)RightSample_8Bit
) >> 1);
264 /* Load the sample into the PWM timer channel */
265 OCRxA
= ((uint8_t)MixedSample_8Bit
^ (1 << 7));
266 #elif defined(AUDIO_OUT_STEREO)
267 /* Load the dual 8-bit samples into the PWM timer channels */
268 OCRxA
= ((uint8_t)LeftSample_8Bit
^ (1 << 7));
269 OCRxB
= ((uint8_t)RightSample_8Bit
^ (1 << 7));
270 #elif defined(AUDIO_OUT_PORTC)
271 /* Mix the two channels together to produce a mono, 8-bit sample */
272 int8_t MixedSample_8Bit
= (((int16_t)LeftSample_8Bit
+ (int16_t)RightSample_8Bit
) >> 1);
274 PORTC
= MixedSample_8Bit
;
276 uint8_t LEDMask
= LEDS_NO_LEDS
;
278 /* Make left channel positive (absolute) */
279 if (LeftSample_8Bit
< 0)
280 LeftSample_8Bit
= -LeftSample_8Bit
;
282 /* Make right channel positive (absolute) */
283 if (RightSample_8Bit
< 0)
284 RightSample_8Bit
= -RightSample_8Bit
;
286 /* Set first LED based on sample value */
287 if (LeftSample_8Bit
< ((128 / 8) * 1))
288 LEDMask
|= LEDS_LED2
;
289 else if (LeftSample_8Bit
< ((128 / 8) * 3))
290 LEDMask
|= (LEDS_LED1
| LEDS_LED2
);
292 LEDMask
|= LEDS_LED1
;
294 /* Set second LED based on sample value */
295 if (RightSample_8Bit
< ((128 / 8) * 1))
296 LEDMask
|= LEDS_LED4
;
297 else if (RightSample_8Bit
< ((128 / 8) * 3))
298 LEDMask
|= (LEDS_LED3
| LEDS_LED4
);
300 LEDMask
|= LEDS_LED3
;
302 LEDs_SetAllLEDs(LEDMask
);