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 #include "AudioOutput.h"
39 /* Scheduler Task List */
42 { Task
: USB_USBTask
, TaskStatus
: TASK_STOP
},
43 { Task
: USB_Audio_Task
, TaskStatus
: TASK_STOP
},
47 /** Main program entry point. This routine configures the hardware required by the application, then
48 * starts the scheduler to run the application tasks.
52 /* Disable watchdog if enabled by bootloader/fuses */
53 MCUSR
&= ~(1 << WDRF
);
56 /* Disable clock division */
57 clock_prescale_set(clock_div_1
);
59 /* Hardware Initialization */
62 /* Indicate USB not ready */
63 UpdateStatus(Status_USBNotReady
);
65 /* Initialize Scheduler so that it can be used */
68 /* Initialize USB Subsystem */
71 /* Scheduling - routine never returns, so put this last in the main function */
75 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs, and
76 * configures the sample update and PWM timers.
78 EVENT_HANDLER(USB_Connect
)
80 /* Start USB management task */
81 Scheduler_SetTaskMode(USB_USBTask
, TASK_RUN
);
83 /* Indicate USB enumerating */
84 UpdateStatus(Status_USBEnumerating
);
86 /* Sample reload timer initialization */
87 OCR0A
= (F_CPU
/ AUDIO_SAMPLE_FREQUENCY
) - 1;
88 TCCR0A
= (1 << WGM01
); // CTC mode
89 TCCR0B
= (1 << CS00
); // Fcpu speed
91 #if defined(AUDIO_OUT_MONO)
92 /* Set speaker as output */
94 #elif defined(AUDIO_OUT_STEREO)
95 /* Set speakers as outputs */
96 DDRC
|= ((1 << 6) | (1 << 5));
97 #elif defined(AUDIO_OUT_PORTC)
98 /* Set PORTC as outputs */
102 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
103 /* PWM speaker timer initialization */
104 TCCRxA
= ((1 << WGMx0
) | (1 << COMxA1
) | (1 << COMxA0
)
105 | (1 << COMxB1
) | (1 << COMxB0
)); // Set on match, clear on TOP
106 TCCRxB
= ((1 << WGMx2
) | (1 << CSx0
)); // Fast 8-Bit PWM, Fcpu speed
110 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
111 * the status LEDs, disables the sample update and PWM output timers and stops the USB and Audio management tasks.
113 EVENT_HANDLER(USB_Disconnect
)
115 /* Stop the timers */
117 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
121 #if defined(AUDIO_OUT_MONO)
122 /* Set speaker as input to reduce current draw */
124 #elif defined(AUDIO_OUT_STEREO)
125 /* Set speakers as inputs to reduce current draw */
126 DDRC
&= ~((1 << 6) | (1 << 5));
127 #elif defined(AUDIO_OUT_PORTC)
132 /* Stop running audio and USB management tasks */
133 Scheduler_SetTaskMode(USB_Audio_Task
, TASK_STOP
);
134 Scheduler_SetTaskMode(USB_USBTask
, TASK_STOP
);
136 /* Indicate USB not ready */
137 UpdateStatus(Status_USBNotReady
);
140 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
141 * of the USB device after enumeration - the device endpoints are configured.
143 EVENT_HANDLER(USB_ConfigurationChanged
)
145 /* Setup audio stream endpoint */
146 Endpoint_ConfigureEndpoint(AUDIO_STREAM_EPNUM
, EP_TYPE_ISOCHRONOUS
,
147 ENDPOINT_DIR_OUT
, AUDIO_STREAM_EPSIZE
,
148 ENDPOINT_BANK_DOUBLE
);
150 /* Indicate USB connected and ready */
151 UpdateStatus(Status_USBReady
);
154 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
155 * control requests that are not handled internally by the USB library (including the Audio class-specific
156 * requests) so that they can be handled appropriately for the application.
158 EVENT_HANDLER(USB_UnhandledControlPacket
)
160 /* Process General and Audio specific control requests */
163 case REQ_SetInterface
:
164 /* Set Interface is not handled by the library, as its function is application-specific */
165 if (bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_INTERFACE
))
167 uint16_t wValue
= Endpoint_Read_Word_LE();
169 Endpoint_ClearControlSETUP();
171 /* Check if the host is enabling the audio interface (setting AlternateSetting to 1) */
174 /* Start audio task */
175 Scheduler_SetTaskMode(USB_Audio_Task
, TASK_RUN
);
179 /* Stop audio task */
180 Scheduler_SetTaskMode(USB_Audio_Task
, TASK_STOP
);
183 /* Acknowledge status stage */
184 while (!(Endpoint_IsINReady()));
185 Endpoint_ClearControlIN();
192 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
193 * log to a serial port, or anything else that is suitable for status updates.
195 * \param CurrentStatus Current status of the system, from the AudioOutput_StatusCodes_t enum
197 void UpdateStatus(uint8_t CurrentStatus
)
199 uint8_t LEDMask
= LEDS_NO_LEDS
;
201 /* Set the LED mask to the appropriate LED mask based on the given status code */
202 switch (CurrentStatus
)
204 case Status_USBNotReady
:
205 LEDMask
= (LEDS_LED1
);
207 case Status_USBEnumerating
:
208 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
210 case Status_USBReady
:
211 LEDMask
= (LEDS_LED2
| LEDS_LED4
);
215 /* Set the board LEDs to the new LED mask */
216 LEDs_SetAllLEDs(LEDMask
);
219 /** Task to manage the Audio interface, reading in audio samples from the host, and outputting them to the speakers/LEDs as
224 /* Select the audio stream endpoint */
225 Endpoint_SelectEndpoint(AUDIO_STREAM_EPNUM
);
227 /* Check if the current endpoint can be read from (contains a packet) and that the next sample should be read */
228 if (Endpoint_IsOUTReceived() && (TIFR0
& (1 << OCF0A
)))
230 /* Clear the sample reload timer */
231 TIFR0
|= (1 << OCF0A
);
233 /* Retrieve the signed 16-bit left and right audio samples */
234 int16_t LeftSample_16Bit
= (int16_t)Endpoint_Read_Word_LE();
235 int16_t RightSample_16Bit
= (int16_t)Endpoint_Read_Word_LE();
237 /* Check to see if the bank is now empty */
238 if (!(Endpoint_IsReadWriteAllowed()))
240 /* Acknowledge the packet, clear the bank ready for the next packet */
244 /* Massage signed 16-bit left and right audio samples into signed 8-bit */
245 int8_t LeftSample_8Bit
= (LeftSample_16Bit
>> 8);
246 int8_t RightSample_8Bit
= (RightSample_16Bit
>> 8);
248 #if defined(AUDIO_OUT_MONO)
249 /* Mix the two channels together to produce a mono, 8-bit sample */
250 int8_t MixedSample_8Bit
= (((int16_t)LeftSample_8Bit
+ (int16_t)RightSample_8Bit
) >> 1);
252 /* Load the sample into the PWM timer channel */
253 OCRxA
= ((uint8_t)MixedSample_8Bit
^ (1 << 7));
254 #elif defined(AUDIO_OUT_STEREO)
255 /* Load the dual 8-bit samples into the PWM timer channels */
256 OCRxA
= ((uint8_t)LeftSample_8Bit
^ (1 << 7));
257 OCRxB
= ((uint8_t)RightSample_8Bit
^ (1 << 7));
258 #elif defined(AUDIO_OUT_PORTC)
259 /* Mix the two channels together to produce a mono, 8-bit sample */
260 int8_t MixedSample_8Bit
= (((int16_t)LeftSample_8Bit
+ (int16_t)RightSample_8Bit
) >> 1);
262 PORTC
= MixedSample_8Bit
;
264 uint8_t LEDMask
= LEDS_NO_LEDS
;
266 /* Make left channel positive (absolute) */
267 if (LeftSample_8Bit
< 0)
268 LeftSample_8Bit
= -LeftSample_8Bit
;
270 /* Make right channel positive (absolute) */
271 if (RightSample_8Bit
< 0)
272 RightSample_8Bit
= -RightSample_8Bit
;
274 /* Set first LED based on sample value */
275 if (LeftSample_8Bit
< ((128 / 8) * 1))
276 LEDMask
|= LEDS_LED2
;
277 else if (LeftSample_8Bit
< ((128 / 8) * 3))
278 LEDMask
|= (LEDS_LED1
| LEDS_LED2
);
280 LEDMask
|= LEDS_LED1
;
282 /* Set second LED based on sample value */
283 if (RightSample_8Bit
< ((128 / 8) * 1))
284 LEDMask
|= LEDS_LED4
;
285 else if (RightSample_8Bit
< ((128 / 8) * 3))
286 LEDMask
|= (LEDS_LED3
| LEDS_LED4
);
288 LEDMask
|= LEDS_LED3
;
290 LEDs_SetAllLEDs(LEDMask
);