Added USE_INTERNAL_SERIAL compile time option to automatically read out the internal...
[pub/USBasp.git] / Demos / Device / ClassDriver / 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 /** \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 .DataINEndpointNumber = AUDIO_STREAM_EPNUM,
50 .DataINEndpointSize = AUDIO_STREAM_EPSIZE,
51 },
52
53 .State =
54 {
55 // Leave all state values to their defaults
56 }
57 };
58
59 /** Main program entry point. This routine contains the overall program flow, including initial
60 * setup of all components and the main program loop.
61 */
62 int main(void)
63 {
64 SetupHardware();
65
66 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
67
68 for (;;)
69 {
70 if (Speaker_Audio_Interface.State.InterfaceEnabled)
71 ProcessNextSample();
72
73 Audio_Device_USBTask(&Speaker_Audio_Interface);
74 USB_USBTask();
75 }
76 }
77
78 /** Configures the board hardware and chip peripherals for the demo's functionality. */
79 void SetupHardware(void)
80 {
81 /* Disable watchdog if enabled by bootloader/fuses */
82 MCUSR &= ~(1 << WDRF);
83 wdt_disable();
84
85 /* Disable clock division */
86 clock_prescale_set(clock_div_1);
87
88 /* Hardware Initialization */
89 LEDs_Init();
90 USB_Init();
91 }
92
93 /** Processes the next audio sample by reading the last ADC conversion and writing it to the audio
94 * interface, each time the sample reload timer period elapses to give a constant sample rate.
95 */
96 void ProcessNextSample(void)
97 {
98 if ((TIFR0 & (1 << OCF0A)) && Audio_Device_IsSampleReceived(&Speaker_Audio_Interface))
99 {
100 /* Clear the sample reload timer */
101 TIFR0 |= (1 << OCF0A);
102
103 /* Retrieve the signed 16-bit left and right audio samples */
104 int16_t LeftSample_16Bit = (int16_t)Audio_Device_ReadSample16();
105 int16_t RightSample_16Bit = (int16_t)Audio_Device_ReadSample16();
106
107 /* Massage signed 16-bit left and right audio samples into signed 8-bit */
108 int8_t LeftSample_8Bit = (LeftSample_16Bit >> 8);
109 int8_t RightSample_8Bit = (RightSample_16Bit >> 8);
110
111 /* Mix the two channels together to produce a mono, 8-bit sample */
112 int8_t MixedSample_8Bit = (((int16_t)LeftSample_8Bit + (int16_t)RightSample_8Bit) >> 1);
113
114 #if defined(AUDIO_OUT_MONO)
115 /* Load the sample into the PWM timer channel */
116 OCRxA = ((uint8_t)MixedSample_8Bit ^ (1 << 7));
117 #elif defined(AUDIO_OUT_STEREO)
118 /* Load the dual 8-bit samples into the PWM timer channels */
119 OCRxA = ((uint8_t)LeftSample_8Bit ^ (1 << 7));
120 OCRxB = ((uint8_t)RightSample_8Bit ^ (1 << 7));
121 #elif defined(AUDIO_OUT_PORTC)
122 PORTC = MixedSample_8Bit;
123 #else
124 uint8_t LEDMask = LEDS_NO_LEDS;
125
126 /* Make mixed sample value positive (absolute) */
127 if (MixedSample_8Bit < 0)
128 MixedSample_8Bit = -MixedSample_8Bit;
129
130 if (MixedSample_8Bit > ((128 / 8) * 1))
131 LEDMask |= LEDS_LED1;
132
133 if (MixedSample_8Bit > ((128 / 8) * 2))
134 LEDMask |= LEDS_LED2;
135
136 if (MixedSample_8Bit > ((128 / 8) * 3))
137 LEDMask |= LEDS_LED3;
138
139 if (MixedSample_8Bit > ((128 / 8) * 4))
140 LEDMask |= LEDS_LED4;
141
142 LEDs_SetAllLEDs(LEDMask);
143 #endif
144 }
145 }
146
147 /** Event handler for the library USB Connection event. */
148 void EVENT_USB_Connect(void)
149 {
150 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
151
152 /* Sample reload timer initialization */
153 OCR0A = (F_CPU / AUDIO_SAMPLE_FREQUENCY) - 1;
154 TCCR0A = (1 << WGM01); // CTC mode
155 TCCR0B = (1 << CS00); // Fcpu speed
156
157 #if defined(AUDIO_OUT_MONO)
158 /* Set speaker as output */
159 DDRC |= (1 << 6);
160 #elif defined(AUDIO_OUT_STEREO)
161 /* Set speakers as outputs */
162 DDRC |= ((1 << 6) | (1 << 5));
163 #elif defined(AUDIO_OUT_PORTC)
164 /* Set PORTC as outputs */
165 DDRC |= 0xFF;
166 #endif
167
168 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
169 /* PWM speaker timer initialization */
170 TCCRxA = ((1 << WGMx0) | (1 << COMxA1) | (1 << COMxA0)
171 | (1 << COMxB1) | (1 << COMxB0)); // Set on match, clear on TOP
172 TCCRxB = ((1 << WGMx2) | (1 << CSx0)); // Fast 8-Bit PWM, Fcpu speed
173 #endif
174 }
175
176 /** Event handler for the library USB Disconnection event. */
177 void EVENT_USB_Disconnect(void)
178 {
179 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
180
181 /* Stop the sample reload timer */
182 TCCR0B = 0;
183
184 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
185 /* Stop the PWM generation timer */
186 TCCRxB = 0;
187 #endif
188
189 #if defined(AUDIO_OUT_MONO)
190 /* Set speaker as input to reduce current draw */
191 DDRC &= ~(1 << 6);
192 #elif defined(AUDIO_OUT_STEREO)
193 /* Set speakers as inputs to reduce current draw */
194 DDRC &= ~((1 << 6) | (1 << 5));
195 #elif defined(AUDIO_OUT_PORTC)
196 /* Set PORTC low */
197 PORTC = 0x00;
198 #endif
199 }
200
201 /** Event handler for the library USB Configuration Changed event. */
202 void EVENT_USB_ConfigurationChanged(void)
203 {
204 LEDs_SetAllLEDs(LEDMASK_USB_READY);
205
206 if (!(Audio_Device_ConfigureEndpoints(&Speaker_Audio_Interface)))
207 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
208 }
209
210 /** Event handler for the library USB Unhandled Control Packet event. */
211 void EVENT_USB_UnhandledControlPacket(void)
212 {
213 Audio_Device_ProcessControlPacket(&Speaker_Audio_Interface);
214 }