⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vcomuser.c

📁 NXP产品LPC23XX的开发板的源文件
💻 C
📖 第 1 页 / 共 2 页
字号:
/*----------------------------------------------------------------------------
 *      U S B  -  K e r n e l
 *----------------------------------------------------------------------------
 *      Name:    vcomuser.c
 *      Purpose: Virtual COM custom user module file for Philips LPC214x Family 
 *		Microprocessors
 *      Version: V1.04
 *----------------------------------------------------------------------------
 *      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 
 *      Philips LPC2xxx microcontroller devices only. Nothing else gives you the 
 *      right to use this software. 
 *
 *      Copyright (c) 2005 Keil Software.
 *		Modified by Philips Semiconductor
 *---------------------------------------------------------------------------*/
#include <string.h>
#include "LPC23xx.h"                 

#include "type.h"
#include "usb.h"
#include "usbhw.h"
#include "usbcfg.h"
#include "usbcore.h"
#include "vcomuser.h"
#include "demo.h"

#define CR     0x0D
BYTE KeyPressed = 0;

/* Function that initializes LEDs                                             */
void LED_Init(void) {
  PINSEL10 = 0;                               /* Disable ETM interface, enable LEDs */
  FIO2DIR  = 0x000000FF;                /* P2.0..7 defined as Outputs         */
  FIO2MASK = 0x00000000;
}

/* Function that turns on requested LED                                       */
void LED_On (unsigned int num) {
  FIO2SET = (1 << num);
}

/* Function that turns off requested LED                                      */
void LED_Off (unsigned int num) {
  FIO2CLR = (1 << num);
}

/* Function that outputs value to LEDs                                        */
void LED_Out(unsigned int value) {
  FIO2CLR = 0xFF;                       /* Turn off all LEDs                  */
  FIO2SET = (value & 0xFF);             /* Turn on requested LEDs             */
}


BYTE ReportStatus0 = 0, ReportStatus1 = 0;
BYTE Data2Host0 = 0, Data2Host1 = 0;

#if USB_VCOM
BYTE RxLength0, RxLength1;

BYTE  USB2UARTBuf0[USB_MAX_PACKET0];
BYTE  USB2UARTBuf1[USB_MAX_PACKET0];

BYTE  UART2USBBuf0[USB_MAX_PACKET0];
BYTE  UART2USBBuf1[USB_MAX_PACKET0];

/* if the portNum is 0, EP1 is the endpoints for 
STATUS IN, EP2 EP is BULK IN, BULK OUT and UART0 Virtual COM port.
   if the portNum is 1, EP4 is the endpoints for 
STATUS IN, EP5 is BULK IN, BULK OUT and UART1 Virtual COM port */

void DeviceData2UART( BYTE portNum )
{
#if NO_UART_CABLE
  DWORD cnt;
#else
  DWORD cnt, i;
#endif
  BYTE *pData;

  if ( portNum == 0 ) {
	pData = &USB2UARTBuf0[0];
	cnt = USB_ReadEP(0x02, pData);
#if NO_UART_CABLE

	if (*pData & 0x01) LED_On(0x01); else LED_Off(0x01);
	if (*pData & 0x02) LED_On(0x02); else LED_Off(0x01);
	if (*pData & 0x04) LED_On(0x04); else LED_Off(0x04);
	if (*pData & 0x08) LED_On(0x08); else LED_Off(0x08);
	if (*pData & 0x10) LED_On(0x10); else LED_Off(0x10); 
	if (*pData & 0x20) LED_On(0x20); else LED_Off(0x20);
	if (*pData & 0x40) LED_On(0x40); else LED_Off(0x40);
	if (*pData & 0x80) LED_On(0x80); else LED_Off(0x80);
#else
	for ( i = 0; i < cnt; i++ ) {
  		putchar( portNum, (BYTE)*pData );
		pData++;
	}
#endif
  }
  else if ( portNum == 1 ) {
	pData = &USB2UARTBuf1[0];
	cnt = USB_ReadEP(0x05, pData);
#if NO_UART_CABLE	   
	if (*pData & 0x01) LED_On(0x01); else LED_Off(0x01);
	if (*pData & 0x02) LED_On(0x02); else LED_Off(0x01);
	if (*pData & 0x04) LED_On(0x04); else LED_Off(0x04);
	if (*pData & 0x08) LED_On(0x08); else LED_Off(0x08);
	if (*pData & 0x10) LED_On(0x10); else LED_Off(0x10); 
	if (*pData & 0x20) LED_On(0x20); else LED_Off(0x20);
	if (*pData & 0x40) LED_On(0x40); else LED_Off(0x40);
	if (*pData & 0x80) LED_On(0x80); else LED_Off(0x80);
#else
	for ( i = 0; i < cnt; i++ ) {
  		putchar( portNum, (BYTE)*pData );
		pData++;
	}
#endif  
  }
  return;
}

void DeviceData2Host( BYTE portNum )
{
#if NO_UART_CABLE
  const BYTE msg[] = "INT1 pressed\r\n";
  BYTE length;
#endif

  if ( portNum == 0 ) {
#if NO_UART_CABLE
    if ((IOPIN0 & S2) == 0 && !KeyPressed )				/* Check if S2 is pressed */
	{
		KeyPressed = 1;
		length = strlen(msg);
		memcpy( UART2USBBuf0, msg, length );
		USB_WriteEP(0x80 | 0x02, &UART2USBBuf0[0], length );
		KeyPressed = 0;
//		Data2Host0 = 0;
	}
#else
	/* if the RX buffer is not empty, suck in data from UART0 */
	if ( ((U0LSR & 0x01) == 1) && (RxLength0 < USB_VCOM_BUFSIZE) ) {
		UART2USBBuf0[RxLength0] = getchar( portNum );
		RxLength0++;
	}
	else if ( (RxLength0 == USB_VCOM_BUFSIZE) ) {
		USB_WriteEP(0x80 | 0x02, &UART2USBBuf0[0], USB_VCOM_BUFSIZE );
		RxLength0 = 0;
	}
#endif
  }
  else if ( portNum == 1 )
  {
#if NO_UART_CABLE
	if ((IOPIN0 & S2) == 0 && !KeyPressed )				/* Check if S2 is pressed */
	{
		KeyPressed = 1;
		length = strlen(msg);
		memcpy( UART2USBBuf1, msg, length );
		USB_WriteEP(0x80 | 0x05, &UART2USBBuf1[0], length ); 
		KeyPressed = 0;
		Data2Host1 = 0;
	}
#else
	/* if the RX buffer is not empty, suck in data from UART1 */
	if ( ((U1LSR & 0x01) == 1) && (RxLength1 < USB_VCOM_BUFSIZE) ) {
		UART2USBBuf1[RxLength1] = getchar( portNum );
		RxLength1++;
	}
	else if ( (RxLength1 == USB_VCOM_BUFSIZE) ) {
		USB_WriteEP(0x80 | 0x05, &UART2USBBuf1[0], USB_VCOM_BUFSIZE );
		RxLength1 = 0;
	}
#endif	
  }
  return;
}
#endif

/*
 *  USB Power Event Callback
 *    Parameter:       power: On(TRUE)/Off(FALSE)
 */

#if USB_POWER_EVENT
void USB_Power_Event (BOOL  power) {
  power;
}
#endif


/*
 *  USB Reset Event Callback
 */

#if USB_RESET_EVENT
void USB_Reset_Event (void) {
  USB_ResetCore();
}
#endif


/*
 *  USB Suspend Event Callback
 */

#if USB_SUSPEND_EVENT
void USB_Suspend_Event (void) {
}
#endif


/*
 *  USB Resume Event Callback
 */

#if USB_RESUME_EVENT
void USB_Resume_Event (void) {
}
#endif


/*
 *  USB Remote Wakeup Event Callback
 */

#if USB_WAKEUP_EVENT
void USB_WakeUp_Event (void) {
}
#endif


/*
 *  USB Start of frame Event Callback
 *    Parameter:       frame: 11-bit Frame Number
 */

#if USB_SOF_EVENT
void USB_SOF_Event (DWORD frame) {
  frame;
}
#endif


/*
 *  USB Error Event Callback
 *    Parameter:       error: Error Code
 */

#if USB_ERROR_EVENT
void USB_Error_Event (DWORD error) {
  error;
}
#endif

/*
 *  USB Set Configuration Event Callback
 */
#if USB_CONFIGURE_EVENT
void USB_Configure_Event (void) {
  if (USB_Configuration) {             /* Check if USB is configured */
    ReportStatus0 = ReportStatus1 = 1;
	Data2Host0 = Data2Host1 = 1;
  }
}
#endif

/*
 *  USB Set Interface Event Callback
 */

#if USB_INTERFACE_EVENT
void USB_Interface_Event (void) {
}
#endif

/*
 *  USB Set/Clear Feature Event Callback
 */

#if USB_FEATURE_EVENT
void USB_Feature_Event (void) {
}
#endif


#define P_EP(n) ((USB_EP_EVENT & (1 << (n))) ? USB_EndPoint##n : NULL)

/* USB Endpoint Events Callback Pointers */
const void (* USB_P_EP[16]) (DWORD event) = {
  P_EP(0),
  P_EP(1),
  P_EP(2),
  P_EP(3),
  P_EP(4),
  P_EP(5),
  P_EP(6),
  P_EP(7),
  P_EP(8),
  P_EP(9),
  P_EP(10),
  P_EP(11),
  P_EP(12),
  P_EP(13),
  P_EP(14),
  P_EP(15),
};

/*
 *  USB Endpoint 1 Event Callback
 *    Parameter:       event
 */

void USB_EndPoint1 (DWORD event) {
  event;
}

/*
 *  USB Endpoint 2 Event Callback
 *    Parameter:       event
 */

void USB_EndPoint2 (DWORD event) {
  switch (event) {
    case USB_EVT_IN:
	  Data2Host0 = 1;
      break;
	case USB_EVT_OUT:
	  DeviceData2UART( 0 );
	  break;
  }
  event;
}


/*
 *  USB Endpoint 3 Event Callback
 *    Parameter:       event
 */

void USB_EndPoint3 (DWORD event) {
  event;
}

/*
 *  USB Endpoint 4 Event Callback
 *    Parameter:       event
 */

void USB_EndPoint4 (DWORD event) {
  event;
}

/*
 *  USB Endpoint 5 Event Callback
 *    Parameter:       event
 */

void USB_EndPoint5 (DWORD event) {
  switch (event) {
    case USB_EVT_IN:
	  Data2Host1 = 1;
      break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -