Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BSP_DISCO_F746NG_patch mbed-rtos mbed
usbh_hid_parser.c
00001 /** 00002 ****************************************************************************** 00003 * @file usbh_hid_parser.c 00004 * @author MCD Application Team 00005 * @version V3.2.2 00006 * @date 07-July-2015 00007 * @brief This file is the HID Layer Handlers for USB Host HID class. 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT 2015 STMicroelectronics</center></h2> 00012 * 00013 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00014 * You may not use this file except in compliance with the License. 00015 * You may obtain a copy of the License at: 00016 * 00017 * http://www.st.com/software_license_agreement_liberty_v2 00018 * 00019 * Unless required by applicable law or agreed to in writing, software 00020 * distributed under the License is distributed on an "AS IS" BASIS, 00021 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00022 * See the License for the specific language governing permissions and 00023 * limitations under the License. 00024 * 00025 ****************************************************************************** 00026 */ 00027 /* Includes ------------------------------------------------------------------*/ 00028 #include "usbh_hid_parser.h" 00029 00030 00031 /** @addtogroup USBH_LIB 00032 * @{ 00033 */ 00034 00035 /** @addtogroup USBH_CLASS 00036 * @{ 00037 */ 00038 00039 /** @addtogroup USBH_HID_CLASS 00040 * @{ 00041 */ 00042 00043 /** @defgroup USBH_HID_PARSER 00044 * @brief This file includes HID parsers for USB Host HID class. 00045 * @{ 00046 */ 00047 00048 /** @defgroup USBH_HID_PARSER_Private_TypesDefinitions 00049 * @{ 00050 */ 00051 /** 00052 * @} 00053 */ 00054 00055 00056 /** @defgroup USBH_HID_PARSER_Private_Defines 00057 * @{ 00058 */ 00059 /** 00060 * @} 00061 */ 00062 00063 00064 /** @defgroup USBH_HID_PARSER_Private_Macros 00065 * @{ 00066 */ 00067 /** 00068 * @} 00069 */ 00070 00071 /** @defgroup USBH_HID_PARSER_Private_FunctionPrototypes 00072 * @{ 00073 */ 00074 00075 /** 00076 * @} 00077 */ 00078 00079 00080 /** @defgroup USBH_HID_PARSER_Private_Variables 00081 * @{ 00082 */ 00083 00084 /** 00085 * @} 00086 */ 00087 00088 00089 /** @defgroup USBH_HID_PARSER_Private_Functions 00090 * @{ 00091 */ 00092 00093 /** 00094 * @brief HID_ReadItem 00095 * The function read a report item. 00096 * @param ri: report item 00097 * @param ndx: report index 00098 * @retval status (0 : fail / otherwise: item value) 00099 */ 00100 uint32_t HID_ReadItem(HID_Report_ItemTypedef *ri, uint8_t ndx) 00101 { 00102 uint32_t val=0; 00103 uint32_t x=0; 00104 uint32_t bofs; 00105 uint8_t *data=ri->data; 00106 uint8_t shift=ri->shift; 00107 00108 /* get the logical value of the item */ 00109 00110 /* if this is an array, wee may need to offset ri->data.*/ 00111 if (ri->count > 0) 00112 { 00113 /* If app tries to read outside of the array. */ 00114 if (ri->count <= ndx) 00115 { 00116 return(0); 00117 } 00118 00119 /* calculate bit offset */ 00120 bofs = ndx*ri->size; 00121 bofs += shift; 00122 /* calculate byte offset + shift pair from bit offset. */ 00123 data+=bofs/8; 00124 shift=(uint8_t)(bofs%8); 00125 } 00126 /* read data bytes in little endian order */ 00127 for(x=0; x < ((ri->size & 0x7) ? (ri->size/8)+1 : (ri->size/8)); x++) 00128 { 00129 val=(uint32_t)(*data << (x*8)); 00130 } 00131 val=(val >> shift) & ((1<<ri->size)-1); 00132 00133 if (val < ri->logical_min || val > ri->logical_max) 00134 { 00135 return(0); 00136 } 00137 00138 /* convert logical value to physical value */ 00139 /* See if the number is negative or not. */ 00140 if ((ri->sign) && (val & (1<<(ri->size-1)))) 00141 { 00142 /* yes, so sign extend value to 32 bits. */ 00143 int vs=(int)((-1 & ~((1<<(ri->size))-1)) | val); 00144 00145 if(ri->resolution == 1) 00146 { 00147 return((uint32_t)vs); 00148 } 00149 return((uint32_t)(vs*ri->resolution)); 00150 } 00151 else 00152 { 00153 if(ri->resolution == 1) 00154 { 00155 return(val); 00156 } 00157 return(val*ri->resolution); 00158 } 00159 } 00160 00161 /** 00162 * @brief HID_WriteItem 00163 * The function write a report item. 00164 * @param ri: report item 00165 * @param ndx: report index 00166 * @retval status (1: fail/ 0 : Ok) 00167 */ 00168 uint32_t HID_WriteItem(HID_Report_ItemTypedef *ri, uint32_t value, uint8_t ndx) 00169 { 00170 uint32_t x; 00171 uint32_t mask; 00172 uint32_t bofs; 00173 uint8_t *data=ri->data; 00174 uint8_t shift=ri->shift; 00175 00176 if (value < ri->physical_min || value > ri->physical_max) 00177 { 00178 return(1); 00179 } 00180 00181 /* if this is an array, wee may need to offset ri->data.*/ 00182 if (ri->count > 0) 00183 { 00184 /* If app tries to read outside of the array. */ 00185 if (ri->count >= ndx) 00186 { 00187 return(0); 00188 } 00189 /* calculate bit offset */ 00190 bofs = ndx*ri->size; 00191 bofs += shift; 00192 /* calculate byte offset + shift pair from bit offset. */ 00193 data+=bofs/8; 00194 shift=(uint8_t)(bofs%8); 00195 00196 } 00197 00198 /* Convert physical value to logical value. */ 00199 if (ri->resolution != 1) 00200 { 00201 value=value/ri->resolution; 00202 } 00203 00204 /* Write logical value to report in little endian order. */ 00205 mask=(uint32_t)((1<<ri->size)-1); 00206 value = (value & mask) << shift; 00207 00208 for(x=0; x < ((ri->size & 0x7) ? (ri->size/8)+1 : (ri->size/8)); x++) 00209 { 00210 *(ri->data+x)=(uint8_t)((*(ri->data+x) & ~(mask>>(x*8))) | ((value>>(x*8)) & (mask>>(x*8)))); 00211 } 00212 return(0); 00213 } 00214 00215 /** 00216 * @} 00217 */ 00218 00219 /** 00220 * @} 00221 */ 00222 00223 /** 00224 * @} 00225 */ 00226 00227 /** 00228 * @} 00229 */ 00230 00231 00232 /** 00233 * @} 00234 */ 00235 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Tue Jul 12 2022 14:58:24 by
1.7.2