initial usb
[rapper.git] / new_cmsis / usb / vcomdemo.c
1 /*----------------------------------------------------------------------------\r
2  *      Name:    vcomdemo.c\r
3  *      Purpose: USB virtual COM port Demo\r
4  *      Version: V1.20\r
5  *----------------------------------------------------------------------------\r
6  *      This software is supplied "AS IS" without any warranties, express,\r
7  *      implied or statutory, including but not limited to the implied\r
8  *      warranties of fitness for purpose, satisfactory quality and\r
9  *      noninfringement. Keil extends you a royalty-free right to reproduce\r
10  *      and distribute executable files created using this software for use\r
11  *      on NXP Semiconductors LPC microcontroller devices only. Nothing else\r
12  *      gives you the right to use this software.\r
13  *\r
14  * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.\r
15  *---------------------------------------------------------------------------*/\r
16 \r
17 #include "../LPC17xx.h"\r
18 #include "lpc_types.h"\r
19 \r
20 #include "usb.h"\r
21 #include "usbcfg.h"\r
22 #include "usbhw.h"\r
23 #include "usbcore.h"\r
24 #include "cdc.h"\r
25 #include "cdcuser.h"\r
26 #include "serial.h"\r
27 #include "vcomdemo.h"\r
28 \r
29 /* Example group ----------------------------------------------------------- */\r
30 /** @defgroup USBDEV_USBCDC     USBCDC\r
31  * @ingroup USBDEV_Examples\r
32  * @{\r
33  */\r
34 \r
35 /*----------------------------------------------------------------------------\r
36  Initialises the VCOM port.\r
37  Call this function before using VCOM_putchar or VCOM_getchar\r
38  *---------------------------------------------------------------------------*/\r
39 void VCOM_Init(void) {\r
40 #if PORT_NUM\r
41   CDC_Init (1);\r
42 #else\r
43   CDC_Init (0);\r
44 #endif\r
45 }\r
46 \r
47 \r
48 /*----------------------------------------------------------------------------\r
49   Reads character from serial port buffer and writes to USB buffer\r
50  *---------------------------------------------------------------------------*/\r
51 void VCOM_Serial2Usb(void) {\r
52   static char serBuf [USB_CDC_BUFSIZE];\r
53          int  numBytesRead, numAvailByte;\r
54 \r
55   ser_AvailChar (&numAvailByte);\r
56   if (numAvailByte > 0) {\r
57     if (CDC_DepInEmpty) {\r
58       numBytesRead = ser_Read (&serBuf[0], &numAvailByte);\r
59 \r
60       CDC_DepInEmpty = 0;\r
61           USB_WriteEP (CDC_DEP_IN, (unsigned char *)&serBuf[0], numBytesRead);\r
62     }\r
63   }\r
64 \r
65 }\r
66 \r
67 /*----------------------------------------------------------------------------\r
68   Reads character from USB buffer and writes to serial port buffer\r
69  *---------------------------------------------------------------------------*/\r
70 void VCOM_Usb2Serial(void) {\r
71   static char serBuf [32];\r
72          int  numBytesToRead, numBytesRead, numAvailByte;\r
73 \r
74   CDC_OutBufAvailChar (&numAvailByte);\r
75   if (numAvailByte > 0) {\r
76       numBytesToRead = numAvailByte > 32 ? 32 : numAvailByte;\r
77       numBytesRead = CDC_RdOutBuf (&serBuf[0], &numBytesToRead);\r
78 #if PORT_NUM\r
79       ser_Write (1, &serBuf[0], &numBytesRead);\r
80 #else\r
81       ser_Write (0, &serBuf[0], &numBytesRead);\r
82 #endif\r
83   }\r
84 \r
85 }\r
86 \r
87 \r
88 /*----------------------------------------------------------------------------\r
89   checks the serial state and initiates notification\r
90  *---------------------------------------------------------------------------*/\r
91 void VCOM_CheckSerialState (void) {\r
92          unsigned short temp;\r
93   static unsigned short serialState;\r
94 \r
95   temp = CDC_GetSerialState();\r
96   if (serialState != temp) {\r
97      serialState = temp;\r
98      CDC_NotificationIn();                  // send SERIAL_STATE notification\r
99   }\r
100 }\r
101 \r
102 /*----------------------------------------------------------------------------\r
103   Main Program\r
104  *---------------------------------------------------------------------------*/\r
105 int main (void) {\r
106   SystemInit();\r
107 \r
108   VCOM_Init();                              // VCOM Initialization\r
109 \r
110   USB_Init();                               // USB Initialization\r
111   USB_Connect(TRUE);                        // USB Connect\r
112 \r
113   while (!USB_Configuration) ;              // wait until USB is configured\r
114 \r
115   while (1) {                               // Loop forever\r
116     VCOM_Serial2Usb();                      // read serial port and initiate USB event\r
117     VCOM_CheckSerialState();\r
118         VCOM_Usb2Serial();\r
119   } // end while\r
120 } // end main ()\r
121 \r
122 /*\r
123  * @}\r
124  */\r