3 Copyright (C) Dean Camera, 2012.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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 AudioInputHost demo. This file contains the main tasks of
34 * the demo and is responsible for the initial application hardware configuration.
37 #include "AudioInputHost.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_Host_t Microphone_Audio_Interface
=
47 .DataINPipeNumber
= 1,
52 /** Main program entry point. This routine configures the hardware required by the application, then
53 * enters a loop to run the application tasks in sequence.
59 puts_P(PSTR(ESC_FG_CYAN
"Audio Input Host Demo running.\r\n" ESC_FG_WHITE
));
61 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
66 Audio_Host_USBTask(&Microphone_Audio_Interface
);
71 /** ISR to handle the reloading of the PWM timer with the next sample. */
72 ISR(TIMER0_COMPA_vect
, ISR_BLOCK
)
74 uint8_t PrevPipe
= Pipe_GetCurrentPipe();
76 /* Check that the USB bus is ready for the next sample to read */
77 if (Audio_Host_IsSampleReceived(&Microphone_Audio_Interface
))
79 /* Retrieve the signed 16-bit audio sample, convert to 8-bit */
80 int8_t Sample_8Bit
= (Audio_Host_ReadSample16(&Microphone_Audio_Interface
) >> 8);
82 /* Load the sample into the PWM timer channel */
83 OCR3A
= (Sample_8Bit
^ (1 << 7));
85 uint8_t LEDMask
= LEDS_NO_LEDS
;
87 /* Turn on LEDs as the sample amplitude increases */
89 LEDMask
= (LEDS_LED1
| LEDS_LED2
| LEDS_LED3
| LEDS_LED4
);
90 else if (Sample_8Bit
> 8)
91 LEDMask
= (LEDS_LED1
| LEDS_LED2
| LEDS_LED3
);
92 else if (Sample_8Bit
> 4)
93 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
94 else if (Sample_8Bit
> 2)
95 LEDMask
= (LEDS_LED1
);
97 LEDs_SetAllLEDs(LEDMask
);
100 Pipe_SelectPipe(PrevPipe
);
103 /** Configures the board hardware and chip peripherals for the demo's functionality. */
104 void SetupHardware(void)
106 /* Disable watchdog if enabled by bootloader/fuses */
107 MCUSR
&= ~(1 << WDRF
);
110 /* Disable clock division */
111 clock_prescale_set(clock_div_1
);
113 /* Hardware Initialization */
114 Serial_Init(9600, false);
118 /* Create a stdio stream for the serial port for stdin and stdout */
119 Serial_CreateStream(NULL
);
122 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
123 * starts the library USB task to begin the enumeration and USB management process.
125 void EVENT_USB_Host_DeviceAttached(void)
127 puts_P(PSTR("Device Attached.\r\n"));
128 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
131 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
132 * stops the library USB task management process.
134 void EVENT_USB_Host_DeviceUnattached(void)
136 puts_P(PSTR("\r\nDevice Unattached.\r\n"));
137 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
140 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
141 * enumerated by the host and is now ready to be used by the application.
143 void EVENT_USB_Host_DeviceEnumerationComplete(void)
145 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
147 uint16_t ConfigDescriptorSize
;
148 uint8_t ConfigDescriptorData
[512];
150 if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize
, ConfigDescriptorData
,
151 sizeof(ConfigDescriptorData
)) != HOST_GETCONFIG_Successful
)
153 puts_P(PSTR("Error Retrieving Configuration Descriptor.\r\n"));
154 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
158 if (Audio_Host_ConfigurePipes(&Microphone_Audio_Interface
,
159 ConfigDescriptorSize
, ConfigDescriptorData
) != AUDIO_ENUMERROR_NoError
)
161 puts_P(PSTR("Attached Device Not a Valid Audio Input Device.\r\n"));
162 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
166 if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful
)
168 puts_P(PSTR("Error Setting Device Configuration.\r\n"));
169 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
173 if (Audio_Host_StartStopStreaming(&Microphone_Audio_Interface
, true) != HOST_SENDCONTROL_Successful
)
175 puts_P(PSTR("Error Enabling Audio Stream.\r\n"));
176 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
177 USB_Host_SetDeviceConfiguration(0);
181 USB_Audio_SampleFreq_t SampleRate
= AUDIO_SAMPLE_FREQ(48000);
182 if (Audio_Host_GetSetEndpointProperty(&Microphone_Audio_Interface
, Microphone_Audio_Interface
.Config
.DataINPipeNumber
,
183 AUDIO_REQ_SetCurrent
, AUDIO_EPCONTROL_SamplingFreq
,
184 sizeof(SampleRate
), &SampleRate
) != HOST_SENDCONTROL_Successful
)
186 puts_P(PSTR("Error Setting Audio Sampling Frequency.\r\n"));
187 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
188 USB_Host_SetDeviceConfiguration(0);
192 /* Sample reload timer initialization */
193 TIMSK0
= (1 << OCIE0A
);
194 OCR0A
= ((F_CPU
/ 8 / 48000) - 1);
195 TCCR0A
= (1 << WGM01
); // CTC mode
196 TCCR0B
= (1 << CS01
); // Fcpu/8 speed
198 /* Set speaker as output */
201 /* PWM speaker timer initialization */
202 TCCR3A
= ((1 << WGM30
) | (1 << COM3A1
) | (1 << COM3A0
)); // Set on match, clear on TOP
203 TCCR3B
= ((1 << WGM32
) | (1 << CS30
)); // Fast 8-Bit PWM, F_CPU speed
205 puts_P(PSTR("Audio Device Enumerated.\r\n"));
206 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
209 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
210 void EVENT_USB_Host_HostError(const uint8_t ErrorCode
)
214 printf_P(PSTR(ESC_FG_RED
"Host Mode Error\r\n"
215 " -- Error Code %d\r\n" ESC_FG_WHITE
), ErrorCode
);
217 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
221 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
222 * enumerating an attached USB device.
224 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode
,
225 const uint8_t SubErrorCode
)
227 printf_P(PSTR(ESC_FG_RED
"Dev Enum Error\r\n"
228 " -- Error Code %d\r\n"
229 " -- Sub Error Code %d\r\n"
230 " -- In State %d\r\n" ESC_FG_WHITE
), ErrorCode
, SubErrorCode
, USB_HostState
);
232 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);