3 Copyright (C) Dean Camera, 2012.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2012 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
34 * the demo and is responsible for the initial application hardware configuration.
37 #include "AudioOutput.h"
39 /** LUFA Audio Class driver interface configuration and state information. This structure is
40 * passed to all Audio Class driver functions, so that multiple instances of the same class
41 * within a device can be differentiated from one another.
43 USB_ClassInfo_Audio_Device_t Speaker_Audio_Interface
=
47 .ControlInterfaceNumber
= 0,
48 .StreamingInterfaceNumber
= 1,
50 .DataOUTEndpointNumber
= AUDIO_STREAM_EPNUM
,
51 .DataOUTEndpointSize
= AUDIO_STREAM_EPSIZE
,
55 /** Current audio sampling frequency of the streaming audio endpoint. */
56 static uint32_t CurrentAudioSampleFrequency
= 48000;
59 /** Main program entry point. This routine contains the overall program flow, including initial
60 * setup of all components and the main program loop.
66 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
71 Audio_Device_USBTask(&Speaker_Audio_Interface
);
76 /** Configures the board hardware and chip peripherals for the demo's functionality. */
77 void SetupHardware(void)
79 /* Disable watchdog if enabled by bootloader/fuses */
80 MCUSR
&= ~(1 << WDRF
);
83 /* Disable clock division */
84 clock_prescale_set(clock_div_1
);
86 /* Hardware Initialization */
91 /** ISR to handle the reloading of the PWM timer with the next sample. */
92 ISR(TIMER0_COMPA_vect
, ISR_BLOCK
)
94 uint8_t PrevEndpoint
= Endpoint_GetCurrentEndpoint();
96 /* Check that the USB bus is ready for the next sample to read */
97 if (Audio_Device_IsSampleReceived(&Speaker_Audio_Interface
))
99 /* Retrieve the signed 16-bit left and right audio samples, convert to 8-bit */
100 int8_t LeftSample_8Bit
= (Audio_Device_ReadSample16(&Speaker_Audio_Interface
) >> 8);
101 int8_t RightSample_8Bit
= (Audio_Device_ReadSample16(&Speaker_Audio_Interface
) >> 8);
103 /* Mix the two channels together to produce a mono, 8-bit sample */
104 int8_t MixedSample_8Bit
= (((int16_t)LeftSample_8Bit
+ (int16_t)RightSample_8Bit
) >> 1);
106 #if defined(AUDIO_OUT_MONO)
107 /* Load the sample into the PWM timer channel */
108 OCR3A
= (MixedSample_8Bit
^ (1 << 7));
109 #elif defined(AUDIO_OUT_STEREO)
110 /* Load the dual 8-bit samples into the PWM timer channels */
111 OCR3A
= (LeftSample_8Bit
^ (1 << 7));
112 OCR3B
= (RightSample_8Bit
^ (1 << 7));
113 #elif defined(AUDIO_OUT_PORTC)
114 /* Load the 8-bit mixed sample into PORTC */
115 PORTC
= MixedSample_8Bit
;
118 uint8_t LEDMask
= LEDS_NO_LEDS
;
120 /* Turn on LEDs as the sample amplitude increases */
121 if (MixedSample_8Bit
> 16)
122 LEDMask
= (LEDS_LED1
| LEDS_LED2
| LEDS_LED3
| LEDS_LED4
);
123 else if (MixedSample_8Bit
> 8)
124 LEDMask
= (LEDS_LED1
| LEDS_LED2
| LEDS_LED3
);
125 else if (MixedSample_8Bit
> 4)
126 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
127 else if (MixedSample_8Bit
> 2)
128 LEDMask
= (LEDS_LED1
);
130 LEDs_SetAllLEDs(LEDMask
);
133 Endpoint_SelectEndpoint(PrevEndpoint
);
136 /** Event handler for the library USB Connection event. */
137 void EVENT_USB_Device_Connect(void)
139 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
141 /* Sample reload timer initialization */
142 TIMSK0
= (1 << OCIE0A
);
143 OCR0A
= ((F_CPU
/ 8 / CurrentAudioSampleFrequency
) - 1);
144 TCCR0A
= (1 << WGM01
); // CTC mode
145 TCCR0B
= (1 << CS01
); // Fcpu/8 speed
147 #if defined(AUDIO_OUT_MONO)
148 /* Set speaker as output */
150 #elif defined(AUDIO_OUT_STEREO)
151 /* Set speakers as outputs */
152 DDRC
|= ((1 << 6) | (1 << 5));
153 #elif defined(AUDIO_OUT_PORTC)
154 /* Set PORTC as outputs */
158 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
159 /* PWM speaker timer initialization */
160 TCCR3A
= ((1 << WGM30
) | (1 << COM3A1
) | (1 << COM3A0
)
161 | (1 << COM3B1
) | (1 << COM3B0
)); // Set on match, clear on TOP
162 TCCR3B
= ((1 << WGM32
) | (1 << CS30
)); // Fast 8-Bit PWM, F_CPU speed
166 /** Event handler for the library USB Disconnection event. */
167 void EVENT_USB_Device_Disconnect(void)
169 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
171 /* Stop the sample reload timer */
174 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
175 /* Stop the PWM generation timer */
179 #if defined(AUDIO_OUT_MONO)
180 /* Set speaker as input to reduce current draw */
182 #elif defined(AUDIO_OUT_STEREO)
183 /* Set speakers as inputs to reduce current draw */
184 DDRC
&= ~((1 << 6) | (1 << 5));
185 #elif defined(AUDIO_OUT_PORTC)
191 /** Event handler for the library USB Configuration Changed event. */
192 void EVENT_USB_Device_ConfigurationChanged(void)
194 bool ConfigSuccess
= true;
196 ConfigSuccess
&= Audio_Device_ConfigureEndpoints(&Speaker_Audio_Interface
);
198 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY
: LEDMASK_USB_ERROR
);
201 /** Event handler for the library USB Control Request reception event. */
202 void EVENT_USB_Device_ControlRequest(void)
204 Audio_Device_ProcessControlRequest(&Speaker_Audio_Interface
);
207 /** Audio class driver callback for the setting and retrieval of streaming endpoint properties. This callback must be implemented
208 * in the user application to handle property manipulations on streaming audio endpoints.
210 * When the DataLength parameter is NULL, this callback should only indicate whether the specified operation is valid for
211 * the given endpoint index, and should return as fast as possible. When non-NULL, this value may be altered for GET operations
212 * to indicate the size of the retreived data.
214 * \note The length of the retrieved data stored into the Data buffer on GET operations should not exceed the initial value
215 * of the \c DataLength parameter.
217 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
218 * \param[in] EndpointProperty Property of the endpoint to get or set, a value from Audio_ClassRequests_t.
219 * \param[in] EndpointAddress Address of the streaming endpoint whose property is being referenced.
220 * \param[in] EndpointControl Parameter of the endpoint to get or set, a value from Audio_EndpointControls_t.
221 * \param[in,out] DataLength For SET operations, the length of the parameter data to set. For GET operations, the maximum
222 * length of the retrieved data. When NULL, the function should return whether the given property
223 * and parameter is valid for the requested endpoint without reading or modifying the Data buffer.
224 * \param[in,out] Data Pointer to a location where the parameter data is stored for SET operations, or where
225 * the retrieved data is to be stored for GET operations.
227 * \return Boolean true if the property get/set was successful, false otherwise
229 bool CALLBACK_Audio_Device_GetSetEndpointProperty(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
,
230 const uint8_t EndpointProperty
,
231 const uint8_t EndpointAddress
,
232 const uint8_t EndpointControl
,
233 uint16_t* const DataLength
,
236 /* Check the requested endpoint to see if a supported endpoint is being manipulated */
237 if (EndpointAddress
== (ENDPOINT_DIR_OUT
| Speaker_Audio_Interface
.Config
.DataOUTEndpointNumber
))
239 /* Check the requested control to see if a supported control is being manipulated */
240 if (EndpointControl
== AUDIO_EPCONTROL_SamplingFreq
)
242 switch (EndpointProperty
)
244 case AUDIO_REQ_SetCurrent
:
245 /* Check if we are just testing for a valid property, or actually adjusting it */
246 if (DataLength
!= NULL
)
248 /* Set the new sampling frequency to the value given by the host */
249 CurrentAudioSampleFrequency
= (((uint32_t)Data
[2] << 16) | ((uint32_t)Data
[1] << 8) | (uint32_t)Data
[0]);
251 /* Adjust sample reload timer to the new frequency */
252 OCR0A
= ((F_CPU
/ 8 / CurrentAudioSampleFrequency
) - 1);
256 case AUDIO_REQ_GetCurrent
:
257 /* Check if we are just testing for a valid property, or actually reading it */
258 if (DataLength
!= NULL
)
262 Data
[2] = (CurrentAudioSampleFrequency
>> 16);
263 Data
[1] = (CurrentAudioSampleFrequency
>> 8);
264 Data
[0] = (CurrentAudioSampleFrequency
& 0xFF);
275 /** Audio class driver callback for the setting and retrieval of streaming interface properties. This callback must be implemented
276 * in the user application to handle property manipulations on streaming audio interfaces.
278 * When the DataLength parameter is NULL, this callback should only indicate whether the specified operation is valid for
279 * the given entity and should return as fast as possible. When non-NULL, this value may be altered for GET operations
280 * to indicate the size of the retreived data.
282 * \note The length of the retrieved data stored into the Data buffer on GET operations should not exceed the initial value
283 * of the \c DataLength parameter.
285 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
286 * \param[in] Property Property of the interface to get or set, a value from Audio_ClassRequests_t.
287 * \param[in] EntityAddress Address of the audio entity whose property is being referenced.
288 * \param[in] Parameter Parameter of the entity to get or set, specific to each type of entity (see USB Audio specification).
289 * \param[in,out] DataLength For SET operations, the length of the parameter data to set. For GET operations, the maximum
290 * length of the retrieved data. When NULL, the function should return whether the given property
291 * and parameter is valid for the requested endpoint without reading or modifying the Data buffer.
292 * \param[in,out] Data Pointer to a location where the parameter data is stored for SET operations, or where
293 * the retrieved data is to be stored for GET operations.
295 * \return Boolean \c true if the property GET/SET was successful, \c false otherwise
297 bool CALLBACK_Audio_Device_GetSetInterfaceProperty(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
,
298 const uint8_t Property
,
299 const uint8_t EntityAddress
,
300 const uint16_t Parameter
,
301 uint16_t* const DataLength
,
304 /* No audio interface entities in the device descriptor, thus no properties to get or set. */