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 AudioOutputHost demo. This file contains the main tasks of
34 * the demo and is responsible for the initial application hardware configuration.
37 #include "AudioOutputHost.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 Output 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);
70 ADC_Init(ADC_FREE_RUNNING
| ADC_PRESCALE_32
);
71 ADC_SetupChannel(MIC_IN_ADC_CHANNEL
);
75 /* Create a stdio stream for the serial port for stdin and stdout */
76 Serial_CreateStream(NULL
);
78 /* Start the ADC conversion in free running mode */
79 ADC_StartReading(ADC_REFERENCE_AVCC
| ADC_RIGHT_ADJUSTED
| ADC_GET_CHANNEL_MASK(MIC_IN_ADC_CHANNEL
));
82 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
83 * starts the library USB task to begin the enumeration and USB management process.
85 void EVENT_USB_Host_DeviceAttached(void)
87 puts_P(PSTR(ESC_FG_GREEN
"Device Attached.\r\n" ESC_FG_WHITE
));
88 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
91 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
92 * stops the library USB task management process.
94 void EVENT_USB_Host_DeviceUnattached(void)
96 puts_P(PSTR(ESC_FG_GREEN
"Device Unattached.\r\n" ESC_FG_WHITE
));
97 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
100 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
101 * enumerated by the host and is now ready to be used by the application.
103 void EVENT_USB_Host_DeviceEnumerationComplete(void)
105 puts_P(PSTR("Getting Config Data.\r\n"));
109 /* Get and process the configuration descriptor data */
110 if ((ErrorCode
= ProcessConfigurationDescriptor()) != SuccessfulConfigRead
)
112 if (ErrorCode
== ControlError
)
113 puts_P(PSTR(ESC_FG_RED
"Control Error (Get Configuration).\r\n"));
115 puts_P(PSTR(ESC_FG_RED
"Invalid Device.\r\n"));
117 printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE
), ErrorCode
);
119 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
123 /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
124 if ((ErrorCode
= USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful
)
126 printf_P(PSTR(ESC_FG_RED
"Control Error (Set Configuration).\r\n"
127 " -- Error Code: %d\r\n" ESC_FG_WHITE
), ErrorCode
);
129 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
133 if ((ErrorCode
= USB_Host_SetInterfaceAltSetting(StreamingInterfaceIndex
,
134 StreamingInterfaceAltSetting
)) != HOST_SENDCONTROL_Successful
)
136 printf_P(PSTR(ESC_FG_RED
"Could not set alternative streaming interface setting.\r\n"
137 " -- Error Code: %d\r\n" ESC_FG_WHITE
), ErrorCode
);
139 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
140 USB_Host_SetDeviceConfiguration(0);
144 USB_ControlRequest
= (USB_Request_Header_t
)
146 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_ENDPOINT
),
147 .bRequest
= AUDIO_REQ_SetCurrent
,
148 .wValue
= (AUDIO_EPCONTROL_SamplingFreq
<< 8),
149 .wIndex
= StreamingEndpointAddress
,
150 .wLength
= sizeof(USB_Audio_SampleFreq_t
),
153 USB_Audio_SampleFreq_t SampleRate
= AUDIO_SAMPLE_FREQ(48000);
155 /* Select the control pipe for the request transfer */
156 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
158 /* Set the sample rate on the streaming interface endpoint */
159 if ((ErrorCode
= USB_Host_SendControlRequest(&SampleRate
)) != HOST_SENDCONTROL_Successful
)
161 printf_P(PSTR(ESC_FG_RED
"Could not set requested Audio sample rate.\r\n"
162 " -- Error Code: %d\r\n" ESC_FG_WHITE
), ErrorCode
);
164 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
165 USB_Host_SetDeviceConfiguration(0);
169 /* Sample reload timer initialization */
170 TIMSK0
= (1 << OCIE0A
);
171 OCR0A
= ((F_CPU
/ 8 / 48000) - 1);
172 TCCR0A
= (1 << WGM01
); // CTC mode
173 TCCR0B
= (1 << CS01
); // Fcpu/8 speed
175 puts_P(PSTR("Speaker Enumerated.\r\n"));
176 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
179 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
180 void EVENT_USB_Host_HostError(const uint8_t ErrorCode
)
184 printf_P(PSTR(ESC_FG_RED
"Host Mode Error\r\n"
185 " -- Error Code %d\r\n" ESC_FG_WHITE
), ErrorCode
);
187 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
191 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
192 * enumerating an attached USB device.
194 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode
,
195 const uint8_t SubErrorCode
)
197 printf_P(PSTR(ESC_FG_RED
"Dev Enum Error\r\n"
198 " -- Error Code %d\r\n"
199 " -- Sub Error Code %d\r\n"
200 " -- In State %d\r\n" ESC_FG_WHITE
), ErrorCode
, SubErrorCode
, USB_HostState
);
202 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
205 /** ISR to handle the reloading of the endpoint with the next sample. */
206 ISR(TIMER0_COMPA_vect
, ISR_BLOCK
)
208 uint8_t PrevPipe
= Pipe_GetCurrentPipe();
210 Pipe_SelectPipe(AUDIO_DATA_OUT_PIPE
);
213 /* Check if the current pipe can be written to (device ready for more data) */
214 if (Pipe_IsOUTReady())
218 #if defined(USE_TEST_TONE)
219 static uint8_t SquareWaveSampleCount
;
220 static int16_t CurrentWaveValue
;
222 /* In test tone mode, generate a square wave at 1/256 of the sample rate */
223 if (SquareWaveSampleCount
++ == 0xFF)
224 CurrentWaveValue
^= 0x8000;
226 /* Only generate audio if the board button is being pressed */
227 AudioSample
= (Buttons_GetStatus() & BUTTONS_BUTTON1
) ? CurrentWaveValue
: 0;
229 /* Audio sample is ADC value scaled to fit the entire range */
230 AudioSample
= ((SAMPLE_MAX_RANGE
/ ADC_MAX_RANGE
) * ADC_GetResult());
232 #if defined(MICROPHONE_BIASED_TO_HALF_RAIL)
233 /* Microphone is biased to half rail voltage, subtract the bias from the sample value */
234 AudioSample
-= (SAMPLE_MAX_RANGE
/ 2);
238 Pipe_Write_16_LE(AudioSample
);
239 Pipe_Write_16_LE(AudioSample
);
241 if (!(Pipe_IsReadWriteAllowed()))
246 Pipe_SelectPipe(PrevPipe
);