Debian Codegraph @

ports/stm32/usbhost/Class/HID/Src/usbh_hid.c

raw
1 /**
2 ******************************************************************************
3 * @file usbh_hid.c
4 * @author MCD Application Team
5 * @version V3.0.0
6 * @date 18-February-2014
7 * @brief This file is the HID Layer Handlers for USB Host HID class.
8 *
9 * @verbatim
10 *
11 * ===================================================================
12 * HID Class Description
13 * ===================================================================
14 * This module manages the MSC class V1.11 following the "Device Class Definition
15 * for Human Interface Devices (HID) Version 1.11 Jun 27, 2001".
16 * This driver implements the following aspects of the specification:
17 * - The Boot Interface Subclass
18 * - The Mouse and Keyboard protocols
19 *
20 * @endverbatim
21 *
22 ******************************************************************************
23 * @attention
24 *
25 * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
26 *
27 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
28 * You may not use this file except in compliance with the License.
29 * You may obtain a copy of the License at:
30 *
31 * http://www.st.com/software_license_agreement_liberty_v2
32 *
33 * Unless required by applicable law or agreed to in writing, software
34 * distributed under the License is distributed on an "AS IS" BASIS,
35 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 * See the License for the specific language governing permissions and
37 * limitations under the License.
38 *
39 ******************************************************************************
40 */
41
42 /* Includes ------------------------------------------------------------------*/
43 #include "usbh_hid.h"
44 #include "usbh_hid_parser.h"
45
46
47 /** @addtogroup USBH_LIB
48 * @{
49 */
50
51 /** @addtogroup USBH_CLASS
52 * @{
53 */
54
55 /** @addtogroup USBH_HID_CLASS
56 * @{
57 */
58
59 /** @defgroup USBH_HID_CORE
60 * @brief This file includes HID Layer Handlers for USB Host HID class.
61 * @{
62 */
63
64 /** @defgroup USBH_HID_CORE_Private_TypesDefinitions
65 * @{
66 */
67 /**
68 * @}
69 */
70
71
72 /** @defgroup USBH_HID_CORE_Private_Defines
73 * @{
74 */
75 /**
76 * @}
77 */
78
79
80 /** @defgroup USBH_HID_CORE_Private_Macros
81 * @{
82 */
83 /**
84 * @}
85 */
86
87
88 /** @defgroup USBH_HID_CORE_Private_Variables
89 * @{
90 */
91
92 /**
93 * @}
94 */
95
96
97 /** @defgroup USBH_HID_CORE_Private_FunctionPrototypes
98 * @{
99 */
100
101 static USBH_StatusTypeDef USBH_HID_InterfaceInit (USBH_HandleTypeDef *phost);
102 static USBH_StatusTypeDef USBH_HID_InterfaceDeInit (USBH_HandleTypeDef *phost);
103 static USBH_StatusTypeDef USBH_HID_ClassRequest(USBH_HandleTypeDef *phost);
104 static USBH_StatusTypeDef USBH_HID_Process(USBH_HandleTypeDef *phost);
105 static USBH_StatusTypeDef USBH_HID_SOFProcess(USBH_HandleTypeDef *phost);
106 static void USBH_HID_ParseHIDDesc (HID_DescTypeDef *desc, uint8_t *buf);
107
108 extern USBH_StatusTypeDef USBH_HID_MouseInit(USBH_HandleTypeDef *phost);
109 extern USBH_StatusTypeDef USBH_HID_KeybdInit(USBH_HandleTypeDef *phost);
110
111 USBH_ClassTypeDef HID_Class =
112 {
113 "HID",
114 USB_HID_CLASS,
115 USBH_HID_InterfaceInit,
116 USBH_HID_InterfaceDeInit,
117 USBH_HID_ClassRequest,
118 USBH_HID_Process,
119 USBH_HID_SOFProcess,
120 NULL,
121 };
122 /**
123 * @}
124 */
125
126
127 /** @defgroup USBH_HID_CORE_Private_Functions
128 * @{
129 */
130
131
132 /**
133 * @brief USBH_HID_InterfaceInit
134 * The function init the HID class.
135 * @param phost: Host handle
136 * @retval USBH Status
137 */
138 static USBH_StatusTypeDef USBH_HID_InterfaceInit (USBH_HandleTypeDef *phost)
139 {
140 uint8_t max_ep;
141 uint8_t num = 0;
142 uint8_t interface;
143
144 USBH_StatusTypeDef status = USBH_FAIL ;
145 HID_HandleTypeDef *HID_Handle;
146
147 interface = USBH_FindInterface(phost, phost->pActiveClass->ClassCode, HID_BOOT_CODE, 0xFF);
148
149 if(interface == 0xFF) /* No Valid Interface */
150 {
151 status = USBH_FAIL;
152 USBH_DbgLog ("Cannot Find the interface for %s class.", phost->pActiveClass->Name);
153 }
154 else
155 {
156 USBH_SelectInterface (phost, interface);
157 phost->pActiveClass->pData = (HID_HandleTypeDef *)USBH_malloc (sizeof(HID_HandleTypeDef));
158 HID_Handle = phost->pActiveClass->pData;
159 HID_Handle->state = HID_ERROR;
160
161 /*Decode Bootclass Protocl: Mouse or Keyboard*/
162 if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bInterfaceProtocol == HID_KEYBRD_BOOT_CODE)
163 {
164 USBH_UsrLog ("KeyBoard device found!");
165 HID_Handle->Init = USBH_HID_KeybdInit;
166 }
167 else if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bInterfaceProtocol == HID_MOUSE_BOOT_CODE)
168 {
169 USBH_UsrLog ("Mouse device found!");
170 HID_Handle->Init = USBH_HID_MouseInit;
171 }
172 else
173 {
174 USBH_UsrLog ("Protocol not supported.");
175 return USBH_FAIL;
176 }
177
178 HID_Handle->state = HID_INIT;
179 HID_Handle->ctl_state = HID_REQ_INIT;
180 HID_Handle->ep_addr = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].bEndpointAddress;
181 HID_Handle->length = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].wMaxPacketSize;
182 HID_Handle->poll = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].bInterval ;
183
184 if (HID_Handle->poll < HID_MIN_POLL)
185 {
186 HID_Handle->poll = HID_MIN_POLL;
187 }
188
189 /* Check fo available number of endpoints */
190 /* Find the number of EPs in the Interface Descriptor */
191 /* Choose the lower number in order not to overrun the buffer allocated */
192 max_ep = ( (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bNumEndpoints <= USBH_MAX_NUM_ENDPOINTS) ?
193 phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bNumEndpoints :
194 USBH_MAX_NUM_ENDPOINTS);
195
196
197 /* Decode endpoint IN and OUT address from interface descriptor */
198 for ( ;num < max_ep; num++)
199 {
200 if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[num].bEndpointAddress & 0x80)
201 {
202 HID_Handle->InEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[num].bEndpointAddress);
203 HID_Handle->InPipe =\
204 USBH_AllocPipe(phost, HID_Handle->InEp);
205
206 /* Open pipe for IN endpoint */
207 USBH_OpenPipe (phost,
208 HID_Handle->InPipe,
209 HID_Handle->InEp,
210 phost->device.address,
211 phost->device.speed,
212 USB_EP_TYPE_INTR,
213 HID_Handle->length);
214
215 USBH_LL_SetToggle (phost, HID_Handle->InPipe, 0);
216
217 }
218 else
219 {
220 HID_Handle->OutEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[num].bEndpointAddress);
221 HID_Handle->OutPipe =\
222 USBH_AllocPipe(phost, HID_Handle->OutEp);
223
224 /* Open pipe for OUT endpoint */
225 USBH_OpenPipe (phost,
226 HID_Handle->OutPipe,
227 HID_Handle->OutEp,
228 phost->device.address,
229 phost->device.speed,
230 USB_EP_TYPE_INTR,
231 HID_Handle->length);
232
233 USBH_LL_SetToggle (phost, HID_Handle->OutPipe, 0);
234 }
235
236 }
237 status = USBH_OK;
238 }
239 return status;
240 }
241
242 /**
243 * @brief USBH_HID_InterfaceDeInit
244 * The function DeInit the Pipes used for the HID class.
245 * @param phost: Host handle
246 * @retval USBH Status
247 */
248 USBH_StatusTypeDef USBH_HID_InterfaceDeInit (USBH_HandleTypeDef *phost )
249 {
250 HID_HandleTypeDef *HID_Handle = phost->pActiveClass->pData;
251
252 if(HID_Handle->InPipe != 0x00)
253 {
254 USBH_ClosePipe (phost, HID_Handle->InPipe);
255 USBH_FreePipe (phost, HID_Handle->InPipe);
256 HID_Handle->InPipe = 0; /* Reset the pipe as Free */
257 }
258
259 if(HID_Handle->OutPipe != 0x00)
260 {
261 USBH_ClosePipe(phost, HID_Handle->OutPipe);
262 USBH_FreePipe (phost, HID_Handle->OutPipe);
263 HID_Handle->OutPipe = 0; /* Reset the pipe as Free */
264 }
265
266 if(phost->pActiveClass->pData)
267 {
268 USBH_free (phost->pActiveClass->pData);
269 }
270
271 return USBH_OK;
272 }
273
274 /**
275 * @brief USBH_HID_ClassRequest
276 * The function is responsible for handling Standard requests
277 * for HID class.
278 * @param phost: Host handle
279 * @retval USBH Status
280 */
281 static USBH_StatusTypeDef USBH_HID_ClassRequest(USBH_HandleTypeDef *phost)
282 {
283
284 USBH_StatusTypeDef status = USBH_BUSY;
285 USBH_StatusTypeDef classReqStatus = USBH_BUSY;
286 HID_HandleTypeDef *HID_Handle = phost->pActiveClass->pData;
287
288 /* Switch HID state machine */
289 switch (HID_Handle->ctl_state)
290 {
291 case HID_REQ_INIT:
292 case HID_REQ_GET_HID_DESC:
293
294 /* Get HID Desc */
295 if (USBH_HID_GetHIDDescriptor (phost, USB_HID_DESC_SIZE)== USBH_OK)
296 {
297
298 USBH_HID_ParseHIDDesc(&HID_Handle->HID_Desc, phost->device.Data);
299 HID_Handle->ctl_state = HID_REQ_GET_REPORT_DESC;
300 }
301
302 break;
303 case HID_REQ_GET_REPORT_DESC:
304
305
306 /* Get Report Desc */
307 if (USBH_HID_GetHIDReportDescriptor(phost, HID_Handle->HID_Desc.wItemLength) == USBH_OK)
308 {
309 /* The decriptor is available in phost->device.Data */
310
311 HID_Handle->ctl_state = HID_REQ_SET_IDLE;
312 }
313
314 break;
315
316 case HID_REQ_SET_IDLE:
317
318 classReqStatus = USBH_HID_SetIdle (phost, 0, 0);
319
320 /* set Idle */
321 if (classReqStatus == USBH_OK)
322 {
323 HID_Handle->ctl_state = HID_REQ_SET_PROTOCOL;
324 }
325 else if(classReqStatus == USBH_NOT_SUPPORTED)
326 {
327 HID_Handle->ctl_state = HID_REQ_SET_PROTOCOL;
328 }
329 break;
330
331 case HID_REQ_SET_PROTOCOL:
332 /* set protocol */
333 if (USBH_HID_SetProtocol (phost, 0) == USBH_OK)
334 {
335 HID_Handle->ctl_state = HID_REQ_IDLE;
336
337 /* all requests performed*/
338 phost->pUser(phost, HOST_USER_CLASS_ACTIVE);
339 status = USBH_OK;
340 }
341 break;
342
343 case HID_REQ_IDLE:
344 default:
345 break;
346 }
347
348 return status;
349 }
350
351 /**
352 * @brief USBH_HID_Process
353 * The function is for managing state machine for HID data transfers
354 * @param phost: Host handle
355 * @retval USBH Status
356 */
357 static USBH_StatusTypeDef USBH_HID_Process(USBH_HandleTypeDef *phost)
358 {
359 USBH_StatusTypeDef status = USBH_OK;
360 HID_HandleTypeDef *HID_Handle = phost->pActiveClass->pData;
361
362 switch (HID_Handle->state)
363 {
364 case HID_INIT:
365 HID_Handle->Init(phost);
366 case HID_IDLE:
367 if(USBH_HID_GetReport (phost,
368 0x01,
369 0,
370 HID_Handle->pData,
371 HID_Handle->length) == USBH_OK)
372 {
373
374 fifo_write(&HID_Handle->fifo, HID_Handle->pData, HID_Handle->length);
375 HID_Handle->state = HID_SYNC;
376 }
377
378 break;
379
380 case HID_SYNC:
381
382 /* Sync with start of Even Frame */
383 if(phost->Timer & 1)
384 {
385 HID_Handle->state = HID_GET_DATA;
386 }
387 #if (USBH_USE_OS == 1)
388 osMessagePut ( phost->os_event, USBH_URB_EVENT, 0);
389 #endif
390 break;
391
392 case HID_GET_DATA:
393
394 USBH_InterruptReceiveData(phost,
395 HID_Handle->pData,
396 HID_Handle->length,
397 HID_Handle->InPipe);
398
399 HID_Handle->state = HID_POLL;
400 HID_Handle->timer = phost->Timer;
401 HID_Handle->DataReady = 0;
402 break;
403
404 case HID_POLL:
405
406 if(USBH_LL_GetURBState(phost , HID_Handle->InPipe) == USBH_URB_DONE)
407 {
408 if(HID_Handle->DataReady == 0)
409 {
410 fifo_write(&HID_Handle->fifo, HID_Handle->pData, HID_Handle->length);
411 HID_Handle->DataReady = 1;
412 USBH_HID_EventCallback(phost);
413 #if (USBH_USE_OS == 1)
414 osMessagePut ( phost->os_event, USBH_URB_EVENT, 0);
415 #endif
416 }
417 }
418 else if(USBH_LL_GetURBState(phost , HID_Handle->InPipe) == USBH_URB_STALL) /* IN Endpoint Stalled */
419 {
420
421 /* Issue Clear Feature on interrupt IN endpoint */
422 if(USBH_ClrFeature(phost,
423 HID_Handle->ep_addr) == USBH_OK)
424 {
425 /* Change state to issue next IN token */
426 HID_Handle->state = HID_GET_DATA;
427 }
428 }
429
430
431 break;
432
433 default:
434 break;
435 }
436 return status;
437 }
438
439 /**
440 * @brief USBH_HID_SOFProcess
441 * The function is for managing the SOF Process
442 * @param phost: Host handle
443 * @retval USBH Status
444 */
445 static USBH_StatusTypeDef USBH_HID_SOFProcess(USBH_HandleTypeDef *phost)
446 {
447 HID_HandleTypeDef *HID_Handle = phost->pActiveClass->pData;
448
449 if(HID_Handle->state == HID_POLL)
450 {
451 if(( phost->Timer - HID_Handle->timer) >= HID_Handle->poll)
452 {
453 HID_Handle->state = HID_GET_DATA;
454 #if (USBH_USE_OS == 1)
455 osMessagePut ( phost->os_event, USBH_URB_EVENT, 0);
456 #endif
457 }
458 }
459 return USBH_OK;
460 }
461
462 /**
463 * @brief USBH_Get_HID_ReportDescriptor
464 * Issue report Descriptor command to the device. Once the response
465 * received, parse the report descriptor and update the status.
466 * @param phost: Host handle
467 * @param Length : HID Report Descriptor Length
468 * @retval USBH Status
469 */
470 USBH_StatusTypeDef USBH_HID_GetHIDReportDescriptor (USBH_HandleTypeDef *phost,
471 uint16_t length)
472 {
473
474 USBH_StatusTypeDef status;
475
476 status = USBH_GetDescriptor(phost,
477 USB_REQ_RECIPIENT_INTERFACE | USB_REQ_TYPE_STANDARD,
478 USB_DESC_HID_REPORT,
479 phost->device.Data,
480 length);
481
482 /* HID report descriptor is available in phost->device.Data.
483 In case of USB Boot Mode devices for In report handling ,
484 HID report descriptor parsing is not required.
485 In case, for supporting Non-Boot Protocol devices and output reports,
486 user may parse the report descriptor*/
487
488
489 return status;
490 }
491
492 /**
493 * @brief USBH_Get_HID_Descriptor
494 * Issue HID Descriptor command to the device. Once the response
495 * received, parse the report descriptor and update the status.
496 * @param phost: Host handle
497 * @param Length : HID Descriptor Length
498 * @retval USBH Status
499 */
500 USBH_StatusTypeDef USBH_HID_GetHIDDescriptor (USBH_HandleTypeDef *phost,
501 uint16_t length)
502 {
503
504 USBH_StatusTypeDef status;
505
506 status = USBH_GetDescriptor( phost,
507 USB_REQ_RECIPIENT_INTERFACE | USB_REQ_TYPE_STANDARD,
508 USB_DESC_HID,
509 phost->device.Data,
510 length);
511
512 return status;
513 }
514
515 /**
516 * @brief USBH_Set_Idle
517 * Set Idle State.
518 * @param phost: Host handle
519 * @param duration: Duration for HID Idle request
520 * @param reportId : Targetted report ID for Set Idle request
521 * @retval USBH Status
522 */
523 USBH_StatusTypeDef USBH_HID_SetIdle (USBH_HandleTypeDef *phost,
524 uint8_t duration,
525 uint8_t reportId)
526 {
527
528 phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE |\
529 USB_REQ_TYPE_CLASS;
530
531
532 phost->Control.setup.b.bRequest = USB_HID_SET_IDLE;
533 phost->Control.setup.b.wValue.w = (duration << 8 ) | reportId;
534
535 phost->Control.setup.b.wIndex.w = 0;
536 phost->Control.setup.b.wLength.w = 0;
537
538 return USBH_CtlReq(phost, 0 , 0 );
539 }
540
541
542 /**
543 * @brief USBH_HID_Set_Report
544 * Issues Set Report
545 * @param phost: Host handle
546 * @param reportType : Report type to be sent
547 * @param reportId : Targetted report ID for Set Report request
548 * @param reportBuff : Report Buffer
549 * @param reportLen : Length of data report to be send
550 * @retval USBH Status
551 */
552 USBH_StatusTypeDef USBH_HID_SetReport (USBH_HandleTypeDef *phost,
553 uint8_t reportType,
554 uint8_t reportId,
555 uint8_t* reportBuff,
556 uint8_t reportLen)
557 {
558
559 phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE |\
560 USB_REQ_TYPE_CLASS;
561
562
563 phost->Control.setup.b.bRequest = USB_HID_SET_REPORT;
564 phost->Control.setup.b.wValue.w = (reportType << 8 ) | reportId;
565
566 phost->Control.setup.b.wIndex.w = 0;
567 phost->Control.setup.b.wLength.w = reportLen;
568
569 return USBH_CtlReq(phost, reportBuff , reportLen );
570 }
571
572
573 /**
574 * @brief USBH_HID_GetReport
575 * retreive Set Report
576 * @param phost: Host handle
577 * @param reportType : Report type to be sent
578 * @param reportId : Targetted report ID for Set Report request
579 * @param reportBuff : Report Buffer
580 * @param reportLen : Length of data report to be send
581 * @retval USBH Status
582 */
583 USBH_StatusTypeDef USBH_HID_GetReport (USBH_HandleTypeDef *phost,
584 uint8_t reportType,
585 uint8_t reportId,
586 uint8_t* reportBuff,
587 uint8_t reportLen)
588 {
589
590 phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_RECIPIENT_INTERFACE |\
591 USB_REQ_TYPE_CLASS;
592
593
594 phost->Control.setup.b.bRequest = USB_HID_GET_REPORT;
595 phost->Control.setup.b.wValue.w = (reportType << 8 ) | reportId;
596
597 phost->Control.setup.b.wIndex.w = 0;
598 phost->Control.setup.b.wLength.w = reportLen;
599
600 return USBH_CtlReq(phost, reportBuff , reportLen );
601 }
602
603 /**
604 * @brief USBH_Set_Protocol
605 * Set protocol State.
606 * @param phost: Host handle
607 * @param protocol : Set Protocol for HID : boot/report protocol
608 * @retval USBH Status
609 */
610 USBH_StatusTypeDef USBH_HID_SetProtocol(USBH_HandleTypeDef *phost,
611 uint8_t protocol)
612 {
613
614
615 phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE |\
616 USB_REQ_TYPE_CLASS;
617
618
619 phost->Control.setup.b.bRequest = USB_HID_SET_PROTOCOL;
620 phost->Control.setup.b.wValue.w = protocol != 0 ? 0 : 1;
621 phost->Control.setup.b.wIndex.w = 0;
622 phost->Control.setup.b.wLength.w = 0;
623
624 return USBH_CtlReq(phost, 0 , 0 );
625
626 }
627
628 /**
629 * @brief USBH_ParseHIDDesc
630 * This function Parse the HID descriptor
631 * @param desc: HID Descriptor
632 * @param buf: Buffer where the source descriptor is available
633 * @retval None
634 */
635 static void USBH_HID_ParseHIDDesc (HID_DescTypeDef *desc, uint8_t *buf)
636 {
637
638 desc->bLength = *(uint8_t *) (buf + 0);
639 desc->bDescriptorType = *(uint8_t *) (buf + 1);
640 desc->bcdHID = LE16 (buf + 2);
641 desc->bCountryCode = *(uint8_t *) (buf + 4);
642 desc->bNumDescriptors = *(uint8_t *) (buf + 5);
643 desc->bReportDescriptorType = *(uint8_t *) (buf + 6);
644 desc->wItemLength = LE16 (buf + 7);
645 }
646
647 /**
648 * @brief USBH_HID_GetDeviceType
649 * Return Device function.
650 * @param phost: Host handle
651 * @retval HID function: HID_MOUSE / HID_KEYBOARD
652 */
653 HID_TypeTypeDef USBH_HID_GetDeviceType(USBH_HandleTypeDef *phost)
654 {
655 HID_TypeTypeDef type = HID_UNKNOWN;
656
657 if(phost->gState == HOST_CLASS)
658 {
659
660 if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bInterfaceProtocol \
661 == HID_KEYBRD_BOOT_CODE)
662 {
663 type = HID_KEYBOARD;
664 }
665 else if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bInterfaceProtocol \
666 == HID_MOUSE_BOOT_CODE)
667 {
668 type= HID_MOUSE;
669 }
670 }
671 return type;
672 }
673
674 /**
675 * @brief fifo_init
676 * Initialize FIFO.
677 * @param f: Fifo address
678 * @param buf: Fifo buffer
679 * @param size: Fifo Size
680 * @retval none
681 */
682 void fifo_init(FIFO_TypeDef * f, uint8_t * buf, uint16_t size)
683 {
684 f->head = 0;
685 f->tail = 0;
686 f->lock = 0;
687 f->size = size;
688 f->buf = buf;
689 }
690
691 /**
692 * @brief fifo_read
693 * Read from FIFO.
694 * @param f: Fifo address
695 * @param buf: read buffer
696 * @param nbytes: number of item to read
697 * @retval number of read items
698 */
699 uint16_t fifo_read(FIFO_TypeDef * f, void * buf, uint16_t nbytes)
700 {
701 uint16_t i;
702 uint8_t * p;
703 p = buf;
704
705 if(f->lock == 0)
706 {
707 f->lock = 1;
708 for(i=0; i < nbytes; i++)
709 {
710 if( f->tail != f->head )
711 {
712 *p++ = f->buf[f->tail];
713 f->tail++;
714 if( f->tail == f->size )
715 {
716 f->tail = 0;
717 }
718 } else
719 {
720 f->lock = 0;
721 return i;
722 }
723 }
724 }
725 f->lock = 0;
726 return nbytes;
727 }
728
729 /**
730 * @brief fifo_write
731 * Read from FIFO.
732 * @param f: Fifo address
733 * @param buf: read buffer
734 * @param nbytes: number of item to write
735 * @retval number of written items
736 */
737 uint16_t fifo_write(FIFO_TypeDef * f, const void * buf, uint16_t nbytes)
738 {
739 uint16_t i;
740 const uint8_t * p;
741 p = buf;
742 if(f->lock == 0)
743 {
744 f->lock = 1;
745 for(i=0; i < nbytes; i++)
746 {
747 if( (f->head + 1 == f->tail) ||
748 ( (f->head + 1 == f->size) && (f->tail == 0)) )
749 {
750 f->lock = 0;
751 return i;
752 }
753 else
754 {
755 f->buf[f->head] = *p++;
756 f->head++;
757 if( f->head == f->size )
758 {
759 f->head = 0;
760 }
761 }
762 }
763 }
764 f->lock = 0;
765 return nbytes;
766 }
767
768
769 /**
770 * @brief The function is a callback about HID Data events
771 * @param phost: Selected device
772 * @retval None
773 */
774 __weak void USBH_HID_EventCallback(USBH_HandleTypeDef *phost)
775 {
776
777 }
778 /**
779 * @}
780 */
781
782 /**
783 * @}
784 */
785
786 /**
787 * @}
788 */
789
790
791 /**
792 * @}
793 */
794
795
796 /**
797 * @}
798 */
799
800 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/