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 /** \cond usb_hid_mouse
00031  * \page usb_hid_mouse USB HID Mouse Example
00032  *
00033  * \section Purpose
00034  *
00035  * The USB HID Mouse Example will help you to get familiar with the
00036  * USB Device Port(UDP) and PIO interface on SAMV7/E7 Microcontrollers. Also
00037  * it can help you to be familiar with the USB Framework that is used for
00038  * rapid development of USB-compliant class drivers such as USB Human
00039  * Interface Device class (HID).
00040  *
00041  * \section Requirements
00042  *
00043  * This package can be used with SAMV71 Xplained Ultra board or SAME70 Xplained board
00044  * that has UDP interface and have push button or joystick on it.
00045  *
00046  * \section Description
00047  *
00048  * When a board running this program connected to a host (PC for example),
00049  * with USB cable, the board appears as a HID-compliant mouse for the host.
00050  * Then you can use the joystick or buttons on the board to control the
00051  * pointer on the host. * E.g., to move it.
00052  *
00053  * \section Usage
00054  *
00055  *  -# Build the program and download it inside the board.
00056  *     Please refer to the Getting Started with SAM V71/E70 Microcontrollers.pdf
00057  * -# On the computer, open and configure a terminal application
00058  *    (e.g. HyperTerminal on Microsoft Windows) with these settings:
00059  *   - 115200 baud rate
00060  *   - 8 bits of data
00061  *   - No parity
00062  *   - 1 stop bit
00063  *   - No flow control
00064  * -# Start the application.
00065  * -# In the terminal window, the following text should appear:
00066  *     \code
00067  *     -- USB Device HID Mouse Project xxx --
00068  *     -- SAMxxxxx-xx
00069  *     -- Compiled: xxx xx xxxx xx:xx:xx --
00070  *     \endcode
00071  * -# When connecting USB cable to windows, the
00072  *    new "HID Mouse Device" appears in the
00073  *    hardware %device list.
00074  * -# Once the device is connected and configured, pressing the joystick or
00075  *    the configured board buttons move the cursor.
00076  *
00077  * \section References
00078  * - usb_hid_mouse/main.c
00079  * - pio: PIO interface driver
00080  *    - pio.h
00081  *    - pio_it.h
00082  * - usb: USB Framework, USB HID driver and UDP interface driver
00083  *    - \ref usbd_framework
00084  *       - \ref usbd_api
00085  *    - hid-mouse
00086  *       - \ref usbd_hid_mouse_drv
00087  *
00088  */
00089 
00090 /**
00091  * \file
00092  *
00093  * This file contains all the specific code for the
00094  * usb_hid_mouse
00095  *
00096  * \section Contents
00097  *
00098  * The code can be roughly broken down as follows:
00099  *    - Configuration functions
00100  *       - USB configuration
00101  *       - PIO & Timer configurations in start of main
00102  *    - Interrupt handlers
00103  *    - The main function, which implements the program behaviour
00104  */
00105 
00106 /*-----------------------------------------------------------------------------
00107  *         Headers
00108  *-----------------------------------------------------------------------------*/
00109 
00110 #include "board.h"
00111 
00112 #include "USBD.h"
00113 #include "HIDDMouseDriver.h"
00114 
00115 #include <string.h>
00116 #include <stdbool.h>
00117 #include <stdint.h>
00118 
00119 /*----------------------------------------------------------------------------
00120  *         Definitions
00121  *----------------------------------------------------------------------------*/
00122 
00123 #define NO_PUSHBUTTON
00124 
00125 /** Speed of pointer movement X */
00126 #define SPEED_X             4
00127 
00128 /** Speed of pointer movement Y */
00129 #define SPEED_Y             4
00130 
00131 /*----------------------------------------------------------------------------
00132  *         External variables
00133  *----------------------------------------------------------------------------*/
00134 
00135 extern USBDDriverDescriptors hiddMouseDriverDescriptors;
00136 
00137 /*----------------------------------------------------------------------------
00138  *         Internal variables
00139  *----------------------------------------------------------------------------*/
00140 
00141 #ifndef NO_PUSHBUTTON
00142 /** List of pinsJoystick (push button) to configure for the application. */
00143 static Pin pinsJoystick[] = {PINS_PUSHBUTTONS};
00144 #endif
00145 
00146 /*----------------------------------------------------------------------------
00147  *         Remote wake-up support (optional)
00148  *----------------------------------------------------------------------------*/
00149 
00150 
00151 /**
00152  * Monitor buttons of joystick status.
00153  * \param pBtnStatus Pointer to button status bitmap.
00154  * \param pDx        Pointer to fill x value.
00155  * \param pDy        Pointer to fill y value.
00156  */
00157 static uint8_t _ButtonsMonitor(uint8_t *pBtnStatus,
00158                                int8_t *pDx,
00159                                int8_t *pDy)
00160 {
00161     uint8_t isChanged = 0;
00162     pBtnStatus = pBtnStatus; /*dummy */
00163 #ifdef NO_PUSHBUTTON
00164 
00165     /* - Movement W S A D */
00166     if (DBG_IsRxReady()) {
00167         uint8_t key = DBG_GetChar();
00168         *pDx = 0; *pDy = 0;
00169 
00170         switch (key) {
00171         case 'w': case 'W':
00172             *pDy = -SPEED_Y; isChanged = 1;
00173             break;
00174 
00175         case 's': case 'S':
00176             *pDy = +SPEED_Y; isChanged = 1;
00177             break;
00178 
00179         case 'a': case 'A':
00180             *pDx = -SPEED_X; isChanged = 1;
00181             break;
00182 
00183         case 'd': case 'D':
00184             *pDx = +SPEED_X; isChanged = 1;
00185             break;
00186 
00187         default:
00188             break;
00189         }
00190     }
00191 
00192 #else
00193 
00194     /* - Movement buttons, Joystick or Push buttons */
00195     /* Left */
00196     if (PIO_Get(&pinsJoystick[JOYSTICK_LEFT]) == 0) {
00197         *pDx = -SPEED_X;
00198         isChanged = 1;
00199     }
00200     /* Right */
00201     else if (PIO_Get(&pinsJoystick[JOYSTICK_RIGHT]) == 0) {
00202         *pDx = SPEED_X;
00203         isChanged = 1;
00204     } else
00205         *pDx = 0;
00206 
00207 #endif
00208     return isChanged;
00209 }
00210 
00211 /*----------------------------------------------------------------------------
00212  *         Callbacks re-implementation
00213  *----------------------------------------------------------------------------*/
00214 
00215 /**
00216  *  Invoked whenever a SETUP request is received from the host. Forwards the
00217  *  request to the standard handler.
00218  */
00219 void USBDCallbacks_RequestReceived(const USBGenericRequest *request)
00220 {
00221     HIDDMouseDriver_RequestHandler(request);
00222 }
00223 
00224 /**
00225  * Invoked when the configuration of the device changes. Start reading
00226  * output reports.
00227  * \param cfgnum New configuration number.
00228  */
00229 void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum)
00230 {
00231     HIDDMouseDriver_ConfigurationChangedHandler(cfgnum);
00232 }
00233 
00234 /*----------------------------------------------------------------------------
00235  *         Internal functions
00236  *----------------------------------------------------------------------------*/
00237 /**
00238  * Configure USB settings for USB device
00239  */
00240 static void _ConfigureUotghs(void)
00241 {
00242 
00243     /* UTMI parallel mode, High/Full/Low Speed */
00244     /* UUSBCK not used in this configuration (High Speed) */
00245     PMC->PMC_SCDR = PMC_SCDR_USBCLK;
00246     /* USB clock register: USB Clock Input is UTMI PLL */
00247     PMC->PMC_USB = PMC_USB_USBS;
00248     /* Enable peripheral clock for USBHS */
00249     PMC_EnablePeripheral(ID_USBHS);
00250     USBHS->USBHS_CTRL = USBHS_CTRL_UIMOD_DEVICE;
00251     /* Enable PLL 480 MHz */
00252     PMC->CKGR_UCKR = CKGR_UCKR_UPLLEN | CKGR_UCKR_UPLLCOUNT(0xF);
00253 
00254     /* Wait that PLL is considered locked by the PMC */
00255     while (!(PMC->PMC_SR & PMC_SR_LOCKU));
00256 
00257     /* IRQ */
00258     NVIC_EnableIRQ(USBHS_IRQn);
00259 }
00260 
00261 /*----------------------------------------------------------------------------
00262  *         Exported function
00263  *----------------------------------------------------------------------------*/
00264 
00265 /**
00266  * usb_hid_mouse application entry.
00267  *
00268  * Initializes the system and then monitors buttons, sending the
00269  * corresponding character when one is pressed.
00270  */
00271 int main(void)
00272 {
00273     uint8_t bmButtons = 0;
00274     int8_t dX = 0, dY = 0;
00275     uint8_t isChanged;
00276     
00277     /* Disable watchdog */
00278     WDT_Disable(WDT);
00279 
00280     SCB_EnableICache();
00281     SCB_EnableDCache();
00282 
00283     printf("-- USB Device HID Mouse Project %s --\n\r", SOFTPACK_VERSION);
00284     printf("-- %s\n\r", BOARD_NAME);
00285     printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ ,
00286             COMPILER_NAME);
00287 
00288     /* If they are present, configure Vbus & Wake-up pins */
00289     PIO_InitializeInterrupts(0);
00290 
00291     /* Initialize all USB power (off) */
00292     _ConfigureUotghs();
00293 
00294 #ifdef NO_PUSHBUTTON
00295     printf("-- Press W S A D to move cursor\n\r");
00296 #else
00297     /* Initialize key statuses and configure push buttons */
00298     PIO_Configure(pinsJoystick, PIO_LISTSIZE(pinsJoystick));
00299 #endif
00300 
00301     /* HID driver initialization */
00302     HIDDMouseDriver_Initialize(&hiddMouseDriverDescriptors);
00303 
00304     // Start USB stack to authorize VBus monitoring
00305     USBD_Connect();
00306 
00307     /* Infinite loop */
00308     while (1) {
00309         if (USBD_GetState() < USBD_STATE_CONFIGURED)
00310             continue;
00311 
00312         isChanged = _ButtonsMonitor(&bmButtons, &dX, &dY);
00313 
00314         if (isChanged) {
00315             uint8_t status;
00316 
00317             do {
00318                 status = HIDDMouseDriver_ChangePoints(bmButtons,
00319                                                       dX, dY);
00320             } while (status != USBD_STATUS_SUCCESS);
00321         }
00322     }
00323 }
00324 /** \endcond */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines