PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
UsbDeviceHidKeyboard.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 /******************************************************************************/
54 /*****************************************************************************/
55 /* Include files */
56 /*****************************************************************************/
57 #include "UsbDeviceHidKeyboard.h"
58 
59 #if ((FM_PERIPHERAL_USB_DEVICE_ENABLED == ON) && (USBDEVICEHIDKEYBOARD_ENABLED == ON))
60 /*****************************************************************************/
61 /* Local pre-processor symbols/macros ('#define') */
62 /*****************************************************************************/
63 
64 static boolean_t SendData(uint8_t* pu8Buffer, uint32_t u32DataSize, en_usbsend_mode_t enMode);
65 
66 /*****************************************************************************/
67 /* Global variable definitions (declared in header file with 'extern') */
68 /*****************************************************************************/
69 
70 /*****************************************************************************/
71 /* Local type definitions ('typedef') */
72 /*****************************************************************************/
73 
74 
75 
76 /*****************************************************************************/
77 /* Local variable definitions ('static') */
78 /*****************************************************************************/
79 const uint8_t u8AsciiToKeyCode[] = {
80 0, // 0 =
81 0, // 1 =
82 0, // 2 =
83 0, // 3 =
84 0, // 4 =
85 0, // 5 =
86 0, // 6 =
87 0, // 7 =
88 0, // 8 =
89 0, // 9 =
90 0x28, // 10 = \r
91 0, // 11 =
92 0, // 12 =
93 0x58, // 13 = \n
94 0, // 14 =
95 0, // 15 =
96 0, // 16 =
97 0, // 17 =
98 0, // 18 =
99 0, // 19 =
100 0, // 20 =
101 0, // 21 =
102 0, // 22 =
103 0, // 23 =
104 0, // 24 =
105 0, // 25 =
106 0, // 26 =
107 29, // 27 = ESC
108 0, // 28 =
109 0, // 29 =
110 0, // 30 =
111 0, // 31 =
112 44, // 32 = SPACE
113 0, // 33 = !
114 0, // 34 = "
115 0, // 35 = #
116 0, // 36 = $
117 0, // 37 = %
118 0, // 38 = &
119 0, // 39 = '
120 0, // 40 = (
121 0, // 41 = )
122 0, // 42 = *
123 0, // 43 = +
124 0, // 44 = ,
125 0, // 45 = -
126 0x37, // 46 = .
127 0, // 47 = /
128 39, // 48 = 0
129 30, // 49 = 1
130 31, // 50 = 2
131 32, // 51 = 3
132 33, // 52 = 4
133 34, // 53 = 5
134 35, // 54 = 6
135 36, // 55 = 7
136 37, // 56 = 8
137 38, // 57 = 9
138 0, // 58 = :
139 0, // 59 = ;
140 0, // 60 = <
141 0, // 61 = =
142 0, // 62 = >
143 0, // 63 = ?
144 0, // 64 = @
145 0, // 65 = A
146 0, // 66 = B
147 0, // 67 = C
148 0, // 68 = D
149 0, // 69 = E
150 0, // 70 = F
151 0, // 71 = G
152 0, // 72 = H
153 0, // 73 = I
154 0, // 74 = J
155 0, // 75 = K
156 0, // 76 = L
157 0, // 77 = M
158 0, // 78 = N
159 0, // 79 = O
160 0, // 80 = P
161 0, // 81 = Q
162 0, // 82 = R
163 0, // 83 = S
164 0, // 84 = T
165 0, // 85 = U
166 0, // 86 = V
167 0, // 87 = W
168 0, // 88 = X
169 0, // 89 = Y
170 0, // 90 = Z
171 0, // 91 = [
172 0, // 92 =
173 0, // 93 = ]
174 0, // 94 = ^
175 0, // 95 = _
176 0, // 96 = `
177 4, // 97 = a
178 5, // 98 = b
179 6, // 99 = c
180 7, // 100 = d
181 8, // 101 = e
182 9, // 102 = f
183 10, // 103 = g
184 11, // 104 = h
185 12, // 105 = i
186 13, // 106 = j
187 14, // 107 = k
188 15, // 108 = l
189 16, // 109 = m
190 17, // 110 = n
191 18, // 111 = o
192 19, // 112 = p
193 20, // 113 = q
194 21, // 114 = r
195 22, // 115 = s
196 23, // 116 = t
197 24, // 117 = u
198 25, // 118 = v
199 26, // 119 = w
200 27, // 120 = x
201 28, // 121 = y
202 29, // 122 = z
203 0, // 123 = {
204 0, // 124 = |
205 0, // 125 = }
206 0, // 126 = ~
207 0, // 127 = 
208 0, // 128 = ?
209 0, // 129 = ?
210 0, // 130 = ?
211 0, // 131 = ?
212 0, // 132 = ?
213 0, // 133 = ?
214 0, // 134 = ?
215 0, // 135 = ?
216 0, // 136 = ?
217 0, // 137 = ?
218 0, // 138 = ?
219 0, // 139 = ?
220 0, // 140 = ?
221 0, // 141 = ?
222 0, // 142 = ?
223 0, // 143 = ?
224 0, // 144 = ?
225 0, // 145 = ?
226 0, // 146 = ?
227 0, // 147 = ?
228 0, // 148 = ?
229 0, // 149 = ?
230 0, // 150 = ?
231 0, // 151 = ?
232 0, // 152 = ?
233 0, // 153 = ?
234 0, // 154 = ?
235 0, // 155 = ?
236 0, // 156 = ?
237 0, // 157 = ?
238 0, // 158 = ?
239 0, // 159 = ?
240 0, // 160 = ?
241 0, // 161 = ?
242 0, // 162 = ?
243 0, // 163 = ?
244 0, // 164 = ?
245 0, // 165 = ?
246 0, // 166 = ?
247 0, // 167 = ?
248 0, // 168 = ?
249 0, // 169 = ?
250 0, // 170 = ?
251 0, // 171 = ?
252 0, // 172 = ?
253 0, // 173 = ?
254 0, // 174 = ?
255 0, // 175 = ?
256 0, // 176 = ?
257 0, // 177 = ?
258 0, // 178 = ?
259 0, // 179 = ?
260 0, // 180 = ?
261 0, // 181 = ?
262 0, // 182 = ?
263 0, // 183 = ?
264 0, // 184 = ?
265 0, // 185 = ?
266 0, // 186 = ?
267 0, // 187 = ?
268 0, // 188 = ?
269 0, // 189 = ?
270 0, // 190 = ?
271 0, // 191 = ?
272 0, // 192 = ?
273 0, // 193 = ?
274 0, // 194 = ?
275 0, // 195 = ?
276 0, // 196 = ?
277 0, // 197 = ?
278 0, // 198 = ?
279 0, // 199 = ?
280 0, // 200 = ?
281 0, // 201 = ?
282 0, // 202 = ?
283 0, // 203 = ?
284 0, // 204 = ?
285 0, // 205 = ?
286 0, // 206 = ?
287 0, // 207 = ?
288 0, // 208 = ?
289 0, // 209 = ?
290 0, // 210 = ?
291 0, // 211 = ?
292 0, // 212 = ?
293 0, // 213 = ?
294 0, // 214 = ?
295 0, // 215 = ?
296 0, // 216 = ?
297 0, // 217 = ?
298 0, // 218 = ?
299 0, // 219 = ?
300 0, // 220 = ?
301 0, // 221 = ?
302 0, // 222 = ?
303 0, // 223 = ?
304 0, // 224 = ?
305 0, // 225 = ?
306 0, // 226 = ?
307 0, // 227 = ?
308 0, // 228 = ?
309 0, // 229 = ?
310 0, // 230 = ?
311 0, // 231 = ?
312 0, // 232 = ?
313 0, // 233 = ?
314 0, // 234 = ?
315 0, // 235 = ?
316 0, // 236 = ?
317 0, // 237 = ?
318 0, // 238 = ?
319 0, // 239 = ?
320 0, // 240 = ?
321 0, // 241 = ?
322 0, // 242 = ?
323 0, // 243 = ?
324 0, // 244 = ?
325 0, // 245 = ?
326 0, // 246 = ?
327 0, // 247 = ?
328 0, // 248 = ?
329 0, // 249 = ?
330 0, // 250 = ?
331 0, // 251 = ?
332 0, // 252 = ?
333 0, // 253 = ?
334 0, // 254 = ?
335 0, // 255 = ?
336 };
337 static uint8_t DataSentFlags = 0; //Status for sent data
338 static uint8_t pu8KeyboardData[8] = {0,0,0,0,0,0,0,0};
341 static stc_usbn_t* pstcUsbHandle = NULL;
343 /*****************************************************************************/
344 /* Local function prototypes ('static') */
345 /*****************************************************************************/
346 
347 
358 static void ConfCallback(stc_usbn_t* pstcUsb)
359 {
360  if (UsbDevice_GetStatus(pstcUsb) == UsbConfigured)
361  {
362  bReady = TRUE;
363  } else
364  {
365  bReady = FALSE;
366  }
367 }
368 
369 /*****************************************************************************/
370 /* Function implementation - global ('extern') and local ('static') */
371 /*****************************************************************************/
372 
373 void UsbDeviceHidKeyboard_Init(stc_usbn_t* pstcUsb)
374 {
375  stc_usbdevice_class_config_t stcUsbClassConfig;
376  stc_usbdevice_endpoint_config_t stcEndpointConfig;
377  uint8_t* pu8Interface = NULL;
378  uint8_t u8InterfaceLength = 0;
379  uint8_t u8i = 0;
380  uint8_t u8NumEndpoints = 0;
381  pstcUsbHandle = pstcUsb;
382 
383  stcUsbClassConfig.u8InterfaceNumber = 0xFF; // do not use fix interface number, choose by class, subclass, protocol
384  stcUsbClassConfig.u8InterfaceClass = 0x03; // HID Class
385  stcUsbClassConfig.u8InterfaceSubClass = 0x01; // Custom Sub Class
386  stcUsbClassConfig.u8InterfaceProtocoll = 0x01; // Custom Protocol
387  stcUsbClassConfig.pfnSetupRequestCallback = NULL; // setup requests handled
388  stcUsbClassConfig.pfnConnectionCallback = NULL; // No connection callback handled
389  stcUsbClassConfig.pfnDisconnectionCallback = NULL; // No disconnection callback handled
390  stcUsbClassConfig.pfnConfCallback = ConfCallback; // Callback for configuration set
391  UsbDevice_RegisterVendorClass(pstcUsb,&stcUsbClassConfig,&pu8Interface,&u8InterfaceLength);
392 
393  for(u8i = 0;u8i < u8InterfaceLength;)
394  {
395  u8i += pu8Interface[u8i];
396  if (USBDESCR_ENDPOINT == pu8Interface[u8i + 1])
397  {
398  stcEndpointConfig.u8EndpointAddress = pu8Interface[u8i + 2];
399  if (((stcEndpointConfig.u8EndpointAddress) & 0x80) == 0)
400  {
401  /* NOT USED */
402  }
403  else
404  {
405  stcEndpointConfig.pstcEndpointBuffer = &stcEndpointBufferIN;
406  pstcEndpointIN = UsbDevice_SetupEndpoint(pstcUsb, &stcEndpointConfig);
407  }
408  u8NumEndpoints++;
409  }
410  }
411 
412 }
413 
424 static boolean_t SendData(uint8_t* pu8Buffer, uint32_t u32DataSize, en_usbsend_mode_t enMode)
425 {
427  {
428  DataSentFlags &= ~(1<<1);
429 
430  UsbDevice_SendData(pstcUsbHandle, pstcEndpointIN, pu8Buffer, u32DataSize, enMode);
431  if (enMode == UsbPOLL)
432  {
433  DataSentFlags |= (1<<1);
434  }
435  return TRUE;
436  }
437  return FALSE;
438 }
439 
440 
457 void UsbDeviceHidKeyboard_SetModifierKey(uint8_t u8ModifierKey)
458 {
459  pu8KeyboardData[0] |= u8ModifierKey;
460  SendData((uint8_t*)pu8KeyboardData,8,UsbPOLL);
461 }
478 void UsbDeviceHidKeyboard_ClearModifierKey(uint8_t u8ModifierKey)
479 {
480  pu8KeyboardData[0] &= ~u8ModifierKey;
481  SendData((uint8_t*)pu8KeyboardData,8,UsbPOLL);
482 }
483 
493 void UsbDeviceHidKeyboard_KeyPress(uint8_t u8Index, uint8_t u8KeyCode)
494 {
495  pu8KeyboardData[u8Index + 2] = u8KeyCode;
496  SendData((uint8_t*)pu8KeyboardData,8,UsbPOLL);
497  pu8KeyboardData[u8Index + 2] = 0;
498  SendData((uint8_t*)pu8KeyboardData,8,UsbPOLL);
499 }
500 
510 void UsbDeviceHidKeyboard_KeyDown(uint8_t u8Index, uint8_t u8KeyCode)
511 {
512  pu8KeyboardData[u8Index + 2] = u8KeyCode;
513  SendData((uint8_t*)pu8KeyboardData,8,UsbPOLL);
514 }
515 
525 void UsbDeviceHidKeyboard_KeyUp(uint8_t u8Index, uint8_t u8KeyCode)
526 {
527  pu8KeyboardData[u8Index + 2] = u8KeyCode;
528  SendData((uint8_t*)pu8KeyboardData,8,UsbPOLL);
529 }
530 
531 
540 {
541  return bReady;
542 }
543 #endif