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
31 #include "AudioOutput.h"
33 USB_ClassInfo_Audio_t Speaker_Audio_Interface
=
37 .DataOUTEndpointNumber
= AUDIO_STREAM_EPNUM
,
38 .DataOUTEndpointSize
= AUDIO_STREAM_EPSIZE
,
45 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
49 if (Speaker_Audio_Interface
.InterfaceEnabled
)
56 void SetupHardware(void)
58 /* Disable watchdog if enabled by bootloader/fuses */
59 MCUSR
&= ~(1 << WDRF
);
62 /* Disable clock division */
63 clock_prescale_set(clock_div_1
);
65 /* Hardware Initialization */
70 void ProcessNextSample(void)
72 if ((TIFR0
& (1 << OCF0A
)) && USB_Audio_IsSampleReceived(&Speaker_Audio_Interface
))
74 /* Clear the sample reload timer */
75 TIFR0
|= (1 << OCF0A
);
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();
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);
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);
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);
99 PORTC
= MixedSample_8Bit
;
101 uint8_t LEDMask
= LEDS_NO_LEDS
;
103 /* Make left channel positive (absolute) */
104 if (LeftSample_8Bit
< 0)
105 LeftSample_8Bit
= -LeftSample_8Bit
;
107 /* Make right channel positive (absolute) */
108 if (RightSample_8Bit
< 0)
109 RightSample_8Bit
= -RightSample_8Bit
;
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
);
117 LEDMask
|= LEDS_LED1
;
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
);
125 LEDMask
|= LEDS_LED3
;
127 LEDs_SetAllLEDs(LEDMask
);
132 void EVENT_USB_Connect(void)
134 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
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
141 #if defined(AUDIO_OUT_MONO)
142 /* Set speaker as output */
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 */
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
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.
163 void EVENT_USB_Disconnect(void)
165 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
167 /* Stop the timers */
169 #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
173 #if defined(AUDIO_OUT_MONO)
174 /* Set speaker as input to reduce current draw */
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)
185 void EVENT_USB_ConfigurationChanged(void)
187 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
189 if (!(USB_Audio_ConfigureEndpoints(&Speaker_Audio_Interface
)))
190 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
193 void EVENT_USB_UnhandledControlPacket(void)
195 USB_Audio_ProcessControlPacket(&Speaker_Audio_Interface
);