SAMV71 Xplained Ultra Software Package 1.3

HIDDMouseDriver.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 /** \file
00031  * \addtogroup usbd_hid_mouse
00032  *@{
00033  */
00034 
00035 /*------------------------------------------------------------------------------
00036  *         Headers
00037  *------------------------------------------------------------------------------*/
00038 
00039 #include <HIDDFunction.h>
00040 #include <HIDDMouseDriver.h>
00041 
00042 #include "USBD.h"
00043 #include <USBD_HAL.h>
00044 #include <USBDDriver.h>
00045 
00046 #include <USBLib_Trace.h>
00047 
00048 /*------------------------------------------------------------------------------
00049  *         Internal Defines
00050  *------------------------------------------------------------------------------*/
00051 
00052 /** Tag bit (Always 1) */
00053 #define HIDDMouse_TAG       (1 << 3)
00054 /** Xsign bit */
00055 #define HIDDMouse_Xsign     (1 << 4)
00056 /** Ysign bit */
00057 #define HIDDMouse_Ysign     (1 << 5)
00058 
00059 /*------------------------------------------------------------------------------
00060  *         Internal types
00061  *------------------------------------------------------------------------------*/
00062 
00063 /**
00064  * Struct for an HID Mouse report.
00065  */
00066 typedef struct _HIDDMouseReport {
00067     /** Callback when report done */
00068     HIDDReportEventCallback fCallback;
00069     /** Callback arguments */
00070     void* pArg;
00071 
00072     /** Report size (ID + DATA) */
00073     uint16_t wMaxSize;
00074     /** Transfered size */
00075     uint16_t wTransferred;
00076     /** Report idle rate */
00077     uint8_t bIdleRate;
00078     /** Delay count for Idle */
00079     uint8_t bDelay;
00080     /** Report ID */
00081     uint8_t bID;
00082     /** Report data block */
00083     HIDDMouseInputReport report;
00084 } HIDDMouseReport;
00085 
00086 /**
00087  * Driver structure for an HID device implementing keyboard functionalities.
00088  */
00089 typedef struct _HIDDMouseDriver {
00090 
00091     /** Mouse function instance */
00092     HIDDFunction hidDrv;
00093     /** Mouse input report */
00094     HIDDReport   *inputReports[1];
00095 } HIDDMouseDriver;
00096 
00097 /*------------------------------------------------------------------------------
00098  *         Internal variables
00099  *------------------------------------------------------------------------------*/
00100 
00101 /** Static instance of the HID mouse device driver. */
00102 static HIDDMouseDriver hiddMouseDriver;
00103 
00104 /** Input report */
00105 static HIDDMouseReport hiddInputReport;
00106 
00107 /** Report descriptor used by the driver. */
00108 static const uint8_t hiddReportDescriptor[] = {
00109 
00110     /* Global Usage Page */
00111     HIDReport_GLOBAL_USAGEPAGE + 1, HIDGenericDesktop_PAGEID,
00112     /* Collection: Application */
00113     HIDReport_LOCAL_USAGE + 1, HIDGenericDesktop_MOUSE,
00114     HIDReport_COLLECTION + 1, HIDReport_COLLECTION_APPLICATION,
00115     /* Physical collection: Pointer */
00116     HIDReport_LOCAL_USAGE + 1, HIDGenericDesktop_POINTER,
00117     HIDReport_COLLECTION + 1, HIDReport_COLLECTION_PHYSICAL,
00118 
00119     /* Input report: buttons */
00120     HIDReport_GLOBAL_USAGEPAGE + 1, HIDButton_PAGEID,
00121 
00122     HIDReport_GLOBAL_REPORTCOUNT + 1, 3,
00123     HIDReport_GLOBAL_REPORTSIZE + 1, 1,
00124     HIDReport_LOCAL_USAGEMINIMUM + 1, 1,
00125     HIDReport_LOCAL_USAGEMAXIMUM + 1, 3,
00126     HIDReport_GLOBAL_LOGICALMINIMUM + 1, 0,
00127     HIDReport_GLOBAL_LOGICALMAXIMUM + 1, 1,
00128     HIDReport_INPUT + 1, HIDReport_VARIABLE,    /* 3 button bits */
00129 
00130     /* Input report: padding */
00131     HIDReport_GLOBAL_REPORTCOUNT + 1, 1,
00132     HIDReport_GLOBAL_REPORTSIZE + 1, 5,
00133     HIDReport_INPUT + 1, HIDReport_CONSTANT,    /* 5 bit padding */
00134 
00135     /* Input report: pointer */
00136     HIDReport_GLOBAL_USAGEPAGE + 1, HIDGenericDesktop_PAGEID,
00137     HIDReport_GLOBAL_REPORTSIZE + 1, 8,
00138     HIDReport_GLOBAL_REPORTCOUNT + 1, 2,
00139     HIDReport_LOCAL_USAGE + 1, HIDGenericDesktop_X,
00140     HIDReport_LOCAL_USAGE + 1, HIDGenericDesktop_Y,
00141     HIDReport_GLOBAL_LOGICALMINIMUM + 1, (uint8_t) -127,
00142     HIDReport_GLOBAL_LOGICALMAXIMUM + 1, 127,
00143     HIDReport_INPUT + 1, HIDReport_VARIABLE | HIDReport_RELATIVE,
00144 
00145     HIDReport_ENDCOLLECTION,
00146     HIDReport_ENDCOLLECTION
00147 };
00148 
00149 /*------------------------------------------------------------------------------
00150  *         Internal functions
00151  *------------------------------------------------------------------------------*/
00152 
00153 /*------------------------------------------------------------------------------
00154  *      Exported functions
00155  *------------------------------------------------------------------------------*/
00156 
00157 /**
00158  * Initializes the HID Mouse %device driver.
00159  * \param pDescriptors Pointer to descriptor list for the HID Mouse.
00160  */
00161 void HIDDMouseDriver_Initialize(const USBDDriverDescriptors *pDescriptors)
00162 {
00163     HIDDMouseDriver* pMouse = &hiddMouseDriver;
00164     HIDDFunction* pHidd = &pMouse->hidDrv;
00165     USBDDriver* pUsbd = USBD_GetDriver();
00166 
00167     /* One input report */
00168     pMouse->inputReports[0] = (HIDDReport*)&hiddInputReport;
00169     HIDDFunction_InitializeReport(pMouse->inputReports[0],
00170                                   HIDDMouseDriver_REPORTDESCRIPTORSIZE,
00171                                   0,
00172                                   0, 0);
00173 
00174     /* Initialize USBD Driver instance */
00175     USBDDriver_Initialize(pUsbd,
00176                           pDescriptors,
00177                           0); /* Multiple interface settings not supported */
00178     /* Function initialize */
00179     HIDDFunction_Initialize(pHidd,
00180                             pUsbd, 0,
00181                             hiddReportDescriptor,
00182                             (HIDDReport**)(&pMouse->inputReports), 1,
00183                             0, 0);
00184     USBD_Init();
00185 }
00186 
00187 /**
00188  * Handles configureation changed event.
00189  * \param cfgnum New configuration number
00190  */
00191 void HIDDMouseDriver_ConfigurationChangedHandler(uint8_t cfgnum)
00192 {
00193     HIDDMouseDriver * pMouse = &hiddMouseDriver;
00194     HIDDFunction * pHidd = &pMouse->hidDrv;
00195     USBDDriver * pUsbd = pHidd->pUsbd;
00196     USBConfigurationDescriptor *pDesc;
00197 
00198     if (cfgnum > 0) {
00199 
00200         /* Parse endpoints for reports */
00201         pDesc = USBDDriver_GetCfgDescriptors(pUsbd, cfgnum);
00202         HIDDFunction_ParseInterface(pHidd,
00203                                     (USBGenericDescriptor*)pDesc,
00204                                     pDesc->wTotalLength);
00205     }
00206 }
00207 
00208 /**
00209  * Handles HID-specific SETUP request sent by the host.
00210  * \param request Pointer to a USBGenericRequest instance
00211  */
00212 void HIDDMouseDriver_RequestHandler(const USBGenericRequest *request)
00213 {
00214     HIDDMouseDriver * pMouse = &hiddMouseDriver;
00215     HIDDFunction * pHidd = &pMouse->hidDrv;
00216     USBDDriver * pUsbd = pHidd->pUsbd;
00217     
00218     TRACE_INFO("NewReq ");
00219 
00220     /* Process HID requests */
00221     if (USBRC_SUCCESS == HIDDFunction_RequestHandler(pHidd,
00222                                                      request)) {
00223         return;
00224     }
00225     /* Process STD requests */
00226     else {
00227 
00228         USBDDriver_RequestHandler(pUsbd, request);
00229     }
00230 
00231 }
00232 
00233 /**
00234  * Update the Mouse button status and location changes via input report
00235  * to host
00236  * \param bmButtons Bit map of the button status
00237  * \param deltaX Movment on X direction
00238  * \param deltaY Movment on Y direction
00239  */
00240 uint8_t HIDDMouseDriver_ChangePoints(uint8_t bmButtons,
00241                                      int8_t deltaX,
00242                                      int8_t deltaY)
00243 {
00244     HIDDMouseDriver * pMouse = &hiddMouseDriver;
00245     HIDDFunction * pHidd = &pMouse->hidDrv;
00246     HIDDMouseInputReport * pReport = &hiddInputReport.report;
00247 
00248     pReport->bmButtons = (bmButtons & 0x07) | HIDDMouse_TAG;
00249     pReport->bX        = deltaX;
00250     pReport->bY        = deltaY;
00251 
00252     /* Send input report through the interrupt IN endpoint */
00253     return USBD_Write(pHidd->bPipeIN,
00254                       (void*)pReport,
00255                       sizeof(HIDDMouseInputReport),
00256                       0,
00257                       0);
00258 }
00259 
00260 /**
00261  * Starts a remote wake-up sequence if the host has explicitely enabled it
00262  * by sending the appropriate SET_FEATURE request.
00263  */
00264 void HIDDMouseDriver_RemoteWakeUp(void)
00265 {
00266     HIDDMouseDriver * pMouse = &hiddMouseDriver;
00267     HIDDFunction * pHidd = &pMouse->hidDrv;
00268     USBDDriver * pUsbd = pHidd->pUsbd;
00269 
00270     /* Remote wake-up has been enabled */
00271     if (USBDDriver_IsRemoteWakeUpEnabled(pUsbd)) {
00272 
00273         USBD_RemoteWakeUp();
00274     }
00275 }
00276 
00277 /**@}*/
00278 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines