diff options
author | Dean Camera <dean@fourwalledcubicle.com> | 2013-01-07 16:27:33 +0000 |
---|---|---|
committer | Dean Camera <dean@fourwalledcubicle.com> | 2013-01-07 16:27:33 +0000 |
commit | 48e87c8e8f4395a107bd63c3be8c57a636c25cf1 (patch) | |
tree | 8a89db5e9f5776515306efc2add59b44f20bd151 /LUFA/CodeTemplates | |
parent | 30545fdec79763ff3671958d4ba0da76fd16f60d (diff) | |
download | lufa-48e87c8e8f4395a107bd63c3be8c57a636c25cf1.tar.gz lufa-48e87c8e8f4395a107bd63c3be8c57a636c25cf1.tar.bz2 lufa-48e87c8e8f4395a107bd63c3be8c57a636c25cf1.zip |
Add device and host mode template projects.
Diffstat (limited to 'LUFA/CodeTemplates')
-rw-r--r-- | LUFA/CodeTemplates/DeviceTemplate/Descriptors.c | 180 | ||||
-rw-r--r-- | LUFA/CodeTemplates/DeviceTemplate/Descriptors.h | 59 | ||||
-rw-r--r-- | LUFA/CodeTemplates/DeviceTemplate/DeviceApplication.c | 91 | ||||
-rw-r--r-- | LUFA/CodeTemplates/DeviceTemplate/DeviceApplication.h | 52 | ||||
-rw-r--r-- | LUFA/CodeTemplates/DeviceTemplate/asf.xml | 34 | ||||
-rw-r--r-- | LUFA/CodeTemplates/HostTemplate/HostApplication.c | 133 | ||||
-rw-r--r-- | LUFA/CodeTemplates/HostTemplate/HostApplication.h | 56 | ||||
-rw-r--r-- | LUFA/CodeTemplates/HostTemplate/asf.xml | 32 |
8 files changed, 637 insertions, 0 deletions
diff --git a/LUFA/CodeTemplates/DeviceTemplate/Descriptors.c b/LUFA/CodeTemplates/DeviceTemplate/Descriptors.c new file mode 100644 index 000000000..a3aacdee5 --- /dev/null +++ b/LUFA/CodeTemplates/DeviceTemplate/Descriptors.c @@ -0,0 +1,180 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2013. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2013 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 Descriptors, for library use when in USB device mode. Descriptors are special + * computer-readable structures which the host requests upon device enumeration, to determine + * the device's capabilities and functions. + */ + +#include "Descriptors.h" + +/** Device descriptor structure. This descriptor describes the overall device + * characteristics, including the supported USB version, control endpoint size + * and the number of device configurations. The descriptor is read out by the + * USB host when the enumeration process begins. + */ +const USB_Descriptor_Device_t PROGMEM DeviceDescriptor = +{ + .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device}, + + .USBSpecification = VERSION_BCD(02.00), + .Class = USB_CSCP_NoDeviceClass, + .SubClass = USB_CSCP_NoDeviceSubclass, + .Protocol = USB_CSCP_NoDeviceProtocol, + + .Endpoint0Size = 64, + + .VendorID = 0x0000, + .ProductID = 0x0000, + .ReleaseNumber = VERSION_BCD(00.02), + + .ManufacturerStrIndex = 0x01, + .ProductStrIndex = 0x02, + .SerialNumStrIndex = NO_DESCRIPTOR, + + .NumberOfConfigurations = 1 +}; + +/** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage + * of the device in one of its supported configurations, including information about any device interfaces + * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting + * a configuration so that the host may correctly communicate with the USB device. + */ +const USB_Descriptor_Configuration_t ConfigurationDescriptor = +{ + .Config = + { + .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration}, + + .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t), + .TotalInterfaces = 0, + + .ConfigurationNumber = 1, + .ConfigurationStrIndex = NO_DESCRIPTOR, + + .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED), + + .MaxPowerConsumption = USB_CONFIG_POWER_MA(100) + }, +}; + +/** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests + * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate + * via the language ID table available at USB.org what languages the device supports for its string descriptors. + */ +const USB_Descriptor_String_t LanguageString = +{ + .Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String}, + + .UnicodeString = {LANGUAGE_ID_ENG} +}; + +/** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable + * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device + * Descriptor. + */ +const USB_Descriptor_String_t ManufacturerString = +{ + .Header = {.Size = USB_STRING_LEN(14), .Type = DTYPE_String}, + + .UnicodeString = L"Your Name Here" +}; + +/** Product descriptor string. This is a Unicode string containing the product's details in human readable form, + * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device + * Descriptor. + */ +const USB_Descriptor_String_t ProductString = +{ + .Header = {.Size = USB_STRING_LEN(15), .Type = DTYPE_String}, + + .UnicodeString = L"LUFA USB Device" +}; + +/** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors" + * documentation) by the application code so that the address and size of a requested descriptor can be given + * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function + * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the + * USB host. + */ +uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, + const uint8_t wIndex, + const void** const DescriptorAddress + #if defined(HAS_MULTIPLE_DESCRIPTOR_ADDRESS_SPACES) + , uint8_t* const DescriptorMemorySpace + #endif + ) +{ + const uint8_t DescriptorType = (wValue >> 8); + const uint8_t DescriptorNumber = (wValue & 0xFF); + + const void* Address = NULL; + uint16_t Size = NO_DESCRIPTOR; + + switch (DescriptorType) + { + case DTYPE_Device: + Address = &DeviceDescriptor; + Size = sizeof(USB_Descriptor_Device_t); + break; + case DTYPE_Configuration: + Address = &ConfigurationDescriptor; + Size = sizeof(USB_Descriptor_Configuration_t); + break; + case DTYPE_String: + switch (DescriptorNumber) + { + case 0x00: + Address = &LanguageString; + Size = pgm_read_byte(&LanguageString.Header.Size); + break; + case 0x01: + Address = &ManufacturerString; + Size = pgm_read_byte(&ManufacturerString.Header.Size); + break; + case 0x02: + Address = &ProductString; + Size = pgm_read_byte(&ProductString.Header.Size); + break; + } + + break; + } + + #if defined(HAS_MULTIPLE_DESCRIPTOR_ADDRESS_SPACES) + *DescriptorMemorySpace = MEMSPACE_RAM; + #endif + + *DescriptorAddress = Address; + return Size; +} + diff --git a/LUFA/CodeTemplates/DeviceTemplate/Descriptors.h b/LUFA/CodeTemplates/DeviceTemplate/Descriptors.h new file mode 100644 index 000000000..c1cc3d4bb --- /dev/null +++ b/LUFA/CodeTemplates/DeviceTemplate/Descriptors.h @@ -0,0 +1,59 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2013. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2013 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 Descriptors.c. + */ + +#ifndef _DESCRIPTORS_H_ +#define _DESCRIPTORS_H_ + + /* Includes: */ + #include <LUFA/Drivers/USB/USB.h> + + /* Macros: */ + #if (defined(ARCH_HAS_MULTI_ADDRESS_SPACE) && \ + !(defined(USE_FLASH_DESCRIPTORS) || defined(USE_EEPROM_DESCRIPTORS) || defined(USE_RAM_DESCRIPTORS))) + #define HAS_MULTIPLE_DESCRIPTOR_ADDRESS_SPACES + #endif + + /* Type Defines: */ + /** Type define for the device configuration descriptor structure. This must be defined in the + * application code, as the configuration descriptor contains several sub-descriptors which + * vary between devices, and which describe the device's usage to the host. + */ + typedef struct + { + USB_Descriptor_Configuration_Header_t Config; + } USB_Descriptor_Configuration_t; + +#endif + diff --git a/LUFA/CodeTemplates/DeviceTemplate/DeviceApplication.c b/LUFA/CodeTemplates/DeviceTemplate/DeviceApplication.c new file mode 100644 index 000000000..893c733a6 --- /dev/null +++ b/LUFA/CodeTemplates/DeviceTemplate/DeviceApplication.c @@ -0,0 +1,91 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2013. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2013 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 USB device application. This file contains the + * main tasks of the application and is responsible for the initial + * application hardware configuration. + */ + +#include "DeviceApplication.h" + +/** Main program entry point. This routine contains the overall program flow, including initial + * setup of all components and the main program loop. + */ +int main(void) +{ + SetupHardware(); + + GlobalInterruptEnable(); + + for (;;) + { + USB_USBTask(); + } +} + +/** Configures the board hardware and chip peripherals for the demo's functionality. */ +void SetupHardware(void) +{ + /* Disable watchdog if enabled by bootloader/fuses */ + MCUSR &= ~(1 << WDRF); + wdt_disable(); + + /* Disable clock division */ + clock_prescale_set(clock_div_1); + + /* Hardware Initialization */ + USB_Init(USB_MODE_Device, USB_DEVICE_OPT_FULLSPEED | USB_OPT_AUTO_PLL); +} + +/** Event handler for the library USB Connection event. */ +void EVENT_USB_Device_Connect(void) +{ + +} + +/** Event handler for the library USB Disconnection event. */ +void EVENT_USB_Device_Disconnect(void) +{ + +} + +/** Event handler for the library USB Configuration Changed event. */ +void EVENT_USB_Device_ConfigurationChanged(void) +{ + +} + +/** Event handler for the library USB Control Request reception event. */ +void EVENT_USB_Device_ControlRequest(void) +{ + +} diff --git a/LUFA/CodeTemplates/DeviceTemplate/DeviceApplication.h b/LUFA/CodeTemplates/DeviceTemplate/DeviceApplication.h new file mode 100644 index 000000000..9dac556a1 --- /dev/null +++ b/LUFA/CodeTemplates/DeviceTemplate/DeviceApplication.h @@ -0,0 +1,52 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2013. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2013 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 DeviceApplication.c. + */ + +#ifndef _USB_DEVICE_APPLICATION_H_ +#define _USB_DEVICE_APPLICATION_ + + /* Includes: */ + #include <avr/io.h> + #include <avr/wdt.h> + #include <avr/power.h> + + #include <LUFA/Drivers/USB/USB.h> + + #include "Descriptors.h" + + /* Function Prototypes: */ + void SetupHardware(void); + +#endif + diff --git a/LUFA/CodeTemplates/DeviceTemplate/asf.xml b/LUFA/CodeTemplates/DeviceTemplate/asf.xml new file mode 100644 index 000000000..3c7dd5814 --- /dev/null +++ b/LUFA/CodeTemplates/DeviceTemplate/asf.xml @@ -0,0 +1,34 @@ +<asf xmlversion="1.0">
+ <project caption="USB Device Template" id="lufa.templates.device.project">
+ <require idref="lufa.templates.device"/>
+ <generator value="as5_8_template"/>
+
+ <device-support value="at90usb1287"/>
+ <config name="lufa.drivers.board.name" value="usbkey"/>
+
+ <build type="define" name="F_CPU" value="8000000UL"/>
+ <build type="define" name="F_USB" value="8000000UL"/>
+ </project>
+
+ <module type="application" id="lufa.templates.device" caption="USB Device Template">
+ <info type="description" value="summary">
+ Template for a LUFA USB device mode application.
+ </info>
+
+ <device-support-alias value="lufa_avr8"/>
+ <device-support-alias value="lufa_xmega"/>
+ <device-support-alias value="lufa_uc3"/>
+
+ <build type="c-source" value="DeviceApplication.c"/>
+ <build type="c-source" value="Descriptors.c"/>
+ <build type="header-file" value="DeviceApplication.h"/>
+ <build type="header-file" value="Descriptors.h"/>
+
+ <build type="module-config" subtype="path" value=".."/>
+ <build type="header-file" value="../LUFAConfig.h"/>
+
+ <require idref="lufa.common"/>
+ <require idref="lufa.platform"/>
+ <require idref="lufa.drivers.usb"/>
+ </module>
+</asf>
diff --git a/LUFA/CodeTemplates/HostTemplate/HostApplication.c b/LUFA/CodeTemplates/HostTemplate/HostApplication.c new file mode 100644 index 000000000..6f9e7af36 --- /dev/null +++ b/LUFA/CodeTemplates/HostTemplate/HostApplication.c @@ -0,0 +1,133 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2013. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2013 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 USB host application. This file contains the + * main tasks of the application and is responsible for the initial + * application hardware configuration. + */ + +#include "HostApplication.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(); + + GlobalInterruptEnable(); + + for (;;) + { + USB_USBTask(); + } +} + +/** Configures the board hardware and chip peripherals for the demo's functionality. */ +void SetupHardware(void) +{ + /* Disable watchdog if enabled by bootloader/fuses */ + MCUSR &= ~(1 << WDRF); + wdt_disable(); + + /* Disable clock division */ + clock_prescale_set(clock_div_1); + + /* Hardware Initialization */ + USB_Init(USB_MODE_Host, USB_DEVICE_OPT_FULLSPEED | USB_OPT_AUTO_PLL); +} + +/** 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) +{ + +} + +/** 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) +{ + +} + +/** 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) +{ + uint16_t ConfigDescriptorSize; + uint8_t ConfigDescriptorData[512]; + + if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData, + sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful) + { + return; + } + + if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful) + { + return; + } +} + +/** 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(); + 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) +{ + +} + +/* Required callback for retrieving descriptors from a LUFA device - unless the USB_HOST_ONLY configuration + * option is set, this is still required even in an application that uses host mode only. + */ +uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, + const uint8_t wIndex, + const void** const DescriptorAddress +#if defined(HAS_MULTIPLE_DESCRIPTOR_ADDRESS_SPACES) + , uint8_t* const DescriptorMemorySpace +#endif +) +{ + return 0; +} diff --git a/LUFA/CodeTemplates/HostTemplate/HostApplication.h b/LUFA/CodeTemplates/HostTemplate/HostApplication.h new file mode 100644 index 000000000..9aba9338f --- /dev/null +++ b/LUFA/CodeTemplates/HostTemplate/HostApplication.h @@ -0,0 +1,56 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2013. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2013 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 HostApplication.c. + */ + +#ifndef _USB_HOST_APPLICATION_H_ +#define _USB_HOST_APPLICATION_H_ + + /* Includes: */ + #include <avr/io.h> + #include <avr/wdt.h> + #include <avr/power.h> + + #include <LUFA/Drivers/USB/USB.h> + + /* Macros: */ + #if (defined(ARCH_HAS_MULTI_ADDRESS_SPACE) && \ + !(defined(USE_FLASH_DESCRIPTORS) || defined(USE_EEPROM_DESCRIPTORS) || defined(USE_RAM_DESCRIPTORS))) + #define HAS_MULTIPLE_DESCRIPTOR_ADDRESS_SPACES + #endif + + /* Function Prototypes: */ + void SetupHardware(void); + +#endif + diff --git a/LUFA/CodeTemplates/HostTemplate/asf.xml b/LUFA/CodeTemplates/HostTemplate/asf.xml new file mode 100644 index 000000000..867160a48 --- /dev/null +++ b/LUFA/CodeTemplates/HostTemplate/asf.xml @@ -0,0 +1,32 @@ +<asf xmlversion="1.0">
+ <project caption="USB Host Template" id="lufa.templates.host.project">
+ <require idref="lufa.templates.host"/>
+ <generator value="as5_8_template"/>
+
+ <device-support value="at90usb1287"/>
+ <config name="lufa.drivers.board.name" value="usbkey"/>
+
+ <build type="define" name="F_CPU" value="8000000UL"/>
+ <build type="define" name="F_USB" value="8000000UL"/>
+ </project>
+
+ <module type="application" id="lufa.templates.host" caption="USB Host Template">
+ <info type="description" value="summary">
+ Template for a LUFA USB host mode application.
+ </info>
+
+ <device-support-alias value="lufa_avr8"/>
+ <device-support-alias value="lufa_xmega"/>
+ <device-support-alias value="lufa_uc3"/>
+
+ <build type="c-source" value="HostApplication.c"/>
+ <build type="header-file" value="HostApplication.h"/>
+
+ <build type="module-config" subtype="path" value=".."/>
+ <build type="header-file" value="../LUFAConfig.h"/>
+
+ <require idref="lufa.common"/>
+ <require idref="lufa.platform"/>
+ <require idref="lufa.drivers.usb"/>
+ </module>
+</asf>
|