PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
UsbDeviceCdcCom.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 /*****************************************************************************/
62 /*****************************************************************************/
63 /* Include files */
64 /*****************************************************************************/
65 
66 #include "UsbDeviceCdcCom.h"
67 
68 #if ((FM_PERIPHERAL_USB_DEVICE_ENABLED == ON) && (USBDEVICECDCCOM_ENABLED == ON))
69 
70 /*****************************************************************************/
71 /* Local pre-processor symbols/macros ('#define') */
72 /*****************************************************************************/
73 
74 
75 
76 /*****************************************************************************/
77 /* Global variable definitions (declared in header file with 'extern') */
78 /*****************************************************************************/
79 
80 static const char_t pcASCII[] = "0123456789ABCDEF";
81 
82 /*****************************************************************************/
83 /* Local type definitions ('typedef') */
84 /*****************************************************************************/
85 
86 
87 
88 /*****************************************************************************/
89 /* Local variable definitions ('static') */
90 /*****************************************************************************/
91 
92 static volatile uint8_t DataReceivedFlags = 0; //Status for received data
93 static volatile uint8_t DataSentFlags = 0; //Status for sent data
96 static uint8_t pu8LineCoding[8];
101 static char_t cSeperator = '\0';
102 static volatile uint32_t u32ReceivedData = 0;
105 static stc_usbn_t* pstcUsbHandle = NULL;
111 /*****************************************************************************/
112 /* Local function prototypes ('static') */
113 /*****************************************************************************/
114 static void ConfCallback(stc_usbn_t* pstcUsb);
115 static void RxTxCallback(stc_usbn_t* pstcUsb, struct stc_usbn_endpoint_data* pstcEndpoint);
116 static boolean_t SendData(uint8_t* pu8Buffer, uint32_t u32DataSize, en_usbsend_mode_t enMode);
117 static void DecodeSetupRequest(stc_usbn_t* pstcUsb, stc_usb_request_t* pstcSetup);
118 static void SetLineCoding(stc_usbn_t* pstcUsb, stc_usbn_endpoint_data_t* pstcEndpoint);
119 static void SendLineCoding(void);
120 static uint32_t ASCIItobin(uint8_t k);
123 
124 /*****************************************************************************/
125 /* Function implementation - global ('extern') and local ('static') */
126 /*****************************************************************************/
127 
137 static void ConfCallback(stc_usbn_t* pstcUsb)
138 {
139  if (UsbDevice_GetStatus(pstcUsb) == UsbConfigured)
140  {
141  bReady = TRUE;
142  } else
143  {
144  bReady = FALSE;
145  }
146 }
147 
158 static void RxTxCallback(stc_usbn_t* pstcUsb, struct stc_usbn_endpoint_data* pstcEndpoint)
159 {
160  volatile uint32_t u32i;
161  uint8_t* pu8Buffer = pstcEndpoint->pstcEndpointBuffer->pu8Buffer;
162  uint32_t u32DataSize = pstcEndpoint->pstcEndpointBuffer->u32DataSize;
163 
164  if ((pstcEndpoint->u8EndpointAddress) & 0x80)
165  {
166  DataSentFlags |= (1<<1);
167  if (pstnSentData != NULL)
168  {
169  pstnSentData();
170  }
171  }
172  else
173  {
174  DataReceivedFlags |= (1<<3); // setting data received flag
175  if (pstnReceivedData != NULL)
176  {
177  pstnReceivedData(pu8Buffer,u32DataSize);
178  UsbDevice_ReceiveData(pstcUsb, pstcEndpoint, pstcEndpoint->pstcEndpointBuffer->pu8Buffer, pstcEndpoint->pstcEndpointBuffer->u32BufferSize, UsbIRQ);
179  return;
180  }
181 
183  {
184  UsbDevice_SendData(pstcUsbHandle, pstcEndpointIN, pu8Buffer, u32DataSize, UsbPOLL);
185  }
186  for(u32i=0;u32i<u32DataSize;u32i++)
187  {
188  if ((cSeperator != '\0') && (*pu8Buffer == cSeperator)) {
190  }
192  {
193  pu8VcommBuffer[u32ReceivedData++] = *pu8Buffer++;
194  }
195  if ((cSeperator != '\0') && (bEchoMode) && (bVcommActive) && (bEchoAtSeperatorOnly) && (bSeperatorFlag))
196  {
198  }
199  }
200  UsbDevice_ReceiveData(pstcUsb, pstcEndpoint, pstcEndpoint->pstcEndpointBuffer->pu8Buffer, pstcEndpoint->pstcEndpointBuffer->u32BufferSize, UsbIRQ);
201  }
202 }
203 
204 
213 void UsbDeviceCdcCom_Init(stc_usbn_t* pstcUsb)
214 {
215  stc_usbdevice_class_config_t stcUsbClassConfig;
216  stc_usbdevice_endpoint_config_t stcEndpointConfig;
217  uint8_t* pu8Interface = NULL;
218  uint8_t u8InterfaceLength = 0;
219  uint8_t u8i = 0;
220  uint8_t u8NumEndpoints = 0;
221  pstcUsbHandle = pstcUsb;
222 
223  stcUsbClassConfig.u8InterfaceNumber = 0xFF; // do not use fix interface number, choose by class, subclass, protocol
224  stcUsbClassConfig.u8InterfaceClass = 0x02; // CDC Class
225  stcUsbClassConfig.u8InterfaceSubClass = 0x02; // Custom Sub Class
226  stcUsbClassConfig.u8InterfaceProtocoll = 0x01; // Custom Protocol
227  stcUsbClassConfig.pfnSetupRequestCallback = DecodeSetupRequest; // setup requests handled
228  stcUsbClassConfig.pfnConnectionCallback = NULL; // No connection callback handled
229  stcUsbClassConfig.pfnDisconnectionCallback = NULL; // No disconnection callback handled
230  stcUsbClassConfig.pfnConfCallback = ConfCallback; // Callback for configuration set
231  UsbDevice_RegisterVendorClass(pstcUsb,&stcUsbClassConfig,&pu8Interface,&u8InterfaceLength);
232 
233  stcUsbClassConfig.u8InterfaceNumber = 0xFF; // do not use fix interface number, choose by class, subclass, protocol
234  stcUsbClassConfig.u8InterfaceClass = 0x0A; // CDC Class
235  stcUsbClassConfig.u8InterfaceSubClass = 0x00; // Custom Sub Class
236  stcUsbClassConfig.u8InterfaceProtocoll = 0x00; // Custom Protocol
237  stcUsbClassConfig.pfnSetupRequestCallback = DecodeSetupRequest; // setup requests handled
238  stcUsbClassConfig.pfnConnectionCallback = NULL; // No connection callback handled
239  stcUsbClassConfig.pfnDisconnectionCallback = NULL; // No disconnection callback handled
240  stcUsbClassConfig.pfnConfCallback = ConfCallback; // Callback for configuration set
241  UsbDevice_RegisterVendorClass(pstcUsb,&stcUsbClassConfig,&pu8Interface,&u8InterfaceLength);
242 
243  for(u8i = 0;u8i < u8InterfaceLength;)
244  {
245  u8i += pu8Interface[u8i];
246  if (USBDESCR_ENDPOINT == pu8Interface[u8i + 1])
247  {
248  stcEndpointConfig.u8EndpointAddress = pu8Interface[u8i + 2];
249  stcEndpointConfig.pfnRxTxCallback = RxTxCallback;
250  if (((stcEndpointConfig.u8EndpointAddress) & 0x80) == 0)
251  {
252 
253  stcEndpointConfig.pstcEndpointBuffer = &stcEndpointBufferOUT;
254  pstcEndpointOUT = UsbDevice_SetupEndpoint(pstcUsb, &stcEndpointConfig);
255 
257  }
258  else
259  {
260  stcEndpointConfig.pstcEndpointBuffer = &stcEndpointBufferIN;
261  pstcEndpointIN = UsbDevice_SetupEndpoint(pstcUsb, &stcEndpointConfig);
262  }
263  u8NumEndpoints++;
264  }
265  }
266 
267 }
268 
277 {
278  pstnReceivedData = pstnCallback;
279 }
280 
289 {
290  pstnSentData = pstnCallback;
291 }
292 
303 static boolean_t SendData(uint8_t* pu8Buffer, uint32_t u32DataSize, en_usbsend_mode_t enMode)
304 {
306  {
307  DataSentFlags &= ~(1<<1);
308 
309  while(UsbDevice_SendData(pstcUsbHandle, pstcEndpointIN, pu8Buffer, u32DataSize, enMode) != Ok);
310  if (enMode == UsbPOLL)
311  {
312  DataSentFlags |= (1<<1);
313  }
314  return TRUE;
315  }
316  return FALSE;
317 }
318 
326 {
327  if ((DataSentFlags & (1<<1)) > 0)
328  {
329  return TRUE;
330  }
331  else
332  {
333  return FALSE;
334  }
335 }
336 
344 {
345  pfnLineCodingChange = pfnLineCodingChangeCallback;
346 }
347 
356 static void DecodeSetupRequest(stc_usbn_t* pstcUsb, stc_usb_request_t* pstcSetup)
357 {
358  switch (pstcSetup->bRequest)
359  {
360  case SET_LINE_CODING:
362  UsbDevice_SendDataControl(pstcUsbHandle,(uint8_t *)NULL, 0);
363  return;
364  case GET_LINE_CODING:
365  SendLineCoding();
366  return;
368  if (pstcSetup->wValue & 0x01)
369  {
370  bVcommActive = TRUE;
371  }
372  else
373  {
375  }
376  if (pstcSetup->wValue & 0x02)
377  {
378 
379  }
380  else
381  {
382 
383  }
384  UsbDevice_SendDataControl(pstcUsbHandle,(uint8_t *)NULL, 0);
385  return;
386  default:
387  UsbDevice_SendDataControl(pstcUsbHandle,(uint8_t *)NULL, 0);
388  return;
389  }
390 }
391 
401 static void SetLineCoding(stc_usbn_t* pstcUsb, stc_usbn_endpoint_data_t* pstcEndpoint)
402 {
403  static uint8_t pu8Buffer[8];
404  static volatile uint32_t u32Rate = 0;
405  memcpy(pu8Buffer,pstcEndpoint->pstcEndpointBuffer->pu8Buffer,8);
406  u32Rate = 0;
407  u32Rate += ((uint32_t)pu8Buffer[3]) << 24;
408  u32Rate += ((uint32_t)pu8Buffer[2]) << 16;
409  u32Rate += ((uint32_t)pu8Buffer[1]) << 8;
410  u32Rate += ((uint32_t)pu8Buffer[0]) << 0;
411  /*switch(u32Rate)
412  {
413  case 110:
414  break;
415  case 300:
416  break;
417  case 1200:
418  break;
419  case 2400:
420  break;
421  case 4800:
422  break;
423  case 9600:
424  break;
425  case 19200:
426  break;
427  case 38400:
428  break;
429  case 57600:
430  break;
431  case 115200:
432  break;
433  case 230400:
434  break;
435  case 460800:
436  break;
437  case 921600:
438  break;
439  default:
440  u32Rate = u32Rate;
441  return;
442  }*/
443  stcLineCoding.dwDTERate = u32Rate;
444  stcLineCoding.bCharFormat = pu8Buffer[4];
445  stcLineCoding.bParityType = pu8Buffer[5];
446  stcLineCoding.bDataBits = pu8Buffer[6];
447  if (pfnLineCodingChange != NULL)
448  {
449  pfnLineCodingChange(&stcLineCoding);
450  }
451 
452 }
458 static void SendLineCoding(void)
459 {
460  pu8LineCoding[3] = (uint8_t)((stcLineCoding.dwDTERate >> 24) & 0xFF);
461  pu8LineCoding[2] = (uint8_t)((stcLineCoding.dwDTERate >> 16) & 0xFF);
462  pu8LineCoding[1] = (uint8_t)((stcLineCoding.dwDTERate >> 8) & 0xFF);
463  pu8LineCoding[0] = (uint8_t)((stcLineCoding.dwDTERate >> 0) & 0xFF);
464  pu8LineCoding[4] = stcLineCoding.bCharFormat;
465  pu8LineCoding[5] = stcLineCoding.bParityType;
466  pu8LineCoding[6] = stcLineCoding.bDataBits;
468 
469 }
470 
471 
472 
480 {
481  return (bVcommActive && bReady);
482 }
483 
484 
495 {
496  if (bVcommActive)
497  {
498  SendData((uint8_t*)&c,1,UsbPOLL);
499  return 0;
500  }
501  return 1;
502 }
503 
504 
515 {
516  volatile uint32_t u32Size;
517  if (bVcommActive)
518  {
519  for(u32Size=0;(pcBuffer[u32Size] != '\0');u32Size++); //calculating size
520 
521  SendData((uint8_t *)pcBuffer,u32Size,UsbPOLL);
522  return 0;
523  }
524  return 1;
525 }
526 
527 
538 uint8_t UsbDeviceCdcCom_SendBuffer(uint8_t *pu8Buffer, uint32_t u32Size)
539 {
540  if (bVcommActive)
541  {
542  #if USB_USES_DMA == L3_ON
543  SendData((uint8_t *)pu8Buffer,u32Size,UsbDMA);
544  #else
545  SendData((uint8_t *)pu8Buffer,u32Size,UsbIRQ);
546  #endif
547  return 0;
548  }
549  return 1;
550 }
551 
552 
563 void UsbDeviceCdcCom_SetSeparator(const char_t cNewSeperator)
564 {
565  cSeperator = cNewSeperator;
566 }
567 
568 
578 {
579  bEchoMode = bEchoOnOff;
580 }
581 
590 void UsbDeviceCdcCom_SetEchoAtSeperatorOnly(boolean_t bEchoAtSeperatorOnlyOnOff)
591 {
592  bEchoAtSeperatorOnly = bEchoAtSeperatorOnlyOnOff;
593 }
594 
604 {
605  if ((cSeperator == '\0') || (bSeperatorFlag == TRUE))
606  {
607  return u32ReceivedData;
608  }
609  return 0;
610 }
611 
612 
613 
623 uint32_t UsbDeviceCdcCom_ReceiveBuffer(uint8_t *pu8Buffer)
624 {
625  uint32_t u32ReturnValue;
626  for(u32ReturnValue=0;u32ReturnValue<u32ReceivedData;u32ReturnValue++)
627  {
628  pu8Buffer[u32ReturnValue] = pu8VcommBuffer[u32ReturnValue];
629  }
630  u32ReceivedData = 0;
632  return u32ReturnValue;
633 }
634 
635 
645 {
646  uint32_t i;
647  uint8_t c;
648  while (u32ReceivedData == 0);
649  c = pu8VcommBuffer[0];
650  /* shift buffer by 1 byte */
651  for(i=0;i<(u32ReceivedData - 1);i++)
652  {
653  pu8VcommBuffer[i] = pu8VcommBuffer[i + 1];
654  }
655  u32ReceivedData--;
656  return c;
657 }
658 
659 
669 {
670  if (u32ReceivedData > 0)
671  {
673  }
674  return 0;
675 }
676 
677 
688 uint8_t UsbDeviceCdcCom_SendHex(uint32_t n, uint8_t digits)
689 {
690  uint8_t i,ch,div=0;
691 
692  if (UsbDeviceCdcCom_SendString("0x")) return 1; /* hex string */
693  div=(digits-1) << 2; /* init shift divisor */
694 
695  for (i=0; i<digits; i++) {
696  ch = (n >> div) & 0xF;/* get hex-digit value */
697  if (UsbDeviceCdcCom_SendByte(pcASCII[ch])) return 1; /* prompt to terminal as ASCII */
698  div-=4; /* next digit shift */
699  }
700  return 0;
701 }
702 
703 
713 uint8_t UsbDeviceCdcCom_SendDec(uint32_t x)
714 {
715  int16_t i;
716  char_t buf[9];
717  if (x == 0)
718  {
719  if (UsbDeviceCdcCom_SendString(" 0")) return 1;
720  return 0;
721  }
722  buf[8]='\0'; /* end sign of string */
723 
724  for (i=8; i>0; i--)
725  {
726  buf[i-1] = pcASCII[x % 10];
727  x = x/10;
728 
729  }
730 
731  for (i=0; buf[i]=='0'; i++) // no print16_t of zero
732  {
733  buf[i] = ' ';
734  }
735  if (UsbDeviceCdcCom_SendString(buf)) return 1; /* send string */
736  return 0;
737 }
738 
739 
750 void UsbDeviceCdcCom_SendBinAsci4(uint16_t a, uint8_t base, char_t fill)
751 {
752  uint8_t val;
753  a &= 0x0F;
754 
755  switch(base)
756  {
757  case HEX: if (a <= 9) UsbDeviceCdcCom_SendByte(a + '0');
758  else UsbDeviceCdcCom_SendByte(a + 'A' - 10);
759  break;
760 
761  case DEC: val = a / 10;
762  if( val ) UsbDeviceCdcCom_SendByte( a / 10 + '0');
763  else UsbDeviceCdcCom_SendByte( fill );
764  UsbDeviceCdcCom_SendByte( a % 10 + '0');
765  break;
766 
767  default: UsbDeviceCdcCom_SendString(" <format not supported> ");
768  }
769 }
770 
771 
782 void UsbDeviceCdcCom_SendBinAsci8(uint16_t a, uint8_t base, char_t fill)
783 {
784  uint16_t rem,val,d;
785  uint8_t filling=1;
786 
787  switch(base)
788  {
789  case HEX: UsbDeviceCdcCom_SendBinAsci4(a >> 4, HEX, fill);
791  break;
792 
793  case DEC: rem = a;
794  for( d=100; d >= 10; d = d / 10)
795  {
796  val = rem / d; rem = rem % d;
797  if( val ) filling=0;
798  if( filling ) UsbDeviceCdcCom_SendByte( fill );
799  else UsbDeviceCdcCom_SendByte( val + '0' );
800  }
801  UsbDeviceCdcCom_SendByte( rem + '0');
802  break;
803 
804  default: UsbDeviceCdcCom_SendString(" <format not supported> ");
805  }
806 
807 }
808 
809 
819 {
820 
821  uint8_t rem, val;
823 
824  rem = a;
825 
826  val = rem / 10; rem = rem % 10;
827  UsbDeviceCdcCom_SendByte( val + '0' );
828 
829  UsbDeviceCdcCom_SendByte( rem + '0');
830 
831 
832 }
833 
834 
844 uint32_t UsbDeviceCdcCom_ReceiveHex(uint8_t digits)
845 {
846  uint32_t number=0;
847  signed digit=0;
848  uint8_t abort=0,mlt=0,i,key;
851  mlt=(4*(digits-1)); /* 20 for 6 hex-digit numers, 4 for 2 hex-digits */
852  for (i=0;i<digits;i++)
853  {
854  digit = -1;
855  while ((digit==-1) & (!abort)) /* input next valid digit */
856  {
857  key = UsbDeviceCdcCom_ReceiveByte(); /* wait for next key input (ASCII) */
858  if (key == 27) abort=1; /* ESC aborts */
859  digit = ASCIItobin(key); /* convert to number */
860  if (digit == -1) UsbDeviceCdcCom_SendByte(8); /* backspace if not valid */
861  }
862  number+= (digit << mlt); /* add digit value to number */
863  mlt-=4; /* next digit shift */
864  }
866  if (!abort)
867  return number; /* return input value */
868  else
869  {
870  UsbDeviceCdcCom_SendString("\n\n input cancled \n");
871  return 0; /* return abort indicator */
872  }
873 }
874 
875 /* Internal */
876 static uint32_t ASCIItobin(uint8_t k)
877 {
878  char_t d=(char_t) -1;
879  if ((k > 47) & (k < 58)) d = k - 48; /* 0..9 */
880  if ((k > 64) & (k < 71)) d = k - 55; /* A..F */
881  if ((k > 96) & (k < 103)) d = k - 87; /* a..f */
882  return d;
883 }
884 #endif
885 
886 
887 
888