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