Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
main.c
Package: TX-1C-usbdriver.rar [view]
Upload User: lyfy_2008
Upload Date: 2014-07-13
Package Size: 3016k
Code Size: 7k
Category:
USB develop
Development Platform:
Visual C++
- //#include <stdio.h>
- #include <string.h>
- #include <reg51.h> /* special function register declarations */
- //#include "epphal.h"
- #include "d12ci.h"
- #include "mainloop.h"
- #include "usb100.h"
- #include "chap_9.h"
- //#include "protodma.h"
- #include"address.h"
- //#include"2-16.h"
- //#include"24C00.H"
- /*
- //*************************************************************************
- // USB protocol function pointer arrays
- //*************************************************************************
- */
- #define uchar unsigned char
- unsigned char code Tab[]=
- { //数码管数据0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
- 0x12,0x9F,0x31,0x15,0x9C,0x54,0x50,0x1F,
- 0x10,0x14,0x18,0xD0,0x72,0x91,0x70,0x78
- };
- void send(uchar buf1,uchar buf2); //数码管显示程序
- void EndpointII();//处理 2*16LCD和IIC
- //USB标准请求
- code void (*StandardDeviceRequest[])(void) =
- {
- get_status,
- clear_feature,
- reserved,
- set_feature,
- reserved,
- set_address,
- get_descriptor,
- reserved,
- get_configuration,
- set_configuration,
- get_interface,
- set_interface,
- reserved,
- reserved,
- reserved,
- reserved
- };
- //用户厂商请求
- code void (*VendorDeviceRequest[])(void) =
- {
- reserved,
- reserved,
- reserved,
- reserved,
- reserved,
- reserved,
- reserved,
- reserved,
- reserved,
- reserved,
- reserved,
- reserved,
- reserved,
- reserved,
- reserved,
- reserved
- };
- /*
- //*************************************************************************
- // Public static data
- //*************************************************************************
- */
- extern EPPFLAGS bEPPflags;
- extern unsigned long ClockTicks;
- extern unsigned char idata GenEpBuf[];
- extern IO_REQUEST idata ioRequest;
- extern unsigned char ioSize, ioCount;
- extern unsigned char idata EpBuf[];
- extern unsigned char idata mainbuflen;
- CONTROL_XFER ControlData;
- BOOL bNoRAM;
- //定时器0初始化程序
- //定时器用来定时检测按键状态
- void init_timer0(void)
- {
- TMOD &= 0XF0; /* clear Timer 0 */
- TMOD |= 0X1;
- TL0 = 0X0; /* value set by user */
- TH0 = 0X0; /* value set by user */
- ET0 = 1; /* IE.1*/
- TR0 = 1; /* TCON.4 start timer */
- PT0 = 1;
- EA = 1;
- }
- //中断设置
- void init_special_interrupts(void)
- {
- IT1 = 0;
- EX1 = 1;
- PX1 = 0;
- }
- //I/O口初始化程序
- void init_port()
- {
- P0 = 0xFF;
- P1 = 0xFF;
- P2 = 0xFF;
- P3 = 0xFF;
- }
- void init()
- {
- send(0xff,0xff);//数码管关
- LEDDATA=0x00;//LED关
- }
- void main(void)
- {
- init_port();//初始化I/O口
- // initlcd();
- // printscreen("Hello!!" ,"Wellcome to USB");
- init_timer0();//初始化定时器0
- init_special_interrupts();//设置中断
- init();
- bEPPflags.value = 0;
- reconnect_USB();//重新连接USB
- /* Main program loop */
- while( TRUE )
- {
- if (bEPPflags.bits.timer)
- {
- DISABLE;//定时器溢出,检测按键状态
- bEPPflags.bits.timer = 0;
- ENABLE;
- if(bEPPflags.bits.configuration)//设备未配置返回
- check_key_LED(); //检测键盘
- }
- if (bEPPflags.bits.bus_reset)
- {//设备复位处理
- DISABLE;
- bEPPflags.bits.bus_reset = 0;
- ENABLE;
- } // if bus reset
- if(bEPPflags.bits.ep1_rxdone) //端点1输出
- { //控制LED/数码管/
- /*
- GenEpBuf[0] 操作对象
- 1 LED
- 2 数码管
- 3 锋鸣器
- 0x55 读IIC
- */
- DISABLE;
- bEPPflags.bits.ep1_rxdone=0;
- ENABLE;
- if(GenEpBuf[0]==1) P1 = GenEpBuf[1]; //LEDDATA=GenEpBuf[1];
- if(GenEpBuf[0]==2)send(Tab[GenEpBuf[1]],Tab[GenEpBuf[2]]);
- if(GenEpBuf[0]==3)BEEB=GenEpBuf[1]&0x01;
- if(GenEpBuf[0]==0x55)
- {
- // Read24c02_nByte(GenEpBuf[1],10,EpBuf) ;
- D12_WriteEndpoint(5, 10, EpBuf);
- }
- GenEpBuf[0]=0;
- }
- if (bEPPflags.bits.setup_packet)
- {//Setup包处理
- DISABLE;
- bEPPflags.bits.setup_packet = 0;
- ENABLE;
- control_handler();//调用请求处理子程序
- } // if setup_packet
- if(bEPPflags.bits.main_rxdone) //端点2输出
- {
- DISABLE;
- bEPPflags.bits.main_rxdone=0;
- ENABLE;
- EndpointII();//操作LCD和I2C
- } //if main_rxdone
- } // Main Loop
- }
- //返回stall应答
- void stall_ep0(void)
- {
- D12_SetEndpointStatus(0, 1);
- D12_SetEndpointStatus(1, 1);
- }
- //断开USB总线
- void disconnect_USB(void)
- {
- // Initialize D12 configuration
- D12_SetMode(D12_NOLAZYCLOCK, D12_SETTOONE | D12_CLOCK_12M);
- }
- //连接USB总线
- void connect_USB(void)
- {
- // reset event flags
- DISABLE;
- bEPPflags.value = 0;//清除所有状态
- ENABLE;
- // V2.1 enable normal+sof interrupt
- D12_SetDMA(D12_ENDP4INTENABLE | D12_ENDP5INTENABLE);
- // Initialize D12 configuration
- D12_SetMode(D12_NOLAZYCLOCK|D12_SOFTCONNECT, D12_SETTOONE | D12_CLOCK_12M);
- }
- //重新连接到USB总线
- void reconnect_USB(void)
- {
- unsigned long clk_cnt;
- disconnect_USB();
- clk_cnt = ClockTicks;
- while(ClockTicks < clk_cnt + 20);
- connect_USB();
- }
- //恢复到未配置状态
- void init_unconfig(void)
- {
- D12_SetEndpointEnable(0); /* Disable all endpoints but EPP0. */
- }
- //设置配置状态
- void init_config(void)
- {
- D12_SetEndpointEnable(1); /* Enable generic/iso endpoints. */
- }
- //从端点号1发送数据
- void single_transmit(unsigned char * buf, unsigned char len)
- {
- if( len <= EP0_PACKET_SIZE) {
- D12_WriteEndpoint(1, len, buf);
- }
- }
- //发送端点号1建立代码
- void code_transmit(unsigned char code * pRomData, unsigned short len)
- {
- ControlData.wCount = 0;
- if(ControlData.wLength > len)
- ControlData.wLength = len;
- ControlData.pData = pRomData;
- if( ControlData.wLength >= EP0_PACKET_SIZE) {
- D12_WriteEndpoint(1, EP0_PACKET_SIZE, ControlData.pData);//发送16字节数据
- ControlData.wCount += EP0_PACKET_SIZE;
- DISABLE;
- bEPPflags.bits.control_state = USB_TRANSMIT;
- ENABLE;
- }
- else {
- D12_WriteEndpoint(1, ControlData.wLength, pRomData);//发送16字节内数据
- ControlData.wCount += ControlData.wLength;
- DISABLE;
- bEPPflags.bits.control_state = USB_IDLE;
- ENABLE;
- }
- }
- //LED和按键处理子程序
- void check_key_LED(void)
- {
- static unsigned char c ;
- c =KEYDATA;
- D12_WriteEndpoint(3,1,&c); //输入键盘数据
- }
- //数码管显示程序,左buf1,右buf2
- void send(uchar buf1,uchar buf2)
- {
- uchar i;
- uchar temp=buf2;
- for(i=0;i!=8;i++)
- {
- if(temp&0x01)SPIDATA=1;
- else SPIDATA=0;
- SPICLK=0;SPICLK=1;
- temp>>=1;
- }
- temp=buf1;
- for(i=0;i!=8;i++)
- {
- if(temp&0x01)SPIDATA=1;
- else SPIDATA=0;
- SPICLK=0;SPICLK=1;
- temp>>=1;
- }
- SPILatch=0;
- SPILatch=1;
- }
- void EndpointII()//2*16LCD/IIC
- {
- // if(EpBuf[0]==1) PrintUsb(EpBuf+2,EpBuf[1]); //LCD
- // if(EpBuf[0]==2) Write24c02_nByte(EpBuf[2],EpBuf[1],EpBuf+3);//IIC
- EpBuf[0]=0;
- }
- //请求处理子程序 处理Setup包
- void control_handler()
- {
- unsigned char type, req;
- type = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_TYPE_MASK;
- req = ControlData.DeviceRequest.bRequest & USB_REQUEST_MASK;
- if (type == USB_STANDARD_REQUEST)
- (*StandardDeviceRequest[req])();//调用标准请求
- else if (type == USB_VENDOR_REQUEST)
- (*VendorDeviceRequest[req])();//调用厂商请求
- else
- stall_ep0();
- }