3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
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_t Speaker_Audio_Interface
=
45 .StreamingInterfaceNumber
= 1,
47 .DataOUTEndpointNumber
= AUDIO_STREAM_EPNUM
,
48 .DataOUTEndpointSize
= AUDIO_STREAM_EPSIZE
,
51 /** Main program entry point. This routine contains the overall program flow, including initial
52 * setup of all components and the main program loop.
58 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
62 if (Speaker_Audio_Interface
.InterfaceEnabled
)
65 Audio_Device_USBTask(&Speaker_Audio_Interface
);
70 /** Configures the board hardware and chip peripherals for the demo's functionality. */
71 void SetupHardware(void)
73 /* Disable watchdog if enabled by bootloader/fuses */
74 MCUSR
&= ~(1 << WDRF
);
77 /* Disable clock division */
78 clock_prescale_set(clock_div_1
);
80 /* Hardware Initialization */
85 /** Processes the next audio sample by reading the last ADC conversion and writing it to the audio
86 * interface, each time the sample reload timer period elapses to give a constant sample rate.
88 void ProcessNextSample(void)
90 if ((TIFR0
& (1 << OCF0A
)) && Audio_Device_IsSampleReceived(&Speaker_Audio_Interface
))
92 /* Clear the sample reload timer */
93 TIFR0
|= (1 << OCF0A
);
95 /* Retrieve the signed 16-bit left and right audio samples */
96 int16_t LeftSample_16Bit
= (int16_t)Audio_Device_ReadSample16();
97 int16_t RightSample_16Bit
= (int16_t)Audio_Device_ReadSample16();
99 /* Massage signed 16-bit left and right audio samples into signed 8-bit */
100 int8_t LeftSample_8Bit
= (LeftSample_16Bit
>> 8);
101 int8_t RightSample_8Bit
= (RightSample_16Bit
>> 8);
103 /* Mix the two channels together to produce a mono, 8-bit sample */
104 int8_t MixedSample_8Bit
= (((int16_t)LeftSample_8Bit
+ (int16_t)RightSample_8Bit
) >> 1);
106 #if defined(AUDIO_OUT_MONO)
107 /* Load the sample into the PWM timer channel */
108 OCRxA
= ((uint8_t)MixedSample_8Bit
^ (1 << 7));
109 #elif defined(AUDIO_OUT_STEREO)
110 /* Load the dual 8-bit samples into the PWM timer channels */
111 OCRxA
= ((uint8_t)LeftSample_8Bit
^ (1 << 7));
112 OCRxB
= ((uint8_t)RightSample_8Bit
^ (1 << 7));
113 #elif defined(AUDIO_OUT_PORTC)
114 PORTC
= MixedSample_8Bit
;
116 uint8_t LEDMask
= LEDS_NO_LEDS
;
118 /* Make mixed sample value positive (absolute) */
119 if (MixedSample_8Bit
< 0)
120 MixedSample_8Bit
= -MixedSample_8Bit
;
122 if (MixedSample_8Bit
> ((128 / 8) * 1))
123 LEDMask
|= LEDS_LED1
;
125 if (MixedSample_8Bit
> ((128 / 8) * 2))
126 LEDMask
|= LEDS_LED2
;
128 if (MixedSample_8Bit
> ((128 / 8) * 3))
129 LEDMask
|= LEDS_LED3
;
131 if (MixedSample_8Bit
> ((128 / 8) * 4))
132 LEDMask
|= LEDS_LED4
;
134 LEDs_SetAllLEDs(LEDMask
);
139 /** Event handler for the library USB Connection event. */
140 void EVENT_USB_Connect(void)
142 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
144 /* Sample reload timer initialization */
145 OCR0A
= (F_CPU
/ AUDIO_SAMPLE_FREQUENCY
) - 1;
146 TCCR0A
= (1 << WGM01
); // CTC mode
147 TCCR0B
= (1 << CS00
); // Fcpu speed
149 #if defined(AUDIO_OUT_MONO)
150 /* Set speaker as output */
152 #elif defined(AUDIO_OUT_STEREO)
153 /* Set speakers as outputs */
154 DDRC
|= ((1 << 6) | (1 << 5));
155 #elif defined(AUDIO_OUT_PORTC)
156 /* Set PORTC as outputs */
160 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
161 /* PWM speaker timer initialization */
162 TCCRxA
= ((1 << WGMx0
) | (1 << COMxA1
) | (1 << COMxA0
)
163 | (1 << COMxB1
) | (1 << COMxB0
)); // Set on match, clear on TOP
164 TCCRxB
= ((1 << WGMx2
) | (1 << CSx0
)); // Fast 8-Bit PWM, Fcpu speed
168 /** Event handler for the library USB Disconnection event. */
169 void EVENT_USB_Disconnect(void)
171 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
173 /* Stop the sample reload timer */
176 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
177 /* Stop the PWM generation timer */
181 #if defined(AUDIO_OUT_MONO)
182 /* Set speaker as input to reduce current draw */
184 #elif defined(AUDIO_OUT_STEREO)
185 /* Set speakers as inputs to reduce current draw */
186 DDRC
&= ~((1 << 6) | (1 << 5));
187 #elif defined(AUDIO_OUT_PORTC)
193 /** Event handler for the library USB Configuration Changed event. */
194 void EVENT_USB_ConfigurationChanged(void)
196 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
198 if (!(Audio_Device_ConfigureEndpoints(&Speaker_Audio_Interface
)))
199 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
202 /** Event handler for the library USB Unhandled Control Packet event. */
203 void EVENT_USB_UnhandledControlPacket(void)
205 Audio_Device_ProcessControlPacket(&Speaker_Audio_Interface
);