00001 /* ---------------------------------------------------------------------------- 00002 * SAM Software Package License 00003 * ---------------------------------------------------------------------------- 00004 * Copyright (c) 2014, Atmel Corporation 00005 * 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions are met: 00010 * 00011 * - Redistributions of source code must retain the above copyright notice, 00012 * this list of conditions and the disclaimer below. 00013 * 00014 * Atmel's name may not be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR 00018 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00019 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 00020 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00022 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 00023 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00024 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00025 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00026 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 * ---------------------------------------------------------------------------- 00028 */ 00029 00030 /** 00031 * \page usb_core USB Device Enumeration Example 00032 * 00033 * \section Purpose 00034 * 00035 * The USB Device Enumeration Example will help you to get familiar with the 00036 * USB Device Port(UDP)interface on SAMv7 Microcontrollers. 00037 * 00038 * \section Requirements 00039 * 00040 * This package can be used with all Atmel Xplained board that have UDP 00041 * interface. 00042 * 00043 * \section Description 00044 * 00045 * The demo works as a USB device that can be recognized by host. 00046 * 00047 * When an Xplained board running this program connected to a host (PC for 00048 * example), with USB cable, host will notice the attachment of a USB device. 00049 * No device driver offered for the device now. 00050 * 00051 * \section Usage 00052 * 00053 * -# Build the program and download it inside the SAM V71 Xplained Ultra board. 00054 * Please refer to the Getting Started with SAM V71 Microcontrollers.pdf 00055 * -# On the computer, open and configure a terminal application 00056 * (e.g. HyperTerminal on Microsoft Windows) with these settings: 00057 * - 115200 bauds 00058 * - 8 bits of data 00059 * - No parity 00060 * - 1 stop bit 00061 * - No flow control 00062 * -# Start the application. 00063 * -# In the terminal window, the following text should appear: 00064 * \code 00065 * -- USB Device Core Project xxx -- 00066 * -- SAMxxxxx-xx 00067 * -- Compiled: xxx xx xxxx xx:xx:xx -- 00068 * \endcode 00069 * -# When connecting USB cable to windows, the LED blinks, and the host 00070 * reports a new USB %device attachment. 00071 * 00072 * \section References 00073 * - usb_core/main.c 00074 * - pio.h 00075 * - pio_it.h 00076 * - usb: USB Device Framework and UDP interface driver 00077 * - \ref usbd_framework 00078 * - \ref usbd_api 00079 * - \ref usbd_enum 00080 */ 00081 00082 /** 00083 * \file 00084 * 00085 * This file contains all the specific code for the 00086 * usb_core example. 00087 * 00088 * \section Contents 00089 * 00090 * The code can be roughly broken down as follows: 00091 * - Configuration functions 00092 * - VBus_Configure 00093 * - PIO configurations in start of main 00094 * - Interrupt handlers 00095 * - ISR_Vbus 00096 * - Callback functions 00097 * - USBDCallbacks_RequestReceived 00098 * - The main function, which implements the program behaviour 00099 * 00100 */ 00101 00102 /*--------------------------------------------------------------------------- 00103 * Headers 00104 *---------------------------------------------------------------------------*/ 00105 00106 #include "board.h" 00107 00108 #include <USBDescriptors.h> 00109 #include <USBRequests.h> 00110 #include "USBD.h" 00111 #include <USBDDriver.h> 00112 00113 #include <stdbool.h> 00114 #include <stdint.h> 00115 00116 /*---------------------------------------------------------------------------- 00117 * Local types 00118 *----------------------------------------------------------------------------*/ 00119 00120 /** Configuration descriptors with one interface. */ 00121 struct SimpleConfigurationDescriptors { 00122 00123 USBConfigurationDescriptor configuration; 00124 USBInterfaceDescriptor interface; 00125 }; 00126 00127 /*---------------------------------------------------------------------------- 00128 * Local variables 00129 *----------------------------------------------------------------------------*/ 00130 00131 /** Device descriptor. */ 00132 const USBDeviceDescriptor usbDeviceDescriptor = { 00133 00134 sizeof(USBDeviceDescriptor), 00135 USBGenericDescriptor_DEVICE, 00136 USBDeviceDescriptor_USB2_00, 00137 0, // No device class code 00138 0, // No device subclass code 00139 0, // No device protocol code 00140 CHIP_USB_ENDPOINTS_MAXPACKETSIZE(0), 00141 0x03EB, // Atmel vendor ID 00142 0x0001, // Product ID 00143 0x0001, // Product release 0.01 00144 0, // No manufacturer string descriptor 00145 0, // No product string descriptor 00146 0, // No serial number string descriptor 00147 1 // One possible configuration 00148 }; 00149 00150 /** Configuration descriptors. */ 00151 const struct SimpleConfigurationDescriptors configurationDescriptors = { 00152 00153 // Configuration descriptor 00154 { 00155 sizeof(USBConfigurationDescriptor), 00156 USBGenericDescriptor_CONFIGURATION, 00157 sizeof(struct SimpleConfigurationDescriptors), 00158 0, // No interface in this configuration 00159 1, // This is configuration #1 00160 0, // No string descriptor for this configuration 00161 BOARD_USB_BMATTRIBUTES, 00162 USBConfigurationDescriptor_POWER(100) 00163 }, 00164 // Interface descriptor 00165 { 00166 sizeof(USBInterfaceDescriptor), 00167 USBGenericDescriptor_INTERFACE, 00168 0, // This is interface #0 00169 0, // This is setting #0 for interface 00170 0, // Interface has no endpoint 00171 0, // No interface class code 00172 0, // No interface subclass code 00173 0, // No interface protocol code 00174 0, // No string descriptor 00175 } 00176 }; 00177 00178 /** List of descriptors used by the device. */ 00179 const USBDDriverDescriptors usbdDriverDescriptors = { 00180 00181 &usbDeviceDescriptor, 00182 (const USBConfigurationDescriptor *) &configurationDescriptors, 00183 0, // No full-speed device qualifier descriptor 00184 0, // No full-speed other speed configuration descriptor 00185 0, // No high-speed device descriptor (uses FS one) 00186 0, // No high-speed configuration descriptor (uses FS one) 00187 0, // No high-speed device qualifier descriptor 00188 0, // No high-speed other speed configuration descriptor 00189 0, // No string descriptor 00190 0 // No string descriptor 00191 }; 00192 00193 /** USB standard device driver. */ 00194 USBDDriver usbdDriver ; 00195 00196 00197 /** 00198 * Invoked whenever a SETUP request is received from the host. Forwards the 00199 * request to the standard handler. 00200 */ 00201 void USBDCallbacks_RequestReceived(const USBGenericRequest *request) 00202 { 00203 USBDDriver_RequestHandler(&usbdDriver, request); 00204 } 00205 00206 /** 00207 * Configure USBHS settings for USB device 00208 */ 00209 static void _ConfigureUotghs(void) 00210 { 00211 /* UTMI parallel mode, High/Full/Low Speed */ 00212 /* UUSBCK not used in this configuration (High Speed) */ 00213 PMC->PMC_SCDR = PMC_SCDR_USBCLK; 00214 /* USB clock register: USB Clock Input is UTMI PLL */ 00215 PMC->PMC_USB = PMC_USB_USBS; 00216 /* Enable peripheral clock for USBHS */ 00217 PMC_EnablePeripheral(ID_USBHS); 00218 USBHS->USBHS_CTRL = USBHS_CTRL_UIMOD_DEVICE; 00219 /* Enable PLL 480 MHz */ 00220 PMC->CKGR_UCKR = CKGR_UCKR_UPLLEN | CKGR_UCKR_UPLLCOUNT(0xF); 00221 /* Wait that PLL is considered locked by the PMC */ 00222 while( !(PMC->PMC_SR & PMC_SR_LOCKU) ); 00223 00224 /* IRQ */ 00225 NVIC_EnableIRQ(USBHS_IRQn) ; 00226 } 00227 00228 /*---------------------------------------------------------------------------- 00229 * Global functions 00230 *----------------------------------------------------------------------------*/ 00231 00232 /** 00233 * Initializes the system, connects the USB and waits indefinitely. 00234 * 00235 * \callgraph 00236 */ 00237 int main(void) 00238 { 00239 /* Disable watchdog */ 00240 WDT_Disable( WDT ); 00241 00242 SCB_EnableICache(); 00243 SCB_EnableDCache(); 00244 00245 /* Output example information */ 00246 printf( "-- USB Device Core Project %s --\n\r", SOFTPACK_VERSION ) ; 00247 printf( "-- %s\n\r", BOARD_NAME ) ; 00248 printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME) ; 00249 00250 /* If they are present, configure Vbus & Wake-up pins */ 00251 PIO_InitializeInterrupts(0) ; 00252 00253 /* connect if needed */ 00254 /* Interrupt priority */ 00255 NVIC_SetPriority( USBHS_IRQn, 2 ); 00256 /* Initialize USB clocks */ 00257 _ConfigureUotghs(); 00258 00259 /* USB initialization, Disable Pull-up */ 00260 TRACE_INFO("USB initialization\n\r"); 00261 USBDDriver_Initialize(&usbdDriver, &usbdDriverDescriptors, 0); 00262 USBD_Init(); 00263 00264 /* Wait about 10ms so that host detach the device to re-enumerate 00265 Device connection */ 00266 TRACE_INFO("Connecting device\n\r"); 00267 00268 // Start USB stack to authorize VBus monitoring 00269 USBD_Connect(); 00270 00271 while (USBD_GetState() < USBD_STATE_CONFIGURED); 00272 00273 // Infinite loop 00274 while (1) ; 00275 } 00276