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 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 /** 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. 
  43 USB_ClassInfo_Audio_Host_t Speaker_Audio_Interface 
= 
  47                                 .DataOUTPipeNumber 
= 1, 
  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. 
  59         puts_P(PSTR(ESC_FG_CYAN 
"Audio Output Host Demo running.\r\n" ESC_FG_WHITE
)); 
  61         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  66                 switch (USB_HostState
) 
  68                         case HOST_STATE_Addressed
: 
  69                                 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
  71                                 uint16_t ConfigDescriptorSize
; 
  72                                 uint8_t  ConfigDescriptorData
[512]; 
  74                                 if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize
, ConfigDescriptorData
, 
  75                                                                        sizeof(ConfigDescriptorData
)) != HOST_GETCONFIG_Successful
) 
  77                                         puts_P(PSTR("Error Retrieving Configuration Descriptor.\r\n")); 
  78                                         LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
  79                                         USB_HostState 
= HOST_STATE_WaitForDeviceRemoval
; 
  83                                 if (Audio_Host_ConfigurePipes(&Speaker_Audio_Interface
, 
  84                                                               ConfigDescriptorSize
, ConfigDescriptorData
) != AUDIO_ENUMERROR_NoError
) 
  86                                         puts_P(PSTR("Attached Device Not a Valid Audio Output Device.\r\n")); 
  87                                         LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
  88                                         USB_HostState 
= HOST_STATE_WaitForDeviceRemoval
; 
  92                                 if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful
) 
  94                                         puts_P(PSTR("Error Setting Device Configuration.\r\n")); 
  95                                         LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
  96                                         USB_HostState 
= HOST_STATE_WaitForDeviceRemoval
; 
 100                                 if (Audio_Host_StartStopStreaming(&Speaker_Audio_Interface
, true) != HOST_SENDCONTROL_Successful
) 
 102                                         puts_P(PSTR("Error Enabling Audio Stream.\r\n")); 
 103                                         LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 104                                         USB_HostState 
= HOST_STATE_WaitForDeviceRemoval
; 
 108                                 USB_Audio_SampleFreq_t SampleRate 
= AUDIO_SAMPLE_FREQ(48000); 
 109                                 if (Audio_Host_GetSetEndpointProperty(&Speaker_Audio_Interface
, Speaker_Audio_Interface
.Config
.DataOUTPipeNumber
, 
 110                                                                       AUDIO_REQ_SetCurrent
, AUDIO_EPCONTROL_SamplingFreq
, 
 111                                                                       sizeof(SampleRate
), &SampleRate
) != HOST_SENDCONTROL_Successful
) 
 113                                         puts_P(PSTR("Error Setting Audio Sampling Frequency.\r\n")); 
 114                                         LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 115                                         USB_HostState 
= HOST_STATE_WaitForDeviceRemoval
; 
 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 
 125                                 puts_P(PSTR("Audio Device Enumerated.\r\n")); 
 126                                 LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
 127                                 USB_HostState 
= HOST_STATE_Configured
; 
 129                         case HOST_STATE_Configured
: 
 130                                 /* Do nothing - audio stream is handled by the timer interrupt routine */ 
 134                 Audio_Host_USBTask(&Speaker_Audio_Interface
); 
 139 /** ISR to handle the reloading of the PWM timer with the next sample. */ 
 140 ISR(TIMER0_COMPA_vect
, ISR_BLOCK
) 
 142         uint8_t PrevPipe 
= Pipe_GetCurrentPipe(); 
 144         /* Check that the USB bus is ready for the next sample to write */ 
 145         if (Audio_Host_IsReadyForNextSample(&Speaker_Audio_Interface
)) 
 149                 #if defined(USE_TEST_TONE) 
 150                         static uint8_t SquareWaveSampleCount
; 
 151                         static int16_t CurrentWaveValue
; 
 153                         /* In test tone mode, generate a square wave at 1/256 of the sample rate */ 
 154                         if (SquareWaveSampleCount
++ == 0xFF) 
 155                           CurrentWaveValue 
^= 0x8000; 
 157                         /* Only generate audio if the board button is being pressed */ 
 158                         AudioSample 
= (Buttons_GetStatus() & BUTTONS_BUTTON1
) ? CurrentWaveValue 
: 0; 
 160                         /* Audio sample is ADC value scaled to fit the entire range */ 
 161                         AudioSample 
= ((SAMPLE_MAX_RANGE 
/ ADC_MAX_RANGE
) * ADC_GetResult()); 
 163                         #if defined(MICROPHONE_BIASED_TO_HALF_RAIL) 
 164                         /* Microphone is biased to half rail voltage, subtract the bias from the sample value */ 
 165                         AudioSample 
-= (SAMPLE_MAX_RANGE 
/ 2); 
 169                 Audio_Host_WriteSample16(&Speaker_Audio_Interface
, AudioSample
);         
 170                 Audio_Host_WriteSample16(&Speaker_Audio_Interface
, AudioSample
); 
 173         Pipe_SelectPipe(PrevPipe
); 
 176 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
 177 void SetupHardware(void) 
 179         /* Disable watchdog if enabled by bootloader/fuses */ 
 180         MCUSR 
&= ~(1 << WDRF
); 
 183         /* Disable clock division */ 
 184         clock_prescale_set(clock_div_1
); 
 186         /* Hardware Initialization */ 
 187         Serial_Init(9600, false); 
 190         ADC_Init(ADC_FREE_RUNNING 
| ADC_PRESCALE_32
); 
 191         ADC_SetupChannel(MIC_IN_ADC_CHANNEL
); 
 194         /* Create a stdio stream for the serial port for stdin and stdout */ 
 195         Serial_CreateStream(NULL
); 
 198 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and 
 199  *  starts the library USB task to begin the enumeration and USB management process. 
 201 void EVENT_USB_Host_DeviceAttached(void) 
 203         puts_P(PSTR("Device Attached.\r\n")); 
 204         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
 207 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and 
 208  *  stops the library USB task management process. 
 210 void EVENT_USB_Host_DeviceUnattached(void) 
 212         puts_P(PSTR("\r\nDevice Unattached.\r\n")); 
 213         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 216 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully 
 217  *  enumerated by the host and is now ready to be used by the application. 
 219 void EVENT_USB_Host_DeviceEnumerationComplete(void) 
 221         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
 224 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */ 
 225 void EVENT_USB_Host_HostError(const uint8_t ErrorCode
) 
 229         printf_P(PSTR(ESC_FG_RED 
"Host Mode Error\r\n" 
 230                                  " -- Error Code %d\r\n" ESC_FG_WHITE
), ErrorCode
); 
 232         LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 236 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while 
 237  *  enumerating an attached USB device. 
 239 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode
, 
 240                                             const uint8_t SubErrorCode
) 
 242         printf_P(PSTR(ESC_FG_RED 
"Dev Enum Error\r\n" 
 243                                  " -- Error Code %d\r\n" 
 244                                  " -- Sub Error Code %d\r\n" 
 245                                  " -- In State %d\r\n" ESC_FG_WHITE
), ErrorCode
, SubErrorCode
, USB_HostState
); 
 247         LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);