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