Added new callback to the Audio Class driver to allow for endpoint control manipulati...
[pub/USBasp.git] / Demos / Device / ClassDriver / AudioOutput / AudioOutput.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
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.
35 */
36
37 #include "AudioOutput.h"
38
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.
42 */
43 USB_ClassInfo_Audio_Device_t Speaker_Audio_Interface =
44 {
45 .Config =
46 {
47 .StreamingInterfaceNumber = 1,
48
49 .DataOUTEndpointNumber = AUDIO_STREAM_EPNUM,
50 .DataOUTEndpointSize = AUDIO_STREAM_EPSIZE,
51 },
52 };
53
54 /** Current audio sampling frequency of the streaming audio endpoint. */
55 uint32_t CurrentAudioSampleFrequency = 48000;
56
57
58 /** Main program entry point. This routine contains the overall program flow, including initial
59 * setup of all components and the main program loop.
60 */
61 int main(void)
62 {
63 SetupHardware();
64
65 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
66 sei();
67
68 for (;;)
69 {
70 Audio_Device_USBTask(&Speaker_Audio_Interface);
71 USB_USBTask();
72 }
73 }
74
75 /** Configures the board hardware and chip peripherals for the demo's functionality. */
76 void SetupHardware(void)
77 {
78 /* Disable watchdog if enabled by bootloader/fuses */
79 MCUSR &= ~(1 << WDRF);
80 wdt_disable();
81
82 /* Disable clock division */
83 clock_prescale_set(clock_div_1);
84
85 /* Hardware Initialization */
86 LEDs_Init();
87 USB_Init();
88 }
89
90 /** ISR to handle the reloading of the PWM timer with the next sample. */
91 ISR(TIMER0_COMPA_vect, ISR_BLOCK)
92 {
93 uint8_t PrevEndpoint = Endpoint_GetCurrentEndpoint();
94
95 if (Audio_Device_IsSampleReceived(&Speaker_Audio_Interface))
96 {
97 /* Retrieve the signed 16-bit left and right audio samples, convert to 8-bit */
98 int8_t LeftSample_8Bit = (Audio_Device_ReadSample16(&Speaker_Audio_Interface) >> 8);
99 int8_t RightSample_8Bit = (Audio_Device_ReadSample16(&Speaker_Audio_Interface) >> 8);
100
101 /* Mix the two channels together to produce a mono, 8-bit sample */
102 int8_t MixedSample_8Bit = (((int16_t)LeftSample_8Bit + (int16_t)RightSample_8Bit) >> 1);
103
104 #if defined(AUDIO_OUT_MONO)
105 /* Load the sample into the PWM timer channel */
106 OCR3A = (MixedSample_8Bit ^ (1 << 7));
107 #elif defined(AUDIO_OUT_STEREO)
108 /* Load the dual 8-bit samples into the PWM timer channels */
109 OCR3A = (LeftSample_8Bit ^ (1 << 7));
110 OCR3B = (RightSample_8Bit ^ (1 << 7));
111 #elif defined(AUDIO_OUT_PORTC)
112 /* Load the 8-bit mixed sample into PORTC */
113 PORTC = MixedSample_8Bit;
114 #endif
115
116 uint8_t LEDMask = LEDS_NO_LEDS;
117
118 /* Turn on LEDs as the sample amplitude increases */
119 if (MixedSample_8Bit > 16)
120 LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3 | LEDS_LED4);
121 else if (MixedSample_8Bit > 8)
122 LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3);
123 else if (MixedSample_8Bit > 4)
124 LEDMask = (LEDS_LED1 | LEDS_LED2);
125 else if (MixedSample_8Bit > 2)
126 LEDMask = (LEDS_LED1);
127
128 LEDs_SetAllLEDs(LEDMask);
129 }
130
131 Endpoint_SelectEndpoint(PrevEndpoint);
132 }
133
134 /** Event handler for the library USB Connection event. */
135 void EVENT_USB_Device_Connect(void)
136 {
137 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
138
139 /* Sample reload timer initialization */
140 TIMSK0 = (1 << OCIE0A);
141 OCR0A = ((F_CPU / 8 / CurrentAudioSampleFrequency) - 1);
142 TCCR0A = (1 << WGM01); // CTC mode
143 TCCR0B = (1 << CS01); // Fcpu/8 speed
144
145 #if defined(AUDIO_OUT_MONO)
146 /* Set speaker as output */
147 DDRC |= (1 << 6);
148 #elif defined(AUDIO_OUT_STEREO)
149 /* Set speakers as outputs */
150 DDRC |= ((1 << 6) | (1 << 5));
151 #elif defined(AUDIO_OUT_PORTC)
152 /* Set PORTC as outputs */
153 DDRC |= 0xFF;
154 #endif
155
156 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
157 /* PWM speaker timer initialization */
158 TCCR3A = ((1 << WGM30) | (1 << COM3A1) | (1 << COM3A0)
159 | (1 << COM3B1) | (1 << COM3B0)); // Set on match, clear on TOP
160 TCCR3B = ((1 << WGM32) | (1 << CS30)); // Fast 8-Bit PWM, F_CPU speed
161 #endif
162 }
163
164 /** Event handler for the library USB Disconnection event. */
165 void EVENT_USB_Device_Disconnect(void)
166 {
167 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
168
169 /* Stop the sample reload timer */
170 TCCR0B = 0;
171
172 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
173 /* Stop the PWM generation timer */
174 TCCR3B = 0;
175 #endif
176
177 #if defined(AUDIO_OUT_MONO)
178 /* Set speaker as input to reduce current draw */
179 DDRC &= ~(1 << 6);
180 #elif defined(AUDIO_OUT_STEREO)
181 /* Set speakers as inputs to reduce current draw */
182 DDRC &= ~((1 << 6) | (1 << 5));
183 #elif defined(AUDIO_OUT_PORTC)
184 /* Set PORTC low */
185 PORTC = 0x00;
186 #endif
187 }
188
189 /** Event handler for the library USB Configuration Changed event. */
190 void EVENT_USB_Device_ConfigurationChanged(void)
191 {
192 bool ConfigSuccess = true;
193
194 ConfigSuccess &= Audio_Device_ConfigureEndpoints(&Speaker_Audio_Interface);
195
196 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
197 }
198
199 /** Event handler for the library USB Control Request reception event. */
200 void EVENT_USB_Device_ControlRequest(void)
201 {
202 Audio_Device_ProcessControlRequest(&Speaker_Audio_Interface);
203 }
204
205 /** Audio class driver callback for the setting and retrieval of streaming endpoint properties. This callback must be implemented
206 * in the user application to handle property manipulations on streaming audio endpoints.
207 *
208 * When the DataLength parameter is NULL, this callback should only indicate whether the specified operation is valid for
209 * the given endpoint index, and should return as fast as possible. When non-NULL, this value may be altered for GET operations
210 * to indicate the size of the retreived data.
211 *
212 * \note The length of the retrieved data stored into the Data buffer on GET operations should not exceed the initial value
213 * of the \c DataLength parameter.
214 *
215 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
216 * \param[in] EndpointProperty Property of the endpoint to get or set, a value from Audio_ClassRequests_t.
217 * \param[in] EndpointIndex Index of the streaming endpoint whose property is being referenced.
218 * \param[in] EndpointControl Parameter of the endpoint to get or set, a value from Audio_EndpointControls_t.
219 * \param[in,out] DataLength For SET operations, the length of the parameter data to set. For GET operations, the maximum
220 * length of the retrieved data. When NULL, the function should return whether the given property
221 * and parameter is valid for the requested endpoint without reading or modifying the Data buffer.
222 * \param[in,out] Data Pointer to a location where the parameter data is stored for SET operations, or where
223 * the retrieved data is to be stored for GET operations.
224 *
225 * \return Boolean true if the property get/set was successful, false otherwise
226 */
227 bool CALLBACK_Audio_GetSetEndpointProperty(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
228 const uint8_t EndpointProperty,
229 const uint8_t EndpointIndex,
230 const uint8_t EndpointControl,
231 uint16_t* const DataLength,
232 uint8_t* Data)
233 {
234 /* Check the requested endpoint to see if a supported endpoint is being manipulated */
235 if (EndpointIndex == Speaker_Audio_Interface.Config.DataOUTEndpointNumber)
236 {
237 /* Check the requested control to see if a supported control is being manipulated */
238 if (EndpointControl == AUDIO_EPCONTROL_SamplingFreq)
239 {
240 /* Check the requested property to see if a supported property is being manipulated */
241 if (EndpointProperty == AUDIO_REQ_SetCurrent)
242 {
243 /* Check if we are just testing for a valid property, or actually adjusting it */
244 if (DataLength != NULL)
245 {
246 /* Set the new sampling frequency to the value given by the host */
247 CurrentAudioSampleFrequency = (((uint32_t)Data[2] << 16) | ((uint32_t)Data[1] << 8) | (uint32_t)Data[0]);
248
249 /* Adjust sample reload timer to the new frequency */
250 OCR0A = ((F_CPU / 8 / CurrentAudioSampleFrequency) - 1);
251 }
252
253 return true;
254 }
255 else if (EndpointProperty == AUDIO_REQ_GetCurrent)
256 {
257 /* Check if we are just testing for a valid property, or actually reading it */
258 if (DataLength != NULL)
259 {
260 *DataLength = 3;
261
262 Data[2] = (CurrentAudioSampleFrequency >> 16);
263 Data[1] = (CurrentAudioSampleFrequency >> 8);
264 Data[0] = (CurrentAudioSampleFrequency & 0xFF);
265 }
266
267 return true;
268 }
269 }
270 }
271
272 return false;
273 }