PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
usbhost.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 /******************************************************************************/
61 /******************************************************************************/
62 /* Include files */
63 /******************************************************************************/
64 
65 
66 #include "usbhost.h"
67 
68 #if (FM_PERIPHERAL_USB_HOST_ENABLED == ON)
69 
75 
76 #include "usbhostclasssupervisor.h"
77 
78 #if (USE_USBHOSTHW_H == 1)
79 #include "usbhosthw.h"
80 #endif
81 
82 
83 const stc_usb_request_t stcDeviceDescriptorRequest = {0x80,0x06,0x0100,0x00,18};
84 const stc_usb_request_t stcShortDeviceDescriptorRequest = {0x80,0x06,0x0100,0x00,8};
85 const stc_usb_request_t stcClearStallEp0 = {0x02,0x01,0x0000,0x00,0x00};
86 
87 
88 static stc_usbhost_intern_data_t* UsbHostGetInternDataPtr(stc_usbn_t* pstcUsb) ;
89 static void EnumerationStateMachine(stc_usbn_t* pstcUsb);
90 static void ConvertRequestToByteArray(uint8_t* pu8Buffer, stc_usb_request_t* pstcSetup);
91 static void StallCompletionEp0(stc_usbn_t* pstcUsb, uint8_t* pu8Buffer, uint32_t u32Size);
92 static void UsbHost_SetupComplete(stc_usbn_t* pstcUsb);
93 
94 
95 
97 #define USBHOST_INSTANCE_COUNT (uint32_t)(sizeof(m_astcUsbHostInstanceDataLut) / sizeof(m_astcUsbHostInstanceDataLut[0]))
98 
99 /******************************************************************************/
100 /* Global variable definitions (declared in header file with 'extern') */
101 /******************************************************************************/
102 
104 stc_usbhostn_instance_data_t m_astcUsbHostInstanceDataLut[] =
105 {
106  #if ((FM_PERIPHERAL_ENABLE_USB0 == ON) && (defined(USB0)) && (FM_PERIPHERAL_ENABLE_USB0_HOST == ON))
107  { &USB0, // pstcInstance
108  NULL // stcInternData (not initialized yet)
109  },
110  #endif
111  #if ((FM_PERIPHERAL_ENABLE_USB1 == ON) && (defined(USB1)) && (FM_PERIPHERAL_ENABLE_USB1_HOST == ON))
112  { &USB1, // pstcInstance
113  NULL // stcInternData (not initialized yet)
114  }
115  #endif
116 };
117 
118 
119 
129 static stc_usbhost_intern_data_t* UsbHostGetInternDataPtr(stc_usbn_t* pstcUsb)
130 {
131  volatile uint32_t u32Instance;
132 
133  for (u32Instance = 0; u32Instance < USBHOST_INSTANCE_COUNT; u32Instance++)
134  {
135  if ((uint32_t)pstcUsb == (uint32_t)(m_astcUsbHostInstanceDataLut[u32Instance].pstcInstance))
136  {
137  return &m_astcUsbHostInstanceDataLut[u32Instance].stcInternData;
138  }
139  }
140 
141  return NULL;
142 }
143 
144 
164 uint8_t UsbHost_AddHostEndpoint(stc_usbn_t* pstcUsb, stc_host_endpoint_t** pstcHandler, uint8_t u8EndpointAddress, uint16_t u16FifoSize, uint8_t u8Interval, void(* CompletionHandler)(stc_usbn_t* pstcUsb))
165 {
166  uint8_t i;
167  stc_usbhost_intern_data_t* pstcUsbHostIntern;
168  volatile boolean_t bOldSchedulerLockState;
169  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
170  bOldSchedulerLockState = pstcUsbHostIntern->bSchedulerLock;
171  pstcUsbHostIntern->bSchedulerLock = TRUE;
172  pstcUsbHostIntern->bSchedulerLock = TRUE;
173  pstcUsbHostIntern->bSchedulerLock = TRUE;
174  for(i=0;i<MAX_HOST_ENDPOINTS;i++)
175  {
176  if ((pstcUsbHostIntern->astcHostEndpoints[i].u16FifoSize == 0) || ((&pstcUsbHostIntern->astcHostEndpoints[i] == *pstcHandler) && (*pstcHandler != 0)))
177  {
178  if ((&pstcUsbHostIntern->astcHostEndpoints[i] == *pstcHandler) && (*pstcHandler != 0))
179  {
180  USBDBG(" |->Endpoint exists in list...\n");
181  }
182  USBDBGVAL8(" |->New EP, Address: ",u8EndpointAddress);
183  pstcUsbHostIntern->astcHostEndpoints[i].u8Stage = 0;
184  pstcUsbHostIntern->astcHostEndpoints[i].u8Status = 0;
185  pstcUsbHostIntern->astcHostEndpoints[i].u16FifoSize = u16FifoSize;
186  pstcUsbHostIntern->astcHostEndpoints[i].u8Address = u8EndpointAddress;
187  pstcUsbHostIntern->astcHostEndpoints[i].u8Interval = u8Interval;
188  pstcUsbHostIntern->astcHostEndpoints[i].u8IntervalCount = u8Interval;
189  pstcUsbHostIntern->astcHostEndpoints[i].CompletionHandler = CompletionHandler;
190  *pstcHandler = &pstcUsbHostIntern->astcHostEndpoints[i];
191  USBDBGVAL8(" |->New EP, Index: ",i);
192  USBDBGVAL32(" |->New EP, Handler: ",*pstcHandler);
193  pstcUsbHostIntern->u8EndpointListEnd = i;
194  pstcUsbHostIntern->bSchedulerLock = bOldSchedulerLockState;
195  pstcUsbHostIntern->bSchedulerLock = bOldSchedulerLockState;
196  pstcUsbHostIntern->bSchedulerLock = bOldSchedulerLockState;
197  return USBHOST_SUCCESS;
198  }
199  }
200  pstcUsbHostIntern->bSchedulerLock = bOldSchedulerLockState;
201  pstcUsbHostIntern->bSchedulerLock = bOldSchedulerLockState;
202  pstcUsbHostIntern->bSchedulerLock = bOldSchedulerLockState;
203  return USBHOST_ERROR;
204 }
205 
217 stc_host_endpoint_t* UsbHost_EndpointFromNumber(stc_usbn_t* pstcUsb, uint8_t u8EndpointNumber)
218 {
219  uint8_t i;
220  stc_usbhost_intern_data_t* pstcUsbHostIntern;
221  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
222 
223  for(i=0;i<=pstcUsbHostIntern->u8EndpointListEnd;i++)
224  {
225  if ((pstcUsbHostIntern->astcHostEndpoints[i].u8Address & 0x0F) == u8EndpointNumber)
226  {
227  return &pstcUsbHostIntern->astcHostEndpoints[i];
228  }
229  }
230  return NULL;
231 }
232 
240 void UsbHost_AbortTransfer(stc_host_endpoint_t* pstcEndpoint)
241 {
242  if (pstcEndpoint == NULL)
243  {
244  return;
245  }
246  pstcEndpoint->bAbortTransfer = TRUE;
247 }
248 
249 
257 void UsbHost_ResetToggle(stc_host_endpoint_t* pstcEndpoint)
258 {
259  if (pstcEndpoint == NULL)
260  {
261  return;
262  }
263  pstcEndpoint->bToggle = FALSE;
264 }
265 
277 void UsbHost_ClearEndpointCompletion(stc_usbn_t* pstcUsb, uint8_t* pu8Buffer, uint32_t u32DataSize)
278 {
279  stc_usbhost_intern_data_t* pstcUsbHostIntern;
280  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
281  if (pstcUsbHostIntern->pstcClearEndpoint == NULL)
282  {
283  return;
284  }
285  UsbHost_ResetToggle(pstcUsbHostIntern->pstcClearEndpoint);
286  pstcUsbHostIntern->pstcClearEndpoint->bAbortTransfer = FALSE;
287  pstcUsbHostIntern->pstcClearEndpoint = NULL;
288 }
289 
299 void UsbHost_ClearEndpoint(stc_usbn_t* pstcUsb, stc_host_endpoint_t* pstcEndpoint)
300 {
302  stc_usbhost_intern_data_t* pstcUsbHostIntern;
303  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
304  ZERO_STRUCT(setup);
305  setup.bmRequestType=0x02;
306  setup.bRequest=0x01;
307  setup.wValue=0x00;
308  setup.wIndex=pstcEndpoint->u8Address;
309  setup.wLength=0x00;
310  UsbHost_AbortTransfer(pstcEndpoint);
311  pstcUsbHostIntern->pstcClearEndpoint = pstcEndpoint;
312  UsbHost_SetupRequest(pstcUsb,&setup,UsbHost_ClearEndpointCompletion);
313  return;
314 }
324 void UsbHost_ClearEndpointPoll(stc_usbn_t* pstcUsb, stc_host_endpoint_t* pstcEndpoint)
325 {
326  volatile uint32_t u32Timeout = 1000000;
327  stc_usbhost_intern_data_t* pstcUsbHostIntern;
328  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
329  UsbHost_ClearEndpoint(pstcUsb, pstcEndpoint);
330  while((pstcUsbHostIntern->pstcClearEndpoint != NULL) && (u32Timeout > 0)) u32Timeout--;
331 }
332 
341 static void EnumerationStateMachine(stc_usbn_t* pstcUsb)
342 {
343  uint8_t* pu8SetupBuffer;
344  uint32_t u32Size = UsbHost_ReceiveSetupData(pstcUsb, &pu8SetupBuffer);
345  stc_usbhost_intern_data_t* pstcUsbHostIntern;
346  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
347  pstcUsbHostIntern->Ep0IN->CompletionHandler = 0;
348 
349  USBDBG("ENUM STATEMACH\n");
350  if (pu8SetupBuffer != 0)
351  {
352  if (u32Size > 0)
353  {
354  if (pu8SetupBuffer[1] == 0x02)
355  {
356  USBDBG("Received Configuration\n");
357  }
358  if (pu8SetupBuffer[1] == 0x01)
359  {
360  USBDBG("Enumeration Process:\n");
361  USBDBG(" ->Received Device Descriptor\n");
362  USBDBGVAL16(" ->New EP0 buffersize: ",(uint16_t)((uint8_t)pu8SetupBuffer[7]));
363  pstcUsbHostIntern->Ep0IN->u16FifoSize = (uint16_t)((uint8_t)pu8SetupBuffer[7]);
364  pstcUsbHostIntern->Ep0OUT->u16FifoSize = (uint16_t)((uint8_t)pu8SetupBuffer[7]);
365  }
366  }
367  else
368  {
369  if (pstcUsbHostIntern->u8DeviceStatus == USBHOST_DEVICE_ADDRESSING)
370  {
371  pstcUsbHostIntern->u8EnumerationCounter = 0;
372  pstcUsb->HADR = pstcUsbHostIntern->u8DeviceAddress;
373  USBDBG("Enumeration Process:\n");
374  USBDBGVAL8(" ->Addressed: ",pstcUsbHostIntern->u8DeviceAddress);
375  pstcUsbHostIntern->u8DeviceStatus = USBHOST_DEVICE_ADDRESSED;
376  //UsbClassSupervisor_Init(pstcUsb);
377  }
378  }
379  }
380  else
381  {
382  if (pstcUsbHostIntern->Ep0IN->u16FifoSize == 20)
383  {
384  USBDBG("Enumeration Process:\n");
385  USBDBG(" -> Endpoint 0 has no Fifo size specified.\n");
386  USBDBG(" Requesting Device Descriptor.\n");
387  UsbHost_SetupRequest(pstcUsb,(stc_usb_request_t *)&stcShortDeviceDescriptorRequest,0);
388  }
389  else
390  {
391  if (pstcUsbHostIntern->u8DeviceStatus == USBHOST_DEVICE_IDLE)
392  {
393  USBDBG("Enumeration Process:\n");
394  pstcUsbHostIntern->Ep0IN->CompletionHandler = EnumerationStateMachine;
395  pstcUsbHostIntern->u8DeviceStatus = USBHOST_DEVICE_ADDRESSING;
396  UsbHost_AddressDevice(pstcUsb, 2);
397  pstcUsbHostIntern->u8EnumerationCounter = 0;
398  }
399  }
400  }
401  if (pstcUsbHostIntern->u8DeviceStatus != USBHOST_DEVICE_ADDRESSED)
402  {
403  pstcUsbHostIntern->u8EnumerationCounter++;
404  if (pstcUsbHostIntern->u8EnumerationCounter == 40)
405  {
406  pstcUsbHostIntern->u32ConnectionDelay -= 1000;
407  if (pstcUsbHostIntern->u32ConnectionDelay <= 1000)
408  {
409  pstcUsbHostIntern->u32ConnectionDelay = USBHOST_CONNECTION_DELAY;
410  }
411  pstcUsbHostIntern->u8EnumerationCounter = 0;
412  //UsbHost_DeInit();
413  //UsbHost_Init();
414  }
415 
416  if (pstcUsbHostIntern->u8EnumerationCounter > 50)
417  {
418  /*stc_usb_request_t stcSetup;
419  pstcUsbHostIntern->u8DeviceStatus = USBHOST_DEVICE_IDLE;
420  USBDBG("Enumeration timed out...\n");
421  pstcUsbHostIntern->u8EnumerationCounter = 0;
422  USBDBG("ENUMERATION FAILED - SYSTEM HALT\n");
423  stcSetup.bmRequestType=0x82;
424  stcSetup.bRequest=0x00;
425  stcSetup.wValue=0x00;
426  stcSetup.wIndex=0x00;
427  stcSetup.wLength=0x02;
428  UsbHost_SetupRequest(pstcUsb, (stc_usb_request_t *)&stcShortDeviceDescriptorRequest,EndpointInStatusUpdate);*/
429  }
430  else
431  {
432  Usb_AddTimeOut(pstcUsb, EnumerationStateMachine,100);
433  }
434  }
435  else
436  {
437  UsbClassSupervisor_Init(pstcUsb);
438  }
439 }
440 
450 static void ConvertRequestToByteArray(uint8_t* pu8Buffer, stc_usb_request_t* pstcSetup)
451 {
452  pu8Buffer[0] = pstcSetup->bmRequestType;
453  pu8Buffer[1] = pstcSetup->bRequest;
454  pu8Buffer[2] = (uint8_t)(pstcSetup->wValue & 0xFF);
455  pu8Buffer[3] = (uint8_t)((pstcSetup->wValue >> 8) & 0xFF);
456  pu8Buffer[4] = (uint8_t)(pstcSetup->wIndex & 0xFF);
457  pu8Buffer[5] = (uint8_t)((pstcSetup->wIndex >> 8) & 0xFF);
458  pu8Buffer[6] = (uint8_t)(pstcSetup->wLength & 0xFF);
459  pu8Buffer[7] = (uint8_t)((pstcSetup->wLength >> 8) & 0xFF);
460 }
461 
471 void UsbHost_AddressDevice(stc_usbn_t* pstcUsb, uint8_t u8NewAddress)
472 {
473  stc_usb_request_t stcSetup = {0x00,0x05,0x02,0x00,0x00};
474  stc_usbhost_intern_data_t* pstcUsbHostIntern;
475  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
476 
477  USBDBGVAL8("Start Addressing: ",u8NewAddress);
478  stcSetup.wValue = u8NewAddress;
479  pstcUsbHostIntern->u8DeviceAddress = u8NewAddress; // Device Address = NewAddress
480  pstcUsbHostIntern->u8DeviceStatus = USBHOST_DEVICE_ADDRESSING;
481  UsbHost_SetupRequest(pstcUsb, &stcSetup,NULL);
482 }
483 
495 uint32_t UsbHost_ReceiveSetupData(stc_usbn_t* pstcUsb, uint8_t** pu8Buffer)
496 {
497  uint32_t u32Size;
498  stc_usbhost_intern_data_t* pstcUsbHostIntern;
499  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
500 
501  if (pstcUsbHostIntern->Ep0IN->u8Status & USBHOST_ENDPOINTSTATUS_DATA)
502  {
503  pstcUsbHostIntern->Ep0IN->u8Status &= ~USBHOST_ENDPOINTSTATUS_DATA;
504  *pu8Buffer = pstcUsbHostIntern->Ep0IN->pu8Buffer;
505  u32Size = (pstcUsbHostIntern->Ep0IN->u32DataSize);
506  return u32Size;
507  }
508  *pu8Buffer = 0;
509  return 0;
510 }
511 
525 void UsbHost_SetupRequestWithData(stc_usbn_t* pstcUsb, stc_usb_request_t* pstcSetup, uint8_t* pu8Data, void(*SetupCompletion)(stc_usbn_t* pstcUsb, uint8_t* pu8Buffer, uint32_t u32DataSize))
526 {
527  stc_usbhost_intern_data_t* pstcUsbHostIntern;
528  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
529  pstcUsbHostIntern->pu8SetupData = pu8Data;
530  UsbHost_SetupRequest(pstcUsb, pstcSetup,SetupCompletion);
531 }
532 
544 void UsbHost_SetupRequest(stc_usbn_t* pstcUsb, stc_usb_request_t* pstcSetup, void(*SetupCompletion)(stc_usbn_t* pstcUsb, uint8_t* pu8Buffer, uint32_t u32DataSize))
545 {
546  stc_usbhost_intern_data_t* pstcUsbHostIntern;
547  volatile boolean_t bOldSchedulerLockState;
548  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
549  bOldSchedulerLockState = pstcUsbHostIntern->bSchedulerLock;
550  if ((pstcUsbHostIntern->Ep0IN->u8Status & USBHOST_ENDPOINTSTATUS_INUSE) || (pstcUsbHostIntern->Ep0OUT->u8Status & USBHOST_ENDPOINTSTATUS_INUSE))
551  {
552  return;
553  }
554  pstcUsbHostIntern->bSchedulerLock = TRUE;
555  pstcUsbHostIntern->SetupCompletionHandler = SetupCompletion;
556  if ((((pstcUsbHostIntern->Ep0IN->u8Status) & USBHOST_ENDPOINTSTATUS_STALL) == 0) && (((pstcUsbHostIntern->Ep0OUT->u8Status) & USBHOST_ENDPOINTSTATUS_STALL) == 0))
557  {
558  memcpy((uint8_t*)&pstcUsbHostIntern->stcLastSetup,(uint8_t*)pstcSetup,sizeof(pstcUsbHostIntern->stcLastSetup));
559  }
560  ConvertRequestToByteArray(pstcUsbHostIntern->pu8Setup,pstcSetup);
561  USBDBGVAL8("Setup [0]",pstcUsbHostIntern->pu8Setup[0]);
562  USBDBGVAL8("[1]",pstcUsbHostIntern->pu8Setup[1]);
563  USBDBGVAL8("[2]",pstcUsbHostIntern->pu8Setup[2]);
564  USBDBGVAL8("[3]",pstcUsbHostIntern->pu8Setup[3]);
565  USBDBGVAL8("[4]",pstcUsbHostIntern->pu8Setup[4]);
566  USBDBGVAL8("[5]",pstcUsbHostIntern->pu8Setup[5]);
567  USBDBGVAL8("[6]",pstcUsbHostIntern->pu8Setup[6]);
568  USBDBGVAL8("[7]",pstcUsbHostIntern->pu8Setup[7]);
569 
570  //UsbHost_TransferDataToFifo(pstcUsb, pstcUsbHostIntern->pu8Setup,8,pstcUsbHostIntern->Ep0OUT->u16FifoSize);
571 
572  pstcUsbHostIntern->Ep0OUT->u8Stage = USBHOST_STAGE_SETUP;
573  pstcUsbHostIntern->Ep0IN->u8Stage = USBHOST_STAGE_SETUP;
574 
575  pstcUsbHostIntern->Ep0IN->u8Status &= ~USBHOST_ENDPOINTSTATUS_INUSE;
576  pstcUsbHostIntern->Ep0OUT->u8Status &= ~USBHOST_ENDPOINTSTATUS_INUSE;
577 
578  pstcUsbHostIntern->Ep0IN->pu8BufferPos = pstcUsbHostIntern->Ep0IN->pu8Buffer;
579  pstcUsbHostIntern->Ep0IN->pu8BufferPosNextPackage = pstcUsbHostIntern->Ep0IN->pu8Buffer;
580  pstcUsbHostIntern->Ep0IN->u8Status &= ~USBHOST_ENDPOINTSTATUS_DATA;
581  pstcUsbHostIntern->Ep0OUT->bToggle = FALSE;
582  pstcUsbHostIntern->Ep0OUT->u32DataSize = 8;
583  pstcUsbHostIntern->Ep0OUT->pu8Buffer = pstcUsbHostIntern->pu8Setup;
584  pstcUsbHostIntern->Ep0OUT->pu8BufferPos = pstcUsbHostIntern->pu8Setup;
585  pstcUsbHostIntern->Ep0OUT->u32BufferSize = 8;
586  pstcUsbHostIntern->Ep0IN->CompletionHandler = UsbHost_SetupComplete;
587  pstcUsbHostIntern->Ep0OUT->CompletionHandler = UsbHost_SetupComplete;
588 
589 
590  //pstcUsbHostIntern->Ep0OUT->u8Status &= ~USBHOST_ENDPOINTSTATUS_INITIATE;
591  //pstcUsbHostIntern->Ep0OUT->u8Status |= USBHOST_ENDPOINTSTATUS_INUSE;
592  //pstcUsbHostIntern->Ep0IN->u8Status &= ~USBHOST_ENDPOINTSTATUS_INITIATE;
593  pstcUsbHostIntern->Ep0OUT->u8Status |= USBHOST_ENDPOINTSTATUS_INITIATE;
594  //pstcUsbHostIntern->u8SetupDelay = 3;
595  //pstcUsbHostIntern->ActiveEndpoint=pstcUsbHostIntern->Ep0OUT;
596  //UsbHost_TransferData(Ep0OUT,u8Setup,8,SetupComplete);
597 
598  // synchronous token (sent after next SOF)
599 
600  //pstcUsbHostIntern->Ep0OUT->u8LastToken = CREATE_TOKEN(pstcUsbHostIntern->Ep0OUT->bToggle,HTOKEN_SETUP,pstcUsbHostIntern->Ep0OUT->u8Address & 0x0F);
601  //pstcUsbHostIntern->ActiveEndpoint = pstcUsbHostIntern->Ep0OUT;
602  //pstcUsbHostIntern->u8LastToken = pstcUsbHostIntern->Ep0OUT->u8LastToken;
603  //pstcUsbHostIntern->u8SyncToken = pstcUsbHostIntern->Ep0OUT->u8LastToken;
604 
605  pstcUsbHostIntern->bSchedulerLock = bOldSchedulerLockState;
606 }
607 
619 static void StallCompletionEp0(stc_usbn_t* pstcUsb, uint8_t* pu8Buffer, uint32_t u32Size)
620 {
621  stc_usbhost_intern_data_t* pstcUsbHostIntern;
622  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
623  USBDBG("Resending last Setup\n");
624  UsbHost_SetupRequest(pstcUsb, &pstcUsbHostIntern->stcLastSetupBeforeStall, pstcUsbHostIntern->SetupCompletionHandler);
625 
626 }
627 
635 static void UsbHost_SetupComplete(stc_usbn_t* pstcUsb)
636 {
637  stc_usbhost_intern_data_t* pstcUsbHostIntern;
638  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
639  if (pstcUsbHostIntern->SetupCompletionHandler != 0)
640  {
641  pstcUsbHostIntern->SetupCompletionHandler(pstcUsb,(pstcUsbHostIntern->Ep0IN->pu8Buffer),(pstcUsbHostIntern->Ep0IN->u32DataSize));
642  }
643 }
644 
652 void ConnectionCallback(stc_usbn_t* pstcUsb)
653 {
654  stc_usbhost_intern_data_t* pstcUsbHostIntern;
655  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
656  if (pstcUsbHostIntern->u32ConTimeout == 0)
657  {
658  if ((pstcUsb->HIRQ_f.URIRQ == 0) || (pstcUsbHostIntern->bBusResetSend == FALSE))
659  {
660  pstcUsb->UDCC_f.RST = 1;
661  if (pstcUsb->HSTATE_f.TMODE == 1) /* TMODE : Full-Speed */
662  {
663  USBDBG("Full-Speed Device\n");
664  pstcUsb->HSTATE_f.ALIVE = 0;
665  pstcUsb->HSTATE_f.CLKSEL = 1;
666  pstcUsbHostIntern->bFullSpeed = TRUE;
667  }
668  else /* Low-Speed */
669  {
670  USBDBG("Low-Speed Device\n");
671  pstcUsbHostIntern->bFullSpeed = FALSE;
672  pstcUsb->HSTATE_f.ALIVE = 1;
673  pstcUsb->HSTATE_f.CLKSEL = 0;
674  }
675  pstcUsb->UDCC_f.RST = 0;
676  }
677  USBDBG("\n### Device Connection - wait...\n");
678  if (pstcUsb->HSTATE_f.TMODE == 1) /* TMODE : Full-Speed */
679  {
680  pstcUsbHostIntern->u32ConTimeout = pstcUsbHostIntern->u32ConnectionDelay;
681  }
682  else
683  {
684  pstcUsbHostIntern->u32ConTimeout = pstcUsbHostIntern->u32ConnectionDelay;
685  }
686  return;
687  }
688  if (pstcUsbHostIntern->u32ConTimeout > 1)
689  {
690  pstcUsbHostIntern->u32ConTimeout = pstcUsbHostIntern->u32ConTimeout - 1;
691  return;
692  }
693  USBDBG("\n### Device Connection...\n");
694  pstcUsb->HIRQ_f.CNNIRQ = 0;
695  if ((pstcUsb->HIRQ_f.URIRQ == 0) || (pstcUsbHostIntern->bBusResetSend == FALSE))
696  {
697  pstcUsbHostIntern->bBusResetSend = TRUE;
698  if (pstcUsb->HSTATE_f.CSTAT == 1)
699  {
700  USBDBG(">> Exec Bus Reset...\n");
701 
702  pstcUsb->HADR = 0; // Device Address = 0
703 
704  pstcUsb->EP1S_f.BFINI = 1;
705  pstcUsb->EP1S_f.BFINI = 0;
706  pstcUsb->EP2S_f.BFINI = 1;
707  pstcUsb->EP2S_f.BFINI = 0;
708 
709  pstcUsb->HCNT_f.URST = 1; // initiate bus reset
710  }
711  }
712  else
713  {
714  USBDBG(" |->Bus Resetted\n");
715  pstcUsb->HIRQ_f.URIRQ = 0;
716  pstcUsbHostIntern->u32WaitForCompletionTimeout = 0;
717  UsbHost_AddHostEndpoint(pstcUsb,&pstcUsbHostIntern->Ep0IN ,USBHOST_EPADDR0_IN,20,0,0);
718  UsbHost_AddHostEndpoint(pstcUsb,&pstcUsbHostIntern->Ep0OUT,USBHOST_EPADDR0_OUT,20,0,0);
719  pstcUsbHostIntern->Ep0IN->u32BufferSize = 255;
720  pstcUsbHostIntern->Ep0OUT->u32BufferSize = 255;
721 
722  pstcUsbHostIntern->Ep0IN->pu8Buffer = pstcUsbHostIntern->pu8GlobalBuffer;
723  pstcUsbHostIntern->Ep0OUT->pu8Buffer = pstcUsbHostIntern->pu8GlobalBuffer;
724 
725  USBDBG(">> Starting SOF\n");
726  pstcUsb->HFRAME = 0;
727  pstcUsb->HEOF = 0x2c9; // Set the time where token are allowed in a frame
728 
729  pstcUsb->HCNT_f.SOFSTEP = 1;
730 
731  pstcUsbHostIntern->Ep0OUT->bToggle = FALSE;
732 
733 
734  pstcUsb->HTOKEN = CREATE_TOKEN(0,HTOKEN_SOF,0);
735  /*SET_TOKEN_NOW(,HTOKEN_SOF);*/
736 
737  pstcUsbHostIntern->u8EnumerationCounter = 0;
738  Usb_AddTimeOut(pstcUsb, EnumerationStateMachine,500);
739  }
740 }
741 
749 void UsbHost_ClearAllEndpoints(stc_usbn_t* pstcUsb)
750 {
751  uint8_t i;
752  stc_usbhost_intern_data_t* pstcUsbHostIntern;
753  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
754  pstcUsbHostIntern->u8EndpointListEnd = 0;
755  pstcUsbHostIntern->u8CurrentEndpointPosition = 2;
756  for(i=0;i<MAX_HOST_ENDPOINTS;i++)
757  {
758  memset((uint8_t*)&pstcUsbHostIntern->astcHostEndpoints[i],0,sizeof(pstcUsbHostIntern->astcHostEndpoints[i]));
759  }
760 }
761 
769 void UsbHost_DisconnectionCallback(stc_usbn_t* pstcUsb)
770 {
771  stc_usbhost_intern_data_t* pstcUsbHostIntern;
772  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
773  USBDBG("\n### Device Disconnection...\n");
774  pstcUsbHostIntern->u8DeviceStatus = USBHOST_DEVICE_IDLE;
775  pstcUsbHostIntern->u32BusResetTimeout = 0;
776  pstcUsbHostIntern->u32ConnectionDelay = USBHOST_CONNECTION_DELAY;
777  pstcUsbHostIntern->u32ConTimeout = 0;
778  pstcUsbHostIntern->bBusResetSend = FALSE;
779  UsbClassSupervisor_Deinit(pstcUsb);
780  UsbHost_ClearAllEndpoints(pstcUsb);
781  pstcUsbHostIntern->bSofToken = FALSE;
782  pstcUsbHostIntern->bSchedulerLock = FALSE;
783  pstcUsbHostIntern->bBusResetSend = FALSE;
784  pstcUsbHostIntern->u8DeviceAddress = 0;
785  pstcUsbHostIntern->u8EnumerationCounter = 0;
786 }
787 
788 
796 void UsbHost_BusresetCallback(stc_usbn_t* pstcUsb)
797 {
798 
799 }
800 
810 void UsbHost_InitCallback(stc_usbn_t* pstcUsb,struct stc_usb_config* pstcConfig)
811 {
812  stc_usbhost_intern_data_t* pstcUsbHostIntern;
813  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
814  pstcUsbHostIntern->bSofToken = FALSE;
815  pstcUsbHostIntern->u32ConTimeout = 0;
816  pstcUsbHostIntern->u32BusResetTimeout = 0;
817  pstcUsbHostIntern->u8CurrentEndpointPosition = 2;
818  pstcUsbHostIntern->u8EndpointListEnd = 0;
819  pstcUsbHostIntern->bSchedulerLock = FALSE;
820  pstcUsbHostIntern->bBusResetSend = FALSE;
821  //pstcUsbHostIntern->SetupCompletionHandler = 0;
822  pstcUsbHostIntern->u8DeviceStatus = USBHOST_DEVICE_IDLE;
823  pstcUsbHostIntern->u8DeviceAddress = 0;
824  pstcUsbHostIntern->u8EnumerationCounter = 0;
825  pstcConfig->pfnCnnirqCallback = ConnectionCallback;
827  pstcConfig->pfnCmpirqCallback = UsbHost_CompletionCallback;
828  pstcConfig->pfnSofCallback = UsbHost_SofCallback;
829  pstcUsbHostIntern->u32ConnectionDelay = USBHOST_CONNECTION_DELAY;
830  pstcUsbHostIntern->pstcUsbHalEndpointIN = Usb_GetEndpointPtr(pstcUsb,0x81);
831  pstcUsbHostIntern->pstcUsbHalEndpointOUT = Usb_GetEndpointPtr(pstcUsb,0x02);
832 
833 }
834 
848 void UsbHost_TransferDataToFifo(stc_usbn_t* pstcUsb, uint8_t* pu8Buffer, uint16_t u16Size, uint16_t u16FifoSize)
849 {
850  volatile uint32_t u32Timeout;
851  boolean_t Odd = u16Size & 1;
852  //stc_usbhost_intern_data_t* pstcUsbHostIntern;
853  //pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
854  USBDBGVAL8("TX: ",u16Size);
855  USBDBGVAL32("#",pu8Buffer);
856  u32Timeout = 10000;
857  while((pstcUsb->EP2S_f.BUSY == 1) && (u32Timeout > 0))
858  {
859  u32Timeout--;
860  }
861  pstcUsb->EP2S_f.BFINI=1;
862  pstcUsb->EP2S_f.BFINI=0;
863  pstcUsb->EP2S = (pstcUsb->EP2S & (~0x1F)) | u16FifoSize;
864 
865  u16Size = u16Size / 2; // transfer 16 Bit words;
866  while(u16Size--)
867  {
868  //EP2DT = *(uint16_t*)pu8Buffer;
869  pstcUsb->EP2DTL = *pu8Buffer++;
870  pstcUsb->EP2DTH = *pu8Buffer++;
871  //pu8Buffer += 2;
872  }
873  if(Odd == TRUE) {
874  pstcUsb->EP2DTL= *(uint8_t*)pu8Buffer; // transfer the first or last byte
875  }
876  pstcUsb->EP2S_f.DRQ = 0; // now the OUT FIFO is valid for the next transfer
877 }
878 
892 uint16_t UsbHost_TransferFifoToBuffer(stc_usbn_t* pstcUsb, uint8_t* pu8Buffer, uint32_t u32MaxBufferSize)
893 {
894  uint16_t u16Size = 0;
895  uint16_t u16ReceivedSize;
896  volatile uint32_t u32Timeout;
897  volatile uint8_t u8Dummy = 0;
898  u32Timeout = 10000;
899  while((pstcUsb->EP1S_f.BUSY == 1) && (u32Timeout > 0))
900  {
901  u32Timeout--;
902  }
903  //stc_usbhost_intern_data_t* pstcUsbHostIntern;
904  //pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
905  u16Size = (uint16_t)(pstcUsb->EP1S & 0x00FF);
906  u16ReceivedSize = u16Size;
907  USBDBGVAL16("RX: ",u16Size);
908  if (u16ReceivedSize > u32MaxBufferSize)
909  {
910  USBDBGVAL16("WOW... more data than expected, new data size: ",u32MaxBufferSize);
911  u16Size = u32MaxBufferSize;
912  u16ReceivedSize = u16Size;
913  }
914  USBDBGVAL32("#",pu8Buffer);
915  u16Size = u16Size / 2; // transfer 16 Bit words;
916  while(u16Size--)
917  {
918  //*(uint16_t*)pu8Buffer = EP1DT;
919  if (pu8Buffer == 0)
920  {
921  u8Dummy = pstcUsb->EP1DTL;
922  u8Dummy = pstcUsb->EP1DTH;
923  }
924  else
925  {
926  *pu8Buffer++ = pstcUsb->EP1DTL;
927  *pu8Buffer++ = pstcUsb->EP1DTH;
928  }
929  //pu8Buffer += 2;
930  }
931  if(u16ReceivedSize & 1 == TRUE) {
932  if (pu8Buffer == 0)
933  {
934  u8Dummy = pstcUsb->EP1DTL; // transfer the first or last byte
935  }
936  else
937  {
938  *(uint8_t*)pu8Buffer = pstcUsb->EP1DTL; // transfer the first or last byte
939  }
940  }
941  pstcUsb->EP1S_f.DRQ = 0; // now the IN FIFO is valid for the next transfer
942  pstcUsb->EP1S_f.BFINI=1;
943  pstcUsb->EP1S_f.BFINI=0;
944  return u16ReceivedSize;
945 }
946 
962 void UsbHost_TransferData(stc_usbn_t* pstcUsb, stc_host_endpoint_t* Handler,uint8_t* pu8Buffer, uint32_t u32BufferSize, void (* CompletionHandler)(stc_usbn_t* pstcUsb))
963 {
964  stc_usbhost_intern_data_t* pstcUsbHostIntern;
965  volatile boolean_t bOldSchedulerLockState;
966  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
967  bOldSchedulerLockState = pstcUsbHostIntern->bSchedulerLock;
968  pstcUsbHostIntern->bSchedulerLock = TRUE;
969  DBGPROCENTER("UsbHost_TransferData");
970 
971  (Handler->u8Status) &= ~USBHOST_ENDPOINTSTATUS_INITIATE;
972  if ((Handler->u8Interval) == 0)
973  {
974  (Handler->u8Status) |= USBHOST_ENDPOINTSTATUS_INUSE;
975  }
976  Handler->bAbortTransfer = FALSE;
977  USBHOST_RESETENDPOINTBUFFER(Handler,pu8Buffer,u32BufferSize);
978 
979  (Handler->CompletionHandler) = CompletionHandler;
980  (Handler->u8Status) &= ~USBHOST_ENDPOINTSTATUS_INUSE;
981 
982  USBDBGVAL32("DATASIZE: ",Handler->u32DataSize);
983 
984  if ((Handler->u8Interval) == 0)
985  {
986  (Handler->u8Status) |= USBHOST_ENDPOINTSTATUS_INITIATE;
987  }
988 
989  DBGPROCRETURN("UsbHost_TransferData");
990  pstcUsbHostIntern->bSchedulerLock = bOldSchedulerLockState;
991 
992  //UsbHost_Tick();
993  //Handler->u8Status |= USBHOST_ENDPOINTSTATUS_DATA;
994 }
995 
1005 void UsbHost_Init(stc_usbn_t* pstcUsb, stc_usb_config_t* pstcUsbConfig)
1006 {
1007  //stc_usbhost_intern_data_t* pstcUsbHostIntern;
1008  //pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
1009 
1010  pstcUsbConfig->enMode |= UsbHostEnabled;
1011  pstcUsbConfig->pfnHostInit = UsbHost_InitCallback;
1012 
1013 
1014 }
1015 
1031 uint8_t UsbHost_GetDeviceStatus(stc_usbn_t* pstcUsb)
1032 {
1033  stc_usbhost_intern_data_t* pstcUsbHostIntern;
1034  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
1035  return pstcUsbHostIntern->u8DeviceStatus;
1036 }
1037 
1038 
1048 void UsbHost_SetConfigurationDescriptor(stc_usbn_t* pstcUsb, uint8_t u8Configuration)
1049 {
1050  stc_usb_request_t stcSetup = {0x00,0x09,0x00,0x00,0x00};
1051  stc_usbhost_intern_data_t* pstcUsbHostIntern;
1052  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
1053  stcSetup.wValue |= u8Configuration;
1054  USBDBGVAL8("Setting Configuration: ",u8Configuration);
1055  pstcUsbHostIntern->u8DeviceStatus = USBHOST_DEVICE_CONFIGURATING;
1056  UsbHost_SetupRequest(pstcUsb,&stcSetup,0);
1057 }
1058 
1074 uint8_t* UsbHost_GetUsbEndpointDescriptor(uint8_t* pu8Buffer, uint16_t u16Size, uint8_t u8InterfaceNumber, uint8_t u8EndpointNumber)
1075 {
1076  uint16_t u16Position = 0;
1077  uint8_t u8DescriptorLength = 0;
1078  uint8_t u8DescriptorType = 0;
1079  uint8_t u8CurrentInterface = 0xFF;
1080  uint8_t u8CurrentEndpoint = 0;
1081  uint16_t u16wTotalLength;
1082 
1083 
1084  if (pu8Buffer[1] != USB_CONFIGURATION_DESCRIPTOR_TYPE)
1085  {
1086  return 0;
1087  }
1088 
1089  u16wTotalLength = (uint16_t)pu8Buffer[2] + (uint16_t)(pu8Buffer[3] << 8);
1090  if (u16Size < u16wTotalLength)
1091  {
1092  u16wTotalLength = u16Size;
1093  }
1094 
1095  while(u16Position < u16wTotalLength)
1096  {
1097  u8DescriptorLength = pu8Buffer[u16Position];
1098  u8DescriptorType = pu8Buffer[u16Position + 1];
1099  if (u8DescriptorType == USB_INTERFACE_DESCRIPTOR_TYPE)
1100  {
1101  u8CurrentEndpoint = 0;
1102  u8CurrentInterface = pu8Buffer[u16Position + 2];
1103  }
1104  if (u8DescriptorType == USB_ENDPOINT_DESCRIPTOR_TYPE)
1105  {
1106  u8CurrentEndpoint = u8CurrentEndpoint + 1;
1107  }
1108  if (((u8CurrentEndpoint == u8EndpointNumber) || (u8EndpointNumber == 0)) && (u8InterfaceNumber == u8CurrentInterface))
1109  {
1110  return (uint8_t*)(pu8Buffer + u16Position);
1111  }
1112  u16Position += u8DescriptorLength;
1113  }
1114  return 0;
1115 }
1116 
1117 
1118 typedef enum en_usbhost_scheduler_event
1119 {
1120  UsbHostSchedulerEventUnknown = 0,
1121  UsbHostSchedulerEventSof = 1,
1122  UsbHostSchedulerEventCompletion = 2,
1123  UsbHostSchedulerEventTick = 3,
1124 } en_usbhost_scheduler_event_t;
1125 
1126 void UsbHost_SchedulerInitiateEndpoint(stc_usbn_t* pstcUsb, stc_usbhost_intern_data_t* pstcUsbHostIntern, stc_host_endpoint_t* pstcEndpoint)
1127 {
1128  uint32_t u32DataSize = 0;
1129  uint8_t u8NextToken = 0;
1130  volatile uint32_t u32Timeout;
1131  DBGPROCENTER("UsbHost_SchedulerInitiateEndpoint");
1132  USBDBGVAL8("Init TKN, EP: ", pstcEndpoint->u8Address);
1133  pstcEndpoint->u8Status &= ~USBHOST_ENDPOINTSTATUS_INITIATE;
1134  pstcEndpoint->u8Status |= USBHOST_ENDPOINTSTATUS_INUSE;
1135  pstcEndpoint->u8IntervalCount = (pstcEndpoint->u8Interval);
1136  pstcUsbHostIntern->u32WaitForCompletionTimeout = 10;
1137  if (((pstcEndpoint->u8Address) & 0x0F) == 0)
1138  {
1139  pstcUsbHostIntern->u32WaitForCompletionTimeout = 10;
1140  }
1141  if ((pstcEndpoint->u8Address) & 0x80)
1142  {
1143  USBDBG("Type IN\n");
1144  if (pstcEndpoint->bIsochronous == TRUE)
1145  {
1146  u8NextToken = CREATE_TOKEN(FALSE,HTOKEN_ISO_IN,((pstcEndpoint->u8Address) & 0x0F));
1147  }
1148  else
1149  {
1150  u8NextToken = CREATE_TOKEN(pstcEndpoint->bToggle,HTOKEN_IN,((pstcEndpoint->u8Address) & 0x0F));
1151  }
1152 
1153  }
1154  else
1155  {
1156  //u32DataSize = (pstcEndpoint->u32DataSize);
1157  u32DataSize = (pstcEndpoint->u32DataSize);
1158  USBDBG("Type OUT\n");
1159  USBDBGVAL32("Data Size:", u32DataSize);
1160  u32DataSize -= (uint32_t)((pstcEndpoint->pu8BufferPos) - (pstcEndpoint->pu8Buffer));
1161  USBDBGVAL32("Data Size Left:", u32DataSize);
1162  if (u32DataSize > (pstcEndpoint->u16FifoSize))
1163  {
1164  u32DataSize = pstcEndpoint->u16FifoSize;
1165  USBDBGVAL32("Resizing new:", u32DataSize);
1166  }
1167  if (u32DataSize > 0)
1168  {
1169  pstcEndpoint->pu8BufferPosNextPackage += u32DataSize;
1170  UsbHost_TransferDataToFifo(pstcUsb,pstcEndpoint->pu8BufferPos, (uint16_t)u32DataSize, pstcEndpoint->u16FifoSize);
1171  u8NextToken = CREATE_TOKEN(pstcEndpoint->bToggle,HTOKEN_OUT,((pstcEndpoint->u8Address) & 0x0F));
1172 
1173  }
1174  else
1175  {
1176  USBDBG("Sending 0 byte\n");
1177  UsbHost_TransferDataToFifo(pstcUsb,pstcEndpoint->pu8BufferPos, (uint16_t)u32DataSize, pstcEndpoint->u16FifoSize);
1178  u8NextToken = CREATE_TOKEN(pstcEndpoint->bToggle,HTOKEN_OUT,((pstcEndpoint->u8Address) & 0x0F));
1179  }
1180  }
1181 
1182  if (pstcEndpoint->u8Stage == USBHOST_STAGE_SETUP)
1183  {
1184  u8NextToken = 0x10;
1185  }
1186 
1187  USBDBGVAL8("",u8NextToken);
1188  USBDBGVAL8("DATA",(u8NextToken & 0x80) > 0);
1189  pstcUsbHostIntern->bSofToken = FALSE;
1190  pstcUsbHostIntern->u8LastToken = u8NextToken;
1191  pstcUsbHostIntern->ActiveEndpoint = pstcEndpoint;
1192  if ((pstcUsbHostIntern->Ep0OUT == pstcEndpoint) || (pstcUsbHostIntern->Ep0IN == pstcEndpoint))
1193  {
1194  pstcUsbHostIntern->Ep0OUT->u8LastToken = u8NextToken;
1195  pstcUsbHostIntern->Ep0IN->u8LastToken = u8NextToken;
1196  }
1197  pstcEndpoint->u8LastToken = u8NextToken;
1198  pstcUsbHostIntern->u8SchedulerState = USBHOST_SCHEDULER_BUSY;
1199  pstcEndpoint->u8Status |= USBHOST_ENDPOINTSTATUS_INUSE;
1200  u32Timeout = 10000;
1201  while(((pstcUsb->EP1S_f.BUSY == 1) || (pstcUsb->EP2S_f.BUSY == 1)) && (u32Timeout > 0))
1202  {
1203  u32Timeout--;
1204  }
1205  pstcUsb->HTOKEN = u8NextToken;
1206  DBGPROCRETURN("UsbHost_SchedulerInitiateEndpoint");
1207 }
1208 
1209 void UsbHost_Scheduler(stc_usbn_t* pstcUsb, en_usbhost_scheduler_event_t enEvent)
1210 {
1211  uint8_t i;
1212  static volatile boolean_t bSchedulerIsRunning = FALSE;
1213  stc_usbhost_intern_data_t* pstcUsbHostIntern;
1214  if (bSchedulerIsRunning == TRUE) return;
1215 
1216  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
1217 
1218  if (enEvent == UsbHostSchedulerEventCompletion)
1219  {
1220  }
1221 
1222  if (enEvent == UsbHostSchedulerEventSof)
1223  {
1224  if (pstcUsbHostIntern->u32WaitForCompletionTimeout > 0)
1225  {
1226  pstcUsbHostIntern->u32WaitForCompletionTimeout--;
1227  }
1228 
1229  for(i = 0;i <= pstcUsbHostIntern->u8EndpointListEnd;i++)
1230  {
1231  if ((pstcUsbHostIntern->astcHostEndpoints[i].u8Interval > 0) && (pstcUsbHostIntern->astcHostEndpoints[i].u8IntervalCount > 0))
1232  {
1233  pstcUsbHostIntern->astcHostEndpoints[i].u8IntervalCount--;
1234  }
1235  }
1236  }
1237 
1238  if (pstcUsbHostIntern->bSchedulerLock == TRUE)
1239  {
1240  bSchedulerIsRunning = FALSE;
1241  return;
1242  }
1243 
1244  if (pstcUsbHostIntern->u32WaitForCompletionTimeout > 0)
1245  {
1246  bSchedulerIsRunning = FALSE;
1247  return;
1248  }
1249 
1250  if ((pstcUsbHostIntern->Ep0IN->bAbortTransfer == TRUE) || (pstcUsbHostIntern->Ep0OUT->bAbortTransfer == TRUE))
1251  {
1252  USBDBG("-> EP0 CANCELED\n");
1253  pstcUsbHostIntern->Ep0IN->u8Status &= ~USBHOST_ENDPOINTSTATUS_INUSE;
1254  pstcUsbHostIntern->Ep0OUT->u8Status &= ~USBHOST_ENDPOINTSTATUS_INUSE;
1255  pstcUsbHostIntern->Ep0IN->bAbortTransfer = FALSE;
1256  pstcUsbHostIntern->Ep0OUT->bAbortTransfer = FALSE;
1257  pstcUsbHostIntern->Ep0IN->u8Stage = 0;
1258  pstcUsbHostIntern->Ep0OUT->u8Stage = 0;
1259  pstcUsbHostIntern->u32ConTimeout = 0;
1260 
1261  }
1262 
1263  // Process control transfers
1264  if (((pstcUsbHostIntern->Ep0IN->u8Status) & USBHOST_ENDPOINTSTATUS_INITIATE) > 0)
1265  {
1266  UsbHost_SchedulerInitiateEndpoint(pstcUsb,pstcUsbHostIntern,pstcUsbHostIntern->Ep0IN);
1267  bSchedulerIsRunning = FALSE;
1268  return;
1269  }
1270 
1271  if (((pstcUsbHostIntern->Ep0OUT->u8Status) & USBHOST_ENDPOINTSTATUS_INITIATE) > 0)
1272  {
1273  UsbHost_SchedulerInitiateEndpoint(pstcUsb,pstcUsbHostIntern,pstcUsbHostIntern->Ep0OUT);
1274  bSchedulerIsRunning = FALSE;
1275  return;
1276  }
1277 
1278  if (pstcUsbHostIntern->u8DeviceStatus != USBHOST_DEVICE_CONFIGURED)
1279  {
1280  bSchedulerIsRunning = FALSE;
1281  return;
1282  }
1283 
1284  // Process interrupt transfers
1285  for(i = 0;i <= pstcUsbHostIntern->u8EndpointListEnd;i++)
1286  {
1287  pstcUsbHostIntern->u8CurrentEndpointPosition++;
1288  if (pstcUsbHostIntern->u8CurrentEndpointPosition > pstcUsbHostIntern->u8EndpointListEnd)
1289  {
1290  pstcUsbHostIntern->u8CurrentEndpointPosition = 0;
1291  }
1292  if ((pstcUsbHostIntern->astcHostEndpoints[pstcUsbHostIntern->u8CurrentEndpointPosition].u8Interval > 0) && (pstcUsbHostIntern->astcHostEndpoints[pstcUsbHostIntern->u8CurrentEndpointPosition].u8IntervalCount == 0))
1293  {
1294  // Init Interrupt Transfer
1295  pstcUsbHostIntern->astcHostEndpoints[pstcUsbHostIntern->u8CurrentEndpointPosition].pu8BufferPos = (pstcUsbHostIntern->astcHostEndpoints[pstcUsbHostIntern->u8CurrentEndpointPosition].pu8Buffer);
1296  pstcUsbHostIntern->astcHostEndpoints[pstcUsbHostIntern->u8CurrentEndpointPosition].pu8BufferPosNextPackage = (pstcUsbHostIntern->astcHostEndpoints[pstcUsbHostIntern->u8CurrentEndpointPosition].pu8Buffer);
1297  UsbHost_SchedulerInitiateEndpoint(pstcUsb,pstcUsbHostIntern,&(pstcUsbHostIntern->astcHostEndpoints[pstcUsbHostIntern->u8CurrentEndpointPosition]));
1298  bSchedulerIsRunning = FALSE;
1299  return;
1300  }
1301  }
1302 
1303  // Process other transfers than interrupt
1304  for(i = 0;i <= pstcUsbHostIntern->u8EndpointListEnd;i++)
1305  {
1306  pstcUsbHostIntern->u8CurrentEndpointPosition++;
1307  if (pstcUsbHostIntern->u8CurrentEndpointPosition > pstcUsbHostIntern->u8EndpointListEnd)
1308  {
1309  pstcUsbHostIntern->u8CurrentEndpointPosition = 0;
1310  }
1311  if (((pstcUsbHostIntern->astcHostEndpoints[pstcUsbHostIntern->u8CurrentEndpointPosition].u8Status) & USBHOST_ENDPOINTSTATUS_INITIATE) > 0)
1312  {
1313  // Init Transfer
1314  UsbHost_SchedulerInitiateEndpoint(pstcUsb,pstcUsbHostIntern,&(pstcUsbHostIntern->astcHostEndpoints[pstcUsbHostIntern->u8CurrentEndpointPosition]));
1315  bSchedulerIsRunning = FALSE;
1316  return;
1317  }
1318  }
1319 }
1320 
1321 void UsbHost_CompletionCallback(stc_usbn_t* pstcUsb)
1322 {
1323  uint32_t tmp;
1324  uint32_t u32DataSize = 0;
1325  volatile boolean_t bOldSchedulerLockState;
1326  stc_host_endpoint_t* pstcEndpointHandle = 0;
1327  stc_usbhost_intern_data_t* pstcUsbHostIntern;
1328  pstcUsbHostIntern = UsbHostGetInternDataPtr(pstcUsb);
1329  pstcEndpointHandle = pstcUsbHostIntern->ActiveEndpoint;
1330  DBGPROCENTER("UsbHost_CompletionCallback");
1331  bOldSchedulerLockState = pstcUsbHostIntern->bSchedulerLock;
1332  pstcUsbHostIntern->bSchedulerLock = TRUE;
1333 
1334 
1335  USBDBGVAL8("EP: ",pstcEndpointHandle->u8Address);
1336 
1337  USBDBGVAL8("EP: ",pstcEndpointHandle->u8Address);
1338 
1339  if (pstcEndpointHandle == 0)
1340  {
1341  USBDBG("WARNING: pstcEndpointHandle == 0\n");
1342  USBDBG("\n< UsbHost_CompletionCallback\n");
1343  pstcUsbHostIntern->bSchedulerLock = bOldSchedulerLockState;
1344  return;
1345  }
1346  pstcEndpointHandle->u8Status &= ~USBHOST_ENDPOINTSTATUS_INITIATE;
1347 
1348  if (pstcEndpointHandle->bAbortTransfer == TRUE)
1349  {
1350  USBDBG("-> CANCELED\n");
1351  pstcEndpointHandle->u32DataSize = 0;
1352  pstcEndpointHandle->u8Status &= ~USBHOST_ENDPOINTSTATUS_INUSE;
1353  if ((pstcUsbHostIntern->Ep0OUT == pstcEndpointHandle) || (pstcUsbHostIntern->Ep0IN == pstcEndpointHandle))
1354  {
1355  pstcUsbHostIntern->Ep0OUT->u8Status &= ~USBHOST_ENDPOINTSTATUS_INUSE;
1356  pstcUsbHostIntern->Ep0IN->u8Status &= ~USBHOST_ENDPOINTSTATUS_INUSE;
1357  }
1358  pstcEndpointHandle->bAbortTransfer = FALSE;
1359  pstcUsbHostIntern->ActiveEndpoint = 0;
1360  DBGPROCRETURN("UsbHost_CompletionCallback");
1361  pstcUsbHostIntern->bSchedulerLock = bOldSchedulerLockState;
1362  return;
1363  }
1364  USBDBGVAL8("HERR: ",pstcUsb->HERR);
1365  USBDBGVAL8("TKN: ",(pstcEndpointHandle->u8LastToken));
1366  if ((pstcUsb->HERR & 0x03) == HERR_ACK)
1367  {
1368  USBDBG("ACK\n");
1369 
1370  if (pstcEndpointHandle->bToggle == TRUE)
1371  {
1372  USBDBG("TGL 1 -> 0\n");
1373  pstcEndpointHandle->bToggle = FALSE;
1374  if ((pstcUsbHostIntern->Ep0OUT == pstcEndpointHandle) || (pstcUsbHostIntern->Ep0IN == pstcEndpointHandle))
1375  {
1376  pstcUsbHostIntern->Ep0OUT->bToggle = FALSE;
1377  pstcUsbHostIntern->Ep0IN->bToggle = FALSE;
1378  }
1379  }
1380  else
1381  {
1382  USBDBG("TGL 0 -> 1\n");
1383  pstcEndpointHandle->bToggle = TRUE;
1384  if ((pstcUsbHostIntern->Ep0OUT == pstcEndpointHandle) || (pstcUsbHostIntern->Ep0IN == pstcEndpointHandle))
1385  {
1386  pstcUsbHostIntern->Ep0OUT->bToggle = TRUE;
1387  pstcUsbHostIntern->Ep0IN->bToggle = TRUE;
1388  }
1389  }
1390 
1391 
1392  if (((pstcEndpointHandle->u8Address) & 0x0F) == 0) // handler == endpoint 0?
1393  {
1394  if (((pstcEndpointHandle->u8LastToken) & 0x70) == HTOKEN_SETUP) // type == SETUP token?
1395  {
1396  USBDBG("SETUP\n");
1397  if (((pstcUsbHostIntern->stcLastSetup.bmRequestType) & 0x80) || (pstcUsbHostIntern->stcLastSetup.wLength == 0))
1398  {
1399  USBDBG("TYPE IN\n");
1400  pstcEndpointHandle = pstcUsbHostIntern->Ep0IN;
1401  }
1402  else
1403  {
1404  USBDBG("TYPE OUT\n");
1405  pstcEndpointHandle = pstcUsbHostIntern->Ep0OUT;
1406  }
1407  pstcUsbHostIntern->Ep0IN->bToggle = TRUE;
1408  pstcUsbHostIntern->Ep0OUT->bToggle = TRUE;
1409  pstcEndpointHandle->pu8BufferPos = (pstcEndpointHandle->pu8Buffer);
1410  pstcEndpointHandle->u32DataSize = 0;
1411  if ((pstcUsbHostIntern->stcLastSetup.wLength == 0)) // || ((stcLastSetup.wLength <= (Ep0IN->u16FifoSize)) && (Handler == Ep0IN)))
1412  {
1413  USBDBG("NO DATA -> ENTERING STATUS STAGE\n");
1414  pstcUsbHostIntern->Ep0OUT->u8Stage = USBHOST_STAGE_STATUS;
1415  pstcUsbHostIntern->Ep0IN->u8Stage = USBHOST_STAGE_STATUS;
1416  }
1417  else
1418  {
1419  USBDBG("DATA -> ENTERING DATA STAGE\n");
1420  pstcUsbHostIntern->Ep0OUT->u8Stage = USBHOST_STAGE_DATA;
1421  pstcUsbHostIntern->Ep0IN->u8Stage = USBHOST_STAGE_DATA;
1422  }
1423  pstcEndpointHandle->u32DataSize = pstcUsbHostIntern->stcLastSetup.wLength;
1424  if (((pstcUsbHostIntern->stcLastSetup.bmRequestType) & 0x80) || (pstcUsbHostIntern->stcLastSetup.wLength == 0))
1425  {
1426  pstcEndpointHandle->u8Status |= USBHOST_ENDPOINTSTATUS_INITIATE;
1427  }
1428  else
1429  {
1430  pstcEndpointHandle->pu8BufferPosNextPackage = pstcUsbHostIntern->pu8SetupData;
1431  pstcEndpointHandle->pu8BufferPos = pstcUsbHostIntern->pu8SetupData;
1432  pstcEndpointHandle->pu8Buffer = pstcUsbHostIntern->pu8SetupData;
1433  pstcEndpointHandle->u32BufferSize = pstcUsbHostIntern->stcLastSetup.wLength;
1434  pstcEndpointHandle->u8Status |= USBHOST_ENDPOINTSTATUS_INITIATE;
1435  }
1436  DBGPROCRETURN("UsbHost_CompletionCallback");
1437  pstcUsbHostIntern->bSchedulerLock = bOldSchedulerLockState;
1438  return;
1439  } // endif (((pstcEndpointHandle->u8LastToken) & 0x70) == HTOKEN_SETUP) --> type == SETUP token?
1440  else
1441  {
1442  if (pstcUsbHostIntern->u8DeviceStatus == USBHOST_DEVICE_ADDRESSING)
1443  {
1444  pstcUsbHostIntern->u8EnumerationCounter = 0;
1445  pstcUsb->HADR = pstcUsbHostIntern->u8DeviceAddress;
1446  USBDBG("Enumeration Process:\n");
1447  USBDBGVAL8(" ->Addressed: ",pstcUsbHostIntern->u8DeviceAddress);
1448  pstcUsbHostIntern->u8DeviceStatus = USBHOST_DEVICE_ADDRESSED;
1449  //UsbClassSupervisor_Init(pstcUsb);
1450  }
1451  if (pstcUsbHostIntern->u8DeviceStatus == USBHOST_DEVICE_CONFIGURATING)
1452  {
1453  USBDBG("-->Configured\n");
1454  pstcUsbHostIntern->u8DeviceStatus = USBHOST_DEVICE_CONFIGURED;
1455  }
1456  }
1457  } // endif (((pstcEndpointHandle->u8Address) & 0x0F) == 0) --> handler == endpoint 0?
1458 
1459  if ((((pstcEndpointHandle->u8LastToken) & 0x70) != HTOKEN_SETUP))
1460  {
1461  USBDBG("NO SETUP\n");
1462  pstcEndpointHandle->u8Status &= ~USBHOST_ENDPOINTSTATUS_INITIATE;
1463  if ((((pstcEndpointHandle->u8LastToken) & 0x70) == HTOKEN_IN) || (((pstcEndpointHandle->u8LastToken) & 0x70) == HTOKEN_ISO_IN))
1464  {
1465  USBDBG("IN TKN\n");
1466  tmp = (pstcEndpointHandle->u32DataSize) - ((uint32_t)((uint32_t)(pstcEndpointHandle->pu8BufferPos) - (uint32_t)(pstcEndpointHandle->pu8Buffer)));
1467  USBDBGVAL32("Max free in buffer: ",(pstcEndpointHandle->u32DataSize));
1468  u32DataSize = (uint32_t)((uint16_t)UsbHost_TransferFifoToBuffer(pstcUsb,pstcEndpointHandle->pu8BufferPos,tmp));
1469  (pstcEndpointHandle->pu8BufferPos) = (pstcEndpointHandle->pu8BufferPos) + u32DataSize;
1470  tmp = ((uint32_t)((uint32_t)(pstcEndpointHandle->pu8BufferPos) - (uint32_t)(pstcEndpointHandle->pu8Buffer)));
1471  USBDBGVAL8("EP#",(uint32_t)(pstcEndpointHandle->u8Address));
1472  USBDBGVAL32("#",(uint32_t)(pstcEndpointHandle->pu8Buffer));
1473  USBDBGVAL32("Received: ", u32DataSize);
1474  USBDBGVAL32("New Size: ", tmp);
1475  USBDBGVAL32("Max Size: ",(pstcEndpointHandle->u32DataSize));
1476  if (((u32DataSize == (pstcEndpointHandle->u16FifoSize)) && ((pstcEndpointHandle->u32DataSize) == 0)) || ((u32DataSize == (pstcEndpointHandle->u16FifoSize) && (pstcEndpointHandle->u32DataSize) > 0) && ((pstcEndpointHandle->u32DataSize) > tmp)))
1477  {
1478  pstcEndpointHandle->u8Status |= USBHOST_ENDPOINTSTATUS_INITIATE;
1479  }
1480  else
1481  {
1482  pstcEndpointHandle->u32DataSize = (uint32_t)((uint32_t)(pstcEndpointHandle->pu8BufferPos) - (uint32_t)(pstcEndpointHandle->pu8Buffer));
1483  USBDBGVAL32("RX-Size: ", (pstcEndpointHandle->u32DataSize));
1484  pstcEndpointHandle->pu8BufferPos = (pstcEndpointHandle->pu8Buffer);
1485  pstcEndpointHandle->u8Status |= USBHOST_ENDPOINTSTATUS_DATA;
1486  pstcEndpointHandle->u8Status &= ~USBHOST_ENDPOINTSTATUS_INUSE;
1487  if ((pstcUsbHostIntern->Ep0OUT == pstcEndpointHandle) || (pstcUsbHostIntern->Ep0IN == pstcEndpointHandle))
1488  {
1489  pstcUsbHostIntern->Ep0OUT->u8Status &= ~USBHOST_ENDPOINTSTATUS_INUSE;
1490  pstcUsbHostIntern->Ep0IN->u8Status &= ~USBHOST_ENDPOINTSTATUS_INUSE;
1491  }
1492  if ((((pstcEndpointHandle->u8Address) & 0x0F) == 0) && ((pstcEndpointHandle->u8Stage) == USBHOST_STAGE_DATA))
1493  {
1494  pstcUsbHostIntern->Ep0IN->u8Stage = USBHOST_STAGE_STATUS;
1495  pstcUsbHostIntern->Ep0OUT->u8Stage = USBHOST_STAGE_STATUS;
1496  pstcUsbHostIntern->Ep0OUT->bToggle = TRUE;
1497  pstcUsbHostIntern->Ep0IN->bToggle = TRUE;
1498  pstcUsbHostIntern->Ep0OUT->u32DataSize = 0;
1499  pstcUsbHostIntern->Ep0OUT->pu8BufferPos = (pstcUsbHostIntern->Ep0OUT->pu8Buffer);
1500  pstcUsbHostIntern->Ep0OUT->pu8BufferPosNextPackage = (pstcUsbHostIntern->Ep0OUT->pu8BufferPos);
1501  pstcUsbHostIntern->Ep0OUT->CompletionHandler = pstcUsbHostIntern->Ep0IN->CompletionHandler;
1502  pstcUsbHostIntern->Ep0IN->u8Status &= ~USBHOST_ENDPOINTSTATUS_INITIATE;
1503  pstcUsbHostIntern->Ep0OUT->u8Status |= USBHOST_ENDPOINTSTATUS_INITIATE;
1504  }
1505  else
1506  {
1507  USBDBG("TRANS COMPLETE\n");
1508  pstcUsbHostIntern->u32WaitForCompletionTimeout = 0;
1509  pstcEndpointHandle->u8Status &= ~USBHOST_ENDPOINTSTATUS_INITIATE;
1510  if ((pstcEndpointHandle->CompletionHandler) != 0)
1511  {
1512  USBDBG("Exec(IN)\n");
1513  pstcEndpointHandle->CompletionHandler(pstcUsb);
1514  }
1515  pstcUsbHostIntern->ActiveEndpoint = 0;
1516  }
1517  }
1518 
1519  }
1520  if ((((pstcEndpointHandle->u8LastToken) & 0x70) == HTOKEN_OUT) || (((pstcEndpointHandle->u8LastToken) & 0x70) == HTOKEN_ISO_OUT))
1521  {
1522  pstcEndpointHandle->pu8BufferPos = (pstcEndpointHandle->pu8BufferPosNextPackage);
1523 
1524  USBDBG("OUT TKN\n");
1525  USBDBGVAL8("EP#",(uint32_t)(pstcEndpointHandle->u8Address));
1526  USBDBGVAL32("#",(uint32_t)(pstcEndpointHandle->pu8Buffer));
1527  USBDBGVAL32("Transferred: ",(uint32_t)((pstcEndpointHandle->pu8BufferPos) - (pstcEndpointHandle->pu8Buffer)));
1528  u32DataSize = (pstcEndpointHandle->u32DataSize);
1529 
1530  USBDBGVAL32("Datalen old: ",u32DataSize);
1531 
1532  u32DataSize -= (uint32_t)((pstcEndpointHandle->pu8BufferPos) - (pstcEndpointHandle->pu8Buffer));
1533 
1534  USBDBGVAL32("Datalen new: ",u32DataSize);
1535 
1536  pstcEndpointHandle->u8Status &= ~USBHOST_ENDPOINTSTATUS_INITIATE;
1537  if (u32DataSize > 0)
1538  {
1539  pstcEndpointHandle->u8Status |= USBHOST_ENDPOINTSTATUS_INITIATE;
1540  }
1541  else
1542  {
1543  USBDBG("SIZE == 0\n");
1544  pstcEndpointHandle->u8Status &= ~USBHOST_ENDPOINTSTATUS_INUSE;
1545  if ((pstcUsbHostIntern->Ep0OUT == pstcEndpointHandle) || (pstcUsbHostIntern->Ep0IN == pstcEndpointHandle))
1546  {
1547  pstcUsbHostIntern->Ep0OUT->u8Status &= ~USBHOST_ENDPOINTSTATUS_INUSE;
1548  pstcUsbHostIntern->Ep0IN->u8Status &= ~USBHOST_ENDPOINTSTATUS_INUSE;
1549  }
1550  if ((((pstcEndpointHandle->u8Address) & 0x0F) == 0) && ((pstcEndpointHandle->u8Stage) == USBHOST_STAGE_DATA))
1551  {
1552  USBDBG("DATA STAGE -> STATUS STAGE\n");
1553  pstcUsbHostIntern->Ep0IN->u8Stage = USBHOST_STAGE_STATUS;
1554  pstcUsbHostIntern->Ep0OUT->u8Stage = USBHOST_STAGE_STATUS;
1555  pstcUsbHostIntern->Ep0IN->CompletionHandler = pstcUsbHostIntern->Ep0OUT->CompletionHandler;
1556  pstcUsbHostIntern->Ep0IN->bToggle = TRUE;
1557  pstcUsbHostIntern->Ep0OUT->bToggle = TRUE;
1558  pstcUsbHostIntern->Ep0IN->u32DataSize = 0;
1559  pstcUsbHostIntern->Ep0IN->pu8BufferPos = (pstcUsbHostIntern->Ep0IN->pu8Buffer);
1560  pstcUsbHostIntern->Ep0IN->pu8BufferPosNextPackage = (pstcUsbHostIntern->Ep0IN->pu8BufferPos);
1561  pstcUsbHostIntern->Ep0OUT->u8Status &= ~USBHOST_ENDPOINTSTATUS_INITIATE;
1562  pstcUsbHostIntern->Ep0IN->u8Status |= USBHOST_ENDPOINTSTATUS_INITIATE;
1563  }
1564  else
1565  {
1566  USBDBG("TRANS COMPLETE\n");
1567  pstcUsbHostIntern->u32WaitForCompletionTimeout = 0;
1568  if (pstcEndpointHandle->u8Interval != 0)
1569  {
1570  pstcEndpointHandle->u32DataSize = 0;
1571  }
1572  pstcEndpointHandle->u8Status &= ~USBHOST_ENDPOINTSTATUS_INITIATE;
1573  if ((pstcEndpointHandle->CompletionHandler) != 0)
1574  {
1575  pstcEndpointHandle->CompletionHandler(pstcUsb);
1576  }
1577 
1578  }
1579  pstcUsbHostIntern->ActiveEndpoint = 0;
1580  }
1581  }
1582 
1583  } // endif ((((pstcEndpointHandle->u8LastToken) & 0x70) != HTOKEN_SETUP))
1584 
1585  } // endif ((pstcUsb->HERR & 0x03) == HERR_ACK)
1586 
1587  if ((pstcUsb->HERR & 0x08) > 0)
1588  {
1589  USBDBG("Toggle Error\n");
1590  pstcUsb->HERR_f.TGERR = 0;
1591  //pstcEndpointHandle->bToggle = ~pstcEndpointHandle->bToggle;
1592  } // endif ((pstcUsb->HERR & 0x08) > 0)
1593 
1594  if ((pstcUsb->HERR & 0x03) == HERR_NULL)
1595  {
1596  USBDBG("NULL\n");
1597  USBDBGVAL8("HERR: ",pstcUsb->HERR);
1598  } // endif ((pstcUsb->HERR & 0x03) == HERR_NULL)
1599 
1600  if (((pstcUsb->HERR & 0x03) == HERR_NAK) || ((pstcUsb->HERR & 0x03) == HERR_NULL))
1601  {
1602  pstcUsbHostIntern->u32WaitForCompletionTimeout = 0;
1603  USBDBG("NAK\n");
1604  if (((pstcEndpointHandle->u8Interval) > 0) && ((pstcEndpointHandle->pu8Buffer) == (pstcEndpointHandle->pu8BufferPos)))
1605  {
1606  USBDBG("Cancel\n");
1607  pstcEndpointHandle->u8Status &= ~USBHOST_ENDPOINTSTATUS_INUSE;
1608  }
1609  else
1610  {
1611  USBDBG("RTY\n");
1612  //UsbHost_TransferFifoToBuffer(pstcUsb,(uint8_t*)0);
1613  pstcEndpointHandle->pu8BufferPosNextPackage = pstcEndpointHandle->pu8BufferPos;
1614  if (pstcEndpointHandle->bAbortTransfer == TRUE)
1615  {
1616  USBDBG("-> CANCELED\n");
1617  pstcEndpointHandle->u32DataSize = 0;
1618  pstcEndpointHandle->u8Status &= ~USBHOST_ENDPOINTSTATUS_INUSE;
1619  if ((pstcUsbHostIntern->Ep0OUT == pstcEndpointHandle) || (pstcUsbHostIntern->Ep0IN == pstcEndpointHandle))
1620  {
1621  pstcUsbHostIntern->Ep0OUT->u8Status &= ~USBHOST_ENDPOINTSTATUS_INUSE;
1622  pstcUsbHostIntern->Ep0IN->u8Status &= ~USBHOST_ENDPOINTSTATUS_INUSE;
1623  }
1624  pstcEndpointHandle->bAbortTransfer = FALSE;
1625  pstcUsbHostIntern->ActiveEndpoint = 0;
1626  }
1627  else
1628  {
1629  pstcEndpointHandle->u8Status |= USBHOST_ENDPOINTSTATUS_INITIATE;
1630  }
1631  }
1632  } // endif ((pstcUsb->HERR & 0x03) == HERR_NAK)
1633 
1634 
1635 
1636  if ((pstcUsb->HERR & 0x03) == HERR_STALL)
1637  {
1638  USBDBG("STALL\n");
1639  pstcUsbHostIntern->u32WaitForCompletionTimeout = 0;
1640  pstcEndpointHandle = pstcUsbHostIntern->ActiveEndpoint;
1641  pstcEndpointHandle->u8Status |= USBHOST_ENDPOINTSTATUS_STALL;
1642  if (((pstcEndpointHandle->u8Address) & 0x0F) == 0)
1643  {
1644  USBDBG("Clearing STALL EP0\n");
1645  pstcUsbHostIntern->StallCompletionHandler = pstcEndpointHandle->CompletionHandler;
1646  memcpy((uint8_t*)&pstcUsbHostIntern->stcLastSetupBeforeStall,(uint8_t*)&pstcUsbHostIntern->stcLastSetup,sizeof(pstcUsbHostIntern->stcLastSetup));
1647  pstcUsbHostIntern->Ep0IN->u8Status &= ~USBHOST_ENDPOINTSTATUS_INUSE;
1648  pstcUsbHostIntern->Ep0OUT->u8Status &= ~USBHOST_ENDPOINTSTATUS_INUSE;
1649  UsbHost_SetupRequest(pstcUsb,(stc_usb_request_t*)&stcClearStallEp0,StallCompletionEp0);
1650  }
1651  else
1652  {
1653  if ((pstcEndpointHandle->CompletionHandler) != 0)
1654  {
1655  pstcEndpointHandle->CompletionHandler(pstcUsb);
1656  }
1657  }
1658  } // endif ((pstcUsb->HERR & 0x03) == HERR_STALL)
1659  DBGPROCRETURN("UsbHost_CompletionCallback");
1660  pstcUsbHostIntern->bSchedulerLock = bOldSchedulerLockState;
1661  pstcUsbHostIntern->u32WaitForCompletionTimeout = 0;
1662  UsbHost_Scheduler(pstcUsb, UsbHostSchedulerEventCompletion);
1663 }
1664 
1665 void UsbHost_SofCallback(stc_usbn_t* pstcUsb)
1666 {
1667  UsbHost_Scheduler(pstcUsb, UsbHostSchedulerEventSof);
1668 }
1669 
1670 void UsbHost_Tick(void)
1671 {
1672  #if (FM_PERIPHERAL_ENABLE_USB0 == ON) && defined(USB0)
1673  UsbHost_Scheduler((stc_usbn_t*)&USB0,UsbHostSchedulerEventTick);
1674  #endif
1675  #if (FM_PERIPHERAL_ENABLE_USB1 == ON) && defined(USB1)
1676  UsbHost_Scheduler((stc_usbn_t*)&USB1, UsbHostSchedulerEventTick);
1677  #endif
1678 }
1679 
1681 
1682 #endif