?? 00000005.htm
字號:
<?xml version="1.0" encoding="gb2312"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"/><title>Re: usb數字小鍵盤 turbolinux </title></head><body><center><h1>BBS 水木清華站∶精華區</h1></center><a name="top"></a>發信人: luckbird (幸福鳥), 信區: Embedded <br />標 題: Re: usb數字小鍵盤 <br />發信站: BBS 水木清華站 (Thu Apr 18 08:17:56 2002) <br /> <br />對了是2131的板子 <br />//------------------------------------------------------------------------- <br />---- <br />// File: fw.c <br />// Contents: Firmware frameworks task dispatcher and device request parser <br />// source. <br />// <br />// Copyright (c) 2001 Cypress Semiconductor, Inc. All rights reserved <br />//-------------------------------------------------------------------------- <br />--- <br />#include <ezusb.h> <br />#include <ezregs.h> <br />//-------------------------------------------------------------------------- <br />--- <br />// Random Macros <br />//-------------------------------------------------------------------------- <br />--- <br />#define min(a,b) (((a)<(b))?(a):(b)) <br />#define max(a,b) (((a)>(b))?(a):(b)) <br />//-------------------------------------------------------------------------- <br />--- <br />// Constants <br />//-------------------------------------------------------------------------- <br />--- <br />#define DELAY_COUNT 0x9248*8L // Delay for 8 sec at 24Mhz, 4 sec at 48 <br />//-------------------------------------------------------------------------- <br />--- <br />// Global Variables <br />//-------------------------------------------------------------------------- <br />--- <br />volatile BOOL GotSUD; <br />BOOL Rwuen; <br />BOOL Selfpwr; <br />volatile BOOL Sleep; // Sleep mode enable flag <br />WORD pDeviceDscr; // Pointer to Device Descriptor; Descriptors may be moved <br />WORD pConfigDscr; <br />WORD pStringDscr; <br />//-------------------------------------------------------------------------- <br />--- <br />// Prototypes <br />//-------------------------------------------------------------------------- <br />--- <br />void SetupCommand(void); <br />void TD_Init(void); <br />void TD_Poll(void); <br />BOOL TD_Suspend(void); <br />BOOL TD_Resume(void); <br />BOOL DR_GetDescriptor(void); <br />BOOL DR_SetConfiguration(void); <br />BOOL DR_GetConfiguration(void); <br />BOOL DR_SetInterface(void); <br />BOOL DR_GetInterface(void); <br />BOOL DR_GetStatus(void); <br />BOOL DR_ClearFeature(void); <br />BOOL DR_SetFeature(void); <br />BOOL DR_VendorCmnd(void); <br />BOOL DR_ClassRequest(void); <br />//-------------------------------------------------------------------------- <br />--- <br />// Code <br />//-------------------------------------------------------------------------- <br />--- <br />// Task dispatcher <br />void main(void) <br />{ <br /> DWORD i; <br /> WORD offset; <br /> DWORD DevDescrLen; <br /> DWORD j=0; <br /> WORD IntDescrAddr; <br /> WORD ExtDescrAddr; <br /> // Initialize Global States <br /> Sleep = FALSE; // Disable sleep mode <br /> Rwuen = FALSE; // Disable remote wakeup <br /> Selfpwr = FALSE; // Disable self powered <br /> GotSUD = FALSE; // Clear "Got setup data" flag <br /> // Initialize user device <br /> TD_Init(); <br /> // The following section of code is used to relocate the descriptor table. <br /> // Since the SUDPTRH and SUDPTRL are assigned the address of the descriptor <br /> <br /> // table, the descriptor table must be located in on-part memory. <br /> // The 4K demo tools locate all code sections in external memory. <br /> // The descriptor table is relocated by the frameworks ONLY if it is found <br /> // to be located in external memory. <br /> pDeviceDscr = (WORD)&DeviceDscr; <br /> pConfigDscr = (WORD)&ConfigDscr; <br /> pStringDscr = (WORD)&StringDscr; <br /> if ((WORD)&DeviceDscr & 0xe000) <br /> { <br /> IntDescrAddr = INTERNAL_DSCR_ADDR; <br /> ExtDescrAddr = (WORD)&DeviceDscr; <br /> DevDescrLen = (WORD)&UserDscr - (WORD)&DeviceDscr + 2; <br /> for (i = 0; i < DevDescrLen; i++) <br /> *((BYTE xdata *)IntDescrAddr+i) = 0xCD; <br /> for (i = 0; i < DevDescrLen; i++) <br /> *((BYTE xdata *)IntDescrAddr+i) = *((BYTE xdata *)ExtDescrAddr+i); <br /> pDeviceDscr = IntDescrAddr; <br /> offset = (WORD)&DeviceDscr - INTERNAL_DSCR_ADDR; <br /> pConfigDscr -= offset; <br /> pStringDscr -= offset; <br /> } <br /> EZUSB_IRQ_ENABLE(); // Enable USB interrupt (INT2) <br /> EZUSB_ENABLE_RSMIRQ(); // Wake-up interrupt <br /> // The 8051 is responsible for all USB events, even those that have happene <br />d <br /> // before this point. We cannot ignore pending USB interrupts. <br /> // The chip will come out of reset with the flags all cleared. <br /> // USBIRQ = 0xff; // Clear any pending USB interrupt requests <br /> PORTCCFG |= 0xc0; // Turn on r/w lines for external memory <br /> USBBAV = USBBAV | 1 & ~bmBREAK; // Disable breakpoints and autovectoring <br /> USBIEN |= bmSUDAV | bmSUTOK | bmSUSP | bmURES; // Enable selected interrupt <br />s <br /> EA = 1; // Enable 8051 interrupts <br /> // Note: at full speed, high speed hosts may take 5 sec to detect device <br /> <br /> #ifndef NO_RENUM <br /> EZUSB_Discon(TRUE); // renumerate <br /> #endif <br /> CKCON = (CKCON&(~bmSTRETCH)) | FW_STRETCH_VALUE; // Set stretch to 0 (after <br /> renumeration) <br /> // Task Dispatcher <br /> while(TRUE) // Main Loop <br /> { <br /> if(GotSUD) // Wait for SUDAV <br /> { <br /> SetupCommand(); // Implement setup command <br /> GotSUD = FALSE; // Clear SUDAV flag <br /> } <br /> // Poll User Device <br /> // NOTE: Idle mode stops the processor clock. There are only two <br /> // ways out of idle mode, the WAKEUP pin, and detection of the USB <br /> // resume state on the USB bus. The timers will stop and the <br /> // processor will not wake up on any other interrupts. <br /> if (Sleep) <br /> { <br /> if(TD_Suspend()) <br /> { <br /> Sleep = FALSE; // Clear the "go to sleep" flag. Do it here t <br />o prevent any race condition between wakeup and the next sleep. <br /> do <br /> { <br /> EZUSB_Susp(); // Place processor in idle mode. <br /> } <br /> while(!Rwuen && EZUSB_EXTWAKEUP()); <br /> // Must continue to go back into suspend if the host has dis <br />abled remote wakeup <br /> // *and* the wakeup was caused by the external wakeup pin. <br /> // 8051 activity will resume here due to USB bus or Wakeup# pin activ <br />ity. <br /> EZUSB_Resume(); // If source is the Wakeup# pin, signal the host to R <br />esume. <br /> TD_Resume(); <br /> } <br /> } <br /> TD_Poll(); <br /> } <br />} <br />// Device request parser <br />void SetupCommand(void) <br />{ <br /> void *dscr_ptr; <br /> DWORD i; <br /> switch(SETUPDAT[0] & SETUP_MASK) <br /> { <br /> case SETUP_STANDARD_REQUEST: //Standard Request <br /> switch(SETUPDAT[1]) <br /> { <br /> case SC_GET_DESCRIPTOR: // *** Get Descriptor <br /> if(DR_GetDescriptor()) <br /> switch(SETUPDAT[3]) <br /> { <br /> case GD_DEVICE: // Device <br /> SUDPTRH = MSB(pDeviceDscr); <br /> SUDPTRL = LSB(pDeviceDscr); <br /> break; <br /> case GD_CONFIGURATION: // Configuration <br /> if(dscr_ptr = (void *)EZUSB_GetConfigDscr(SETUPDAT[2])) <br /> { <br /> SUDPTRH = MSB(dscr_ptr); <br /> SUDPTRL = LSB(dscr_ptr); <br /> } <br /> else <br /> EZUSB_STALL_EP0(); // Stall End Point 0 <br /> break; <br /> case GD_STRING: // String <br /> if(dscr_ptr = (void *)EZUSB_GetStringDscr(SETUPDAT[2])) <br /> { <br />
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -