SAMV71 Xplained Ultra Software Package 1.5

HIDDMouseDriver.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 /** \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     /* Process STD requests */
00225     else
00226 
00227         USBDDriver_RequestHandler(pUsbd, request);
00228 
00229 }
00230 
00231 /**
00232  * Update the Mouse button status and location changes via input report
00233  * to host
00234  * \param bmButtons Bit map of the button status
00235  * \param deltaX Movment on X direction
00236  * \param deltaY Movment on Y direction
00237  */
00238 uint8_t HIDDMouseDriver_ChangePoints(uint8_t bmButtons,
00239                                      int8_t deltaX,
00240                                      int8_t deltaY)
00241 {
00242     HIDDMouseDriver *pMouse = &hiddMouseDriver;
00243     HIDDFunction *pHidd = &pMouse->hidDrv;
00244     HIDDMouseInputReport *pReport = &hiddInputReport.report;
00245 
00246     pReport->bmButtons = (bmButtons & 0x07) | HIDDMouse_TAG;
00247     pReport->bX        = deltaX;
00248     pReport->bY        = deltaY;
00249 
00250     /* Send input report through the interrupt IN endpoint */
00251     return USBD_Write(pHidd->bPipeIN,
00252                       (void *)pReport,
00253                       sizeof(HIDDMouseInputReport),
00254                       0,
00255                       0);
00256 }
00257 
00258 /**
00259  * Starts a remote wake-up sequence if the host has explicitely enabled it
00260  * by sending the appropriate SET_FEATURE request.
00261  */
00262 void HIDDMouseDriver_RemoteWakeUp(void)
00263 {
00264     HIDDMouseDriver *pMouse = &hiddMouseDriver;
00265     HIDDFunction *pHidd = &pMouse->hidDrv;
00266     USBDDriver *pUsbd = pHidd->pUsbd;
00267 
00268     /* Remote wake-up has been enabled */
00269     if (USBDDriver_IsRemoteWakeUpEnabled(pUsbd))
00270 
00271         USBD_RemoteWakeUp();
00272 }
00273 
00274 /**@}*/
00275 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines