Add MIDI class driver, update MIDI device demo to use the new USB class driver. All...
[pub/USBasp.git] / Demos / Device / AudioOutput / AudioOutput.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
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 #include "AudioOutput.h"
32
33 USB_ClassInfo_Audio_t Speaker_Audio_Interface =
34 {
35 .InterfaceNumber = 0,
36
37 .DataOUTEndpointNumber = AUDIO_STREAM_EPNUM,
38 .DataOUTEndpointSize = AUDIO_STREAM_EPSIZE,
39 };
40
41 int main(void)
42 {
43 SetupHardware();
44
45 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
46
47 for (;;)
48 {
49 if (Speaker_Audio_Interface.InterfaceEnabled)
50 ProcessNextSample();
51
52 USB_USBTask();
53 }
54 }
55
56 void SetupHardware(void)
57 {
58 /* Disable watchdog if enabled by bootloader/fuses */
59 MCUSR &= ~(1 << WDRF);
60 wdt_disable();
61
62 /* Disable clock division */
63 clock_prescale_set(clock_div_1);
64
65 /* Hardware Initialization */
66 LEDs_Init();
67 USB_Init();
68 }
69
70 void ProcessNextSample(void)
71 {
72 if ((TIFR0 & (1 << OCF0A)) && USB_Audio_IsSampleReceived(&Speaker_Audio_Interface))
73 {
74 /* Clear the sample reload timer */
75 TIFR0 |= (1 << OCF0A);
76
77 /* Retrieve the signed 16-bit left and right audio samples */
78 int16_t LeftSample_16Bit = (int16_t)USB_Audio_ReadSample16();
79 int16_t RightSample_16Bit = (int16_t)USB_Audio_ReadSample16();
80
81 /* Massage signed 16-bit left and right audio samples into signed 8-bit */
82 int8_t LeftSample_8Bit = (LeftSample_16Bit >> 8);
83 int8_t RightSample_8Bit = (RightSample_16Bit >> 8);
84
85 #if defined(AUDIO_OUT_MONO)
86 /* Mix the two channels together to produce a mono, 8-bit sample */
87 int8_t MixedSample_8Bit = (((int16_t)LeftSample_8Bit + (int16_t)RightSample_8Bit) >> 1);
88
89 /* Load the sample into the PWM timer channel */
90 OCRxA = ((uint8_t)MixedSample_8Bit ^ (1 << 7));
91 #elif defined(AUDIO_OUT_STEREO)
92 /* Load the dual 8-bit samples into the PWM timer channels */
93 OCRxA = ((uint8_t)LeftSample_8Bit ^ (1 << 7));
94 OCRxB = ((uint8_t)RightSample_8Bit ^ (1 << 7));
95 #elif defined(AUDIO_OUT_PORTC)
96 /* Mix the two channels together to produce a mono, 8-bit sample */
97 int8_t MixedSample_8Bit = (((int16_t)LeftSample_8Bit + (int16_t)RightSample_8Bit) >> 1);
98
99 PORTC = MixedSample_8Bit;
100 #else
101 uint8_t LEDMask = LEDS_NO_LEDS;
102
103 /* Make left channel positive (absolute) */
104 if (LeftSample_8Bit < 0)
105 LeftSample_8Bit = -LeftSample_8Bit;
106
107 /* Make right channel positive (absolute) */
108 if (RightSample_8Bit < 0)
109 RightSample_8Bit = -RightSample_8Bit;
110
111 /* Set first LED based on sample value */
112 if (LeftSample_8Bit < ((128 / 8) * 1))
113 LEDMask |= LEDS_LED2;
114 else if (LeftSample_8Bit < ((128 / 8) * 3))
115 LEDMask |= (LEDS_LED1 | LEDS_LED2);
116 else
117 LEDMask |= LEDS_LED1;
118
119 /* Set second LED based on sample value */
120 if (RightSample_8Bit < ((128 / 8) * 1))
121 LEDMask |= LEDS_LED4;
122 else if (RightSample_8Bit < ((128 / 8) * 3))
123 LEDMask |= (LEDS_LED3 | LEDS_LED4);
124 else
125 LEDMask |= LEDS_LED3;
126
127 LEDs_SetAllLEDs(LEDMask);
128 #endif
129 }
130 }
131
132 void EVENT_USB_Connect(void)
133 {
134 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
135
136 /* Sample reload timer initialization */
137 OCR0A = (F_CPU / AUDIO_SAMPLE_FREQUENCY) - 1;
138 TCCR0A = (1 << WGM01); // CTC mode
139 TCCR0B = (1 << CS00); // Fcpu speed
140
141 #if defined(AUDIO_OUT_MONO)
142 /* Set speaker as output */
143 DDRC |= (1 << 6);
144 #elif defined(AUDIO_OUT_STEREO)
145 /* Set speakers as outputs */
146 DDRC |= ((1 << 6) | (1 << 5));
147 #elif defined(AUDIO_OUT_PORTC)
148 /* Set PORTC as outputs */
149 DDRC |= 0xFF;
150 #endif
151
152 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
153 /* PWM speaker timer initialization */
154 TCCRxA = ((1 << WGMx0) | (1 << COMxA1) | (1 << COMxA0)
155 | (1 << COMxB1) | (1 << COMxB0)); // Set on match, clear on TOP
156 TCCRxB = ((1 << WGMx2) | (1 << CSx0)); // Fast 8-Bit PWM, Fcpu speed
157 #endif
158 }
159
160 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
161 * the status LEDs, disables the sample update and PWM output timers and stops the USB and Audio management tasks.
162 */
163 void EVENT_USB_Disconnect(void)
164 {
165 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
166
167 /* Stop the timers */
168 TCCR0B = 0;
169 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
170 TCCRxB = 0;
171 #endif
172
173 #if defined(AUDIO_OUT_MONO)
174 /* Set speaker as input to reduce current draw */
175 DDRC &= ~(1 << 6);
176 #elif defined(AUDIO_OUT_STEREO)
177 /* Set speakers as inputs to reduce current draw */
178 DDRC &= ~((1 << 6) | (1 << 5));
179 #elif defined(AUDIO_OUT_PORTC)
180 /* Set PORTC low */
181 PORTC = 0x00;
182 #endif
183 }
184
185 void EVENT_USB_ConfigurationChanged(void)
186 {
187 LEDs_SetAllLEDs(LEDMASK_USB_READY);
188
189 if (!(USB_Audio_ConfigureEndpoints(&Speaker_Audio_Interface)))
190 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
191 }
192
193 void EVENT_USB_UnhandledControlPacket(void)
194 {
195 USB_Audio_ProcessControlPacket(&Speaker_Audio_Interface);
196 }