Added all the F4 libraries to the project
[fw/stlink] / exampleF4 / STM32_USB_OTG_Driver / src / usb_hcd_int.c
1 /**
2   ******************************************************************************
3   * @file    usb_hcd_int.c
4   * @author  MCD Application Team
5   * @version V2.0.0
6   * @date    22-July-2011
7   * @brief   Host driver interrupt subroutines
8   ******************************************************************************
9   * @attention
10   *
11   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
14   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17   *
18   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
19   ******************************************************************************
20   */
21
22 /* Includes ------------------------------------------------------------------*/
23 #include "usb_core.h"
24 #include "usb_defines.h"
25 #include "usb_hcd_int.h"
26
27 #if defined   (__CC_ARM) /*!< ARM Compiler */
28     #pragma O0
29 #elif defined ( __ICCARM__ ) /*!< IAR Compiler */
30     #pragma O0
31 #elif defined (__GNUC__) /*!< GNU Compiler */
32     #pragma GCC optimize ("O0")
33 #elif defined  (__TASKING__) /*!< TASKING Compiler */ 
34     #pragma optimize=0                          
35
36 #endif /* __CC_ARM */
37
38 /** @addtogroup USB_OTG_DRIVER
39   * @{
40   */
41   
42 /** @defgroup USB_HCD_INT 
43   * @brief This file contains the interrupt subroutines for the Host mode.
44   * @{
45   */
46
47
48 /** @defgroup USB_HCD_INT_Private_Defines
49   * @{
50   */ 
51 /**
52   * @}
53   */ 
54  
55
56 /** @defgroup USB_HCD_INT_Private_TypesDefinitions
57   * @{
58   */ 
59 /**
60   * @}
61   */ 
62
63
64
65 /** @defgroup USB_HCD_INT_Private_Macros
66   * @{
67   */ 
68 /**
69   * @}
70   */ 
71
72
73 /** @defgroup USB_HCD_INT_Private_Variables
74   * @{
75   */ 
76 /**
77   * @}
78   */ 
79
80
81 /** @defgroup USB_HCD_INT_Private_FunctionPrototypes
82   * @{
83   */ 
84
85 static uint32_t USB_OTG_USBH_handle_sof_ISR(USB_OTG_CORE_HANDLE *pdev);
86 static uint32_t USB_OTG_USBH_handle_port_ISR(USB_OTG_CORE_HANDLE *pdev);
87 static uint32_t USB_OTG_USBH_handle_hc_ISR (USB_OTG_CORE_HANDLE *pdev);
88 static uint32_t USB_OTG_USBH_handle_hc_n_In_ISR (USB_OTG_CORE_HANDLE *pdev ,
89                                                  uint32_t num);
90 static uint32_t USB_OTG_USBH_handle_hc_n_Out_ISR (USB_OTG_CORE_HANDLE *pdev , 
91                                                   uint32_t num);
92 static uint32_t USB_OTG_USBH_handle_rx_qlvl_ISR (USB_OTG_CORE_HANDLE *pdev);
93 static uint32_t USB_OTG_USBH_handle_nptxfempty_ISR (USB_OTG_CORE_HANDLE *pdev);
94 static uint32_t USB_OTG_USBH_handle_ptxfempty_ISR (USB_OTG_CORE_HANDLE *pdev);
95 static uint32_t USB_OTG_USBH_handle_Disconnect_ISR (USB_OTG_CORE_HANDLE *pdev);
96 static uint32_t USB_OTG_USBH_handle_IncompletePeriodicXfer_ISR (USB_OTG_CORE_HANDLE *pdev);
97
98 /**
99   * @}
100   */ 
101
102
103 /** @defgroup USB_HCD_INT_Private_Functions
104   * @{
105   */ 
106
107 /**
108   * @brief  HOST_Handle_ISR 
109   *         This function handles all USB Host Interrupts
110   * @param  pdev: Selected device
111   * @retval status 
112   */
113
114 uint32_t USBH_OTG_ISR_Handler (USB_OTG_CORE_HANDLE *pdev)
115 {
116   USB_OTG_GINTSTS_TypeDef  gintsts;
117   uint32_t retval = 0;
118   
119   gintsts.d32 = 0;
120   
121   /* Check if HOST Mode */
122   if (USB_OTG_IsHostMode(pdev))
123   {
124     gintsts.d32 = USB_OTG_ReadCoreItr(pdev);
125     if (!gintsts.d32)
126     {
127       return 0;
128     }
129     
130     if (gintsts.b.sofintr)
131     {
132       retval |= USB_OTG_USBH_handle_sof_ISR (pdev);
133     }
134     
135     if (gintsts.b.rxstsqlvl)
136     {
137       retval |= USB_OTG_USBH_handle_rx_qlvl_ISR (pdev);
138     }
139     
140     if (gintsts.b.nptxfempty)
141     {
142       retval |= USB_OTG_USBH_handle_nptxfempty_ISR (pdev);
143     }
144     
145     if (gintsts.b.ptxfempty)
146     {
147       retval |= USB_OTG_USBH_handle_ptxfempty_ISR (pdev);
148     }    
149     
150     if (gintsts.b.hcintr)
151     {
152       retval |= USB_OTG_USBH_handle_hc_ISR (pdev);
153     }
154     
155     if (gintsts.b.portintr)
156     {
157       retval |= USB_OTG_USBH_handle_port_ISR (pdev);
158     }
159     
160     if (gintsts.b.disconnect)
161     {
162       retval |= USB_OTG_USBH_handle_Disconnect_ISR (pdev);  
163       
164     }
165     
166      if (gintsts.b.incomplisoout)
167       {
168          retval |= USB_OTG_USBH_handle_IncompletePeriodicXfer_ISR (pdev);
169       }
170       
171     
172   }
173   return retval;
174 }
175
176 /**
177   * @brief  USB_OTG_USBH_handle_hc_ISR 
178   *         This function indicates that one or more host channels has a pending
179   * @param  pdev: Selected device
180   * @retval status 
181   */
182 static uint32_t USB_OTG_USBH_handle_hc_ISR (USB_OTG_CORE_HANDLE *pdev)
183 {
184   USB_OTG_HAINT_TypeDef        haint;
185   USB_OTG_HCCHAR_TypeDef       hcchar;
186   uint32_t i = 0;
187   uint32_t retval = 0;
188   
189   /* Clear appropriate bits in HCINTn to clear the interrupt bit in
190   * GINTSTS */
191   
192   haint.d32 = USB_OTG_ReadHostAllChannels_intr(pdev);
193   
194   for (i = 0; i < pdev->cfg.host_channels ; i++)
195   {
196     if (haint.b.chint & (1 << i))
197     {
198       hcchar.d32 = USB_OTG_READ_REG32(&pdev->regs.HC_REGS[i]->HCCHAR);
199       
200       if (hcchar.b.epdir)
201       {
202         retval |= USB_OTG_USBH_handle_hc_n_In_ISR (pdev, i);
203       }
204       else
205       {
206         retval |=  USB_OTG_USBH_handle_hc_n_Out_ISR (pdev, i);
207       }
208     }
209   }
210   
211   return retval;
212 }
213
214 /**
215   * @brief  USB_OTG_otg_hcd_handle_sof_intr 
216   *         Handles the start-of-frame interrupt in host mode.
217   * @param  pdev: Selected device
218   * @retval status 
219   */
220 static uint32_t USB_OTG_USBH_handle_sof_ISR (USB_OTG_CORE_HANDLE *pdev)
221 {
222   USB_OTG_GINTSTS_TypeDef      gintsts;
223   
224   
225   gintsts.d32 = 0;
226   /* Clear interrupt */
227   gintsts.b.sofintr = 1;
228   USB_OTG_WRITE_REG32(&pdev->regs.GREGS->GINTSTS, gintsts.d32);
229   
230   return 1;
231 }
232
233 /**
234   * @brief  USB_OTG_USBH_handle_Disconnect_ISR 
235   *         Handles disconnect event.
236   * @param  pdev: Selected device
237   * @retval status 
238   */
239 static uint32_t USB_OTG_USBH_handle_Disconnect_ISR (USB_OTG_CORE_HANDLE *pdev)
240 {
241   USB_OTG_GINTSTS_TypeDef      gintsts;
242   
243   pdev->host.ConnSts = 0;
244   gintsts.d32 = 0;
245   
246   pdev->host.port_cb->Disconnect(pdev);
247   
248   /* Clear interrupt */
249   gintsts.b.disconnect = 1;
250   USB_OTG_WRITE_REG32(&pdev->regs.GREGS->GINTSTS, gintsts.d32);
251   
252   return 1;
253 }
254
255 /**
256   * @brief  USB_OTG_USBH_handle_nptxfempty_ISR 
257   *         Handles non periodic tx fifo empty.
258   * @param  pdev: Selected device
259   * @retval status 
260   */
261 static uint32_t USB_OTG_USBH_handle_nptxfempty_ISR (USB_OTG_CORE_HANDLE *pdev)
262 {
263   USB_OTG_GINTMSK_TypeDef      intmsk;
264   USB_OTG_HNPTXSTS_TypeDef     hnptxsts; 
265   uint16_t                     len_words , len; 
266   
267   hnptxsts.d32 = USB_OTG_READ_REG32(&pdev->regs.GREGS->HNPTXSTS);
268   
269   len_words = (pdev->host.hc[hnptxsts.b.chnum].xfer_len + 3) / 4;
270   
271   while ((hnptxsts.b.nptxfspcavail > len_words)&&
272       (pdev->host.hc[hnptxsts.b.chnum].xfer_len != 0))
273   {
274     
275     len = hnptxsts.b.nptxfspcavail * 4;
276     
277     if (len > pdev->host.hc[hnptxsts.b.chnum].xfer_len)
278     {
279       /* Last packet */
280       len = pdev->host.hc[hnptxsts.b.chnum].xfer_len;
281       
282       intmsk.d32 = 0;
283       intmsk.b.nptxfempty = 1;
284       USB_OTG_MODIFY_REG32( &pdev->regs.GREGS->GINTMSK, intmsk.d32, 0);       
285     }
286     
287     len_words = (pdev->host.hc[hnptxsts.b.chnum].xfer_len + 3) / 4;
288     
289     USB_OTG_WritePacket (pdev , pdev->host.hc[hnptxsts.b.chnum].xfer_buff, hnptxsts.b.chnum, len);
290     
291     pdev->host.hc[hnptxsts.b.chnum].xfer_buff  += len;
292     pdev->host.hc[hnptxsts.b.chnum].xfer_len   -= len;
293     pdev->host.hc[hnptxsts.b.chnum].xfer_count  += len; 
294         
295     hnptxsts.d32 = USB_OTG_READ_REG32(&pdev->regs.GREGS->HNPTXSTS);
296   }  
297
298   return 1;
299 }
300
301 /**
302   * @brief  USB_OTG_USBH_handle_ptxfempty_ISR 
303   *         Handles periodic tx fifo empty
304   * @param  pdev: Selected device
305   * @retval status 
306   */
307 static uint32_t USB_OTG_USBH_handle_ptxfempty_ISR (USB_OTG_CORE_HANDLE *pdev)
308 {
309   USB_OTG_GINTMSK_TypeDef      intmsk;
310   USB_OTG_HPTXSTS_TypeDef      hptxsts; 
311   uint16_t                     len_words , len; 
312   
313   hptxsts.d32 = USB_OTG_READ_REG32(&pdev->regs.HREGS->HPTXSTS);
314   
315   len_words = (pdev->host.hc[hptxsts.b.chnum].xfer_len + 3) / 4;
316   
317   while ((hptxsts.b.ptxfspcavail > len_words)&&
318       (pdev->host.hc[hptxsts.b.chnum].xfer_len != 0))    
319   {
320     
321     len = hptxsts.b.ptxfspcavail * 4;
322     
323     if (len > pdev->host.hc[hptxsts.b.chnum].xfer_len)
324     {
325       len = pdev->host.hc[hptxsts.b.chnum].xfer_len;
326       /* Last packet */
327       intmsk.d32 = 0;
328       intmsk.b.ptxfempty = 1;
329       USB_OTG_MODIFY_REG32( &pdev->regs.GREGS->GINTMSK, intmsk.d32, 0); 
330     }
331     
332     len_words = (pdev->host.hc[hptxsts.b.chnum].xfer_len + 3) / 4;
333     
334     USB_OTG_WritePacket (pdev , pdev->host.hc[hptxsts.b.chnum].xfer_buff, hptxsts.b.chnum, len);
335     
336     pdev->host.hc[hptxsts.b.chnum].xfer_buff  += len;
337     pdev->host.hc[hptxsts.b.chnum].xfer_len   -= len;
338     pdev->host.hc[hptxsts.b.chnum].xfer_count  += len; 
339      
340     hptxsts.d32 = USB_OTG_READ_REG32(&pdev->regs.HREGS->HPTXSTS);
341   }  
342   
343   return 1;
344 }
345
346 /**
347   * @brief  USB_OTG_USBH_handle_port_ISR 
348   *         This function determines which interrupt conditions have occurred
349   * @param  pdev: Selected device
350   * @retval status 
351   */
352 static uint32_t USB_OTG_USBH_handle_port_ISR (USB_OTG_CORE_HANDLE *pdev)
353 {
354   USB_OTG_HPRT0_TypeDef  hprt0;
355   USB_OTG_HPRT0_TypeDef  hprt0_dup;
356   USB_OTG_HCFG_TypeDef   hcfg;    
357   uint32_t do_reset = 0;
358   uint32_t retval = 0;
359   
360   hcfg.d32 = 0;
361   hprt0.d32 = 0;
362   hprt0_dup.d32 = 0;
363   
364   hprt0.d32 = USB_OTG_READ_REG32(pdev->regs.HPRT0);
365   hprt0_dup.d32 = USB_OTG_READ_REG32(pdev->regs.HPRT0);
366   
367   /* Clear the interrupt bits in GINTSTS */
368   
369   hprt0_dup.b.prtena = 0;
370   hprt0_dup.b.prtconndet = 0;
371   hprt0_dup.b.prtenchng = 0;
372   hprt0_dup.b.prtovrcurrchng = 0;
373   
374   /* Port Connect Detected */
375   if (hprt0.b.prtconndet)
376   {
377     pdev->host.port_cb->Connect(pdev);
378     hprt0_dup.b.prtconndet = 1;
379     do_reset = 1;
380     retval |= 1;
381   }
382   
383   /* Port Enable Changed */
384   if (hprt0.b.prtenchng)
385   {
386     hprt0_dup.b.prtenchng = 1;
387     if (hprt0.b.prtena == 1)
388     {
389       pdev->host.ConnSts = 1;
390       
391       if ((hprt0.b.prtspd == HPRT0_PRTSPD_LOW_SPEED) ||
392           (hprt0.b.prtspd == HPRT0_PRTSPD_FULL_SPEED))
393       {
394         
395         hcfg.d32 = USB_OTG_READ_REG32(&pdev->regs.HREGS->HCFG);
396         
397         if (hprt0.b.prtspd == HPRT0_PRTSPD_LOW_SPEED)
398         {
399           USB_OTG_WRITE_REG32(&pdev->regs.HREGS->HFIR, 6000 );
400           if (hcfg.b.fslspclksel != HCFG_6_MHZ)
401           {
402             if(pdev->cfg.coreID == USB_OTG_FS_CORE_ID)
403             {          
404               USB_OTG_InitFSLSPClkSel(pdev ,HCFG_6_MHZ );
405             }          
406             do_reset = 1;
407           }
408         }
409         else
410         {
411           
412           USB_OTG_WRITE_REG32(&pdev->regs.HREGS->HFIR, 48000 );            
413           if (hcfg.b.fslspclksel != HCFG_48_MHZ)
414           {
415             USB_OTG_InitFSLSPClkSel(pdev ,HCFG_48_MHZ );
416             do_reset = 1;
417           }
418         }
419       }
420       else
421       {
422         do_reset = 1;
423       }
424     }
425   }
426   /* Overcurrent Change Interrupt */
427   if (hprt0.b.prtovrcurrchng)
428   {
429     hprt0_dup.b.prtovrcurrchng = 1;
430     retval |= 1;
431   }
432   if (do_reset)
433   {
434     USB_OTG_ResetPort(pdev);
435     
436   }
437   /* Clear Port Interrupts */
438   USB_OTG_WRITE_REG32(pdev->regs.HPRT0, hprt0_dup.d32);
439   
440   return retval;
441 }
442
443 /**
444   * @brief  USB_OTG_USBH_handle_hc_n_Out_ISR 
445   *         Handles interrupt for a specific Host Channel
446   * @param  pdev: Selected device
447   * @param  hc_num: Channel number
448   * @retval status 
449   */
450 uint32_t USB_OTG_USBH_handle_hc_n_Out_ISR (USB_OTG_CORE_HANDLE *pdev , uint32_t num)
451 {
452   
453   USB_OTG_HCINTn_TypeDef     hcint;
454   USB_OTG_HCGINTMSK_TypeDef  hcintmsk;
455   USB_OTG_HC_REGS *hcreg;
456   USB_OTG_HCCHAR_TypeDef     hcchar; 
457   
458   hcreg = pdev->regs.HC_REGS[num];
459   hcint.d32 = USB_OTG_READ_REG32(&hcreg->HCINT);
460   hcintmsk.d32 = USB_OTG_READ_REG32(&hcreg->HCGINTMSK);
461   hcint.d32 = hcint.d32 & hcintmsk.d32;
462   
463   hcchar.d32 = USB_OTG_READ_REG32(&pdev->regs.HC_REGS[num]->HCCHAR);
464   
465   if (hcint.b.ahberr)
466   {
467     CLEAR_HC_INT(hcreg ,ahberr);
468     UNMASK_HOST_INT_CHH (num);
469   } 
470   else if (hcint.b.ack)
471   {
472     CLEAR_HC_INT(hcreg , ack);
473   }
474   
475   else if (hcint.b.xfercompl)
476   {
477     pdev->host.ErrCnt[num] = 0;
478     UNMASK_HOST_INT_CHH (num);
479     USB_OTG_HC_Halt(pdev, num);
480     CLEAR_HC_INT(hcreg , xfercompl);
481     pdev->host.HC_Status[num] = HC_XFRC;            
482   }
483   
484   else if (hcint.b.stall)
485   {
486     CLEAR_HC_INT(hcreg , stall);
487     UNMASK_HOST_INT_CHH (num);
488     USB_OTG_HC_Halt(pdev, num);
489     pdev->host.HC_Status[num] = HC_STALL;      
490   }
491   
492   else if (hcint.b.nak)
493   {
494     pdev->host.ErrCnt[num] = 0;
495     UNMASK_HOST_INT_CHH (num);
496     USB_OTG_HC_Halt(pdev, num);
497     CLEAR_HC_INT(hcreg , nak);
498     pdev->host.HC_Status[num] = HC_NAK;      
499   }
500   
501   else if (hcint.b.xacterr)
502   {
503     UNMASK_HOST_INT_CHH (num);
504     USB_OTG_HC_Halt(pdev, num);
505     pdev->host.ErrCnt[num] ++;
506     pdev->host.HC_Status[num] = HC_XACTERR;
507     CLEAR_HC_INT(hcreg , xacterr);
508   }
509   else if (hcint.b.nyet)
510   {
511     pdev->host.ErrCnt[num] = 0;
512     UNMASK_HOST_INT_CHH (num);
513     USB_OTG_HC_Halt(pdev, num);
514     CLEAR_HC_INT(hcreg , nyet);
515     pdev->host.HC_Status[num] = HC_NYET;    
516   }
517   else if (hcint.b.datatglerr)
518   {
519    
520     UNMASK_HOST_INT_CHH (num);
521     USB_OTG_HC_Halt(pdev, num);
522     CLEAR_HC_INT(hcreg , nak);   
523     pdev->host.HC_Status[num] = HC_DATATGLERR;
524     
525     CLEAR_HC_INT(hcreg , datatglerr);
526   }  
527   else if (hcint.b.chhltd)
528   {
529     MASK_HOST_INT_CHH (num);
530     
531     if(pdev->host.HC_Status[num] == HC_XFRC)
532     {
533       pdev->host.URB_State[num] = URB_DONE;  
534       
535       if (hcchar.b.eptype == EP_TYPE_BULK)
536       {
537         pdev->host.hc[num].toggle_out ^= 1; 
538       }
539     }
540     else if(pdev->host.HC_Status[num] == HC_NAK)
541     {
542       pdev->host.URB_State[num] = URB_NOTREADY;      
543     }    
544     else if(pdev->host.HC_Status[num] == HC_NYET)
545     {
546       if(pdev->host.hc[num].do_ping == 1)
547       {
548         USB_OTG_HC_DoPing(pdev, num);
549       }
550       pdev->host.URB_State[num] = URB_NOTREADY;      
551     }      
552     else if(pdev->host.HC_Status[num] == HC_STALL)
553     {
554       pdev->host.URB_State[num] = URB_STALL;      
555     }  
556     else if(pdev->host.HC_Status[num] == HC_XACTERR)
557     {
558       if (pdev->host.ErrCnt[num] == 3)
559       {
560         pdev->host.URB_State[num] = URB_ERROR;  
561         pdev->host.ErrCnt[num] = 0;
562       }
563     }
564     CLEAR_HC_INT(hcreg , chhltd);    
565   }
566   
567
568   return 1;
569 }
570
571 /**
572   * @brief  USB_OTG_USBH_handle_hc_n_In_ISR 
573   *         Handles interrupt for a specific Host Channel
574   * @param  pdev: Selected device
575   * @param  hc_num: Channel number
576   * @retval status 
577   */
578 uint32_t USB_OTG_USBH_handle_hc_n_In_ISR (USB_OTG_CORE_HANDLE *pdev , uint32_t num)
579 {
580   USB_OTG_HCINTn_TypeDef     hcint;
581   USB_OTG_HCGINTMSK_TypeDef  hcintmsk;
582   USB_OTG_HCCHAR_TypeDef     hcchar; 
583   USB_OTG_HCTSIZn_TypeDef  hctsiz;
584   USB_OTG_HC_REGS *hcreg;
585
586   
587   hcreg = pdev->regs.HC_REGS[num];
588   hcint.d32 = USB_OTG_READ_REG32(&hcreg->HCINT);
589   hcintmsk.d32 = USB_OTG_READ_REG32(&hcreg->HCGINTMSK);
590   hcint.d32 = hcint.d32 & hcintmsk.d32;
591   hcchar.d32 = USB_OTG_READ_REG32(&pdev->regs.HC_REGS[num]->HCCHAR);
592   hcintmsk.d32 = 0;
593   
594   
595   if (hcint.b.ahberr)
596   {
597     CLEAR_HC_INT(hcreg ,ahberr);
598     UNMASK_HOST_INT_CHH (num);
599   }  
600   else if (hcint.b.ack)
601   {
602     CLEAR_HC_INT(hcreg ,ack);
603   }
604   
605   else if (hcint.b.stall)  
606   {
607     UNMASK_HOST_INT_CHH (num);
608     pdev->host.HC_Status[num] = HC_STALL; 
609     CLEAR_HC_INT(hcreg , nak);   /* Clear the NAK Condition */
610     CLEAR_HC_INT(hcreg , stall); /* Clear the STALL Condition */
611     hcint.b.nak = 0;           /* NOTE: When there is a 'stall', reset also nak, 
612                                   else, the pdev->host.HC_Status = HC_STALL
613                                   will be overwritten by 'nak' in code below */
614     USB_OTG_HC_Halt(pdev, num);    
615   }
616   else if (hcint.b.datatglerr)
617   {
618
619       UNMASK_HOST_INT_CHH (num);
620       USB_OTG_HC_Halt(pdev, num);
621       CLEAR_HC_INT(hcreg , nak);   
622       pdev->host.HC_Status[num] = HC_DATATGLERR; 
623     CLEAR_HC_INT(hcreg , datatglerr);
624   }    
625   
626   if (hcint.b.frmovrun)
627   {
628     UNMASK_HOST_INT_CHH (num);
629     USB_OTG_HC_Halt(pdev, num);
630     CLEAR_HC_INT(hcreg ,frmovrun);
631   }
632   
633   else if (hcint.b.xfercompl)
634   {
635     
636     if (pdev->cfg.dma_enable == 1)
637     {
638       hctsiz.d32 = USB_OTG_READ_REG32(&pdev->regs.HC_REGS[num]->HCTSIZ);
639       pdev->host.XferCnt[num] =  pdev->host.hc[num].xfer_len - hctsiz.b.xfersize;
640     }
641  
642     pdev->host.HC_Status[num] = HC_XFRC;     
643     pdev->host.ErrCnt [num]= 0;
644     CLEAR_HC_INT(hcreg , xfercompl);
645     
646     if ((hcchar.b.eptype == EP_TYPE_CTRL)||
647         (hcchar.b.eptype == EP_TYPE_BULK))
648     {
649       UNMASK_HOST_INT_CHH (num);
650       USB_OTG_HC_Halt(pdev, num);
651       CLEAR_HC_INT(hcreg , nak); 
652       pdev->host.hc[num].toggle_in ^= 1;
653
654     }
655     else if(hcchar.b.eptype == EP_TYPE_INTR)
656     {
657       hcchar.b.oddfrm  = 1;
658       USB_OTG_WRITE_REG32(&pdev->regs.HC_REGS[num]->HCCHAR, hcchar.d32); 
659       pdev->host.URB_State[num] = URB_DONE;  
660     }
661     
662   }
663   else if (hcint.b.chhltd)
664   {
665     MASK_HOST_INT_CHH (num);
666     
667     if(pdev->host.HC_Status[num] == HC_XFRC)
668     {
669       pdev->host.URB_State[num] = URB_DONE;      
670     }
671     
672     else if (pdev->host.HC_Status[num] == HC_STALL) 
673     {
674        pdev->host.URB_State[num] = URB_STALL;
675     }   
676     
677     else if((pdev->host.HC_Status[num] == HC_XACTERR) ||
678             (pdev->host.HC_Status[num] == HC_DATATGLERR))
679     {
680         pdev->host.ErrCnt[num] = 0;
681         pdev->host.URB_State[num] = URB_ERROR;  
682
683     }
684     else if(hcchar.b.eptype == EP_TYPE_INTR)
685     {
686       pdev->host.hc[num].toggle_in ^= 1;
687     }
688     
689     CLEAR_HC_INT(hcreg , chhltd);    
690     
691   }    
692   else if (hcint.b.xacterr)
693   {
694     UNMASK_HOST_INT_CHH (num);
695     pdev->host.ErrCnt[num] ++;
696     pdev->host.HC_Status[num] = HC_XACTERR;
697     USB_OTG_HC_Halt(pdev, num);
698     CLEAR_HC_INT(hcreg , xacterr);    
699     
700   }
701   else if (hcint.b.nak)  
702   {  
703     if(hcchar.b.eptype == EP_TYPE_INTR)
704     {
705       UNMASK_HOST_INT_CHH (num);
706       USB_OTG_HC_Halt(pdev, num);
707       CLEAR_HC_INT(hcreg , nak);   
708     }
709      else if  ((hcchar.b.eptype == EP_TYPE_CTRL)||
710              (hcchar.b.eptype == EP_TYPE_BULK))
711     {
712       /* re-activate the channel  */
713       hcchar.b.chen = 1;
714       hcchar.b.chdis = 0;
715       USB_OTG_WRITE_REG32(&pdev->regs.HC_REGS[num]->HCCHAR, hcchar.d32); 
716     }
717     pdev->host.HC_Status[num] = HC_NAK;
718   }
719
720   
721   return 1;
722   
723 }
724
725 /**
726   * @brief  USB_OTG_USBH_handle_rx_qlvl_ISR 
727   *         Handles the Rx Status Queue Level Interrupt
728   * @param  pdev: Selected device
729   * @retval status 
730   */
731
732 static uint32_t USB_OTG_USBH_handle_rx_qlvl_ISR (USB_OTG_CORE_HANDLE *pdev)
733 {
734   USB_OTG_GRXFSTS_TypeDef       grxsts;
735   USB_OTG_GINTMSK_TypeDef       intmsk;
736   USB_OTG_HCTSIZn_TypeDef       hctsiz; 
737   USB_OTG_HCCHAR_TypeDef        hcchar;
738   __IO uint8_t                  channelnum =0;  
739   uint32_t                      count;    
740   
741   /* Disable the Rx Status Queue Level interrupt */
742   intmsk.d32 = 0;
743   intmsk.b.rxstsqlvl = 1;
744   USB_OTG_MODIFY_REG32( &pdev->regs.GREGS->GINTMSK, intmsk.d32, 0);
745   
746   grxsts.d32 = USB_OTG_READ_REG32(&pdev->regs.GREGS->GRXSTSP);
747   channelnum = grxsts.b.chnum;  
748   hcchar.d32 = USB_OTG_READ_REG32(&pdev->regs.HC_REGS[channelnum]->HCCHAR);
749   
750   switch (grxsts.b.pktsts)
751   {
752   case GRXSTS_PKTSTS_IN:
753     /* Read the data into the host buffer. */
754     if ((grxsts.b.bcnt > 0) && (pdev->host.hc[channelnum].xfer_buff != (void  *)0))
755     {  
756       
757       USB_OTG_ReadPacket(pdev, pdev->host.hc[channelnum].xfer_buff, grxsts.b.bcnt);
758       /*manage multiple Xfer */
759       pdev->host.hc[grxsts.b.chnum].xfer_buff += grxsts.b.bcnt;           
760       pdev->host.hc[grxsts.b.chnum].xfer_count  += grxsts.b.bcnt;
761       
762      
763       count = pdev->host.hc[channelnum].xfer_count;
764       pdev->host.XferCnt[channelnum]  = count;
765       
766       hctsiz.d32 = USB_OTG_READ_REG32(&pdev->regs.HC_REGS[channelnum]->HCTSIZ);
767       if(hctsiz.b.pktcnt > 0)
768       {
769         /* re-activate the channel when more packets are expected */
770         hcchar.b.chen = 1;
771         hcchar.b.chdis = 0;
772         USB_OTG_WRITE_REG32(&pdev->regs.HC_REGS[channelnum]->HCCHAR, hcchar.d32);
773       }
774     }
775     break;
776     
777     case GRXSTS_PKTSTS_IN_XFER_COMP:
778    
779     case GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
780   case GRXSTS_PKTSTS_CH_HALTED:
781   default:
782     break;
783   }
784   
785   /* Enable the Rx Status Queue Level interrupt */
786   intmsk.b.rxstsqlvl = 1;
787   USB_OTG_MODIFY_REG32(&pdev->regs.GREGS->GINTMSK, 0, intmsk.d32);
788   return 1;
789 }
790
791 /**
792   * @brief  USB_OTG_USBH_handle_IncompletePeriodicXfer_ISR 
793   *         Handles the incomplete Periodic transfer Interrupt
794   * @param  pdev: Selected device
795   * @retval status 
796   */
797 static uint32_t USB_OTG_USBH_handle_IncompletePeriodicXfer_ISR (USB_OTG_CORE_HANDLE *pdev)
798 {
799   
800     USB_OTG_GINTSTS_TypeDef       gintsts;
801     USB_OTG_HCCHAR_TypeDef        hcchar; 
802    
803     
804
805     
806     hcchar.d32 = USB_OTG_READ_REG32(&pdev->regs.HC_REGS[0]->HCCHAR);
807     hcchar.b.chen = 1;
808     hcchar.b.chdis = 1;
809     USB_OTG_WRITE_REG32(&pdev->regs.HC_REGS[0]->HCCHAR, hcchar.d32);  
810     
811     gintsts.d32 = 0;
812     /* Clear interrupt */
813     gintsts.b.incomplisoout = 1;
814     USB_OTG_WRITE_REG32(&pdev->regs.GREGS->GINTSTS, gintsts.d32);
815
816     return 1;
817 }
818
819 /**
820 * @}
821 */ 
822
823 /**
824 * @}
825 */ 
826
827 /**
828 * @}
829 */
830
831 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
832