X-Git-Url: http://git.asbjorn.biz/?p=rapper.git;a=blobdiff_plain;f=new_cmsis%2Fusb%2Fvcomdemo.c;fp=new_cmsis%2Fusb%2Fvcomdemo.c;h=e99e94ec4afb066d0f815ac8c09ed88e5d362268;hp=0000000000000000000000000000000000000000;hb=0163b16211dc58d6619b923b370be8491fb984cb;hpb=ed7a4dbc9f9d17923417de5ebc515e76e979c6f9 diff --git a/new_cmsis/usb/vcomdemo.c b/new_cmsis/usb/vcomdemo.c new file mode 100755 index 0000000..e99e94e --- /dev/null +++ b/new_cmsis/usb/vcomdemo.c @@ -0,0 +1,124 @@ +/*---------------------------------------------------------------------------- + * Name: vcomdemo.c + * Purpose: USB virtual COM port Demo + * Version: V1.20 + *---------------------------------------------------------------------------- + * This software is supplied "AS IS" without any warranties, express, + * implied or statutory, including but not limited to the implied + * warranties of fitness for purpose, satisfactory quality and + * noninfringement. Keil extends you a royalty-free right to reproduce + * and distribute executable files created using this software for use + * on NXP Semiconductors LPC microcontroller devices only. Nothing else + * gives you the right to use this software. + * + * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. + *---------------------------------------------------------------------------*/ + +#include "../LPC17xx.h" +#include "lpc_types.h" + +#include "usb.h" +#include "usbcfg.h" +#include "usbhw.h" +#include "usbcore.h" +#include "cdc.h" +#include "cdcuser.h" +#include "serial.h" +#include "vcomdemo.h" + +/* Example group ----------------------------------------------------------- */ +/** @defgroup USBDEV_USBCDC USBCDC + * @ingroup USBDEV_Examples + * @{ + */ + +/*---------------------------------------------------------------------------- + Initialises the VCOM port. + Call this function before using VCOM_putchar or VCOM_getchar + *---------------------------------------------------------------------------*/ +void VCOM_Init(void) { +#if PORT_NUM + CDC_Init (1); +#else + CDC_Init (0); +#endif +} + + +/*---------------------------------------------------------------------------- + Reads character from serial port buffer and writes to USB buffer + *---------------------------------------------------------------------------*/ +void VCOM_Serial2Usb(void) { + static char serBuf [USB_CDC_BUFSIZE]; + int numBytesRead, numAvailByte; + + ser_AvailChar (&numAvailByte); + if (numAvailByte > 0) { + if (CDC_DepInEmpty) { + numBytesRead = ser_Read (&serBuf[0], &numAvailByte); + + CDC_DepInEmpty = 0; + USB_WriteEP (CDC_DEP_IN, (unsigned char *)&serBuf[0], numBytesRead); + } + } + +} + +/*---------------------------------------------------------------------------- + Reads character from USB buffer and writes to serial port buffer + *---------------------------------------------------------------------------*/ +void VCOM_Usb2Serial(void) { + static char serBuf [32]; + int numBytesToRead, numBytesRead, numAvailByte; + + CDC_OutBufAvailChar (&numAvailByte); + if (numAvailByte > 0) { + numBytesToRead = numAvailByte > 32 ? 32 : numAvailByte; + numBytesRead = CDC_RdOutBuf (&serBuf[0], &numBytesToRead); +#if PORT_NUM + ser_Write (1, &serBuf[0], &numBytesRead); +#else + ser_Write (0, &serBuf[0], &numBytesRead); +#endif + } + +} + + +/*---------------------------------------------------------------------------- + checks the serial state and initiates notification + *---------------------------------------------------------------------------*/ +void VCOM_CheckSerialState (void) { + unsigned short temp; + static unsigned short serialState; + + temp = CDC_GetSerialState(); + if (serialState != temp) { + serialState = temp; + CDC_NotificationIn(); // send SERIAL_STATE notification + } +} + +/*---------------------------------------------------------------------------- + Main Program + *---------------------------------------------------------------------------*/ +int main (void) { + SystemInit(); + + VCOM_Init(); // VCOM Initialization + + USB_Init(); // USB Initialization + USB_Connect(TRUE); // USB Connect + + while (!USB_Configuration) ; // wait until USB is configured + + while (1) { // Loop forever + VCOM_Serial2Usb(); // read serial port and initiate USB event + VCOM_CheckSerialState(); + VCOM_Usb2Serial(); + } // end while +} // end main () + +/* + * @} + */