3      Copyright (C) Dean Camera, 2012. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2012  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 Input Host Demo running.\r\n" ESC_FG_WHITE
)); 
  48         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  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                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 156                 USB_Host_SetDeviceConfiguration(0); 
 160         /* Sample reload timer initialization */ 
 161         TIMSK0  
= (1 << OCIE0A
); 
 162         OCR0A   
= ((F_CPU 
/ 8 / 48000) - 1); 
 163         TCCR0A  
= (1 << WGM01
);  // CTC mode 
 164         TCCR0B  
= (1 << CS01
);   // Fcpu/8 speed 
 166         /* Set speaker as output */ 
 169         /* PWM speaker timer initialization */ 
 170         TCCR3A  
= ((1 << WGM30
) | (1 << COM3A1
) | (1 << COM3A0
)); // Set on match, clear on TOP 
 171         TCCR3B  
= ((1 << WGM32
) | (1 << CS30
));  // Fast 8-Bit PWM, F_CPU speed 
 173         puts_P(PSTR("Microphone Enumerated.\r\n")); 
 174         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
 177 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */ 
 178 void EVENT_USB_Host_HostError(const uint8_t ErrorCode
) 
 182         printf_P(PSTR(ESC_FG_RED 
"Host Mode Error\r\n" 
 183                                  " -- Error Code %d\r\n" ESC_FG_WHITE
), ErrorCode
); 
 185         LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 189 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while 
 190  *  enumerating an attached USB device. 
 192 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode
, 
 193                                             const uint8_t SubErrorCode
) 
 195         printf_P(PSTR(ESC_FG_RED 
"Dev Enum Error\r\n" 
 196                                  " -- Error Code %d\r\n" 
 197                                  " -- Sub Error Code %d\r\n" 
 198                                  " -- In State %d\r\n" ESC_FG_WHITE
), ErrorCode
, SubErrorCode
, USB_HostState
); 
 200         LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 203 /** ISR to handle the reloading of the PWM timer with the next sample. */ 
 204 ISR(TIMER0_COMPA_vect
, ISR_BLOCK
) 
 206         uint8_t PrevPipe 
= Pipe_GetCurrentPipe(); 
 208         Pipe_SelectPipe(AUDIO_DATA_IN_PIPE
); 
 211         /* Check if the current pipe can be read from (contains a packet) and the device is sending data */ 
 212         if (Pipe_IsINReceived()) 
 214                 /* Retrieve the signed 16-bit audio sample, convert to 8-bit */ 
 215                 int8_t Sample_8Bit 
= (Pipe_Read_16_LE() >> 8); 
 217                 /* Check to see if the bank is now empty */ 
 218                 if (!(Pipe_IsReadWriteAllowed())) 
 220                         /* Acknowledge the packet, clear the bank ready for the next packet */ 
 224                 /* Load the sample into the PWM timer channel */ 
 225                 OCR3A 
= (Sample_8Bit 
^ (1 << 7)); 
 227                 uint8_t LEDMask 
= LEDS_NO_LEDS
; 
 229                 /* Turn on LEDs as the sample amplitude increases */ 
 230                 if (Sample_8Bit 
> 16) 
 231                   LEDMask 
= (LEDS_LED1 
| LEDS_LED2 
| LEDS_LED3 
| LEDS_LED4
); 
 232                 else if (Sample_8Bit 
> 8) 
 233                   LEDMask 
= (LEDS_LED1 
| LEDS_LED2 
| LEDS_LED3
); 
 234                 else if (Sample_8Bit 
> 4) 
 235                   LEDMask 
= (LEDS_LED1 
| LEDS_LED2
); 
 236                 else if (Sample_8Bit 
> 2) 
 237                   LEDMask 
= (LEDS_LED1
); 
 239                 LEDs_SetAllLEDs(LEDMask
); 
 243         Pipe_SelectPipe(PrevPipe
);