Added new incomplete AudioInputHost Host LowLevel demo.
[pub/USBasp.git] / Demos / Host / Incomplete / 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 /** Main program entry point. This routine configures the hardware required by the application, then
40 * enters a loop to run the application tasks in sequence.
41 */
42 int main(void)
43 {
44 SetupHardware();
45
46 puts_P(PSTR(ESC_FG_CYAN "Audio Host Demo running.\r\n" ESC_FG_WHITE));
47
48 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
49 sei();
50
51 for (;;)
52 {
53 Audio_Task();
54 USB_USBTask();
55 }
56 }
57
58 /** Configures the board hardware and chip peripherals for the demo's functionality. */
59 void SetupHardware(void)
60 {
61 /* Disable watchdog if enabled by bootloader/fuses */
62 MCUSR &= ~(1 << WDRF);
63 wdt_disable();
64
65 /* Disable clock division */
66 clock_prescale_set(clock_div_1);
67
68 /* Hardware Initialization */
69 Serial_Init(9600, false);
70 LEDs_Init();
71 USB_Init();
72
73 /* Create a stdio stream for the serial port for stdin and stdout */
74 Serial_CreateStream(NULL);
75 }
76
77 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
78 * starts the library USB task to begin the enumeration and USB management process.
79 */
80 void EVENT_USB_Host_DeviceAttached(void)
81 {
82 puts_P(PSTR(ESC_FG_GREEN "Device Attached.\r\n" ESC_FG_WHITE));
83 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
84 }
85
86 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
87 * stops the library USB task management process.
88 */
89 void EVENT_USB_Host_DeviceUnattached(void)
90 {
91 puts_P(PSTR(ESC_FG_GREEN "Device Unattached.\r\n" ESC_FG_WHITE));
92 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
93 }
94
95 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
96 * enumerated by the host and is now ready to be used by the application.
97 */
98 void EVENT_USB_Host_DeviceEnumerationComplete(void)
99 {
100 LEDs_SetAllLEDs(LEDMASK_USB_READY);
101 }
102
103 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
104 void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
105 {
106 USB_Disable();
107
108 printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
109 " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
110
111 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
112 for(;;);
113 }
114
115 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
116 * enumerating an attached USB device.
117 */
118 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
119 const uint8_t SubErrorCode)
120 {
121 printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
122 " -- Error Code %d\r\n"
123 " -- Sub Error Code %d\r\n"
124 " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
125
126 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
127 }
128
129 void Audio_Task(void)
130 {
131 uint8_t ErrorCode;
132
133 switch (USB_HostState)
134 {
135 case HOST_STATE_Addressed:
136 puts_P(PSTR("Getting Config Data.\r\n"));
137
138 /* Get and process the configuration descriptor data */
139 if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
140 {
141 if (ErrorCode == ControlError)
142 puts_P(PSTR(ESC_FG_RED "Control Error (Get Configuration).\r\n"));
143 else
144 puts_P(PSTR(ESC_FG_RED "Invalid Device.\r\n"));
145
146 printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
147
148 /* Indicate error status */
149 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
150
151 /* Wait until USB device disconnected */
152 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
153 break;
154 }
155
156 /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
157 if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful)
158 {
159 printf_P(PSTR(ESC_FG_RED "Control Error (Set Configuration).\r\n"
160 " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
161
162 /* Indicate error status */
163 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
164
165 /* Wait until USB device disconnected */
166 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
167 break;
168 }
169
170 if ((ErrorCode = USB_Host_SetInterfaceAltSetting(StreamingInterfaceIndex,
171 StreamingInterfaceAltSetting)) != HOST_SENDCONTROL_Successful)
172 {
173 printf_P(PSTR(ESC_FG_RED "Could not set alternative streaming interface setting.\r\n"
174 " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
175
176 /* Indicate error status */
177 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
178
179 /* Wait until USB device disconnected */
180 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
181 break;
182 }
183
184 USB_ControlRequest = (USB_Request_Header_t)
185 {
186 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_ENDPOINT),
187 .bRequest = AUDIO_REQ_SetCurrent,
188 .wValue = 0x0100,
189 .wIndex = StreamingEndpointAddress,
190 .wLength = sizeof(USB_Audio_SampleFreq_t),
191 };
192
193 USB_Audio_SampleFreq_t SampleRate = AUDIO_SAMPLE_FREQ(48000);
194
195 /* Select the control pipe for the request transfer */
196 Pipe_SelectPipe(PIPE_CONTROLPIPE);
197
198 /* Set the sample rate on the streaming interface endpoint */
199 if ((ErrorCode = USB_Host_SendControlRequest(&SampleRate)) != HOST_SENDCONTROL_Successful)
200 {
201 /* Indicate error status */
202 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
203
204 /* Wait until USB device disconnected */
205 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
206 break;
207 }
208
209 /* Sample reload timer initialization */
210 TIMSK0 = (1 << OCIE0A);
211 OCR0A = ((F_CPU / 8 / 8000) - 1);
212 TCCR0A = (1 << WGM01); // CTC mode
213 TCCR0B = (1 << CS01); // Fcpu/8 speed
214
215 /* Set speaker as output */
216 DDRC |= (1 << 6);
217
218 /* PWM speaker timer initialization */
219 TCCR3A = ((1 << WGM30) | (1 << COM3A1) | (1 << COM3A0)
220 | (1 << COM3B1) | (1 << COM3B0)); // Set on match, clear on TOP
221 TCCR3B = ((1 << WGM32) | (1 << CS30)); // Fast 8-Bit PWM, F_CPU speed
222
223 puts_P(PSTR("Microphone Enumerated.\r\n"));
224
225 USB_HostState = HOST_STATE_Configured;
226 break;
227 case HOST_STATE_Configured:
228 break;
229 }
230 }
231
232 /** ISR to handle the reloading of the PWM timer with the next sample. */
233 ISR(TIMER0_COMPA_vect, ISR_BLOCK)
234 {
235 uint8_t PrevPipe = Pipe_GetCurrentPipe();
236
237 Pipe_SelectPipe(AUDIO_DATA_IN_PIPE);
238 Pipe_Unfreeze();
239
240 /* Check if the current pipe can be read from (contains a packet) and the device is sending data */
241 if (Pipe_IsINReceived())
242 {
243 /* Retrieve the signed 16-bit audio sample, convert to 8-bit */
244 int8_t Sample_8Bit = (Pipe_Read_16_LE() >> 8);
245
246 /* Check to see if the bank is now empty */
247 if (!(Pipe_IsReadWriteAllowed()))
248 {
249 /* Acknowledge the packet, clear the bank ready for the next packet */
250 Pipe_ClearIN();
251 }
252
253 /* Load the sample into the PWM timer channel */
254 OCR3A = (Sample_8Bit ^ (1 << 7));
255
256 uint8_t LEDMask = LEDS_NO_LEDS;
257
258 /* Turn on LEDs as the sample amplitude increases */
259 if (Sample_8Bit > 16)
260 LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3 | LEDS_LED4);
261 else if (Sample_8Bit > 8)
262 LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3);
263 else if (Sample_8Bit > 4)
264 LEDMask = (LEDS_LED1 | LEDS_LED2);
265 else if (Sample_8Bit > 2)
266 LEDMask = (LEDS_LED1);
267
268 LEDs_SetAllLEDs(LEDMask);
269 }
270
271 Pipe_Freeze();
272
273 Pipe_SelectPipe(PrevPipe);
274 }