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

📄 usbtest.c

📁 nxp的ARM9处理器LPC3180代码,提供了几乎所有的外设示例程序.
💻 C
字号:
/*****************************************************************************
 *   usbtest.c:  main C entry file for Philips LPC3180 Family Microprocessors
 *
 *   Copyright(C) 2006, Philips Semiconductor
 *   All rights reserved.
 *
 *   History
 *   2005.10.01  ver 1.00    Prelimnary version, first Release
 *
******************************************************************************/
#include "LPC318x.H"                        /* LPC318x definitions */
#include "type.h"
#include "irq.h"
#include "usb318xcfg.h"
#include "timer.h"

#include "usb.h"
#include "usbcfg.h"
#include "usbhw.h"
#include "usbcore.h"
#include "hiduser.h"

#define STOP_MODE_USB_WAKEUP		0

extern volatile DWORD SuspendFlag;
extern volatile DWORD timer_counter;

BYTE InReport;			/* HID Input Report */
				/*   Bit0:    Button */
				/*   Bit1..7: Reserved */
BYTE OutReport;			/* HID Out Report */
				/*   Bit0..7: LEDs */

/* 
 * Keil USB device HID example is used for LPC318x USB device testing. 
 * Due to the hardware difference between MCB214x board and Nohau's LPC318x.
 * not all the HID LED feature can be tested.
 * Keil provides a Windows GUI utility HIDClient. This utility can be
 * used to test LPC318x USB device. 
 * To test USB DATA_OUT, open Keil's HIDClient, check or uncheck 
 * LED 0 and 1, GPO9 and GPO10 will be on and off accordingly. 
 * To test USB DATA_IN, On the LPC3180, press push button S4, GPI7, the GUI light 
 * should be checked or unchecked accordingly.   
 */

/*
 *  Get HID Input Report -> InReport
 */
void GetInReport (void) {
  if ((PIO_INP_STATE & 0x80) == 0) {	/* Check if push button, S4, is pressed */
    InReport = 0x01;
  } else {
    InReport = 0x00;
  }
}

/*
 *  Set HID Output Report <- OutReport
 */
void SetOutReport (void) {
  if ( OutReport & 0x01 ) {  
     PIO_OUTP_CLR |= 0x200;
  }
  else {
     PIO_OUTP_SET |= 0x200;	
  }
  
  if ( OutReport & 0x02 ) {  
     PIO_OUTP_CLR |= 0x400;
  }
  else {
     PIO_OUTP_SET |= 0x400;	
  }
}


/*****************************************************************************
**   Main Function  main()
******************************************************************************/
int main (void)
{
#if STOP_MODE_USB_WAKEUP
    DWORD WakeupFlag = 0;
#endif

    /* In the USB device initialization. The initialization sequence is crucial.
    Here is the correct sequence to initialize the USB block:
    (1) Initialize USB external tranceiver, ISP1301, the setting includes
	(i) set PSW_SE bit and BI-DI(default) bits in the Mode Control 2
	( to-be-debugged, somehow, the SPD_SUSP_CTRL bit can not be set ).
 	(ii) set USB to DAT_SE0 mode in Mode Control 1.
    (2) Setup USB PLL ( call USBPLL_Config() ).
    (3) Enable USB clock going to the USB block. ( call USB_DevEnable() ).
    (4) Add pull-up on DP (set DP_PULLUP bit ) and set VBUS_DRV bit
    in the OTG control register of ISP 1301.
    (5) Initializing USB device ( call USB_Init() ).
    (6) Set up USB connection ( call USB_Connect(TRUE) ). 
    */
 
    USB_OTGPreset();		/* Initialize ISP1301 USB external tranceiver */
				/* This needs to be done before USB block is
				initialized according to UM. */ 
    init_irq();			/* Disable MIC, SIC1, and SIC2 */
    init_timer();		/* the timer is for USB suspend and resume */

    USBPLL_Config();	/* Initialize USB PLL clock. */

    USB_DevEnable();	/* Enable the USB clock to the USB block */

    /* add pull-up on the USB DP and VBUS charge to 5V on the charge pump */ 
    Set_OTG_1301_Value( OTG_CTRL, 0x0021 );
  
    USB_Init();			/* USB Initialization */
    USB_Connect(TRUE);		/* USB Connect */

    Install_SIC1( 29, INT_HIGH_LEVEL, INT_LEVEL_SENSITIVE, USB_ISR );
    Install_SIC1( 30, INT_HIGH_LEVEL, INT_LEVEL_SENSITIVE, USB_ISR );
    Enable_SIC1( 29 );
    Enable_SIC1( 30 );

#if STOP_MODE_USB_WAKEUP
    START_RSR_INT = (1 << 22);	/* clear captured state */
    START_APR_INT = (1 << 22);	/* rising edge */
    START_ER_INT = (1 << 22);	/* Enable USB_INT to wake from STOP mode */
    START_ER_PIN = (1 << 22);
#endif

    /********* The main Function is an endless loop ***********/ 
    while( 1 ) 
    {
#if STOP_MODE_USB_WAKEUP
	if ( WakeupFlag == 1 )
	{
	    PIO_OUTP_SET = 0x400;

	    PWR_CTRL &= ~(0x01 << 2);
	    while ( PWR_CTRL & (0x01 << 2) );	/* Wait until in the Direct RUN mode */

	    if ( PWR_CTRL & 0x01 )		/* If stopped, resume it. */
	    {
		PWR_CTRL &= ~0x01;
	    }
	    /* somehow, the board resumes operation, but, from the current measurement,
	    it seems that CPU is in direct RUN mode, not back to the RUN mode. Further 
	    testing is needed, */
	    WakeupFlag = 0;
	}

//	if ( (SuspendFlag == 1 ) && (USB_INT_STAT & 0x00000100) == 0x0 )
	/* USB_needclk is not checked for now */
	if ( (SuspendFlag == 1) && (timer_counter > 4000) )
	{
	    disable_timer();
	    PIO_OUTP_CLR = 0x400;

	    START_RSR_INT = (1 << 22);	/* clear captured state */

	    PWR_CTRL &= ~(0x01 << 2);
	    while ( PWR_CTRL & (0x01 << 2) );	/* Wait until in the Direct RUN mode */

	    PWR_CTRL |= 0x01;
	    PWR_CTRL = 0x01;			/* enter STOP mode */
	    WakeupFlag = 1;
	}
#endif
    }
    return 0;
}

/*****************************************************************************
**                            End Of File
*****************************************************************************/

⌨️ 快捷键说明

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