diff options
author | Jack Humbert <jack.humb@gmail.com> | 2017-07-07 11:55:23 -0400 |
---|---|---|
committer | Jack Humbert <jack.humb@gmail.com> | 2017-07-07 11:55:23 -0400 |
commit | 8655d4f4948b2deef7844503c8d690f23ac1a062 (patch) | |
tree | b2c6effc9d6cd5b5b43933a1e53b8bf17e9e82cf /lib/lufa/Demos/Host/LowLevel/AudioInputHost | |
parent | 1896c76a2928c96f9ab7947bec2ef8dd37623cff (diff) | |
parent | 60b30c036397cb5627fa374bb930794b225daa29 (diff) |
Merge commit '60b30c036397cb5627fa374bb930794b225daa29' as 'lib/lufa'
Diffstat (limited to 'lib/lufa/Demos/Host/LowLevel/AudioInputHost')
9 files changed, 3268 insertions, 0 deletions
diff --git a/lib/lufa/Demos/Host/LowLevel/AudioInputHost/AudioInputHost.c b/lib/lufa/Demos/Host/LowLevel/AudioInputHost/AudioInputHost.c new file mode 100644 index 0000000000..9db4798a56 --- /dev/null +++ b/lib/lufa/Demos/Host/LowLevel/AudioInputHost/AudioInputHost.c @@ -0,0 +1,250 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaims all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * + * Main source file for the AudioInputHost demo. This file contains the main tasks of + * the demo and is responsible for the initial application hardware configuration. + */ + +#include "AudioInputHost.h" + +/** Main program entry point. This routine configures the hardware required by the application, then + * enters a loop to run the application tasks in sequence. + */ +int main(void) +{ + SetupHardware(); + + puts_P(PSTR(ESC_FG_CYAN "Audio Input Host Demo running.\r\n" ESC_FG_WHITE)); + + LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); + GlobalInterruptEnable(); + + for (;;) + { + USB_USBTask(); + } +} + +/** Configures the board hardware and chip peripherals for the demo's functionality. */ +void SetupHardware(void) +{ +#if (ARCH == ARCH_AVR8) + /* Disable watchdog if enabled by bootloader/fuses */ + MCUSR &= ~(1 << WDRF); + wdt_disable(); + + /* Disable clock division */ + clock_prescale_set(clock_div_1); +#endif + + /* Hardware Initialization */ + Serial_Init(9600, false); + LEDs_Init(); + USB_Init(); + + /* Create a stdio stream for the serial port for stdin and stdout */ + Serial_CreateStream(NULL); +} + +/** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and + * starts the library USB task to begin the enumeration and USB management process. + */ +void EVENT_USB_Host_DeviceAttached(void) +{ + puts_P(PSTR(ESC_FG_GREEN "Device Attached.\r\n" ESC_FG_WHITE)); + LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING); +} + +/** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and + * stops the library USB task management process. + */ +void EVENT_USB_Host_DeviceUnattached(void) +{ + puts_P(PSTR(ESC_FG_GREEN "Device Unattached.\r\n" ESC_FG_WHITE)); + LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); +} + +/** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully + * enumerated by the host and is now ready to be used by the application. + */ +void EVENT_USB_Host_DeviceEnumerationComplete(void) +{ + puts_P(PSTR("Getting Config Data.\r\n")); + + uint8_t ErrorCode; + + /* Get and process the configuration descriptor data */ + if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead) + { + if (ErrorCode == ControlError) + puts_P(PSTR(ESC_FG_RED "Control Error (Get Configuration).\r\n")); + else + puts_P(PSTR(ESC_FG_RED "Invalid Device.\r\n")); + + printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode); + + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + return; + } + + /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */ + if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful) + { + printf_P(PSTR(ESC_FG_RED "Control Error (Set Configuration).\r\n" + " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode); + + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + return; + } + + if ((ErrorCode = USB_Host_SetInterfaceAltSetting(StreamingInterfaceIndex, + StreamingInterfaceAltSetting)) != HOST_SENDCONTROL_Successful) + { + printf_P(PSTR(ESC_FG_RED "Could not set alternative streaming interface setting.\r\n" + " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode); + + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + USB_Host_SetDeviceConfiguration(0); + return; + } + + USB_ControlRequest = (USB_Request_Header_t) + { + .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_ENDPOINT), + .bRequest = AUDIO_REQ_SetCurrent, + .wValue = (AUDIO_EPCONTROL_SamplingFreq << 8), + .wIndex = StreamingEndpointAddress, + .wLength = sizeof(USB_Audio_SampleFreq_t), + }; + + USB_Audio_SampleFreq_t SampleRate = AUDIO_SAMPLE_FREQ(48000); + + /* Select the control pipe for the request transfer */ + Pipe_SelectPipe(PIPE_CONTROLPIPE); + + /* Set the sample rate on the streaming interface endpoint */ + if ((ErrorCode = USB_Host_SendControlRequest(&SampleRate)) != HOST_SENDCONTROL_Successful) + { + printf_P(PSTR(ESC_FG_RED "Could not set requested Audio sample rate.\r\n" + " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode); + + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + USB_Host_SetDeviceConfiguration(0); + return; + } + + /* Sample reload timer initialization */ + TIMSK0 = (1 << OCIE0A); + OCR0A = ((F_CPU / 8 / 48000) - 1); + TCCR0A = (1 << WGM01); // CTC mode + TCCR0B = (1 << CS01); // Fcpu/8 speed + + /* Set speaker as output */ + DDRC |= (1 << 6); + + /* PWM speaker timer initialization */ + TCCR3A = ((1 << WGM30) | (1 << COM3A1) | (1 << COM3A0)); // Set on match, clear on TOP + TCCR3B = ((1 << WGM32) | (1 << CS30)); // Fast 8-Bit PWM, F_CPU speed + + puts_P(PSTR("Microphone Enumerated.\r\n")); + LEDs_SetAllLEDs(LEDMASK_USB_READY); +} + +/** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */ +void EVENT_USB_Host_HostError(const uint8_t ErrorCode) +{ + USB_Disable(); + + printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n" + " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode); + + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + for(;;); +} + +/** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while + * enumerating an attached USB device. + */ +void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode, + const uint8_t SubErrorCode) +{ + printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n" + " -- Error Code %d\r\n" + " -- Sub Error Code %d\r\n" + " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState); + + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); +} + +/** ISR to handle the reloading of the PWM timer with the next sample. */ +ISR(TIMER0_COMPA_vect, ISR_BLOCK) +{ + uint8_t PrevPipe = Pipe_GetCurrentPipe(); + + Pipe_SelectPipe(AUDIO_DATA_IN_PIPE); + Pipe_Unfreeze(); + + /* Check if the current pipe can be read from (contains a packet) and the device is sending data */ + if (Pipe_IsINReceived()) + { + /* Retrieve the signed 16-bit audio sample, convert to 8-bit */ + int8_t Sample_8Bit = (Pipe_Read_16_LE() >> 8); + + /* Check to see if the bank is now empty */ + if (!(Pipe_IsReadWriteAllowed())) + { + /* Acknowledge the packet, clear the bank ready for the next packet */ + Pipe_ClearIN(); + } + + /* Load the sample into the PWM timer channel */ + OCR3A = (Sample_8Bit ^ (1 << 7)); + + uint8_t LEDMask = LEDS_NO_LEDS; + + /* Turn on LEDs as the sample amplitude increases */ + if (Sample_8Bit > 16) + LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3 | LEDS_LED4); + else if (Sample_8Bit > 8) + LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3); + else if (Sample_8Bit > 4) + LEDMask = (LEDS_LED1 | LEDS_LED2); + else if (Sample_8Bit > 2) + LEDMask = (LEDS_LED1); + + LEDs_SetAllLEDs(LEDMask); + } + + Pipe_Freeze(); + Pipe_SelectPipe(PrevPipe); +} + diff --git a/lib/lufa/Demos/Host/LowLevel/AudioInputHost/AudioInputHost.h b/lib/lufa/Demos/Host/LowLevel/AudioInputHost/AudioInputHost.h new file mode 100644 index 0000000000..cc83270f61 --- /dev/null +++ b/lib/lufa/Demos/Host/LowLevel/AudioInputHost/AudioInputHost.h @@ -0,0 +1,79 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaims all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * + * Header file for AudioInputHost.c. + */ + +#ifndef _AUDIO_INPUT_HOST_H_ +#define _AUDIO_INPUT_HOST_H_ + + /* Includes: */ + #include <avr/io.h> + #include <avr/wdt.h> + #include <avr/pgmspace.h> + #include <avr/power.h> + #include <avr/interrupt.h> + #include <stdio.h> + + #include <LUFA/Drivers/Misc/TerminalCodes.h> + #include <LUFA/Drivers/USB/USB.h> + #include <LUFA/Drivers/Peripheral/Serial.h> + #include <LUFA/Drivers/Board/LEDs.h> + #include <LUFA/Platform/Platform.h> + + #include "ConfigDescriptor.h" + + /* Macros: */ + /** LED mask for the library LED driver, to indicate that the USB interface is not ready. */ + #define LEDMASK_USB_NOTREADY LEDS_LED1 + + /** LED mask for the library LED driver, to indicate that the USB interface is enumerating. */ + #define LEDMASK_USB_ENUMERATING (LEDS_LED2 | LEDS_LED3) + + /** LED mask for the library LED driver, to indicate that the USB interface is ready. */ + #define LEDMASK_USB_READY (LEDS_LED2 | LEDS_LED4) + + /** LED mask for the library LED driver, to indicate that an error has occurred in the USB interface. */ + #define LEDMASK_USB_ERROR (LEDS_LED1 | LEDS_LED3) + + /* Function Prototypes: */ + void SetupHardware(void); + + void EVENT_USB_Host_HostError(const uint8_t ErrorCode); + void EVENT_USB_Host_DeviceAttached(void); + void EVENT_USB_Host_DeviceUnattached(void); + void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode, + const uint8_t SubErrorCode); + void EVENT_USB_Host_DeviceEnumerationComplete(void); + +#endif + diff --git a/lib/lufa/Demos/Host/LowLevel/AudioInputHost/AudioInputHost.txt b/lib/lufa/Demos/Host/LowLevel/AudioInputHost/AudioInputHost.txt new file mode 100644 index 0000000000..0736e0df47 --- /dev/null +++ b/lib/lufa/Demos/Host/LowLevel/AudioInputHost/AudioInputHost.txt @@ -0,0 +1,66 @@ +/** \file + * + * This file contains special DoxyGen information for the generation of the main page and other special + * documentation pages. It is not a project source file. + */ + +/** \mainpage Audio Input Host Demo + * + * \section Sec_Compat Demo Compatibility: + * + * The following list indicates what microcontrollers are compatible with this demo. + * + * \li Series 7 USB AVRs (AT90USBxxx7) + * + * \section Sec_Info USB Information: + * + * The following table gives a rundown of the USB utilization of this demo. + * + * <table> + * <tr> + * <td><b>USB Mode:</b></td> + * <td>Host</td> + * </tr> + * <tr> + * <td><b>USB Class:</b></td> + * <td>Audio Class</td> + * </tr> + * <tr> + * <td><b>USB Subclass:</b></td> + * <td>Standard Audio Device</td> + * </tr> + * <tr> + * <td><b>Relevant Standards:</b></td> + * <td>USBIF Audio 1.0 Class Specification \n + * USBIF Audio 1.0 Class Terminal Types Specification \n + * USBIF Audio 1.0 Data Formats Specification</td> + * </tr> + * <tr> + * <td><b>Supported USB Speeds:</b></td> + * <td>Full Speed Mode</td> + * </tr> + * </table> + * + * \section Sec_Description Project Description: + * + * Audio Input host demonstration application. This gives a simple reference + * application for implementing a USB Audio host, for USB Audio devices using + * the USB Audio 1.0 class profile. + * + * Incoming audio will output in 8-bit PWM onto the timer 3 output compare + * channel A, and will also be indicated on the board LEDs. Decouple the PWM + * output with a capacitor and attach to a speaker to hear the audio. + * + * \section Sec_Options Project Options + * + * The following defines can be found in this demo, which can control the demo behaviour when defined, or changed in value. + * + * <table> + * <tr> + * <td> + * None + * </td> + * </tr> + * </table> + */ + diff --git a/lib/lufa/Demos/Host/LowLevel/AudioInputHost/Config/LUFAConfig.h b/lib/lufa/Demos/Host/LowLevel/AudioInputHost/Config/LUFAConfig.h new file mode 100644 index 0000000000..197122fce1 --- /dev/null +++ b/lib/lufa/Demos/Host/LowLevel/AudioInputHost/Config/LUFAConfig.h @@ -0,0 +1,93 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaims all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * \brief LUFA Library Configuration Header File + * + * This header file is used to configure LUFA's compile time options, + * as an alternative to the compile time constants supplied through + * a makefile. + * + * For information on what each token does, refer to the LUFA + * manual section "Summary of Compile Tokens". + */ + +#ifndef _LUFA_CONFIG_H_ +#define _LUFA_CONFIG_H_ + + #if (ARCH == ARCH_AVR8) + + /* Non-USB Related Configuration Tokens: */ +// #define DISABLE_TERMINAL_CODES + + /* USB Class Driver Related Tokens: */ +// #define HID_HOST_BOOT_PROTOCOL_ONLY +// #define HID_STATETABLE_STACK_DEPTH {Insert Value Here} +// #define HID_USAGE_STACK_DEPTH {Insert Value Here} +// #define HID_MAX_COLLECTIONS {Insert Value Here} +// #define HID_MAX_REPORTITEMS {Insert Value Here} +// #define HID_MAX_REPORT_IDS {Insert Value Here} +// #define NO_CLASS_DRIVER_AUTOFLUSH + + /* General USB Driver Related Tokens: */ +// #define ORDERED_EP_CONFIG + #define USE_STATIC_OPTIONS (USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL) +// #define USB_DEVICE_ONLY + #define USB_HOST_ONLY +// #define USB_STREAM_TIMEOUT_MS {Insert Value Here} +// #define NO_LIMITED_CONTROLLER_CONNECT +// #define NO_SOF_EVENTS + + /* USB Device Mode Driver Related Tokens: */ +// #define USE_RAM_DESCRIPTORS +// #define USE_FLASH_DESCRIPTORS +// #define USE_EEPROM_DESCRIPTORS +// #define NO_INTERNAL_SERIAL +// #define FIXED_CONTROL_ENDPOINT_SIZE {Insert Value Here} +// #define DEVICE_STATE_AS_GPIOR {Insert Value Here} +// #define FIXED_NUM_CONFIGURATIONS {Insert Value Here} +// #define CONTROL_ONLY_DEVICE +// #define INTERRUPT_CONTROL_ENDPOINT +// #define NO_DEVICE_REMOTE_WAKEUP +// #define NO_DEVICE_SELF_POWER + + /* USB Host Mode Driver Related Tokens: */ +// #define HOST_STATE_AS_GPIOR {Insert Value Here} +// #define USB_HOST_TIMEOUT_MS {Insert Value Here} +// #define HOST_DEVICE_SETTLE_DELAY_MS {Insert Value Here} +// #define NO_AUTO_VBUS_MANAGEMENT +// #define INVERTED_VBUS_ENABLE_LINE + + #else + + #error Unsupported architecture for this LUFA configuration file. + + #endif +#endif diff --git a/lib/lufa/Demos/Host/LowLevel/AudioInputHost/ConfigDescriptor.c b/lib/lufa/Demos/Host/LowLevel/AudioInputHost/ConfigDescriptor.c new file mode 100644 index 0000000000..81ebf9d85f --- /dev/null +++ b/lib/lufa/Demos/Host/LowLevel/AudioInputHost/ConfigDescriptor.c @@ -0,0 +1,220 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaims all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * + * USB Device Configuration Descriptor processing routines, to determine the correct pipe configurations + * needed to communication with an attached USB device. Descriptors are special computer-readable structures + * which the host requests upon device enumeration, to determine the device's capabilities and functions. + */ + +#include "ConfigDescriptor.h" + +/** Index of the currently used Audio Streaming Interface within the device. */ +uint8_t StreamingInterfaceIndex = 0; + +/** Alternative Setting of the currently used Audio Streaming Interface within the device. */ +uint8_t StreamingInterfaceAltSetting = 0; + +/** Address of the streaming audio endpoint currently in use within the device. */ +uint8_t StreamingEndpointAddress = 0; + + +/** Reads and processes an attached device's descriptors, to determine compatibility and pipe configurations. This + * routine will read in the entire configuration descriptor, and configure the hosts pipes to correctly communicate + * with compatible devices. + * + * This routine searches for a Streaming Audio interface descriptor containing a valid Isochronous audio endpoint. + * + * \return An error code from the \ref AudioHost_GetConfigDescriptorDataCodes_t enum. + */ +uint8_t ProcessConfigurationDescriptor(void) +{ + uint8_t ConfigDescriptorData[512]; + void* CurrConfigLocation = ConfigDescriptorData; + uint16_t CurrConfigBytesRem; + + USB_Descriptor_Interface_t* AudioControlInterface = NULL; + USB_Descriptor_Interface_t* AudioStreamingInterface = NULL; + USB_Descriptor_Endpoint_t* DataINEndpoint = NULL; + + /* Retrieve the entire configuration descriptor into the allocated buffer */ + switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData))) + { + case HOST_GETCONFIG_Successful: + break; + case HOST_GETCONFIG_InvalidData: + return InvalidConfigDataReturned; + case HOST_GETCONFIG_BuffOverflow: + return DescriptorTooLarge; + default: + return ControlError; + } + + while (!(DataINEndpoint)) + { + /* See if we've found a likely compatible interface, and if there is an endpoint within that interface */ + if (!(AudioControlInterface) || + USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation, + DComp_NextAudioInterfaceDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found) + { + /* Check if we haven't found an Audio Control interface yet, or if we have run out of related Audio Streaming interfaces */ + if (!(AudioControlInterface) || + USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation, + DComp_NextAudioStreamInterface) != DESCRIPTOR_SEARCH_COMP_Found) + { + /* Find a new Audio Control interface if the current one doesn't contain a compatible streaming interface */ + if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation, + DComp_NextAudioControlInterface) != DESCRIPTOR_SEARCH_COMP_Found) + { + /* Descriptor not found, error out */ + return NoCompatibleInterfaceFound; + } + + /* Save the interface in case we need to refer back to it later */ + AudioControlInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t); + + /* Find the next Audio Streaming interface within that Audio Control interface */ + if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation, + DComp_NextAudioStreamInterface) != DESCRIPTOR_SEARCH_COMP_Found) + { + /* Descriptor not found, error out */ + return NoCompatibleInterfaceFound; + } + } + + /* Save the interface in case we need to refer back to it later */ + AudioStreamingInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t); + + /* Skip the remainder of the loop as we have not found an endpoint yet */ + continue; + } + + /* Retrieve the endpoint address from the endpoint descriptor */ + USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t); + + /* Save the endpoint if it is an IN type endpoint */ + if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN) + DataINEndpoint = EndpointData; + } + + StreamingInterfaceIndex = AudioStreamingInterface->InterfaceNumber; + StreamingInterfaceAltSetting = AudioStreamingInterface->AlternateSetting; + StreamingEndpointAddress = DataINEndpoint->EndpointAddress; + + /* Configure the Audio data IN pipe */ + Pipe_ConfigurePipe(AUDIO_DATA_IN_PIPE, EP_TYPE_ISOCHRONOUS, DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, 2); + + /* Valid data found, return success */ + return SuccessfulConfigRead; +} + +/** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's + * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration + * descriptor processing if an incompatible descriptor configuration is found. + * + * This comparator searches for the next Interface descriptor of the correct Audio Control Class, Subclass and Protocol values. + * + * \return A value from the DSEARCH_Return_ErrorCodes_t enum + */ +uint8_t DComp_NextAudioControlInterface(void* CurrentDescriptor) +{ + USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t); + + if (Header->Type == DTYPE_Interface) + { + USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t); + + if ((Interface->Class == AUDIO_CSCP_AudioClass) && + (Interface->SubClass == AUDIO_CSCP_ControlSubclass) && + (Interface->Protocol == AUDIO_CSCP_ControlProtocol)) + { + return DESCRIPTOR_SEARCH_Found; + } + } + + return DESCRIPTOR_SEARCH_NotFound; +} + +/** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's + * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration + * descriptor processing if an incompatible descriptor configuration is found. + * + * This comparator searches for the next Interface descriptor of the correct Audio Streaming Class, Subclass and Protocol values. + * + * \return A value from the DSEARCH_Return_ErrorCodes_t enum + */ +uint8_t DComp_NextAudioStreamInterface(void* CurrentDescriptor) +{ + USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t); + + if (Header->Type == DTYPE_Interface) + { + USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t); + + if ((Interface->Class == AUDIO_CSCP_AudioClass) && + (Interface->SubClass == AUDIO_CSCP_AudioStreamingSubclass) && + (Interface->Protocol == AUDIO_CSCP_StreamingProtocol)) + { + return DESCRIPTOR_SEARCH_Found; + } + } + + return DESCRIPTOR_SEARCH_NotFound; +} + +/** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's + * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration + * descriptor processing if an incompatible descriptor configuration is found. + * + * This comparator searches for the next Isochronous Endpoint descriptor within the current interface, aborting the + * search if another interface descriptor is found before the next endpoint. + * + * \return A value from the DSEARCH_Return_ErrorCodes_t enum + */ +uint8_t DComp_NextAudioInterfaceDataEndpoint(void* CurrentDescriptor) +{ + USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t); + + if (Header->Type == DTYPE_Endpoint) + { + USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t); + + if ((Endpoint->Attributes & EP_TYPE_MASK) == EP_TYPE_ISOCHRONOUS) + return DESCRIPTOR_SEARCH_Found; + } + else if (Header->Type == DTYPE_Interface) + { + return DESCRIPTOR_SEARCH_Fail; + } + + return DESCRIPTOR_SEARCH_NotFound; +} + diff --git a/lib/lufa/Demos/Host/LowLevel/AudioInputHost/ConfigDescriptor.h b/lib/lufa/Demos/Host/LowLevel/AudioInputHost/ConfigDescriptor.h new file mode 100644 index 0000000000..036319d723 --- /dev/null +++ b/lib/lufa/Demos/Host/LowLevel/AudioInputHost/ConfigDescriptor.h @@ -0,0 +1,72 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaims all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * + * Header file for ConfigDescriptor.c. + */ + +#ifndef _CONFIGDESCRIPTOR_H_ +#define _CONFIGDESCRIPTOR_H_ + + /* Includes: */ + #include <LUFA/Drivers/USB/USB.h> + + #include "AudioInputHost.h" + + /* Macros: */ + /** Pipe address for the Audio data IN pipe. */ + #define AUDIO_DATA_IN_PIPE (PIPE_DIR_IN | 1) + + /* Enums: */ + /** Enum for the possible return codes of the \ref ProcessConfigurationDescriptor() function. */ + enum AudioHost_GetConfigDescriptorDataCodes_t + { + SuccessfulConfigRead = 0, /**< Configuration Descriptor was processed successfully */ + ControlError = 1, /**< A control request to the device failed to complete successfully */ + DescriptorTooLarge = 2, /**< The device's Configuration Descriptor is too large to process */ + InvalidConfigDataReturned = 3, /**< The device returned an invalid Configuration Descriptor */ + NoCompatibleInterfaceFound = 4, /**< A compatible interface with the required endpoints was not found */ + }; + + /* External Variables: */ + extern uint8_t StreamingInterfaceIndex; + extern uint8_t StreamingInterfaceAltSetting; + extern uint8_t StreamingEndpointAddress; + + /* Function Prototypes: */ + uint8_t ProcessConfigurationDescriptor(void); + + uint8_t DComp_NextAudioControlInterface(void* CurrentDescriptor); + uint8_t DComp_NextAudioStreamInterface(void* CurrentDescriptor); + uint8_t DComp_NextAudioInterfaceDataEndpoint(void* CurrentDescriptor); + +#endif + diff --git a/lib/lufa/Demos/Host/LowLevel/AudioInputHost/asf.xml b/lib/lufa/Demos/Host/LowLevel/AudioInputHost/asf.xml new file mode 100644 index 0000000000..f6defd91d7 --- /dev/null +++ b/lib/lufa/Demos/Host/LowLevel/AudioInputHost/asf.xml @@ -0,0 +1,50 @@ +<asf xmlversion="1.0">
+ <project caption="Audio Input Host Demo (Low Level APIs)" id="lufa.demos.host.lowlevel.audio_input.example.avr8">
+ <require idref="lufa.demos.host.lowlevel.audio_input"/>
+ <require idref="lufa.boards.dummy.avr8"/>
+ <generator value="as5_8"/>
+
+ <device-support value="at90usb1287"/>
+ <config name="lufa.drivers.board.name" value="none"/>
+
+ <build type="define" name="F_CPU" value="16000000UL"/>
+ <build type="define" name="F_USB" value="16000000UL"/>
+ </project>
+
+ <module type="application" id="lufa.demos.host.lowlevel.audio_input" caption="Audio Input Host Demo (Low Level APIs)">
+ <info type="description" value="summary">
+ Audio Input Host demo, implementing a basic USB audio sink that can output incoming audio data to a speaker. This demo uses the Low Level LUFA APIs to manually implement a USB Class for demonstration purposes without using the simpler in-built LUFA Class Driver APIs.
+ </info>
+
+ <info type="gui-flag" value="move-to-root"/>
+
+ <info type="keyword" value="Technology">
+ <keyword value="Low Level APIs"/>
+ <keyword value="USB Host"/>
+ <keyword value="Audio Class"/>
+ </info>
+
+ <device-support-alias value="lufa_avr8"/>
+ <device-support-alias value="lufa_xmega"/>
+ <device-support-alias value="lufa_uc3"/>
+
+ <build type="distribute" subtype="user-file" value="doxyfile"/>
+ <build type="distribute" subtype="user-file" value="AudioInputHost.txt"/>
+
+ <build type="c-source" value="AudioInputHost.c"/>
+ <build type="c-source" value="ConfigDescriptor.c"/>
+ <build type="header-file" value="AudioInputHost.h"/>
+ <build type="header-file" value="ConfigDescriptor.h"/>
+
+ <build type="module-config" subtype="path" value="Config"/>
+ <build type="header-file" value="Config/LUFAConfig.h"/>
+
+ <require idref="lufa.common"/>
+ <require idref="lufa.platform"/>
+ <require idref="lufa.drivers.usb"/>
+ <require idref="lufa.drivers.peripheral.usart"/>
+ <require idref="lufa.drivers.misc.ansi"/>
+ <require idref="lufa.drivers.board"/>
+ <require idref="lufa.drivers.board.leds"/>
+ </module>
+</asf>
diff --git a/lib/lufa/Demos/Host/LowLevel/AudioInputHost/doxyfile b/lib/lufa/Demos/Host/LowLevel/AudioInputHost/doxyfile new file mode 100644 index 0000000000..dda0086588 --- /dev/null +++ b/lib/lufa/Demos/Host/LowLevel/AudioInputHost/doxyfile @@ -0,0 +1,2395 @@ +# Doxyfile 1.8.9 + +# This file describes the settings to be used by the documentation system +# doxygen (www.doxygen.org) for a project. +# +# All text after a double hash (##) is considered a comment and is placed in |