PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
UsbHostHidMouse.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2013 Spansion LLC. All Rights Reserved.
3 *
4 * This software is owned and published by:
5 * Spansion LLC, 915 DeGuigne Dr. Sunnyvale, CA 94088-3453 ("Spansion").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with Spansion
11 * components. This software is licensed by Spansion to be adapted only
12 * for use in systems utilizing Spansion components. Spansion shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. Spansion is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * SPANSION MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
19 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
20 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
21 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
22 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
23 * WARRANTY OF NONINFRINGEMENT.
24 * SPANSION SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
25 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
26 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
27 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
28 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
30 * SAVINGS OR PROFITS,
31 * EVEN IF SPANSION HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
33 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
34 * FROM, THE SOFTWARE.
35 *
36 * This software may be replicated in part or whole for the licensed use,
37 * with the restriction that this Disclaimer and Copyright notice must be
38 * included with each copy of this software, whether used in part or whole,
39 * at all times.
40 */
41 /******************************************************************************/
58 /*****************************************************************************/
59 /* Include files */
60 /*****************************************************************************/
61 
62 
63 #include "UsbHostHidMouse.h"
64 
65 #if ((FM_PERIPHERAL_USB_HOST_ENABLED == ON) && (USBHOSTHIDMOUSE_ENABLED == ON))
66 
67 /*****************************************************************************/
68 /* Local pre-processor symbols/macros ('#define') */
69 /*****************************************************************************/
70 
71 
72 /*****************************************************************************/
73 /* function */
74 /*****************************************************************************/
75 static void UsbHostHidMouse_DataReceivedCallback(stc_usbn_t* pstcUsb);
76 
77 /*****************************************************************************/
78 /* Global variable definitions (declared in header file with 'extern') */
79 /*****************************************************************************/
80 static stc_host_endpoint_t *EndpointIN;
81 static uint8_t u8MouseData[5];
82 static uint8_t u8EndpointIN;
83 static uint16_t u16MaxPackageSizeIN;
84 void (*MouseEventHandler)(MouseEventType_t stcEventType, stc_mousedata_t stcInternalMouseData) = 0;
87 static stc_usbn_t* pstcUsbHandle = NULL;
88 /*****************************************************************************/
89 /* Local type definitions ('typedef') */
90 /*****************************************************************************/
91 
92 /*****************************************************************************/
93 /* Local variable definitions ('static') */
94 /*****************************************************************************/
95 
96 
97 
98 /*****************************************************************************/
99 /* Local function prototypes ('static') */
100 /*****************************************************************************/
101 
102 /*****************************************************************************/
103 /* Function implementation - global ('extern') and local ('static') */
104 /*****************************************************************************/
105 
106 boolean_t UsbHostHidMouse_InitHandler(stc_usbn_t* pstcUsb, uint8_t* pu8Configuration, uint32_t u32Length)
107 {
108  uint8_t* pu8Buffer;
109  uint8_t u8NumberOfInterfaces;
110  uint8_t u8NumberOfEndpoints;
111  uint8_t u8EndpointNumber;
112  uint8_t u8InterfaceNumber;
113  uint8_t u8Interval;
114 
115  if (pstcUsbHandle != NULL)
116  {
117  return FALSE;
118  }
119  pstcUsbHandle = pstcUsb;
120  USBDBG("Yeah, called if a HID Device was connected ;-)\n");
121 
122  u8NumberOfInterfaces=pu8Configuration[4];
123  for(u8InterfaceNumber=0;u8InterfaceNumber < u8NumberOfInterfaces;u8InterfaceNumber++)
124  {
125  USBDBGVAL8("Interface Index: ",u8InterfaceNumber);
126  pu8Buffer = UsbHost_GetUsbInterfaceDescriptor(pu8Configuration,u32Length,u8InterfaceNumber);
127  if (pu8Buffer == 0)
128  {
129  USBDBG("Error: Could not Request Interface Descriptor.\n");
130  return FALSE;
131  }
132  u8NumberOfEndpoints = pu8Buffer[4];
133 
134  if ((pu8Buffer[6] == 0x01) && (pu8Buffer[7] == 0x2)) //SubClass & Protocol
135  {
136  for(u8EndpointNumber=1;u8EndpointNumber<=u8NumberOfEndpoints;u8EndpointNumber++)
137  {
138  USBDBGVAL8("Endpoint Index: ",u8EndpointNumber);
139  pu8Buffer = UsbHost_GetUsbEndpointDescriptor(pu8Configuration,u32Length,u8InterfaceNumber,u8EndpointNumber);
140  if (pu8Buffer[2] & USB_IN_DIRECTION)
141  {
142  u8EndpointIN = pu8Buffer[2];
143  u16MaxPackageSizeIN = (uint16_t)(pu8Buffer[4] + (pu8Buffer[5] << 8));
144  u8Interval = pu8Buffer[6];
145  }
146  if (u8EndpointIN != 0)
147  {
148  break;
149  }
150  }
151  if (u8EndpointIN == 0)
152  {
153  USBDBG("Hm, error while resolving endpoints...");
154  return FALSE;
155  }
156  USBDBGVAL8("EP-IN:",u8EndpointIN);
157 
158  if (u8EndpointIN != 0)
159  {
161  EndpointIN->pu8Buffer = u8MouseData;
162  EndpointIN->u32DataSize = 4;
163  }
164  USBDBGVAL8("Endpoint-IN Addr: ",u8EndpointIN);
165  USBDBGVAL8("Endpoint-IN Size: ",u16MaxPackageSizeIN);
166  break;
167  }
168  }
169  if (u8EndpointIN == 0)
170  {
171  USBDBG("Hm, error while resolving endpoints...");
172  return FALSE;
173  }
174  return TRUE;
175 }
176 
178 {
179  pstcUsbHandle = NULL;
180  return TRUE;
181 }
182 
183 void UsbHostHidMouse_SetCallback(void (*Handler)(MouseEventType_t stcEventType, stc_mousedata_t stcInternalMouseData))
184 {
185  MouseEventHandler = Handler;
186 }
187 
189 {
190  MouseEventHandler = 0;
191 }
192 
193 static void UsbHostHidMouse_DataReceivedCallback(stc_usbn_t* pstcUsb)
194 {
195  MouseEventType_t tEvent = 0;
196  bMouseMoved = TRUE;
197  if (EndpointIN->pu8Buffer[0] != stcMouseData.tButtons)
198  {
199  tEvent |= MOUSEEVENT_BUTTON_CHANGED;
200  }
201  if (EndpointIN->pu8Buffer[1] != 0)
202  {
204  }
205  if (EndpointIN->pu8Buffer[2] != 0)
206  {
208  }
209  if (EndpointIN->pu8Buffer[3] != 0)
210  {
212  }
213 
214  if (((int8_t)EndpointIN->pu8Buffer[1]) > 0)
215  {
216  if (((int8_t)EndpointIN->pu8Buffer[1]) > (0xFFFFFFFF - stcMouseData.stcPosition.u32X))
217  {
218  stcMouseData.stcPosition.u32X = 0xFFFFFFFF;
219  }
220  else
221  {
222  stcMouseData.stcPosition.u32X += ((int8_t)EndpointIN->pu8Buffer[1]);
223  }
224  }
225  else if (((int8_t)EndpointIN->pu8Buffer[1]) < 0)
226  {
227  if ((-((int8_t)EndpointIN->pu8Buffer[1])) > stcMouseData.stcPosition.u32X)
228  {
229  stcMouseData.stcPosition.u32X = 0;
230  }
231  else
232  {
233  stcMouseData.stcPosition.u32X += ((int8_t)EndpointIN->pu8Buffer[1]);
234  }
235  }
236 
237  if (((int8_t)EndpointIN->pu8Buffer[2]) > 0)
238  {
239  if (((int8_t)EndpointIN->pu8Buffer[2]) > (0xFFFFFFFF - stcMouseData.stcPosition.u32Y))
240  {
241  stcMouseData.stcPosition.u32Y = 0xFFFFFFFF;
242  }
243  else
244  {
245  stcMouseData.stcPosition.u32Y += ((int8_t)EndpointIN->pu8Buffer[2]);
246  }
247  }
248  else if (((int8_t)EndpointIN->pu8Buffer[2]) < 0)
249  {
250  if ((-((int8_t)EndpointIN->pu8Buffer[2])) > stcMouseData.stcPosition.u32Y)
251  {
252  stcMouseData.stcPosition.u32Y = 0;
253  }
254  else
255  {
256  stcMouseData.stcPosition.u32Y += ((int8_t)EndpointIN->pu8Buffer[2]);
257  }
258  }
259  stcMouseData.tButtons = EndpointIN->pu8Buffer[0];
260  stcMouseData.i8Scrolling = (int8_t)(EndpointIN->pu8Buffer[3]);
261 
262  if (stcMouseData.i8Scrolling > 0)
263  {
264  stcMouseData.u32ScrollPosition++;
265  }
266  if (stcMouseData.i8Scrolling < 0)
267  {
268  stcMouseData.u32ScrollPosition--;
269  }
270 
271  if ((tEvent) && (MouseEventHandler))
272  {
273  MouseEventHandler(tEvent,stcMouseData);
274  }
276 }
277 
279 {
280  return stcMouseData.stcPosition;
281 }
282 
283 void UsbHostHidMouse_SetCurrentPosition(uint32_t u32X, uint32_t u32Y)
284 {
285  stcMouseData.stcPosition.u32X = u32X;
286  stcMouseData.stcPosition.u32Y = u32Y;
287 }
288 
290 {
291  return stcMouseData.u32ScrollPosition;
292 }
293 
294 void UsbHostHidMouse_SetCurrentScrollPosition(uint32_t u32ScrollPosition)
295 {
296  stcMouseData.u32ScrollPosition = u32ScrollPosition;
297 }
298 
300 {
301  return stcMouseData.tButtons;
302 }
303 
305 {
306  if (bMouseMoved)
307  {
308  bMouseMoved = FALSE;
309  return TRUE;
310  }
311  return FALSE;
312 }
313 
315 {
316  return (pstcUsbHandle != NULL);
317 }
318 
319 #endif /* (USE_USB_HOST == 1) */