PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
usbdevice.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 /******************************************************************************/
64 /******************************************************************************/
65 /* Include files */
66 /******************************************************************************/
67 
68 
69 #include "usbdevice.h"
70 
71 #if (FM_PERIPHERAL_USB_DEVICE_ENABLED == ON)
72 
73 
74 
80 
81 #if (USE_USBDEVICEHW_H == 1)
82 #include "usbdevicehw.h"
83 #endif
84 
85 
86 static stc_usbdevice_intern_data_t* UsbDeviceGetInternDataPtr(const stc_usbn_t* pstcUsb) ;
87 static void EndpointContinueTxCallback(stc_usbn_t* pstcUsb, stc_usbn_endpoint_data_t* pstcEndpoint);
88 static void EndpointContinueRxCallback(stc_usbn_t* pstcUsb, stc_usbn_endpoint_data_t* pstcEndpoint);
89 static void ControlRxCallback(stc_usbn_t* pstcUsb, stc_usbn_endpoint_data_t* pstcEndpoint);
90 static void ControlTxCallback(stc_usbn_t* pstcUsb, stc_usbn_endpoint_data_t* pstcEndpoint);
91 static void ConfigChangeCallback(stc_usbn_t* pstcUsb);
92 static void SetupRxCallback(stc_usbn_t* pstcUsb);
93 
94 
95 
96 
97 
99 #define USBDEVICE_INSTANCE_COUNT (uint32_t)(sizeof(m_astcUsbDeviceInstanceDataLut) / sizeof(m_astcUsbDeviceInstanceDataLut[0]))
100 
101 /******************************************************************************/
102 /* Global variable definitions (declared in header file with 'extern') */
103 /******************************************************************************/
104 
106 stc_usbdevicen_instance_data_t m_astcUsbDeviceInstanceDataLut[] =
107 {
108  #if ((FM_PERIPHERAL_ENABLE_USB0 == ON) && (defined(USB0)) && (FM_PERIPHERAL_ENABLE_USB0_DEVICE == ON))
109  { &USB0, // pstcInstance
110  0 // stcInternData (not initialized yet)
111  },
112  #endif
113  #if ((FM_PERIPHERAL_ENABLE_USB1 == ON) && (defined(USB1)) && (FM_PERIPHERAL_ENABLE_USB1_DEVICE == ON))
114  { &USB1, // pstcInstance
115  0 // stcInternData (not initialized yet)
116  }
117  #endif
118 };
119 
129 static stc_usbdevice_intern_data_t* UsbDeviceGetInternDataPtr(const stc_usbn_t* pstcUsb)
130 {
131  volatile uint32_t u32Instance;
132 
133  for (u32Instance = 0; u32Instance < USBDEVICE_INSTANCE_COUNT; u32Instance++)
134  {
135  if ((uint32_t)pstcUsb == (uint32_t)(m_astcUsbDeviceInstanceDataLut[u32Instance].pstcInstance))
136  {
137  return &m_astcUsbDeviceInstanceDataLut[u32Instance].stcInternData;
138  }
139  }
140 
141  return NULL;
142 }
143 
144 
156 static void EndpointContinueTxCallback(stc_usbn_t* pstcUsb, stc_usbn_endpoint_data_t* pstcEndpoint)
157 {
158  uint16_t u16DataSend = 0;
159  uint32_t u32TransferredData;
160 
161  USBDBG(">TxClbk\n");
162 
163  u32TransferredData = (((uint32_t)pstcEndpoint->pstcEndpointBuffer->pu8BufferPos) - ((uint32_t)pstcEndpoint->pstcEndpointBuffer->pu8Buffer));
164  USBDBGVAL16("Trsfrd: ",u32TransferredData);
165  USBDBGVAL16("Sze: ",pstcEndpoint->pstcEndpointBuffer->u32DataSize);
166  while(pstcEndpoint->bIsActive == TRUE)
167  {
168  pstcEndpoint->bIsActive = FALSE;
169  }
170  if (u32TransferredData >= ((uint32_t)pstcEndpoint->pstcEndpointBuffer->u32DataSize))
171  {
172  USBDBG("EOT\n");
174 
175  /*if ((pstcEndpoint->u8EndpointAddress == 0x80) && (pstcEndpoint->pstcEndpointBuffer->u32DataSize % 64 == 0) && (pstcEndpoint->pstcEndpointBuffer->u32DataSize != 0))
176  {
177  USBDBG("NULL");
178  Usb_HalSend(pstcEndpoint,pstcEndpoint->pstcEndpointBuffer->pu8Buffer,0,&u16DataSend);
179  }*/
180  if (pstcEndpoint->bAutomaticNullTermination)
181  {
182  if (((pstcEndpoint->pstcEndpointBuffer->u32DataSize % pstcEndpoint->u16EndpointSize) == 0) && (pstcEndpoint->pstcEndpointBuffer->u32DataSize != 0))
183  {
184  Usb_HalSend(pstcEndpoint,pstcEndpoint->pstcEndpointBuffer->pu8Buffer,0,&u16DataSend);
185  }
186  }
187  if (pstcEndpoint->pfnRxTxBlockCallback != NULL)
188  {
189  pstcEndpoint->pfnRxTxBlockCallback(pstcUsb, pstcEndpoint);
190  }
191  return;
192  //Data Transfer End
193  }
195  Usb_HalSend(pstcEndpoint,pstcEndpoint->pstcEndpointBuffer->pu8BufferPos,pstcEndpoint->pstcEndpointBuffer->u32DataSize - u32TransferredData,&u16DataSend);
196  pstcEndpoint->pstcEndpointBuffer->pu8BufferPos += u16DataSend;
197  BITMASK_SET(*(pstcEndpoint->pstcEpStatusRegister),_EPNS_DRQIE);
198 }
199 
211 static void EndpointContinueRxCallback(stc_usbn_t* pstcUsb, stc_usbn_endpoint_data_t* pstcEndpoint)
212 {
213  uint16_t u16DataReceived = 0;
215  Usb_HalReceive(pstcEndpoint,pstcEndpoint->pstcEndpointBuffer->pu8BufferPos,&u16DataReceived,FALSE);
216  BITMASK_CLEAR(*(pstcEndpoint->pstcEpStatusRegister),_EPNS_DRQ);
217 
218  pstcEndpoint->pstcEndpointBuffer->u32DataSize += u16DataReceived;
219  pstcEndpoint->pstcEndpointBuffer->pu8BufferPos += u16DataReceived;
220  if (((((uint32_t)pstcEndpoint->pstcEndpointBuffer->pu8BufferPos) - ((uint32_t)pstcEndpoint->pstcEndpointBuffer->pu8Buffer)) >= ((uint32_t)pstcEndpoint->pstcEndpointBuffer->u32BufferSize)) || (u16DataReceived < pstcEndpoint->u16EndpointSize))
221  {
222  pstcEndpoint->pfnRxTxCallback = NULL;
223  if (pstcEndpoint->pfnRxTxBlockCallback != NULL)
224  {
225  pstcEndpoint->pfnRxTxBlockCallback(pstcUsb, pstcEndpoint);
226  }
227  return;
228  //Data Transfer End
229  }
230 
231  BITMASK_SET(*(pstcEndpoint->pstcEpStatusRegister),_EPNS_DRQIE);
232 }
233 
234 
252 en_result_t UsbDevice_ReceiveData(stc_usbn_t* pstcUsb, stc_usbn_endpoint_data_t* pstcEndpoint, uint8_t* pu8Buffer, uint32_t u32DataSize, en_usbsend_mode_t enMode)
253 {
254  uint16_t u16DataReceived = 0;
255  volatile uint32_t u32Timeout;
256  if (pstcEndpoint->pstcEndpointBuffer == NULL)
257  {
258  return ErrorInvalidParameter;
259  }
260  pstcEndpoint->pstcEndpointBuffer->pu8Buffer = pu8Buffer;
261  pstcEndpoint->pstcEndpointBuffer->pu8BufferPos = pu8Buffer;
262  pstcEndpoint->pstcEndpointBuffer->u32BufferSize = u32DataSize;
263  pstcEndpoint->pstcEndpointBuffer->u32DataSize = 0;
264  u32Timeout = 1000000;
265  while((BITMASK_ISSET(*(pstcEndpoint->pstcEpStatusRegister), _EPNS_BUSY)) && (u32Timeout > 0))
266  {
267  u32Timeout--;
268  }
269  switch(enMode)
270  {
271  case UsbPOLL:
272  while((((uint32_t)pstcEndpoint->pstcEndpointBuffer->pu8BufferPos) - ((uint32_t)pstcEndpoint->pstcEndpointBuffer->pu8Buffer)) < ((uint32_t)pstcEndpoint->pstcEndpointBuffer->u32BufferSize))
273  {
274  while (BITMASK_ISCLEARED(*(pstcEndpoint->pstcEpStatusRegister),_EPNS_DRQ));
275  Usb_HalReceive(pstcEndpoint,pstcEndpoint->pstcEndpointBuffer->pu8BufferPos,&u16DataReceived,TRUE);
276  pstcEndpoint->pstcEndpointBuffer->u32DataSize += u16DataReceived;
277  pstcEndpoint->pstcEndpointBuffer->pu8BufferPos += u16DataReceived;
278  if (u16DataReceived < pstcEndpoint->u16EndpointSize)
279  {
280  return Ok;
281  }
282  }
283  break;
284  case UsbIRQ:
285  pstcEndpoint->pfnRxTxCallback = EndpointContinueRxCallback;
286  BITMASK_SET(*(pstcEndpoint->pstcEpStatusRegister),_EPNS_DRQIE);
287  break;
288  case UsbDMA:
289  #if USB_USES_DMA == ON
290  return Error;
291  #else
292  return Error;
293  #endif
294  }
295  return Ok;
296 }
297 
315 en_result_t UsbDevice_SendData(stc_usbn_t* pstcUsb, stc_usbn_endpoint_data_t* pstcEndpoint, uint8_t* pu8Buffer, uint32_t u32DataSize, en_usbsend_mode_t enMode)
316 {
317  uint16_t u16DataSend = 0;
318  volatile uint32_t u32Timeout;
319  USBDBGVAL8("New ",(uint32_t)pstcEndpoint->u8EndpointAddress);
320  USBDBGVAL8("Sze ",(uint32_t)u32DataSize);
321  if (pstcEndpoint->pstcEndpointBuffer == NULL)
322  {
323  USBDBG("\nERROR: Buffer == NULL!");
324  return ErrorInvalidParameter;
325  }
326  if (pstcEndpoint->bIsActive == TRUE)
327  {
328  return ErrorNotReady;
329  }
330 
331  pstcEndpoint->pstcEndpointBuffer->pu8Buffer = pu8Buffer;
332  pstcEndpoint->pstcEndpointBuffer->pu8BufferPos = pu8Buffer;
333  pstcEndpoint->pstcEndpointBuffer->u32BufferSize = u32DataSize;
334  pstcEndpoint->pstcEndpointBuffer->u32DataSize = u32DataSize;
335  u32Timeout = 1000000;
336  while((BITMASK_ISSET(*(pstcEndpoint->pstcEpStatusRegister), _EPNS_BUSY)) && (u32Timeout > 0))
337  {
338  u32Timeout--;
339  }
340  switch(enMode)
341  {
342  case UsbPOLL:
343  pstcEndpoint->bIsActive = TRUE;
344  while((((uint32_t)pstcEndpoint->pstcEndpointBuffer->pu8BufferPos) - ((uint32_t)pstcEndpoint->pstcEndpointBuffer->pu8Buffer)) < ((uint32_t)pstcEndpoint->pstcEndpointBuffer->u32DataSize))
345  {
346  while (BITMASK_ISCLEARED(*(pstcEndpoint->pstcEpStatusRegister),_EPNS_DRQ));
347  Usb_HalSend(pstcEndpoint,pstcEndpoint->pstcEndpointBuffer->pu8BufferPos,pstcEndpoint->pstcEndpointBuffer->u32DataSize - (uint32_t)(pstcEndpoint->pstcEndpointBuffer->pu8BufferPos - pstcEndpoint->pstcEndpointBuffer->pu8Buffer),&u16DataSend);
348  pstcEndpoint->pstcEndpointBuffer->pu8BufferPos += u16DataSend;
349  }
350  if (pstcEndpoint->bAutomaticNullTermination)
351  {
352  if (((pstcEndpoint->pstcEndpointBuffer->u32DataSize % pstcEndpoint->u16EndpointSize) == 0) && (pstcEndpoint->pstcEndpointBuffer->u32DataSize != 0))
353  {
354  Usb_HalSend(pstcEndpoint,pstcEndpoint->pstcEndpointBuffer->pu8Buffer,0,&u16DataSend);
355  }
356  }
357  while(pstcEndpoint->bIsActive == TRUE)
358  {
359  pstcEndpoint->bIsActive = FALSE;
360  }
361  break;
362  case UsbIRQ:
363  pstcEndpoint->bIsActive = TRUE;
364  pstcEndpoint->pfnRxTxCallback = EndpointContinueTxCallback;
365  Usb_HalSend(pstcEndpoint,pu8Buffer,u32DataSize,&u16DataSend);
366  pstcEndpoint->pstcEndpointBuffer->pu8BufferPos += u16DataSend;
367  BITMASK_SET(*(pstcEndpoint->pstcEpStatusRegister),_EPNS_DRQIE);
368  break;
369  case UsbDMA:
370  #if USB_USES_DMA == ON
371  pstcEndpoint->bIsActive = TRUE;
372  pstcEndpoint->pfnRxTxCallback = EndpointContinueTxCallback;
373  Usb_HalSendDma(pstcEndpoint,pu8Buffer,u32DataSize,&u16DataSend);
374  pstcEndpoint->pstcEndpointBuffer->pu8BufferPos += u16DataSend;
375  BITMASK_SET(*(pstcEndpoint->pstcEpStatusRegister),_EPNS_DRQIE);
376  break;
377  #else
378  while(pstcEndpoint->bIsActive == TRUE)
379  {
380  pstcEndpoint->bIsActive = FALSE;
381  }
382  pstcEndpoint->bIsActive = FALSE;
383  return Error;
384  #endif
385  default:
386  return Error;
387 
388  }
389  return Ok;
390 }
391 
403 static void ControlRxCallback(stc_usbn_t* pstcUsb, stc_usbn_endpoint_data_t* pstcEndpoint)
404 {
405  static uint8_t pu8Buffer[64];
406  uint16_t u16BytesCount;
407  stc_usbdevice_intern_data_t* pstcUsbDeviceIntern;
408  pstcUsbDeviceIntern = UsbDeviceGetInternDataPtr(pstcUsb);
409  pstcUsbDeviceIntern->pu8LastControlTransfer = NULL;
410  Usb_HalReceive(pstcEndpoint,pu8Buffer,&u16BytesCount,FALSE);
411  while(pstcEndpoint->bIsActive == TRUE)
412  {
413  pstcEndpoint->bIsActive = FALSE;
414  }
415  pstcEndpoint->pstcEndpointBuffer->pu8Buffer = pu8Buffer;
416  pstcEndpoint->pstcEndpointBuffer->u32DataSize = u16BytesCount;
417  Usb_HalClearDrq(pstcUsbDeviceIntern->pstcEp0OUT);
418  if (u16BytesCount == 0)
419  {
420  if (pstcUsbDeviceIntern->enControlStage == StatusStage)
421  {
422  pstcUsbDeviceIntern->enControlStage = ControlStageUnknown;
423  //BITMASK_SET(*((uint16_t*)pstcEndpoint->pstcEpStatusRegister),_EPNS_BFINI);
424  //BITMASK_CLEAR(*((uint16_t*)pstcEndpoint->pstcEpStatusRegister),_EPNS_BFINI);
425  }
426  //UsbDevice_SendData(pstcUsb, pstcUsbDeviceIntern->pstcEp0IN, pu8Buffer, 0, UsbIRQ);
427  }
428  else
429  {
430  if (pstcUsbDeviceIntern->pfnControlTransferred != NULL)
431  {
432  pstcUsbDeviceIntern->pfnControlTransferred(pstcUsb, pstcEndpoint);
433  pstcUsbDeviceIntern->pfnControlTransferred = NULL;
434  } else
435  {
436  pstcUsbDeviceIntern->pu8LastControlTransfer = pu8Buffer;
437  }
438  }
439  pstcEndpoint->pstcEndpointBuffer->pu8Buffer = NULL;
440  pstcEndpoint->pstcEndpointBuffer->u32DataSize = 0;
441 }
442 
454 static void ControlTxCallback(stc_usbn_t* pstcUsb, stc_usbn_endpoint_data_t* pstcEndpoint)
455 {
456  stc_usbdevice_intern_data_t* pstcUsbDeviceIntern;
457  pstcUsbDeviceIntern = UsbDeviceGetInternDataPtr(pstcUsb);
458  pstcUsbDeviceIntern->enControlStage = StatusStage;
459  while(pstcEndpoint->bIsActive == TRUE)
460  {
461  pstcEndpoint->bIsActive = FALSE;
462  }
463 }
464 
474 static void ConfigChangeCallback(stc_usbn_t* pstcUsb)
475 {
476  uint8_t u8i;
477  stc_usbdevice_intern_data_t* pstcUsbDeviceIntern;
478  pstcUsbDeviceIntern = UsbDeviceGetInternDataPtr(pstcUsb); // get internal i
479 
480  if (Usb_GetConfiguration(pstcUsb) == 0) // Check configuration
481  {
482  pstcUsbDeviceIntern->enStatus = UsbConnected; // If it is 0, set status connected
483  }
484  else
485  {
486  pstcUsbDeviceIntern->enStatus = UsbConfigured; // If it is not 0, set status configured
487  }
488  for(u8i = 0;u8i < USBDEVICE_MAXCLASSES;u8i++)
489  {
490  if (pstcUsbDeviceIntern->stcUsbClasses[u8i].pfnConfCallback != NULL)
491  {
492  pstcUsbDeviceIntern->stcUsbClasses[u8i].pfnConfCallback(pstcUsb);
493  }
494  }
495 }
496 
506 static void SetupRxCallback(stc_usbn_t* pstcUsb)
507 {
508  static uint8_t u8DataToSend[255];
509  uint16_t u16BytesCount;
510  uint16_t u16Length;
511  static uint8_t pu8Buffer[8];
512  uint8_t u8Size;
513  static stc_usb_request_t stcSetup;
514  stc_usbdevice_intern_data_t* pstcUsbDeviceIntern;
515 
516  USB_ZERO_STRUCT(stcSetup);
517  pstcUsbDeviceIntern = UsbDeviceGetInternDataPtr(pstcUsb);
518 
519  pstcUsb->EP0OS_f.DRQOIE = 0;
520 
521  Usb_HalReceive(pstcUsbDeviceIntern->pstcEp0OUT,pu8Buffer,&u16BytesCount,TRUE);
522 
523 
524  pstcUsbDeviceIntern->enControlStage = SetupStage;
525  pstcUsbDeviceIntern->pu8LastControlTransfer = NULL;
526 
527  /* Decode Setup Package */
528  stcSetup.bmRequestType = pu8Buffer[0];
529  stcSetup.bRequest = pu8Buffer[1];
530  stcSetup.wValue = pu8Buffer[3] << 8;
531  stcSetup.wValue += pu8Buffer[2];
532  stcSetup.wIndex = pu8Buffer[5] << 8;
533  stcSetup.wIndex += pu8Buffer[4];
534  stcSetup.wLength = pu8Buffer[7] << 8;
535  stcSetup.wLength += pu8Buffer[6];
536 
538  {
539  pstcUsbDeviceIntern->enControlStage = DataStageIN;
540  }
541  else
542  {
543  pstcUsbDeviceIntern->enControlStage = DataStageOUT;
544  }
545 
546  //Usb_HalClearDrq(pstcUsbDeviceIntern->pstcEp0OUT);
547  if ((pu8Buffer[0] & 0x60) == USB_DEVREQ_CLSTYPE)
548  {
549  if(pu8Buffer[1] == USB_DEVREQ_SET_IDLE)
550  {
551  //recive SET_IDLE
552  BITMASK_CLEAR(pstcUsb->EP0IS,_EP0IS_DRQI); // DRQi <- 0
553 
554  pstcUsbDeviceIntern->enControlStage = StatusStage;
555  while(pstcUsbDeviceIntern->pstcEp0IN->bIsActive == TRUE)
556  {
557  pstcUsbDeviceIntern->pstcEp0IN->bIsActive = FALSE;
558  }
559 
560  pstcUsb->EP0OS_f.DRQOIE = 1;
561  return;
562  }
563  }
564 
565  switch (stcSetup.bmRequestType_f.Type)
566  {
567  case 0: // Device
568  switch(stcSetup.bRequest)
569  {
570  case GET_DESCRIPTOR:
571  switch((stcSetup.wValue) >> 8)
572  {
573  case 1: // Device Descriptor
574  u16Length = stcSetup.wLength;
575  USBDBG("Req Dev Desc\n");
576  USBDBGVAL16("Len ",u16Length);
577  if (u16Length > pstcUsbDeviceIntern->pu8DeviceDescriptor[0]) u16Length = pstcUsbDeviceIntern->pu8DeviceDescriptor[0];
578  USBDBGVAL16("Len New ",u16Length);
579  pstcUsbDeviceIntern->pstcEp0IN->pfnRxTxBlockCallback = ControlTxCallback;
580  UsbDevice_SendData(pstcUsb, pstcUsbDeviceIntern->pstcEp0IN, pstcUsbDeviceIntern->pu8DeviceDescriptor, u16Length, UsbPOLL);
581  break;
582  case 2: // Config Descriptor
583  u16Length = stcSetup.wLength;
584  USBDBG("Req Conf Desc\n");
585  USBDBGVAL16("Len ",u16Length);
586  if (u16Length > (pstcUsbDeviceIntern->pu8ConfigDescriptor[2] + pstcUsbDeviceIntern->pu8ConfigDescriptor[3]*256)) u16Length = (pstcUsbDeviceIntern->pu8ConfigDescriptor[2] + pstcUsbDeviceIntern->pu8ConfigDescriptor[3]*256);
587  USBDBGVAL16("Len New ",u16Length);
588  pstcUsbDeviceIntern->pstcEp0IN->pfnRxTxBlockCallback = ControlTxCallback;
589  UsbDevice_SendData(pstcUsb, pstcUsbDeviceIntern->pstcEp0IN, pstcUsbDeviceIntern->pu8ConfigDescriptor, u16Length, UsbPOLL);
590  break;
591  case 3: // String Descriptor
592  u16Length = stcSetup.wLength;
593  u8DataToSend[0] = 0;
594  switch ((stcSetup.wValue) & 0xFF)
595  {
596  case 0:
597  u8DataToSend[0] = 0x04;
598  u8DataToSend[1] = 0x03;
599  u8DataToSend[2] = 0x09;
600  u8DataToSend[3] = 0x04;
601  break;
602  default:
603  if (((stcSetup.wValue) & 0xFF) <= pstcUsbDeviceIntern->u8StringDescriptorCount)
604  {
605  if (pstcUsbDeviceIntern->pstcStringDescriptors[(stcSetup.wValue) & 0xFF].pu8String != NULL)
606  {
607  u8DataToSend[0] = 2;
608  u8DataToSend[1] = 0x03;
609  for(u8Size=0;(pstcUsbDeviceIntern->pstcStringDescriptors[((stcSetup.wValue) & 0xFF)-1].pu8String[u8Size] != 0);u8Size++)
610  {
611  u8DataToSend[2 + u8Size * 2] = pstcUsbDeviceIntern->pstcStringDescriptors[((stcSetup.wValue) & 0xFF)-1].pu8String[u8Size];
612  u8DataToSend[3 + u8Size * 2] = 0;
613  }
614  u8DataToSend[0] = u8DataToSend[0] + 2 * u8Size;
615  break;
616  } else
617  {
618  /* tbd UNICODE */
619  }
620  }
621  break;
622 
623  }
624  if (u8DataToSend[0] > stcSetup.wLength)
625  {
626  u8DataToSend[0] = stcSetup.wLength;
627  }
628  UsbDevice_SendData(pstcUsb, pstcUsbDeviceIntern->pstcEp0IN, u8DataToSend, u8DataToSend[0], UsbPOLL);
629  break;
630  case 0x22: // Report Descriptor
631 
632  u16Length = stcSetup.wLength;
633  USBDBG("Req Rep Desc\n");
634  USBDBGVAL16("Len ",u16Length);
635  if (u16Length > pstcUsbDeviceIntern->astcReportDescriptors[(stcSetup.wIndex & 0x03)].u16Size) u16Length = pstcUsbDeviceIntern->astcReportDescriptors[(stcSetup.wIndex & 0x03)].u16Size;
636  USBDBGVAL16("Len New ",u16Length);
637  pstcUsbDeviceIntern->pstcEp0IN->pfnRxTxBlockCallback = ControlTxCallback;
638  UsbDevice_SendData(pstcUsb, pstcUsbDeviceIntern->pstcEp0IN, pstcUsbDeviceIntern->astcReportDescriptors[(stcSetup.wIndex & 0x03)].pu8Descriptor, u16Length, UsbPOLL);
639  break;
640  default:
641  USBDBGVAL8("USB Request Unknown Descriptor: \n",(stcSetup.wValue) >> 8);
642  //while(1);
643  break;
644  }
645  break;
646  default:
647  //while(1);
648  USBDBGVAL8("USB Request Unknown: ",stcSetup.bRequest);
649  }
650  break;
651  case 1: // Class
652  if (stcSetup.wIndex < USBDEVICE_MAXCLASSES)
653  {
654  if (pstcUsbDeviceIntern->stcUsbClasses[stcSetup.wIndex].pfnSetupRequestCallback != NULL)
655  {
656  pstcUsbDeviceIntern->stcUsbClasses[stcSetup.wIndex].pfnSetupRequestCallback(pstcUsb, &stcSetup);
657  }
658  else
659  {
660  //while(1);
661  }
662  }
663  break;
664  case 2: // Vendor
665  break;
666  }
667  pstcUsbDeviceIntern->enControlStage = StatusStage;
668  while(pstcUsbDeviceIntern->pstcEp0IN->bIsActive == TRUE)
669  {
670  pstcUsbDeviceIntern->pstcEp0IN->bIsActive = FALSE;
671  }
672  //pstcUsbDeviceIntern->enControlStage = ControlStageUnknown;
673  /*if ((pu8Buffer[0] & 0x60) == USB_DEVREQ_CLSTYPE)
674  {
675  if(pu8Buffer[1] == USB_DEVREQ_SET_IDLE)
676  {
677  //recive SET_IDLE
678  BITMASK_CLEAR(pstcUsb->EP0IS,_EP0IS_DRQI); // DRQi <- 0
679  pstcUsb->EP0OS_f.DRQOIE = 1;
680  return;
681  }
682  }*/
683  pstcUsb->EP0OS_f.DRQOIE = 1;
684  u16BytesCount = u16BytesCount;
685 }
686 
700 void UsbDevice_SendDataControl(stc_usbn_t * pstcUsb,uint8_t* pu8Buffer, uint32_t u32DataLength)
701 {
702  volatile uint32_t u32Timeout;
703  stc_usbdevice_intern_data_t* pstcUsbDeviceIntern;
704  pstcUsbDeviceIntern = UsbDeviceGetInternDataPtr(pstcUsb);
705  pstcUsbDeviceIntern->pstcEp0IN->pfnRxTxBlockCallback = ControlTxCallback;
706  pstcUsbDeviceIntern->pstcEp0IN->bIsActive = FALSE;
707  UsbDevice_SendData(pstcUsb, pstcUsbDeviceIntern->pstcEp0IN, pu8Buffer, u32DataLength, UsbIRQ);
708 
709 }
710 
722 void UsbDevice_ReceiveDataControl(stc_usbn_t * pstcUsb, usbdevice_controltranfered_func_ptr_t pfnCompletionCallback)
723 {
724  stc_usbdevice_intern_data_t* pstcUsbDeviceIntern;
725  pstcUsbDeviceIntern = UsbDeviceGetInternDataPtr(pstcUsb);
726  if (pstcUsbDeviceIntern->pu8LastControlTransfer != NULL)
727  {
728  pfnCompletionCallback(pstcUsb, pstcUsbDeviceIntern->pstcEp0OUT);
729  pstcUsbDeviceIntern->pu8LastControlTransfer = NULL;
730  } else
731  {
732  pstcUsbDeviceIntern->pfnControlTransferred = pfnCompletionCallback;
733  /*if (pstcUsb->EP0OS_f.DRQO == 1)
734  {
735  ControlRxCallback(pstcUsb,pstcUsbDeviceIntern->pstcEp0OUT);
736  }
737  pstcUsb->EP0OS_f.DRQOIE = 1;*/
738  //Usb_SetEndpointRxTxCallback(pstcUsbDeviceIntern->pstcEp0OUT,ControlRxCallback);
739  }
740 
741 }
742 
758 en_result_t UsbDevice_RegisterVendorClass(stc_usbn_t* pstcUsb, stc_usbdevice_class_config_t* pstcUsbClassConfig, uint8_t** ppu8Buffer, uint8_t* pu8InterfaceLength)
759 {
760  uint16_t u16i;
761  stc_usbdevice_intern_data_t* pstcUsbDeviceIntern;
762  pstcUsbDeviceIntern = UsbDeviceGetInternDataPtr(pstcUsb);
763 
764  for(u16i = 0;u16i < (pstcUsbDeviceIntern->pu8ConfigDescriptor[2] + pstcUsbDeviceIntern->pu8ConfigDescriptor[3]*256);u16i++)
765  {
766  if (pstcUsbDeviceIntern->pu8ConfigDescriptor[u16i+1] == USBDESCR_INTERFACE)
767  {
768  if (((pstcUsbDeviceIntern->pu8ConfigDescriptor[u16i+5] == pstcUsbClassConfig->u8InterfaceClass) &&
769  (pstcUsbDeviceIntern->pu8ConfigDescriptor[u16i+6] == pstcUsbClassConfig->u8InterfaceSubClass) &&
770  (pstcUsbDeviceIntern->pu8ConfigDescriptor[u16i+7] == pstcUsbClassConfig->u8InterfaceProtocoll)) &&
771  ((pstcUsbClassConfig->u8InterfaceNumber == 0xFF) || (pstcUsbClassConfig->u8InterfaceNumber == pstcUsbDeviceIntern->pu8ConfigDescriptor[u16i+2])))
772  {
773  *ppu8Buffer = (pstcUsbDeviceIntern->pu8ConfigDescriptor + u16i);
774  pstcUsbDeviceIntern->stcUsbClasses[pstcUsbDeviceIntern->pu8ConfigDescriptor[u16i+2]].u8InterfaceClass = pstcUsbClassConfig->u8InterfaceClass;
775  pstcUsbDeviceIntern->stcUsbClasses[pstcUsbDeviceIntern->pu8ConfigDescriptor[u16i+2]].u8InterfaceSubClass = pstcUsbClassConfig->u8InterfaceSubClass;
776  pstcUsbDeviceIntern->stcUsbClasses[pstcUsbDeviceIntern->pu8ConfigDescriptor[u16i+2]].u8InterfaceProtocoll = pstcUsbClassConfig->u8InterfaceProtocoll;
777  pstcUsbDeviceIntern->stcUsbClasses[pstcUsbDeviceIntern->pu8ConfigDescriptor[u16i+2]].pfnSetupRequestCallback = pstcUsbClassConfig->pfnSetupRequestCallback;
778  pstcUsbDeviceIntern->stcUsbClasses[pstcUsbDeviceIntern->pu8ConfigDescriptor[u16i+2]].pfnConnectionCallback = pstcUsbClassConfig->pfnConnectionCallback;
779  pstcUsbDeviceIntern->stcUsbClasses[pstcUsbDeviceIntern->pu8ConfigDescriptor[u16i+2]].pfnDisconnectionCallback = pstcUsbClassConfig->pfnDisconnectionCallback;
780  pstcUsbDeviceIntern->stcUsbClasses[pstcUsbDeviceIntern->pu8ConfigDescriptor[u16i+2]].pfnConfCallback = pstcUsbClassConfig->pfnConfCallback;
781  while(u16i < (pstcUsbDeviceIntern->pu8ConfigDescriptor[2] + pstcUsbDeviceIntern->pu8ConfigDescriptor[3]*256))
782  {
783  u16i += pstcUsbDeviceIntern->pu8ConfigDescriptor[u16i]; // go to next descriptor
784  if (pstcUsbDeviceIntern->pu8ConfigDescriptor[u16i+1] == USBDESCR_INTERFACE)
785  {
786  break;
787  }
788  }
789  *pu8InterfaceLength = (uint8_t)((uint32_t)u16i - (uint32_t)(((uint32_t)*ppu8Buffer - (uint32_t)pstcUsbDeviceIntern->pu8ConfigDescriptor)));
790  return Ok;
791  }
792  }
793  u16i += pstcUsbDeviceIntern->pu8ConfigDescriptor[u16i] - 1; // go to next descriptor
794  }
795  return Error;
796 }
797 
808 en_usb_status_t UsbDevice_GetStatus(stc_usbn_t* pstcUsb)
809 {
810  stc_usbdevice_intern_data_t* pstcUsbDeviceIntern;
811  pstcUsbDeviceIntern = UsbDeviceGetInternDataPtr(pstcUsb);
812  return pstcUsbDeviceIntern->enStatus;
813 }
814 
824 /*en_usb_endpoint_status_t UsbDevice_HalEndpointStatus(stc_usbn_endpoint_data_t* pstcEpHandle)
825 {
826  return Usb_HalEndpointStatus(pstcEpHandle);
827 }*/
828 
838 void UsbDevice_InitCallback(stc_usbn_t* pstcUsb, stc_usb_config_t* pstcUsbConfig)
839 {
840  uint16_t i;
841  stc_usbdevice_intern_data_t* pstcUsbDeviceIntern;
842  pstcUsbDeviceIntern = UsbDeviceGetInternDataPtr(pstcUsb);
843  for(i = 0;i<(pstcUsbDeviceIntern->pu8ConfigDescriptor[2] + pstcUsbDeviceIntern->pu8ConfigDescriptor[3]*256);i++)
844  {
845  if (pstcUsbDeviceIntern->pu8ConfigDescriptor[i+1] == USBDESCR_ENDPOINT)
846  {
847  Usb_SetupHalEndpoint(pstcUsb,pstcUsbDeviceIntern->pu8ConfigDescriptor[i+2], pstcUsbDeviceIntern->pu8ConfigDescriptor[i+4] | (pstcUsbDeviceIntern->pu8ConfigDescriptor[i+5] << 8),(en_usb_ep_type_t)(pstcUsbDeviceIntern->pu8ConfigDescriptor[i+3] & 0x03),FALSE);
848  }
849  i += pstcUsbDeviceIntern->pu8ConfigDescriptor[i] - 1; // go to next descriptor
850  }
851  pstcUsbConfig->pfnSetpCallback = &SetupRxCallback;
852  pstcUsbConfig->pfnConfCallback = &ConfigChangeCallback;
853 
854  pstcUsbDeviceIntern->pstcEp0IN = Usb_GetEndpointPtr(pstcUsb,0x80);
855  pstcUsbDeviceIntern->pstcEp0OUT = Usb_GetEndpointPtr(pstcUsb,0x00);
856 
857  pstcUsbDeviceIntern->pstcEp0IN->pstcEndpointBuffer = &(pstcUsbDeviceIntern->stcEndpointBuffer);
858  pstcUsbDeviceIntern->pstcEp0OUT->pstcEndpointBuffer = &(pstcUsbDeviceIntern->stcEndpointBuffer);
859 
860  pstcUsbDeviceIntern->pstcEp0IN->pfnRxTxCallback = &ControlTxCallback;
861  pstcUsbDeviceIntern->pstcEp0IN->bAutomaticNullTermination = TRUE;
862 
863  pstcUsbDeviceIntern->pstcEp0OUT->pfnRxTxCallback = &ControlRxCallback;
864  pstcUsbDeviceIntern->pu8LastControlTransfer = NULL;
865 
866  if (pstcUsbDeviceIntern->pfnInitClassesCallback != NULL)
867  {
868  pstcUsbDeviceIntern->pfnInitClassesCallback(pstcUsb);
869  }
870 }
871 
883 void UsbDevice_Init(stc_usbn_t* pstcUsb, stc_usb_config_t* pstcUsbConfig, stc_usbdevice_config_t* pstcConfig)
884 {
885  stc_usbdevice_intern_data_t* pstcUsbDeviceIntern;
886  pstcUsbDeviceIntern = UsbDeviceGetInternDataPtr(pstcUsb);
887 
888  pstcUsbConfig->enMode |= UsbDeviceEnabled;
889  pstcUsbConfig->pu8DeviceDescriptor = pstcConfig->pu8DeviceDescriptor;
890  pstcUsbConfig->pu8ConfigDescriptor = pstcConfig->pu8ConfigDescriptor;
891  pstcUsbConfig->pfnDeviceInit = UsbDevice_InitCallback;
892  //pstcUsbConfig->pstcStringDescriptors = pstcConfig->pstcStringDescriptors;
893  //pstcUsbConfig->u8StringDescriptorCount = pstcConfig->u8StringDescriptorCount;
894 
895  pstcUsbDeviceIntern->pu8DeviceDescriptor = pstcConfig->pu8DeviceDescriptor;
896  pstcUsbDeviceIntern->pu8ConfigDescriptor = pstcConfig->pu8ConfigDescriptor;
897  pstcUsbDeviceIntern->astcReportDescriptors = pstcConfig->astcReportDescriptors;
898  pstcUsbDeviceIntern->pstcStringDescriptors = pstcConfig->pstcStringDescriptors;
899  pstcUsbDeviceIntern->u8StringDescriptorCount = pstcConfig->u8StringDescriptorCount;
900 
901  pstcUsbDeviceIntern->pfnInitClassesCallback = pstcConfig->pfnInitClassesCallback;
902 
903 
904  pstcUsbDeviceIntern->pstcEp0IN = Usb_GetEndpointPtr(pstcUsb,0x80);
905  pstcUsbDeviceIntern->pstcEp0OUT = Usb_GetEndpointPtr(pstcUsb,0x00);
906 
907  pstcUsbDeviceIntern->pstcEp0IN->pstcEndpointBuffer = &(pstcUsbDeviceIntern->stcEndpointBuffer);
908  pstcUsbDeviceIntern->pstcEp0OUT->pstcEndpointBuffer = &(pstcUsbDeviceIntern->stcEndpointBuffer);
909 
910  pstcUsbDeviceIntern->pstcEp0IN->pfnRxTxCallback = &ControlTxCallback;
911  pstcUsbDeviceIntern->pstcEp0OUT->pfnRxTxCallback = &ControlRxCallback;
912 }
913 
914 
927 {
928  stc_usbn_endpoint_data_t* pstcEndpointHandle;
929  pstcEndpointHandle = Usb_GetEndpointPtr(pstcUsb, pstcConfig->u8EndpointAddress);
930  pstcEndpointHandle->pfnRxTxBlockCallback = pstcConfig->pfnRxTxCallback;
931  pstcEndpointHandle->pstcEndpointBuffer = pstcConfig->pstcEndpointBuffer;
932  return pstcEndpointHandle;
933 }
934 
936 #endif /* (FM_PERIPHERAL_USB_DEVICE_ENABLED == ON) */