PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
UsbHostHidKeyboard.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 /******************************************************************************/
60 /*****************************************************************************/
61 /* Include files */
62 /*****************************************************************************/
63 #define __USBHOSTHIDKEYBOARD_C__
64 
65 #include "UsbHostHidKeyboard.h"
66 
67 #if ((FM_PERIPHERAL_USB_HOST_ENABLED == ON) && (USBHOSTHIDKEYBOARD_ENABLED == ON))
68 
69 
70 
71 
72 /*****************************************************************************/
73 /* Local pre-processor symbols/macros ('#define') */
74 /*****************************************************************************/
75 
76 
77 /*****************************************************************************/
78 /* function */
79 /*****************************************************************************/
80 
81 
82 /*****************************************************************************/
83 /* Global variable definitions (declared in header file with 'extern') */
84 /*****************************************************************************/
85 static stc_host_endpoint_t *EndpointIN, *EndpointOUT;
86 static uint8_t u8MouseData[8];
87 static uint8_t u8EndpointIN, u8EndpointOUT;
90 static uint8_t pu8OldData[8];
92 static stc_usbn_t* pstcUsbHandle = NULL;
93 void (*KeyEventHandler)(KeyboardData_t *stcData);
94 static void UsbHostHidKeyboard_DataReceivedCallback(stc_usbn_t* pstcUsb);
95 const uint8_t u8Keycodes[] =
96 {
97  0, // 00
98  0, // 01
99  0, // 02
100  0, // 03
101  'a', // 04
102  'b', // 05
103  'c', // 06
104  'd', // 07
105  'e', // 08
106  'f', // 09
107  'g', // 0A
108  'h', // 0B
109  'i', // 0C
110  'j', // 0D
111  'k', // 0E
112  'l', // 0F
113  'm', // 10
114  'n', // 11
115  'o', // 12
116  'p', // 13
117  'q', // 14
118  'r', // 15
119  's', // 16
120  't', // 17
121  'u', // 18
122  'v', // 19
123  'w', // 1A
124  'x', // 1B
125  'y', // 1C
126  'z', // 1D
127  '1', // 1E
128  '2', // 1F
129  '3', // 20
130  '4', // 21
131  '5', // 22
132  '6', // 23
133  '7', // 24
134  '8', // 25
135  '9', // 26
136  '0', // 27
137  10, // 28 (RETURN)
138  0x1B, // 29 (ESC)
139  0x08, // 2A (BACKSPACE)
140  0x09, // 2B (TAB)
141  ' ', // 2C
142  '-', // 2D
143  '=', // 2E
144  0, // 2F
145  0, // 30
146  0, // 31
147  0, // 32
148  0, // 33
149  0, // 34
150  '`', // 35
151  '<', // 36
152  '>', // 37
153  '/', // 38
154  0, // 39
155  0, // 3A
156  0, // 3B
157  0, // 3C
158  0, // 3D
159  0, // 3E
160  0, // 3F
161  0, // 40
162  0, // 41
163  0, // 42
164  0, // 43
165  0, // 44
166  0, // 45
167  0, // 46
168  0, // 47
169  0, // 48
170  0, // 49
171  0, // 4A
172  0, // 4B
173  0, // 4C
174  0, // 4D
175  0, // 4E
176  0, // 4F (RIGHT)
177  0, // 50 (LEFT)
178  0, // 51 (DOWN)
179  0, // 52 (UP)
180  0, // 53 (NUM)
181  '/', // 54
182  '*', // 55
183  '-', // 56
184  '+', // 57
185  10, // 58 (RETURN)
186  '1', // 59
187  '2', // 5A
188  '3', // 5B
189  '4', // 5C
190  '5', // 5D
191  '6', // 5E
192  '7', // 5F
193  '8', // 60
194  '9', // 61
195  '0', // 62
196  '.', // 63
197  0, // 64
198  0, // 65
199  0, // 66
200  0, // 67
201  0, // 68
202  0, // 69
203  0, // 6A
204  0, // 6B
205  0, // 6C
206  0, // 6D
207  0, // 6E
208  0, // 6F
209  0, // 70
210  0, // 71
211  0, // 72
212  0, // 73
213  0, // 74
214  0, // 75
215  0, // 76
216  0, // 77
217  0, // 78
218  0, // 79
219  0, // 7A
220  0, // 7B
221  0, // 7C
222  0, // 7D
223  0, // 7E
224  0, // 7F
225  0, // 80
226  0, // 81
227  0, // 82
228  0, // 83
229  0, // 84
230  0, // 85
231  0, // 86
232  0, // 87
233  0, // 88
234  0, // 89
235  0, // 8A
236  0, // 8B
237  0, // 8C
238  0, // 8D
239  0, // 8E
240  0, // 8F
241  0, // 90
242  0, // 91
243  0, // 92
244  0, // 93
245  0, // 94
246  0, // 95
247  0, // 96
248  0, // 97
249  0, // 98
250  0, // 99
251  0, // 9A
252  0, // 9B
253  0, // 9C
254  0, // 9D
255  0, // 9E
256  0, // 9F
257  0, // A0
258  0, // A1
259  0, // A2
260  0, // A3
261  0, // A4
262  0, // A5
263  0, // A6
264  0, // A7
265  0, // A8
266  0, // A9
267  0, // AA
268  0, // AB
269  0, // AC
270  0, // AD
271  0, // AE
272  0, // AF
273  0, // B0
274  0, // B1
275  0, // B2
276  0, // B3
277  0, // B4
278  0, // B5
279  0, // B6
280  0, // B7
281  0, // B8
282  0, // B9
283  0, // BA
284  0, // BB
285  0, // BC
286  0, // BD
287  0, // BE
288  0, // BF
289  0, // C0
290  0, // C1
291  0, // C2
292  0, // C3
293  0, // C4
294  0, // C5
295  0, // C6
296  0, // C7
297  0, // C8
298  0, // C9
299  0, // CA
300  0, // CB
301  0, // CC
302  0, // CD
303  0, // CE
304  0, // CF
305  0, // D0
306  0, // D1
307  0, // D2
308  0, // D3
309  0, // D4
310  0, // D5
311  0, // D6
312  0, // D7
313  0, // D8
314  0, // D9
315  0, // DA
316  0, // DB
317  0, // DC
318  0, // DD
319  0, // DE
320  0, // DF
321  0, // E0
322  0, // E1
323  0, // E2
324  0, // E3
325  0, // E4
326  0, // E5
327  0, // E6
328  0, // E7
329  0, // E8
330  0, // E9
331  0, // EA
332  0, // EB
333  0, // EC
334  0, // ED
335  0, // EE
336  0, // EF
337  0, // F0
338  0, // F1
339  0, // F2
340  0, // F3
341  0, // F4
342  0, // F5
343  0, // F6
344  0, // F7
345  0, // F8
346  0, // F9
347  0, // FA
348  0, // FB
349  0, // FC
350  0, // FD
351  0, // FE
352  0 // FF
353  };
354 
355 /*****************************************************************************/
356 /* Local type definitions ('typedef') */
357 /*****************************************************************************/
358 
359 /*****************************************************************************/
360 /* Local variable definitions ('static') */
361 /*****************************************************************************/
362 
363 
364 
365 /*****************************************************************************/
366 /* Local function prototypes ('static') */
367 /*****************************************************************************/
368 
369 /*****************************************************************************/
370 /* Function implementation - global ('extern') and local ('static') */
371 /*****************************************************************************/
372 
373 boolean_t UsbHostHidKeyboard_InitHandler(stc_usbn_t* pstcUsb, uint8_t* pu8Configuration, uint32_t u32Length)
374 {
375  uint8_t* pu8Buffer;
376  uint8_t u8NumberOfInterfaces;
377  uint8_t u8NumberOfEndpoints;
378  uint8_t u8EndpointNumber;
379  uint8_t u8InterfaceNumber;
380  uint8_t u8IntervalIN, u8IntervalOUT;
381  if (pstcUsbHandle != NULL)
382  {
383  return FALSE;
384  }
385  pstcUsbHandle = pstcUsb;
386 
387  u8EndpointIN = 0;
388  u8EndpointOUT = 0;
389  USBDBG("Yeah, called if a HID Device was connected ;-)\n");
390 
391  u8NumberOfInterfaces=pu8Configuration[4];
392  for(u8InterfaceNumber=0;u8InterfaceNumber < u8NumberOfInterfaces;u8InterfaceNumber++)
393  {
394  USBDBGVAL8("Interface Index: ",u8InterfaceNumber);
395  pu8Buffer = UsbHost_GetUsbInterfaceDescriptor(pu8Configuration,u32Length,u8InterfaceNumber);
396  if (pu8Buffer == 0)
397  {
398  USBDBG("Error: Could not Request Interface Descriptor.\n");
399  return FALSE;
400  }
401  u8NumberOfEndpoints = pu8Buffer[4];
402 
403  if ((pu8Buffer[6] == 0x01) && (pu8Buffer[7] == 0x1)) //SubClass & Protocol
404  {
405  for(u8EndpointNumber=1;u8EndpointNumber<=u8NumberOfEndpoints;u8EndpointNumber++)
406  {
407  USBDBGVAL8("Endpoint Index: ",u8EndpointNumber);
408  pu8Buffer = UsbHost_GetUsbEndpointDescriptor(pu8Configuration,u32Length,u8InterfaceNumber,u8EndpointNumber);
409  if (pu8Buffer[2] & USB_IN_DIRECTION)
410  {
411  u8EndpointIN = pu8Buffer[2];
412  u16MaxPackageSizeIN = (uint16_t)(pu8Buffer[4] + (pu8Buffer[5] << 8));
413  u8IntervalIN = pu8Buffer[6];
414  }
415  else
416  {
417  u8EndpointOUT = pu8Buffer[2];
418  u16MaxPackageSizeOUT = (uint16_t)(pu8Buffer[4] + (pu8Buffer[5] << 8));
419  u8IntervalOUT = pu8Buffer[6];
420  }
421  if ((u8EndpointIN != 0) && (u8EndpointOUT != 0))
422  {
423  break;
424  }
425  }
426  if (u8EndpointIN == 0)
427  {
428  USBDBG("Hm, error while resolving endpoints...");
429  return FALSE;
430  }
431 
432 
433  if (u8EndpointIN != 0)
434  {
436  EndpointIN->pu8Buffer = u8MouseData;
437  EndpointIN->u32DataSize = 8;
438  }
439  if (u8EndpointOUT != 0)
440  {
441  UsbHost_AddHostEndpoint(pstcUsbHandle, &EndpointOUT,u8EndpointOUT,u16MaxPackageSizeOUT,u8IntervalOUT,0);
442  }
443  USBDBGVAL8("Endpoint-IN Addr: ",u8EndpointIN);
444  USBDBGVAL8("Endpoint-IN Size: ",u16MaxPackageSizeIN);
445  break;
446  }
447  }
448  if (u8EndpointIN == 0)
449  {
450  USBDBG("Hm, error while resolving endpoints...");
451  return FALSE;
452  }
453  return TRUE;
454 }
455 
457 {
458  pstcUsbHandle = NULL;
459  return TRUE;
460 }
461 
462 
464 {
465  return (pstcUsbHandle != NULL);
466 }
467 
468 static void UsbHostHidKeyboard_DataReceivedCallback(stc_usbn_t* pstcUsb)
469 {
470  uint8_t i = 0;
471  boolean_t bExecuteEvent = FALSE;
472  for(i=0;i<8;i++)
473  {
474  if ((bExecuteEvent == FALSE) && ((EndpointIN->pu8Buffer[i] != 0) || ((EndpointIN->pu8Buffer[i] == 0) && (pu8OldData[i] != EndpointIN->pu8Buffer[i]))))
475  {
476  bExecuteEvent = TRUE;
477  }
478  pu8OldData[i] = EndpointIN->pu8Buffer[i];
479  }
480 
481  stcKeyboardData.u8ModifierKeys = EndpointIN->pu8Buffer[0];
482  stcKeyboardData.u8Reserved = EndpointIN->pu8Buffer[1];
483  stcKeyboardData.u8KeyCode1 = EndpointIN->pu8Buffer[2];
484  stcKeyboardData.u8KeyCode2 = EndpointIN->pu8Buffer[3];
485  stcKeyboardData.u8KeyCode3 = EndpointIN->pu8Buffer[4];
486  stcKeyboardData.u8KeyCode4 = EndpointIN->pu8Buffer[5];
487  stcKeyboardData.u8KeyCode5 = EndpointIN->pu8Buffer[6];
488  stcKeyboardData.u8KeyCode6 = EndpointIN->pu8Buffer[7];
489  bKeyboardChanged = bExecuteEvent;
490  if ((bExecuteEvent == TRUE) && (KeyEventHandler != 0))
491  {
492  KeyEventHandler(&stcKeyboardData);
493  }
495 
496 }
497 
498 void UsbHostHidKeyboard_SetCallback(void (*Handler)(KeyboardData_t *stcData))
499 {
500  KeyEventHandler = Handler;
501 }
502 
504 {
505  KeyEventHandler = 0;
506 }
507 
508 char_t UsbHostHidKeyboard_GetCh(volatile uint32_t u32TimeOut)
509 {
510  char_t c;
511  while ((bKeyboardChanged == FALSE) && (u32TimeOut > 0))
512  {
513  u32TimeOut--;
514  if ((UsbHost_GetDeviceStatus(pstcUsbHandle) & USBHOST_DEVICE_CONFIGURED) == 0)
515  {
516  return 0;
517  }
518  }
519  if (u32TimeOut == 0) return 0;
520 
521  while((stcKeyboardData.u8KeyCode1 == 0) && (u32TimeOut > 0))
522  {
523  u32TimeOut--;
524  if ((UsbHost_GetDeviceStatus(pstcUsbHandle) & USBHOST_DEVICE_CONFIGURED) == 0)
525  {
526  return 0;
527  }
528  }
529  if (u32TimeOut == 0) return 0;
530 
531  c = (char_t)KEYCODE_ASCII(stcKeyboardData.u8KeyCode1);
532  if (stcKeyboardData.u8KeyCode2 != 0)
533  {
534  c = (char_t)KEYCODE_ASCII(stcKeyboardData.u8KeyCode2);
535  while((stcKeyboardData.u8KeyCode2 != 0) && (u32TimeOut > 0))
536  {
537  u32TimeOut--;
538  if ((UsbHost_GetDeviceStatus(pstcUsbHandle) & USBHOST_DEVICE_CONFIGURED) == 0)
539  {
540  return 0;
541  }
542  }
543  if (u32TimeOut == 0) return 0;
544  }
545  if ((stcKeyboardData.u8ModifierKeys & 0x22))
546  {
547  if ((c >= 'a') && (c <= 'z'))
548  {
549  c += 'A' - 'a';
550  }
551  while((stcKeyboardData.u8ModifierKeys & 0x22) && (u32TimeOut > 0))
552  {
553  u32TimeOut--;
554  if ((UsbHost_GetDeviceStatus(pstcUsbHandle) & USBHOST_DEVICE_CONFIGURED) == 0)
555  {
556  return 0;
557  }
558  }
559  if (u32TimeOut == 0) return 0;
560  }
562  return c;
563 }
564 
565 
566 
567 
568 /* NOT USABLE */
569 uint8_t OutData[4];
570 
571 void HidKeyboard_SetLeds(uint8_t u8Data)
572 {
573  stc_usb_request_t stcRequst;
574  stcRequst.bmRequestType = 0x21;
575  stcRequst.bRequest = 0x09;
576  stcRequst.wValue = 0x0200;
577  stcRequst.wIndex = 0x00;
578  stcRequst.wLength = 0x0001;
579  OutData[0] = u8Data;
580  UsbHost_SetupRequestWithData(pstcUsbHandle,&stcRequst,OutData,0);
581 }
582 
583 #endif /* (USE_USB_HOST == 1) */