SAM3S8/SD8 Software Package: examples_usb/usb_hid_mouse/main.c Source File

SAM3S8/SD8 Software Package 1.2

# main.c

Go to the documentation of this file.

    00001 /* ----------------------------------------------------------------------------
    00002  *         ATMEL Microcontroller Software Support
    00003  * ----------------------------------------------------------------------------
    00004  * Copyright (c) 2008, 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_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 SAM 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 Humen
    00039  * Interface Device class (HID).
    00040  *
    00041  * \section See
    00042  * - pio: PIO interface driver
    00043  * - usb: USB Framework, USB HID driver and UDP interface driver
    00044  *    - \ref usbd_framework
    00045  *       - \ref usbd_api
    00046  *    - hid-mouse
    00047  *       - \ref usbd_hid_mouse_drv
    00048  *
    00049  * You can find following information depends on your needs:
    00050  * - Sample usage of USB HID driver and PIO driver.
    00051  * - USB HID driver development based on the USB Framework.
    00052  * - USB enumerate sequence, the standard and class-specific descriptors and
    00053  *   requests handling.
    00054  * - The initialize sequence and usage of UDP interface.
    00055  *
    00056  * \section Requirements
    00057  *
    00058  * This package can be used with all Atmel evaluation kits that has UDP
    00059  * interface and have push button or joystick on it.
    00060  *
    00061  * \section Description
    00062  *
    00063  * When an EK running this program connected to a host (PC for example), with
    00064  * USB cable, the EK appears as a HID-compliant mouse for the host. Then you
    00065  * can use the joystick or buttons on the EK to control the pointer on the host.
    00066  * E.g., to move it.
    00067  *
    00068  * \section Usage
    00069  *
    00070  * -# Build the program and download it inside the evaluation board. Please
    00071  *    refer to the
    00072  *    <a href="http://www.atmel.com/dyn/resources/prod_documents/6421B.pdf">
    00073  *    SAM-BA User Guide</a>, the
    00074  *    <a href="http://www.atmel.com/dyn/resources/prod_documents/doc6310.pdf">
    00075  *    GNU-Based Software Development</a> application note or to the
    00076  *    <a href="http://www.iar.com/website1/1.0.1.0/78/1/">
    00077  *    IAR EWARM User and reference guides</a>, depending on your chosen solution.
    00078  * -# On the computer, open and configure a terminal application
    00079  *    (e.g. HyperTerminal on Microsoft Windows) with these settings:
    00080  *   - 115200 bauds
    00081  *   - 8 bits of data
    00082  *   - No parity
    00083  *   - 1 stop bit
    00084  *   - No flow control
    00085  * -# Start the application.
    00086  * -# In the terminal window, the following text should appear:
    00087  *     \code
    00088  *     -- USB Device HID Mouse Example xxx --
    00089  *     -- xxxxxx-xx
    00090  *     -- Compiled: xxx xx xxxx xx:xx:xx --
    00091  *     \endcode
    00092  * -# When connecting USB cable to windows, the LED blinks.
    00093  *    Then new "HID Mouse Device" appears in the
    00094  *    hardware %device list.
    00095  * -# Once the device is connected and configured, pressing the joystick or
    00096  *    the configurated board buttons move the cursor.
    00097  */
    00098
    00099 /**
    00100  * \file
    00101  *
    00102  * This file contains all the specific code for the
    00103  * usb_hid_mouse
    00104  *
    00105  * \section Contents
    00106  *
    00107  * The code can be roughly broken down as follows:
    00108  *    - Configuration functions
    00109  *       - VBus_Configure
    00110  *       - PIO & Timer configurations in start of main
    00111  *    - Interrupt handlers
    00112  *       - ISR_Vbus
    00113  *    - The main function, which implements the program behavior
    00114  */
    00115
    00116 /*-----------------------------------------------------------------------------
    00117  *         Headers
    00118  *-----------------------------------------------------------------------------*/
    00119
    00120 #include "board.h"
    00121
    00122 #include "USBD.h"
    00123 #include "HIDDMouseDriver.h"
    00124
    00125 #include <string.h>
    00126 #include <stdbool.h>
    00127 #include <stdint.h>
    00128
    00129 /*----------------------------------------------------------------------------
    00130  *         Definitions
    00131  *----------------------------------------------------------------------------*/
    00132
    00133 /** Speed of pointer movement X */
    00134 #define SPEED_X             4
    00135
    00136 /** Speed of pointer movement Y */
    00137 #define SPEED_Y             4
    00138
    00139
    00140 #define JOYSTICK_LEFT  0
    00141 #define JOYSTICK_RIGHT 1
    00142 /*----------------------------------------------------------------------------
    00143  *         External variables
    00144  *----------------------------------------------------------------------------*/
    00145
    00146 extern USBDDriverDescriptors hiddMouseDriverDescriptors;
    00147
    00148 /*----------------------------------------------------------------------------
    00149  *         Internal variables
    00150  *----------------------------------------------------------------------------*/
    00151
    00152 /** List of pinsJoystick (push button) to configure for the application. */
    00153 static Pin pinsJoystick[] = {PINS_PUSHBUTTONS};
    00154
    00155 /*----------------------------------------------------------------------------
    00156  *         Remote wake-up support (optional)
    00157  *----------------------------------------------------------------------------*/
    00158
    00159 /*----------------------------------------------------------------------------
    00160  *         VBus monitoring (optional)
    00161  *----------------------------------------------------------------------------*/
    00162
    00163 /** VBus pin instance. */
    00164 static const Pin pinVbus = PIN_USB_VBUS;
    00165
    00166 /**
    00167  * Handles interrupts coming from PIO controllers.
    00168  */
    00169 static void ISR_Vbus(const Pin *pPin)
    00170 {
    00171     /* Stop warning */
    00172     pPin = pPin;
    00173
    00174     /* Check current level on VBus */
    00175     if (PIO_Get(&pinVbus)) {
    00176
    00177         TRACE_INFO("VBUS conn\n\r");
    00178         USBD_Connect();
    00179     }
    00180     else {
    00181
    00182         TRACE_INFO("VBUS discon\n\r");
    00183         USBD_Disconnect();
    00184     }
    00185 }
    00186
    00187 /**
    00188  * Configures the VBus pin to trigger an interrupt when the level on that pin
    00189  * changes.
    00190  */
    00191 static void VBus_Configure( void )
    00192 {
    00193     TRACE_INFO("VBus configuration\n\r");
    00194
    00195     /* Configure PIO */
    00196     PIO_Configure(&pinVbus, 1);
    00197     PIO_ConfigureIt(&pinVbus, ISR_Vbus);
    00198     PIO_EnableIt(&pinVbus);
    00199
    00200     /* Check current level on VBus */
    00201     if (PIO_Get(&pinVbus)) {
    00202
    00203         /* if VBUS present, force the connect */
    00204         TRACE_INFO("conn\n\r");
    00205         USBD_Connect();
    00206     }
    00207     else {
    00208         USBD_Disconnect();
    00209     }
    00210 }
    00211
    00212 /**
    00213  * Monitor buttons of joystick status.
    00214  * \param pBtnStatus Pointer to button status bitmap.
    00215  * \param pDx        Pointer to fill x value.
    00216  * \param pDy        Pointer to fill y value.
    00217  */
    00218 static uint8_t _ButtonsMonitor(uint8_t *pBtnStatus,
    00219                                int8_t *pDx,
    00220                                int8_t *pDy)
    00221 {
    00222     uint8_t isChanged = 0;
    00223
    00224     /* Stop warning */
    00225     pBtnStatus = pBtnStatus;
    00226     pDy = pDy;
    00227
    00228     /* - Movment buttons, Joystick or Push buttons */
    00229     /* Left */
    00230     if (PIO_Get(&pinsJoystick[JOYSTICK_LEFT]) == 0) {
    00231
    00232       *pDx = -SPEED_X;
    00233       isChanged = 1;
    00234     }
    00235     /* Right */
    00236     else if (PIO_Get(&pinsJoystick[JOYSTICK_RIGHT]) == 0) {
    00237
    00238       *pDx = SPEED_X;
    00239       isChanged = 1;
    00240     }
    00241     else {
    00242       *pDx = 0;
    00243     }
    00244
    00245     return isChanged;
    00246 }
    00247
    00248 /*----------------------------------------------------------------------------
    00249  *         Callbacks re-implementation
    00250  *----------------------------------------------------------------------------*/
    00251
    00252 /**
    00253  * Invoked after the USB driver has been initialized. By default, configures
    00254  * the UDP/UDPHS interrupt.
    00255  */
    00256 void USBDCallbacks_Initialized(void)
    00257 {
    00258     NVIC_EnableIRQ(UDP_IRQn);
    00259 }
    00260
    00261 /**
    00262  *  Invoked whenever a SETUP request is received from the host. Forwards the
    00263  *  request to the standard handler.
    00264  */
    00265 void USBDCallbacks_RequestReceived(const USBGenericRequest *request)
    00266 {
    00267     HIDDMouseDriver_RequestHandler(request);
    00268 }
    00269
    00270 /**
    00271  * Invoked when the configuration of the device changes. Start reading
    00272  * output reports.
    00273  * \param cfgnum New configuration number.
    00274  */
    00275 void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum)
    00276 {
    00277     HIDDMouseDriver_ConfigurationChangedHandler(cfgnum);
    00278 }
    00279
    00280 /*----------------------------------------------------------------------------
    00281  *         Internal functions
    00282  *----------------------------------------------------------------------------*/
    00283
    00284 /**
    00285  * \brief Configure 48MHz Clock for USB
    00286  */
    00287 static void _ConfigureUsbClock(void)
    00288 {
    00289     /* Enable PLLA for USB */
    00290     PMC->CKGR_PLLAR = CKGR_PLLAR_STUCKTO1
    00291                     | CKGR_PLLAR_DIVA(1)
    00292                     | CKGR_PLLAR_MULA(7)
    00293                     | CKGR_PLLAR_PLLACOUNT_Msk;
    00294     while((PMC->PMC_SR & PMC_SR_LOCKA) == 0);
    00295     /* USB Clock uses PLLA */
    00296     PMC->PMC_USB = PMC_USB_USBDIV(1);       /* /2   */
    00297 }
    00298
    00299 /*----------------------------------------------------------------------------
    00300  *         Exported function
    00301  *----------------------------------------------------------------------------*/
    00302
    00303 /**
    00304  * usb_hid_mouse application entry.
    00305  *
    00306  * Initializes the system and then monitors buttons, sending the
    00307  * corresponding character when one is pressed.
    00308  */
    00309 int main(void)
    00310 {
    00311     uint8_t bmButtons = 0;
    00312     int8_t dX = 0, dY = 0;
    00313     uint8_t isChanged;
    00314
    00315     /* Disable watchdog */
    00316     WDT_Disable( WDT );
    00317
    00318     printf("-- USB Device HID Mouse Example %s --\n\r", SOFTPACK_VERSION);
    00319     printf("-- %s\n\r", BOARD_NAME);
    00320     printf("-- Compiled: %s %s --\n\r", __DATE__, __TIME__);
    00321
    00322     /* If they are present, configure Vbus & Wake-up pins */
    00323     PIO_InitializeInterrupts(0);
    00324
    00325     /* Enable UPLL for USB */
    00326     _ConfigureUsbClock();
    00327
    00328     /* Initialize key statuses and configure push buttons */
    00329     PIO_Configure(pinsJoystick, PIO_LISTSIZE(pinsJoystick));
    00330
    00331     /* HID driver initialization */
    00332     HIDDMouseDriver_Initialize(&hiddMouseDriverDescriptors);
    00333
    00334     /* connect if needed */
    00335     VBus_Configure();
    00336
    00337     /* Infinite loop */
    00338     while (1) {
    00339
    00340         if (USBD_GetState() < USBD_STATE_CONFIGURED)
    00341             continue;
    00342
    00343         isChanged = _ButtonsMonitor(&bmButtons, &dX, &dY);
    00344
    00345         if (isChanged) {
    00346
    00347             uint8_t status;
    00348
    00349             do {
    00350
    00351                 status = HIDDMouseDriver_ChangePoints(bmButtons,
    00352                                                       dX, dY);
    00353
    00354             } while (status != USBD_STATUS_SUCCESS);
    00355         }
    00356
    00357     }
    00358 }
    00359

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines