Jason Engelman / Mbed 2 deprecated USBCDC

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 /*----------------------------------------------------------------------------
00003  *      Name:    vcomdemo.c
00004  *      Purpose: USB virtual COM port Demo
00005  *      Version: V1.20
00006  *----------------------------------------------------------------------------
00007  *      This software is supplied "AS IS" without any warranties, express,
00008  *      implied or statutory, including but not limited to the implied
00009  *      warranties of fitness for purpose, satisfactory quality and
00010  *      noninfringement. Keil extends you a royalty-free right to reproduce
00011  *      and distribute executable files created using this software for use
00012  *      on NXP Semiconductors LPC microcontroller devices only. Nothing else 
00013  *      gives you the right to use this software.
00014  *
00015  * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
00016  *---------------------------------------------------------------------------*/
00017 
00018 #include "LPC17xx.h"    
00019 //#include "LPC23xx.h"
00020 #include "type.h"
00021 
00022 #include "usb.h"
00023 #include "usbcfg.h"
00024 #include "usbhw.h"
00025 #include "usbcore.h"
00026 #include "cdc.h"
00027 #include "cdcuser.h"
00028 #include "serial.h"
00029 #include "vcomdemo.h"
00030 
00031 
00032 /*----------------------------------------------------------------------------
00033  Initialises the VCOM port.
00034  Call this function before using VCOM_putchar or VCOM_getchar
00035  *---------------------------------------------------------------------------*/
00036 void VCOM_Init(void) {
00037 #if PORT_NUM
00038   CDC_Init (1);
00039 #else
00040   CDC_Init (0);
00041 #endif
00042 }
00043 
00044 
00045 /*----------------------------------------------------------------------------
00046   Reads character from serial port buffer and writes to USB buffer
00047  *---------------------------------------------------------------------------*/
00048 void VCOM_Serial2Usb(void) {
00049   static char serBuf [USB_CDC_BUFSIZE];
00050          int  numBytesRead, numAvailByte;
00051     
00052   ser_AvailChar (&numAvailByte);
00053   if (numAvailByte > 0) {
00054     if (CDC_DepInEmpty) {
00055       numBytesRead = ser_Read (&serBuf[0], &numAvailByte);
00056 
00057       CDC_DepInEmpty = 0;
00058       USB_WriteEP (CDC_DEP_IN, (unsigned char *)&serBuf[0], numBytesRead);
00059     }
00060   }
00061 
00062 }
00063 
00064 /*----------------------------------------------------------------------------
00065   Reads character from USB buffer and writes to serial port buffer
00066  *---------------------------------------------------------------------------*/
00067 void VCOM_Usb2Serial(void) {
00068   static char serBuf [32];
00069          int  numBytesToRead, numBytesRead, numAvailByte;
00070 
00071   CDC_OutBufAvailChar (&numAvailByte);
00072   if (numAvailByte > 0) {
00073       numBytesToRead = numAvailByte > 32 ? 32 : numAvailByte; 
00074       numBytesRead = CDC_RdOutBuf (&serBuf[0], &numBytesToRead);
00075 #if PORT_NUM
00076       ser_Write (1, &serBuf[0], &numBytesRead);
00077 #else
00078       ser_Write (0, &serBuf[0], &numBytesRead);
00079 #endif    
00080   }
00081 
00082 }
00083 
00084 
00085 /*----------------------------------------------------------------------------
00086   checks the serial state and initiates notification
00087  *---------------------------------------------------------------------------*/
00088 void VCOM_CheckSerialState (void) {
00089          unsigned short temp;
00090   static unsigned short serialState;
00091 
00092   temp = CDC_GetSerialState();
00093   if (serialState != temp) {
00094      serialState = temp;
00095      CDC_NotificationIn();                  // send SERIAL_STATE notification
00096   }
00097 }
00098 
00099 /*----------------------------------------------------------------------------
00100   Main Program
00101  *---------------------------------------------------------------------------*/
00102 int main (void) {
00103 
00104   SystemInit();
00105 
00106   VCOM_Init();                              // VCOM Initialization
00107 
00108   USB_Init();                               // USB Initialization
00109   USB_Connect(TRUE);                        // USB Connect
00110 
00111   while (!USB_Configuration) ;              // wait until USB is configured
00112 
00113   while (1) {                               // Loop forever
00114     VCOM_Serial2Usb();                      // read serial port and initiate USB event
00115     VCOM_CheckSerialState();
00116     VCOM_Usb2Serial();
00117   } // end while                                               
00118 } // end main ()