SAMV71 Xplained Ultra Software Package 1.5

main.c

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------- */
00002 /*                  Atmel Microcontroller Software Support                      */
00003 /*                       SAM Software Package License                           */
00004 /* ---------------------------------------------------------------------------- */
00005 /* Copyright (c) 2015, Atmel Corporation                                        */
00006 /*                                                                              */
00007 /* All rights reserved.                                                         */
00008 /*                                                                              */
00009 /* Redistribution and use in source and binary forms, with or without           */
00010 /* modification, are permitted provided that the following condition is met:    */
00011 /*                                                                              */
00012 /* - Redistributions of source code must retain the above copyright notice,     */
00013 /* this list of conditions and the disclaimer below.                            */
00014 /*                                                                              */
00015 /* Atmel's name may not be used to endorse or promote products derived from     */
00016 /* this software without specific prior written permission.                     */
00017 /*                                                                              */
00018 /* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR   */
00019 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
00020 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE   */
00021 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,      */
00022 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
00023 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  */
00024 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
00025 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         */
00026 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
00027 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           */
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 SAMV71 Xplained Ultra board or SAME70 Xplained board
00041  *  that have UDP 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 board.
00054  *     Please refer to the Getting Started with SAM V71/E70 Microcontrollers.pdf
00055  *  -# On the computer, open and configure a terminal application
00056  *     (e.g. HyperTerminal on Microsoft Windows) with these settings:
00057  *    - 115200 baud rate
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 
00222     /* Wait that PLL is considered locked by the PMC */
00223     while (!(PMC->PMC_SR & PMC_SR_LOCKU));
00224 
00225     /* IRQ */
00226     NVIC_EnableIRQ(USBHS_IRQn);
00227 }
00228 
00229 /*----------------------------------------------------------------------------
00230  *         Global functions
00231  *----------------------------------------------------------------------------*/
00232 
00233 /**
00234  *  Initializes the system, connects the USB and waits indefinitely.
00235  *
00236  * \callgraph
00237  */
00238 int main(void)
00239 {
00240     /* Disable watchdog */
00241     WDT_Disable(WDT);
00242 
00243     SCB_EnableICache();
00244     SCB_EnableDCache();
00245 
00246     /* Output example information */
00247     printf("-- USB Device Core Project %s --\n\r", SOFTPACK_VERSION);
00248     printf("-- %s\n\r", BOARD_NAME);
00249     printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ ,
00250             COMPILER_NAME);
00251 
00252     /* If they are present, configure Vbus & Wake-up pins */
00253     PIO_InitializeInterrupts(0);
00254 
00255     /* connect if needed */
00256     /* Interrupt priority */
00257     NVIC_SetPriority(USBHS_IRQn, 2);
00258     /* Initialize USB clocks */
00259     _ConfigureUotghs();
00260 
00261     /* USB initialization, Disable Pull-up */
00262     TRACE_INFO("USB initialization\n\r");
00263     USBDDriver_Initialize(&usbdDriver, &usbdDriverDescriptors, 0);
00264     USBD_Init();
00265 
00266     /* Wait about 10ms so that host detach the device to re-enumerate
00267        Device connection */
00268     TRACE_INFO("Connecting device\n\r");
00269 
00270     // Start USB stack to authorize VBus monitoring
00271     USBD_Connect();
00272 
00273     while (USBD_GetState() < USBD_STATE_CONFIGURED);
00274 
00275     // Infinite loop
00276     while (1);
00277 }
00278 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines