SAMV71 Xplained Ultra Software Package 1.4

USBRequests.c

Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License 
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2014, 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  *  \section Purpose
00032  * 
00033  *    Implements for USB requests described by the USB specification.
00034  */
00035 
00036 /** \addtogroup usb_request
00037  * @{
00038  */
00039 
00040 /*------------------------------------------------------------------------------
00041  *         Headers
00042  *------------------------------------------------------------------------------*/
00043      
00044 #include <USBRequests.h>
00045 
00046 /*------------------------------------------------------------------------------
00047  *         Exported functions
00048  *------------------------------------------------------------------------------*/
00049 
00050 /**
00051  * Returns the type of the given request.
00052  * \param request Pointer to a USBGenericRequest instance.
00053  * \return "USB Request Types"
00054  */
00055 extern uint8_t USBGenericRequest_GetType(const USBGenericRequest *request)
00056 {
00057     return ((request->bmRequestType >> 5) & 0x3);
00058 }
00059 
00060 /**
00061  * Returns the request code of the given request.
00062  * \param request Pointer to a USBGenericRequest instance.
00063  * \return Request code.
00064  * \sa "USB Request Codes"
00065  */
00066 uint8_t USBGenericRequest_GetRequest(const USBGenericRequest *request)
00067 {
00068     return request->bRequest;
00069 }
00070 
00071 /**
00072  * Returns the wValue field of the given request.
00073  * \param request - Pointer to a USBGenericRequest instance.
00074  * \return Request value.
00075  */
00076 uint16_t USBGenericRequest_GetValue(const USBGenericRequest *request)
00077 {
00078     return request->wValue;
00079 }
00080 
00081 /**
00082  * Returns the wIndex field of the given request.
00083  * \param request Pointer to a USBGenericRequest instance.
00084  * \return Request index;
00085  */
00086 uint16_t USBGenericRequest_GetIndex(const USBGenericRequest *request)
00087 {
00088     return request->wIndex;
00089 }
00090 
00091 /**
00092  * Returns the expected length of the data phase following a request.
00093  * \param request Pointer to a USBGenericRequest instance.
00094  * \return Length of data phase.
00095  */
00096 uint16_t USBGenericRequest_GetLength(const USBGenericRequest *request)
00097 {
00098     return request->wLength;
00099 }
00100 
00101 /**
00102  * Returns the endpoint number targeted by a given request.
00103  * \param request Pointer to a USBGenericRequest instance.
00104  * \return Endpoint number.
00105  */
00106 uint8_t USBGenericRequest_GetEndpointNumber(
00107     const USBGenericRequest *request)
00108 {
00109     return USBGenericRequest_GetIndex(request) & 0xF;
00110 }
00111 
00112 /**
00113  * Returns the intended recipient of a given request.
00114  * \param request Pointer to a USBGenericRequest instance.
00115  * \return Request recipient.
00116  * \sa "USB Request Recipients"
00117  */
00118 uint8_t USBGenericRequest_GetRecipient(const USBGenericRequest *request)
00119 {
00120     /* Recipient is in bits [0..4] of the bmRequestType field */
00121     return request->bmRequestType & 0xF;
00122 }
00123 
00124 /**
00125  * Returns the direction of the data transfer following the given request.
00126  * \param request Pointer to a USBGenericRequest instance.
00127  * \return Transfer direction.
00128  * \sa "USB Request Directions"
00129  */
00130 uint8_t USBGenericRequest_GetDirection(const USBGenericRequest *request)
00131 {
00132     /* Transfer direction is located in bit D7 of the bmRequestType field */
00133     if ((request->bmRequestType & 0x80) != 0) {
00134 
00135         return USBGenericRequest_IN;
00136     }
00137     else {
00138 
00139         return USBGenericRequest_OUT;
00140     }
00141 }
00142 
00143 
00144 /**
00145  * Returns the type of the descriptor requested by the host given the
00146  * corresponding GET_DESCRIPTOR request.
00147  * \param request Pointer to a USBGenericDescriptor instance.
00148  * \return Type of the requested descriptor.
00149  */
00150 uint8_t USBGetDescriptorRequest_GetDescriptorType(
00151     const USBGenericRequest *request)
00152 {
00153     /* Requested descriptor type is in the high-byte of the wValue field */
00154     return (USBGenericRequest_GetValue(request) >> 8) & 0xFF;
00155 }
00156 
00157 /**
00158  * Returns the index of the requested descriptor, given the corresponding
00159  * GET_DESCRIPTOR request.
00160  * \param request Pointer to a USBGenericDescriptor instance.
00161  * \return Index of the requested descriptor.
00162  */
00163 uint8_t USBGetDescriptorRequest_GetDescriptorIndex(
00164     const USBGenericRequest *request)
00165 {
00166     /* Requested descriptor index if in the low byte of the wValue field */
00167     return USBGenericRequest_GetValue(request) & 0xFF;
00168 }
00169 
00170 
00171 /**
00172  * Returns the address that the device must take in response to a
00173  * SET_ADDRESS request.
00174  * \param request Pointer to a USBGenericRequest instance.
00175  * \return New device address.
00176  */
00177 uint8_t USBSetAddressRequest_GetAddress(const USBGenericRequest *request)
00178 {
00179     return USBGenericRequest_GetValue(request) & 0x7F;
00180 }
00181 
00182 
00183 /**
00184  * Returns the number of the configuration that should be set in response
00185  * to the given SET_CONFIGURATION request.
00186  * \param request Pointer to a USBGenericRequest instance.
00187  * \return Number of the requested configuration.
00188  */
00189 uint8_t USBSetConfigurationRequest_GetConfiguration(
00190     const USBGenericRequest *request)
00191 {
00192     return USBGenericRequest_GetValue(request);
00193 }
00194 
00195 
00196 /**
00197  * Indicates which interface is targeted by a GET_INTERFACE or
00198  * SET_INTERFACE request.
00199  * \param request Pointer to a USBGenericRequest instance.
00200  * \return Interface number.
00201  */
00202 uint8_t USBInterfaceRequest_GetInterface(const USBGenericRequest *request)
00203 {
00204     return (USBGenericRequest_GetIndex(request) & 0xFF);
00205 }
00206 
00207 /**
00208  * Indicates the new alternate setting that the interface targeted by a
00209  * SET_INTERFACE request should use.
00210  * \param request Pointer to a USBGenericRequest instance.
00211  * \return New active setting for the interface.
00212  */
00213 uint8_t USBInterfaceRequest_GetAlternateSetting(
00214     const USBGenericRequest *request)
00215 {
00216     return (USBGenericRequest_GetValue(request) & 0xFF);
00217 }
00218 
00219 
00220 /**
00221  *  Returns the feature selector of a given CLEAR_FEATURE or SET_FEATURE
00222  *  request.
00223  *  \param request Pointer to a USBGenericRequest instance.
00224  *  \return Feature selector.
00225  */
00226 uint8_t USBFeatureRequest_GetFeatureSelector(
00227     const USBGenericRequest *request)
00228 {
00229     return USBGenericRequest_GetValue(request);
00230 }
00231 
00232 /**
00233  *  Indicates the test that the device must undertake following a
00234  *  SET_FEATURE request.
00235  *  \param request Pointer to a USBGenericRequest instance.
00236  *  \return Test selector.
00237  */
00238 uint8_t USBFeatureRequest_GetTestSelector(
00239     const USBGenericRequest *request)
00240 {
00241     return (USBGenericRequest_GetIndex(request) >> 8) & 0xFF;
00242 }
00243 
00244 /**@}*/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines