1 /*----------------------------------------------------------------------------
\r
3 * Purpose: USB virtual COM port Demo
\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
14 * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
\r
15 *---------------------------------------------------------------------------*/
\r
17 #include "../LPC17xx.h"
\r
18 #include "lpc_types.h"
\r
23 #include "usbcore.h"
\r
25 #include "cdcuser.h"
\r
27 #include "vcomdemo.h"
\r
29 /* Example group ----------------------------------------------------------- */
\r
30 /** @defgroup USBDEV_USBCDC USBCDC
\r
31 * @ingroup USBDEV_Examples
\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
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
55 ser_AvailChar (&numAvailByte);
\r
56 if (numAvailByte > 0) {
\r
57 if (CDC_DepInEmpty) {
\r
58 numBytesRead = ser_Read (&serBuf[0], &numAvailByte);
\r
61 USB_WriteEP (CDC_DEP_IN, (unsigned char *)&serBuf[0], numBytesRead);
\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
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
79 ser_Write (1, &serBuf[0], &numBytesRead);
\r
81 ser_Write (0, &serBuf[0], &numBytesRead);
\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
95 temp = CDC_GetSerialState();
\r
96 if (serialState != temp) {
\r
98 CDC_NotificationIn(); // send SERIAL_STATE notification
\r
102 /*----------------------------------------------------------------------------
\r
104 *---------------------------------------------------------------------------*/
\r
108 VCOM_Init(); // VCOM Initialization
\r
110 USB_Init(); // USB Initialization
\r
111 USB_Connect(TRUE); // USB Connect
\r
113 while (!USB_Configuration) ; // wait until USB is configured
\r
115 while (1) { // Loop forever
\r
116 VCOM_Serial2Usb(); // read serial port and initiate USB event
\r
117 VCOM_CheckSerialState();
\r