Add missing AS4 and project description files to the new Audio Host demos.
[pub/USBasp.git] / Demos / Host / ClassDriver / AudioInputHost / AudioInputHost.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
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 AudioInputHost demo. This file contains the main tasks of
34 * the demo and is responsible for the initial application hardware configuration.
35 */
36
37 #include "AudioInputHost.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_Host_t Microphone_Audio_Interface =
44 {
45 .Config =
46 {
47 .DataINPipeNumber = 1,
48 },
49 };
50
51
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.
54 */
55 int main(void)
56 {
57 SetupHardware();
58
59 puts_P(PSTR(ESC_FG_CYAN "Audio Input Host Demo running.\r\n" ESC_FG_WHITE));
60
61 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
62 sei();
63
64 for (;;)
65 {
66 switch (USB_HostState)
67 {
68 case HOST_STATE_Addressed:
69 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
70
71 uint16_t ConfigDescriptorSize;
72 uint8_t ConfigDescriptorData[512];
73
74 if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
75 sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
76 {
77 puts_P(PSTR("Error Retrieving Configuration Descriptor.\r\n"));
78 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
79 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
80 break;
81 }
82
83 if (Audio_Host_ConfigurePipes(&Microphone_Audio_Interface,
84 ConfigDescriptorSize, ConfigDescriptorData) != AUDIO_ENUMERROR_NoError)
85 {
86 puts_P(PSTR("Attached Device Not a Valid Audio Input Device.\r\n"));
87 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
88 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
89 break;
90 }
91
92 if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
93 {
94 puts_P(PSTR("Error Setting Device Configuration.\r\n"));
95 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
96 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
97 break;
98 }
99
100 if (Audio_Host_StartStopStreaming(&Microphone_Audio_Interface, true) != HOST_SENDCONTROL_Successful)
101 {
102 puts_P(PSTR("Error Enabling Audio Stream.\r\n"));
103 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
104 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
105 break;
106 }
107
108 USB_Audio_SampleFreq_t SampleRate = AUDIO_SAMPLE_FREQ(48000);
109 if (Audio_GetSetEndpointProperty(&Microphone_Audio_Interface, Microphone_Audio_Interface.Config.DataINPipeNumber,
110 AUDIO_REQ_SetCurrent, AUDIO_EPCONTROL_SamplingFreq,
111 sizeof(SampleRate), &SampleRate) != HOST_SENDCONTROL_Successful)
112 {
113 puts_P(PSTR("Error Setting Audio Sampling Frequency.\r\n"));
114 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
115 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
116 break;
117 }
118
119 /* Sample reload timer initialization */
120 TIMSK0 = (1 << OCIE0A);
121 OCR0A = ((F_CPU / 8 / 48000) - 1);
122 TCCR0A = (1 << WGM01); // CTC mode
123 TCCR0B = (1 << CS01); // Fcpu/8 speed
124
125 /* Set speaker as output */
126 DDRC |= (1 << 6);
127
128 /* PWM speaker timer initialization */
129 TCCR3A = ((1 << WGM30) | (1 << COM3A1) | (1 << COM3A0)); // Set on match, clear on TOP
130 TCCR3B = ((1 << WGM32) | (1 << CS30)); // Fast 8-Bit PWM, F_CPU speed
131
132 puts_P(PSTR("Audio Device Enumerated.\r\n"));
133 LEDs_SetAllLEDs(LEDMASK_USB_READY);
134 USB_HostState = HOST_STATE_Configured;
135 break;
136 case HOST_STATE_Configured:
137 /* Do nothing - audio stream is handled by the timer interrupt routine */
138 break;
139 }
140
141 Audio_Host_USBTask(&Microphone_Audio_Interface);
142 USB_USBTask();
143 }
144 }
145
146 /** ISR to handle the reloading of the PWM timer with the next sample. */
147 ISR(TIMER0_COMPA_vect, ISR_BLOCK)
148 {
149 uint8_t PrevPipe = Pipe_GetCurrentPipe();
150
151 /* Check that the USB bus is ready for the next sample to read */
152 if (Audio_Host_IsSampleReceived(&Microphone_Audio_Interface))
153 {
154 /* Retrieve the signed 16-bit audio sample, convert to 8-bit */
155 int8_t Sample_8Bit = (Audio_Host_ReadSample16(&Microphone_Audio_Interface) >> 8);
156
157 /* Load the sample into the PWM timer channel */
158 OCR3A = (Sample_8Bit ^ (1 << 7));
159
160 uint8_t LEDMask = LEDS_NO_LEDS;
161
162 /* Turn on LEDs as the sample amplitude increases */
163 if (Sample_8Bit > 16)
164 LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3 | LEDS_LED4);
165 else if (Sample_8Bit > 8)
166 LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3);
167 else if (Sample_8Bit > 4)
168 LEDMask = (LEDS_LED1 | LEDS_LED2);
169 else if (Sample_8Bit > 2)
170 LEDMask = (LEDS_LED1);
171
172 LEDs_SetAllLEDs(LEDMask);
173 }
174
175 Pipe_SelectPipe(PrevPipe);
176 }
177
178 /** Configures the board hardware and chip peripherals for the demo's functionality. */
179 void SetupHardware(void)
180 {
181 /* Disable watchdog if enabled by bootloader/fuses */
182 MCUSR &= ~(1 << WDRF);
183 wdt_disable();
184
185 /* Disable clock division */
186 clock_prescale_set(clock_div_1);
187
188 /* Hardware Initialization */
189 Serial_Init(9600, false);
190 LEDs_Init();
191 USB_Init();
192
193 /* Create a stdio stream for the serial port for stdin and stdout */
194 Serial_CreateStream(NULL);
195 }
196
197 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
198 * starts the library USB task to begin the enumeration and USB management process.
199 */
200 void EVENT_USB_Host_DeviceAttached(void)
201 {
202 puts_P(PSTR("Device Attached.\r\n"));
203 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
204 }
205
206 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
207 * stops the library USB task management process.
208 */
209 void EVENT_USB_Host_DeviceUnattached(void)
210 {
211 puts_P(PSTR("\r\nDevice Unattached.\r\n"));
212 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
213 }
214
215 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
216 * enumerated by the host and is now ready to be used by the application.
217 */
218 void EVENT_USB_Host_DeviceEnumerationComplete(void)
219 {
220 LEDs_SetAllLEDs(LEDMASK_USB_READY);
221 }
222
223 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
224 void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
225 {
226 USB_Disable();
227
228 printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
229 " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
230
231 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
232 for(;;);
233 }
234
235 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
236 * enumerating an attached USB device.
237 */
238 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
239 const uint8_t SubErrorCode)
240 {
241 printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
242 " -- Error Code %d\r\n"
243 " -- Sub Error Code %d\r\n"
244 " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
245
246 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
247 }
248