3 Copyright (C) Dean Camera, 2013.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2013 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 disclaims 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 Input Host Demo running.\r\n" ESC_FG_WHITE
));
48 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
49 GlobalInterruptEnable();
57 /** Configures the board hardware and chip peripherals for the demo's functionality. */
58 void SetupHardware(void)
60 /* Disable watchdog if enabled by bootloader/fuses */
61 MCUSR
&= ~(1 << WDRF
);
64 /* Disable clock division */
65 clock_prescale_set(clock_div_1
);
67 /* Hardware Initialization */
68 Serial_Init(9600, false);
72 /* Create a stdio stream for the serial port for stdin and stdout */
73 Serial_CreateStream(NULL
);
76 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
77 * starts the library USB task to begin the enumeration and USB management process.
79 void EVENT_USB_Host_DeviceAttached(void)
81 puts_P(PSTR(ESC_FG_GREEN
"Device Attached.\r\n" ESC_FG_WHITE
));
82 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
85 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
86 * stops the library USB task management process.
88 void EVENT_USB_Host_DeviceUnattached(void)
90 puts_P(PSTR(ESC_FG_GREEN
"Device Unattached.\r\n" ESC_FG_WHITE
));
91 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
94 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
95 * enumerated by the host and is now ready to be used by the application.
97 void EVENT_USB_Host_DeviceEnumerationComplete(void)
99 puts_P(PSTR("Getting Config Data.\r\n"));
103 /* Get and process the configuration descriptor data */
104 if ((ErrorCode
= ProcessConfigurationDescriptor()) != SuccessfulConfigRead
)
106 if (ErrorCode
== ControlError
)
107 puts_P(PSTR(ESC_FG_RED
"Control Error (Get Configuration).\r\n"));
109 puts_P(PSTR(ESC_FG_RED
"Invalid Device.\r\n"));
111 printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE
), ErrorCode
);
113 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
117 /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
118 if ((ErrorCode
= USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful
)
120 printf_P(PSTR(ESC_FG_RED
"Control Error (Set Configuration).\r\n"
121 " -- Error Code: %d\r\n" ESC_FG_WHITE
), ErrorCode
);
123 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
127 if ((ErrorCode
= USB_Host_SetInterfaceAltSetting(StreamingInterfaceIndex
,
128 StreamingInterfaceAltSetting
)) != HOST_SENDCONTROL_Successful
)
130 printf_P(PSTR(ESC_FG_RED
"Could not set alternative streaming interface setting.\r\n"
131 " -- Error Code: %d\r\n" ESC_FG_WHITE
), ErrorCode
);
133 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
134 USB_Host_SetDeviceConfiguration(0);
138 USB_ControlRequest
= (USB_Request_Header_t
)
140 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_ENDPOINT
),
141 .bRequest
= AUDIO_REQ_SetCurrent
,
142 .wValue
= (AUDIO_EPCONTROL_SamplingFreq
<< 8),
143 .wIndex
= StreamingEndpointAddress
,
144 .wLength
= sizeof(USB_Audio_SampleFreq_t
),
147 USB_Audio_SampleFreq_t SampleRate
= AUDIO_SAMPLE_FREQ(48000);
149 /* Select the control pipe for the request transfer */
150 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
152 /* Set the sample rate on the streaming interface endpoint */
153 if ((ErrorCode
= USB_Host_SendControlRequest(&SampleRate
)) != HOST_SENDCONTROL_Successful
)
155 printf_P(PSTR(ESC_FG_RED
"Could not set requested Audio sample rate.\r\n"
156 " -- Error Code: %d\r\n" ESC_FG_WHITE
), ErrorCode
);
158 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
159 USB_Host_SetDeviceConfiguration(0);
163 /* Sample reload timer initialization */
164 TIMSK0
= (1 << OCIE0A
);
165 OCR0A
= ((F_CPU
/ 8 / 48000) - 1);
166 TCCR0A
= (1 << WGM01
); // CTC mode
167 TCCR0B
= (1 << CS01
); // Fcpu/8 speed
169 /* Set speaker as output */
172 /* PWM speaker timer initialization */
173 TCCR3A
= ((1 << WGM30
) | (1 << COM3A1
) | (1 << COM3A0
)); // Set on match, clear on TOP
174 TCCR3B
= ((1 << WGM32
) | (1 << CS30
)); // Fast 8-Bit PWM, F_CPU speed
176 puts_P(PSTR("Microphone Enumerated.\r\n"));
177 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
180 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
181 void EVENT_USB_Host_HostError(const uint8_t ErrorCode
)
185 printf_P(PSTR(ESC_FG_RED
"Host Mode Error\r\n"
186 " -- Error Code %d\r\n" ESC_FG_WHITE
), ErrorCode
);
188 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
192 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
193 * enumerating an attached USB device.
195 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode
,
196 const uint8_t SubErrorCode
)
198 printf_P(PSTR(ESC_FG_RED
"Dev Enum Error\r\n"
199 " -- Error Code %d\r\n"
200 " -- Sub Error Code %d\r\n"
201 " -- In State %d\r\n" ESC_FG_WHITE
), ErrorCode
, SubErrorCode
, USB_HostState
);
203 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
206 /** ISR to handle the reloading of the PWM timer with the next sample. */
207 ISR(TIMER0_COMPA_vect
, ISR_BLOCK
)
209 uint8_t PrevPipe
= Pipe_GetCurrentPipe();
211 Pipe_SelectPipe(AUDIO_DATA_IN_PIPE
);
214 /* Check if the current pipe can be read from (contains a packet) and the device is sending data */
215 if (Pipe_IsINReceived())
217 /* Retrieve the signed 16-bit audio sample, convert to 8-bit */
218 int8_t Sample_8Bit
= (Pipe_Read_16_LE() >> 8);
220 /* Check to see if the bank is now empty */
221 if (!(Pipe_IsReadWriteAllowed()))
223 /* Acknowledge the packet, clear the bank ready for the next packet */
227 /* Load the sample into the PWM timer channel */
228 OCR3A
= (Sample_8Bit
^ (1 << 7));
230 uint8_t LEDMask
= LEDS_NO_LEDS
;
232 /* Turn on LEDs as the sample amplitude increases */
233 if (Sample_8Bit
> 16)
234 LEDMask
= (LEDS_LED1
| LEDS_LED2
| LEDS_LED3
| LEDS_LED4
);
235 else if (Sample_8Bit
> 8)
236 LEDMask
= (LEDS_LED1
| LEDS_LED2
| LEDS_LED3
);
237 else if (Sample_8Bit
> 4)
238 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
239 else if (Sample_8Bit
> 2)
240 LEDMask
= (LEDS_LED1
);
242 LEDs_SetAllLEDs(LEDMask
);
246 Pipe_SelectPipe(PrevPipe
);