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 disclaims 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,
51 .Address
= AUDIO_STREAM_EPADDR
,
52 .Size
= AUDIO_STREAM_EPSIZE
,
58 /** Current audio sampling frequency of the streaming audio endpoint. */
59 static uint32_t CurrentAudioSampleFrequency
= 48000;
62 /** Main program entry point. This routine contains the overall program flow, including initial
63 * setup of all components and the main program loop.
69 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
70 GlobalInterruptEnable();
74 Audio_Device_USBTask(&Speaker_Audio_Interface
);
79 /** Configures the board hardware and chip peripherals for the demo's functionality. */
80 void SetupHardware(void)
82 /* Disable watchdog if enabled by bootloader/fuses */
83 MCUSR
&= ~(1 << WDRF
);
86 /* Disable clock division */
87 clock_prescale_set(clock_div_1
);
89 /* Hardware Initialization */
94 /** ISR to handle the reloading of the PWM timer with the next sample. */
95 ISR(TIMER0_COMPA_vect
, ISR_BLOCK
)
97 uint8_t PrevEndpoint
= Endpoint_GetCurrentEndpoint();
99 /* Check that the USB bus is ready for the next sample to read */
100 if (Audio_Device_IsSampleReceived(&Speaker_Audio_Interface
))
102 /* Retrieve the signed 16-bit left and right audio samples, convert to 8-bit */
103 int8_t LeftSample_8Bit
= (Audio_Device_ReadSample16(&Speaker_Audio_Interface
) >> 8);
104 int8_t RightSample_8Bit
= (Audio_Device_ReadSample16(&Speaker_Audio_Interface
) >> 8);
106 /* Mix the two channels together to produce a mono, 8-bit sample */
107 int8_t MixedSample_8Bit
= (((int16_t)LeftSample_8Bit
+ (int16_t)RightSample_8Bit
) >> 1);
109 #if defined(AUDIO_OUT_MONO)
110 /* Load the sample into the PWM timer channel */
111 OCR3A
= (MixedSample_8Bit
^ (1 << 7));
112 #elif defined(AUDIO_OUT_STEREO)
113 /* Load the dual 8-bit samples into the PWM timer channels */
114 OCR3A
= (LeftSample_8Bit
^ (1 << 7));
115 OCR3B
= (RightSample_8Bit
^ (1 << 7));
116 #elif defined(AUDIO_OUT_PORTC)
117 /* Load the 8-bit mixed sample into PORTC */
118 PORTC
= MixedSample_8Bit
;
121 uint8_t LEDMask
= LEDS_NO_LEDS
;
123 /* Turn on LEDs as the sample amplitude increases */
124 if (MixedSample_8Bit
> 16)
125 LEDMask
= (LEDS_LED1
| LEDS_LED2
| LEDS_LED3
| LEDS_LED4
);
126 else if (MixedSample_8Bit
> 8)
127 LEDMask
= (LEDS_LED1
| LEDS_LED2
| LEDS_LED3
);
128 else if (MixedSample_8Bit
> 4)
129 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
130 else if (MixedSample_8Bit
> 2)
131 LEDMask
= (LEDS_LED1
);
133 LEDs_SetAllLEDs(LEDMask
);
136 Endpoint_SelectEndpoint(PrevEndpoint
);
139 /** Event handler for the library USB Connection event. */
140 void EVENT_USB_Device_Connect(void)
142 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
144 /* Sample reload timer initialization */
145 TIMSK0
= (1 << OCIE0A
);
146 OCR0A
= ((F_CPU
/ 8 / CurrentAudioSampleFrequency
) - 1);
147 TCCR0A
= (1 << WGM01
); // CTC mode
148 TCCR0B
= (1 << CS01
); // Fcpu/8 speed
150 #if defined(AUDIO_OUT_MONO)
151 /* Set speaker as output */
153 #elif defined(AUDIO_OUT_STEREO)
154 /* Set speakers as outputs */
155 DDRC
|= ((1 << 6) | (1 << 5));
156 #elif defined(AUDIO_OUT_PORTC)
157 /* Set PORTC as outputs */
161 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
162 /* PWM speaker timer initialization */
163 TCCR3A
= ((1 << WGM30
) | (1 << COM3A1
) | (1 << COM3A0
)
164 | (1 << COM3B1
) | (1 << COM3B0
)); // Set on match, clear on TOP
165 TCCR3B
= ((1 << WGM32
) | (1 << CS30
)); // Fast 8-Bit PWM, F_CPU speed
169 /** Event handler for the library USB Disconnection event. */
170 void EVENT_USB_Device_Disconnect(void)
172 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
174 /* Stop the sample reload timer */
177 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
178 /* Stop the PWM generation timer */
182 #if defined(AUDIO_OUT_MONO)
183 /* Set speaker as input to reduce current draw */
185 #elif defined(AUDIO_OUT_STEREO)
186 /* Set speakers as inputs to reduce current draw */
187 DDRC
&= ~((1 << 6) | (1 << 5));
188 #elif defined(AUDIO_OUT_PORTC)
194 /** Event handler for the library USB Configuration Changed event. */
195 void EVENT_USB_Device_ConfigurationChanged(void)
197 bool ConfigSuccess
= true;
199 ConfigSuccess
&= Audio_Device_ConfigureEndpoints(&Speaker_Audio_Interface
);
201 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY
: LEDMASK_USB_ERROR
);
204 /** Event handler for the library USB Control Request reception event. */
205 void EVENT_USB_Device_ControlRequest(void)
207 Audio_Device_ProcessControlRequest(&Speaker_Audio_Interface
);
210 /** Audio class driver callback for the setting and retrieval of streaming endpoint properties. This callback must be implemented
211 * in the user application to handle property manipulations on streaming audio endpoints.
213 * When the DataLength parameter is NULL, this callback should only indicate whether the specified operation is valid for
214 * the given endpoint index, and should return as fast as possible. When non-NULL, this value may be altered for GET operations
215 * to indicate the size of the retrieved data.
217 * \note The length of the retrieved data stored into the Data buffer on GET operations should not exceed the initial value
218 * of the \c DataLength parameter.
220 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
221 * \param[in] EndpointProperty Property of the endpoint to get or set, a value from Audio_ClassRequests_t.
222 * \param[in] EndpointAddress Address of the streaming endpoint whose property is being referenced.
223 * \param[in] EndpointControl Parameter of the endpoint to get or set, a value from Audio_EndpointControls_t.
224 * \param[in,out] DataLength For SET operations, the length of the parameter data to set. For GET operations, the maximum
225 * length of the retrieved data. When NULL, the function should return whether the given property
226 * and parameter is valid for the requested endpoint without reading or modifying the Data buffer.
227 * \param[in,out] Data Pointer to a location where the parameter data is stored for SET operations, or where
228 * the retrieved data is to be stored for GET operations.
230 * \return Boolean true if the property get/set was successful, false otherwise
232 bool CALLBACK_Audio_Device_GetSetEndpointProperty(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
,
233 const uint8_t EndpointProperty
,
234 const uint8_t EndpointAddress
,
235 const uint8_t EndpointControl
,
236 uint16_t* const DataLength
,
239 /* Check the requested endpoint to see if a supported endpoint is being manipulated */
240 if (EndpointAddress
== Speaker_Audio_Interface
.Config
.DataOUTEndpoint
.Address
)
242 /* Check the requested control to see if a supported control is being manipulated */
243 if (EndpointControl
== AUDIO_EPCONTROL_SamplingFreq
)
245 switch (EndpointProperty
)
247 case AUDIO_REQ_SetCurrent
:
248 /* Check if we are just testing for a valid property, or actually adjusting it */
249 if (DataLength
!= NULL
)
251 /* Set the new sampling frequency to the value given by the host */
252 CurrentAudioSampleFrequency
= (((uint32_t)Data
[2] << 16) | ((uint32_t)Data
[1] << 8) | (uint32_t)Data
[0]);
254 /* Adjust sample reload timer to the new frequency */
255 OCR0A
= ((F_CPU
/ 8 / CurrentAudioSampleFrequency
) - 1);
259 case AUDIO_REQ_GetCurrent
:
260 /* Check if we are just testing for a valid property, or actually reading it */
261 if (DataLength
!= NULL
)
265 Data
[2] = (CurrentAudioSampleFrequency
>> 16);
266 Data
[1] = (CurrentAudioSampleFrequency
>> 8);
267 Data
[0] = (CurrentAudioSampleFrequency
& 0xFF);
278 /** Audio class driver callback for the setting and retrieval of streaming interface properties. This callback must be implemented
279 * in the user application to handle property manipulations on streaming audio interfaces.
281 * When the DataLength parameter is NULL, this callback should only indicate whether the specified operation is valid for
282 * the given entity and should return as fast as possible. When non-NULL, this value may be altered for GET operations
283 * to indicate the size of the retrieved data.
285 * \note The length of the retrieved data stored into the Data buffer on GET operations should not exceed the initial value
286 * of the \c DataLength parameter.
288 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
289 * \param[in] Property Property of the interface to get or set, a value from Audio_ClassRequests_t.
290 * \param[in] EntityAddress Address of the audio entity whose property is being referenced.
291 * \param[in] Parameter Parameter of the entity to get or set, specific to each type of entity (see USB Audio specification).
292 * \param[in,out] DataLength For SET operations, the length of the parameter data to set. For GET operations, the maximum
293 * length of the retrieved data. When NULL, the function should return whether the given property
294 * and parameter is valid for the requested endpoint without reading or modifying the Data buffer.
295 * \param[in,out] Data Pointer to a location where the parameter data is stored for SET operations, or where
296 * the retrieved data is to be stored for GET operations.
298 * \return Boolean \c true if the property GET/SET was successful, \c false otherwise
300 bool CALLBACK_Audio_Device_GetSetInterfaceProperty(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
,
301 const uint8_t Property
,
302 const uint8_t EntityAddress
,
303 const uint16_t Parameter
,
304 uint16_t* const DataLength
,
307 /* No audio interface entities in the device descriptor, thus no properties to get or set. */