3 Copyright (C) Dean Camera, 2011.
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 /** 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.
46 puts_P(PSTR(ESC_FG_CYAN
"Audio Host Demo running.\r\n" ESC_FG_WHITE
));
48 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
58 /** Configures the board hardware and chip peripherals for the demo's functionality. */
59 void SetupHardware(void)
61 /* Disable watchdog if enabled by bootloader/fuses */
62 MCUSR
&= ~(1 << WDRF
);
65 /* Disable clock division */
66 clock_prescale_set(clock_div_1
);
68 /* Hardware Initialization */
69 Serial_Init(9600, false);
73 /* Create a stdio stream for the serial port for stdin and stdout */
74 Serial_CreateStream(NULL
);
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.
80 void EVENT_USB_Host_DeviceAttached(void)
82 puts_P(PSTR(ESC_FG_GREEN
"Device Attached.\r\n" ESC_FG_WHITE
));
83 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
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.
89 void EVENT_USB_Host_DeviceUnattached(void)
91 puts_P(PSTR(ESC_FG_GREEN
"Device Unattached.\r\n" ESC_FG_WHITE
));
92 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
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.
98 void EVENT_USB_Host_DeviceEnumerationComplete(void)
100 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
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
)
108 printf_P(PSTR(ESC_FG_RED
"Host Mode Error\r\n"
109 " -- Error Code %d\r\n" ESC_FG_WHITE
), ErrorCode
);
111 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
115 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
116 * enumerating an attached USB device.
118 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode
,
119 const uint8_t SubErrorCode
)
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
);
126 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
129 void Audio_Task(void)
133 switch (USB_HostState
)
135 case HOST_STATE_Addressed
:
136 puts_P(PSTR("Getting Config Data.\r\n"));
138 /* Get and process the configuration descriptor data */
139 if ((ErrorCode
= ProcessConfigurationDescriptor()) != SuccessfulConfigRead
)
141 if (ErrorCode
== ControlError
)
142 puts_P(PSTR(ESC_FG_RED
"Control Error (Get Configuration).\r\n"));
144 puts_P(PSTR(ESC_FG_RED
"Invalid Device.\r\n"));
146 printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE
), ErrorCode
);
148 /* Indicate error status */
149 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
151 /* Wait until USB device disconnected */
152 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
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
)
159 printf_P(PSTR(ESC_FG_RED
"Control Error (Set Configuration).\r\n"
160 " -- Error Code: %d\r\n" ESC_FG_WHITE
), ErrorCode
);
162 /* Indicate error status */
163 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
165 /* Wait until USB device disconnected */
166 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
170 if ((ErrorCode
= USB_Host_SetInterfaceAltSetting(StreamingInterfaceIndex
,
171 StreamingInterfaceAltSetting
)) != HOST_SENDCONTROL_Successful
)
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
);
176 /* Indicate error status */
177 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
179 /* Wait until USB device disconnected */
180 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
184 USB_ControlRequest
= (USB_Request_Header_t
)
186 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_ENDPOINT
),
187 .bRequest
= AUDIO_REQ_SetCurrent
,
189 .wIndex
= StreamingEndpointAddress
,
190 .wLength
= sizeof(USB_Audio_SampleFreq_t
),
193 USB_Audio_SampleFreq_t SampleRate
= AUDIO_SAMPLE_FREQ(48000);
195 /* Select the control pipe for the request transfer */
196 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
198 /* Set the sample rate on the streaming interface endpoint */
199 if ((ErrorCode
= USB_Host_SendControlRequest(&SampleRate
)) != HOST_SENDCONTROL_Successful
)
201 /* Indicate error status */
202 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
204 /* Wait until USB device disconnected */
205 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
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
215 /* Set speaker as output */
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
223 puts_P(PSTR("Microphone Enumerated.\r\n"));
225 USB_HostState
= HOST_STATE_Configured
;
227 case HOST_STATE_Configured
:
232 /** ISR to handle the reloading of the PWM timer with the next sample. */
233 ISR(TIMER0_COMPA_vect
, ISR_BLOCK
)
235 uint8_t PrevPipe
= Pipe_GetCurrentPipe();
237 Pipe_SelectPipe(AUDIO_DATA_IN_PIPE
);
240 /* Check if the current pipe can be read from (contains a packet) and the device is sending data */
241 if (Pipe_IsINReceived())
243 /* Retrieve the signed 16-bit audio sample, convert to 8-bit */
244 int8_t Sample_8Bit
= (Pipe_Read_16_LE() >> 8);
246 /* Check to see if the bank is now empty */
247 if (!(Pipe_IsReadWriteAllowed()))
249 /* Acknowledge the packet, clear the bank ready for the next packet */
253 /* Load the sample into the PWM timer channel */
254 OCR3A
= (Sample_8Bit
^ (1 << 7));
256 uint8_t LEDMask
= LEDS_NO_LEDS
;
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
);
268 LEDs_SetAllLEDs(LEDMask
);
273 Pipe_SelectPipe(PrevPipe
);