1 /**
2 ******************************************************************************
3 * @file usbh_hid_parser.c
4 * @author MCD Application Team
5 * @brief This file is the HID Layer Handlers for USB Host HID class.
6 ******************************************************************************
7 * @attention
8 *
9 * <h2><center>© Copyright (c) 2015 STMicroelectronics.
10 * All rights reserved.</center></h2>
11 *
12 * This software component is licensed by ST under Ultimate Liberty license
13 * SLA0044, the "License"; You may not use this file except in compliance with
14 * the License. You may obtain a copy of the License at:
15 * www.st.com/SLA0044
16 *
17 ******************************************************************************
18 */
19
20 /* BSPDependencies
21 - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c"
22 - "stm32xxxxx_{eval}{discovery}_io.c"
23 - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c"
24 - "stm32xxxxx_{eval}{discovery}_sdram.c"
25 EndBSPDependencies */
26
27 /* Includes ------------------------------------------------------------------*/
28 #include "usbh_hid_parser.h"
29
30
31 /** @addtogroup USBH_LIB
32 * @{
33 */
34
35 /** @addtogroup USBH_CLASS
36 * @{
37 */
38
39 /** @addtogroup USBH_HID_CLASS
40 * @{
41 */
42
43 /** @defgroup USBH_HID_PARSER
44 * @brief This file includes HID parsers for USB Host HID class.
45 * @{
46 */
47
48 /** @defgroup USBH_HID_PARSER_Private_TypesDefinitions
49 * @{
50 */
51 /**
52 * @}
53 */
54
55
56 /** @defgroup USBH_HID_PARSER_Private_Defines
57 * @{
58 */
59 /**
60 * @}
61 */
62
63
64 /** @defgroup USBH_HID_PARSER_Private_Macros
65 * @{
66 */
67 /**
68 * @}
69 */
70
71 /** @defgroup USBH_HID_PARSER_Private_FunctionPrototypes
72 * @{
73 */
74
75 /**
76 * @}
77 */
78
79
80 /** @defgroup USBH_HID_PARSER_Private_Variables
81 * @{
82 */
83
84 /**
85 * @}
86 */
87
88
89 /** @defgroup USBH_HID_PARSER_Private_Functions
90 * @{
91 */
92
93 /**
94 * @brief HID_ReadItem
95 * The function read a report item.
96 * @param ri: report item
97 * @param ndx: report index
98 * @retval status (0 : fail / otherwise: item value)
99 */
HID_ReadItem(HID_Report_ItemTypedef * ri,uint8_t ndx)100 uint32_t HID_ReadItem(HID_Report_ItemTypedef *ri, uint8_t ndx)
101 {
102 uint32_t val = 0U;
103 uint32_t x = 0U;
104 uint32_t bofs;
105 uint8_t *data = ri->data;
106 uint8_t shift = ri->shift;
107
108 /* get the logical value of the item */
109
110 /* if this is an array, wee may need to offset ri->data.*/
111 if (ri->count > 0U)
112 {
113 /* If app tries to read outside of the array. */
114 if (ri->count <= ndx)
115 {
116 return (0U);
117 }
118
119 /* calculate bit offset */
120 bofs = ndx * ri->size;
121 bofs += shift;
122 /* calculate byte offset + shift pair from bit offset. */
123 data += bofs / 8U;
124 shift = (uint8_t)(bofs % 8U);
125 }
126 /* read data bytes in little endian order */
127 for (x = 0U; x < ((ri->size & 0x7U) ? (ri->size / 8U) + 1U : (ri->size / 8U)); x++)
128 {
129 val = (uint32_t)((uint32_t)(*data) << (x * 8U));
130 }
131 val = (val >> shift) & ((1U << ri->size) - 1U);
132
133 if (val < ri->logical_min || val > ri->logical_max)
134 {
135 return (0U);
136 }
137
138 /* convert logical value to physical value */
139 /* See if the number is negative or not. */
140 if ((ri->sign) && (val & (1U << (ri->size - 1U))))
141 {
142 /* yes, so sign extend value to 32 bits. */
143 uint32_t vs = (uint32_t)((0xffffffffU & ~((1U << (ri->size)) - 1U)) | val);
144
145 if (ri->resolution == 1U)
146 {
147 return ((uint32_t)vs);
148 }
149 return ((uint32_t)(vs * ri->resolution));
150 }
151 else
152 {
153 if (ri->resolution == 1U)
154 {
155 return (val);
156 }
157 return (val * ri->resolution);
158 }
159 }
160
161 /**
162 * @brief HID_WriteItem
163 * The function write a report item.
164 * @param ri: report item
165 * @param ndx: report index
166 * @retval status (1: fail/ 0 : Ok)
167 */
HID_WriteItem(HID_Report_ItemTypedef * ri,uint32_t value,uint8_t ndx)168 uint32_t HID_WriteItem(HID_Report_ItemTypedef *ri, uint32_t value, uint8_t ndx)
169 {
170 uint32_t x;
171 uint32_t mask;
172 uint32_t bofs;
173 uint8_t *data = ri->data;
174 uint8_t shift = ri->shift;
175
176 if (value < ri->physical_min || value > ri->physical_max)
177 {
178 return (1U);
179 }
180
181 /* if this is an array, wee may need to offset ri->data.*/
182 if (ri->count > 0U)
183 {
184 /* If app tries to read outside of the array. */
185 if (ri->count >= ndx)
186 {
187 return (0U);
188 }
189 /* calculate bit offset */
190 bofs = ndx * ri->size;
191 bofs += shift;
192 /* calculate byte offset + shift pair from bit offset. */
193 data += bofs / 8U;
194 shift = (uint8_t)(bofs % 8U);
195
196 }
197
198 /* Convert physical value to logical value. */
199 if (ri->resolution != 1U)
200 {
201 value = value / ri->resolution;
202 }
203
204 /* Write logical value to report in little endian order. */
205 mask = (1U << ri->size) - 1U;
206 value = (value & mask) << shift;
207
208 for (x = 0U; x < ((ri->size & 0x7U) ? (ri->size / 8U) + 1U : (ri->size / 8U)); x++)
209 {
210 *(ri->data + x) = (uint8_t)((*(ri->data + x) & ~(mask >> (x * 8U))) |
211 ((value >> (x * 8U)) & (mask >> (x * 8U))));
212 }
213
214 return 0U;
215 }
216
217 /**
218 * @}
219 */
220
221 /**
222 * @}
223 */
224
225 /**
226 * @}
227 */
228
229 /**
230 * @}
231 */
232
233
234 /**
235 * @}
236 */
237 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
238