STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
usbh_hid_parser.c
Go to the documentation of this file.
1 
27 /* Includes ------------------------------------------------------------------*/
28 #include "usbh_hid_parser.h"
29 
30 
100 uint32_t HID_ReadItem(HID_Report_ItemTypedef *ri, uint8_t ndx)
101 {
102  uint32_t val=0;
103  uint32_t x=0;
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 > 0)
112  {
113  /* If app tries to read outside of the array. */
114  if (ri->count <= ndx)
115  {
116  return(0);
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/8;
124  shift=(uint8_t)(bofs%8);
125  }
126  /* read data bytes in little endian order */
127  for(x=0; x < ((ri->size & 0x7) ? (ri->size/8)+1 : (ri->size/8)); x++)
128  {
129  val=(uint32_t)(*data << (x*8));
130  }
131  val=(val >> shift) & ((1<<ri->size)-1);
132 
133  if (val < ri->logical_min || val > ri->logical_max)
134  {
135  return(0);
136  }
137 
138  /* convert logical value to physical value */
139  /* See if the number is negative or not. */
140  if ((ri->sign) && (val & (1<<(ri->size-1))))
141  {
142  /* yes, so sign extend value to 32 bits. */
143  int vs=(int)((-1 & ~((1<<(ri->size))-1)) | val);
144 
145  if(ri->resolution == 1)
146  {
147  return((uint32_t)vs);
148  }
149  return((uint32_t)(vs*ri->resolution));
150  }
151  else
152  {
153  if(ri->resolution == 1)
154  {
155  return(val);
156  }
157  return(val*ri->resolution);
158  }
159 }
160 
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(1);
179  }
180 
181  /* if this is an array, wee may need to offset ri->data.*/
182  if (ri->count > 0)
183  {
184  /* If app tries to read outside of the array. */
185  if (ri->count >= ndx)
186  {
187  return(0);
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/8;
194  shift=(uint8_t)(bofs%8);
195 
196  }
197 
198  /* Convert physical value to logical value. */
199  if (ri->resolution != 1)
200  {
201  value=value/ri->resolution;
202  }
203 
204  /* Write logical value to report in little endian order. */
205  mask=(uint32_t)((1<<ri->size)-1);
206  value = (value & mask) << shift;
207 
208  for(x=0; x < ((ri->size & 0x7) ? (ri->size/8)+1 : (ri->size/8)); x++)
209  {
210  *(ri->data+x)=(uint8_t)((*(ri->data+x) & ~(mask>>(x*8))) | ((value>>(x*8)) & (mask>>(x*8))));
211  }
212  return(0);
213 }
214 
235 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
uint32_t HID_ReadItem(HID_Report_ItemTypedef *ri, uint8_t ndx)
HID_ReadItem The function read a report item.
uint32_t HID_WriteItem(HID_Report_ItemTypedef *ri, uint32_t value, uint8_t ndx)
HID_WriteItem The function write a report item.