📄 usbmouse.c
字号:
/************************************************************************/
/* usbmouse.c */
/* */
/* 丂HSB8S2215ST傪HID儅僂僗偲偟偰擣幆偝偣丄僗僀僢僠2,3,4,5傪墴偡 */
/* 丂偙偲偱丄儅僂僗億僀儞僞傪忋壓嵍塃偵摦偐偡僾儘僌儔儉偱偡丅 */
/* */
/*丂儌僯僞偺L2僐儅儞僪偱USBMOUSE.HKT傪儘乕僪偟偰G僐儅儞僪偱 */
/*丂200200h斣抧偐傜幚峴偟偰偔偩偝偄丅 */
/************************************************************************/
#include "2215.h" /*撪憼I/O傾僪儗僗掕媊僼傽僀儖*/
#include "machine.h"
/*#define DEBUG*/
/*倀俽俛僨僶僀僗儕僋僄僗僩峔憿懱*/
struct DEVICE_REQUEST {
unsigned char bmRequestType;
unsigned char bRequest;
unsigned short wValue;
unsigned short wIndex;
unsigned short wLength;
};
/*昗弨僨僶僀僗僨傿僗僋儕僾僞*/
struct DEVICE_DESCRIPTOR {
unsigned char bLength;
unsigned char bDescriptor;
unsigned short bcdUSB;
unsigned char bDeviceClass;
unsigned char bDeviceSubClass;
unsigned char bDeviceProtocol;
unsigned char bMaxPacketSize0;
unsigned short idVendor;
unsigned short idProduct;
unsigned short bcdDevice;
unsigned char iManufactuer;
unsigned char iProduct;
unsigned char iSerialNumber;
unsigned char bNumConfigurations;
};
/*僐儞僼傿僌儗乕僔儑儞僨傿僗僋儕僾僞*/
struct CONFIGURATION_DESCRIPTOR {
unsigned char bLength;
unsigned char bDescriptor;
unsigned short wTotalLength;
unsigned char bNumInterfaces;
unsigned char bConfigurationValue;
unsigned char iConfiguration;
unsigned char bmAttributes;
unsigned char bMaxPower;
};
/*昗弨僀儞僞乕僼僃乕僗僨傿僗僋儕僾僞*/
struct INTERFACE_DESCRIPTOR {
unsigned char bLength;
unsigned char bDescriptorType;
unsigned char bInterfaceNumber;
unsigned char bAlternateSetting;
unsigned char bNumEndpoints;
unsigned char bInterfaceClass;
unsigned char bInterfaceSubClass;
unsigned char bInterfaceProtocol;
unsigned char iInterface;
};
/*俫俬俢僨傿僗僋儕僾僞*/
struct HID_DESCRIPTOR {
unsigned char _bLength;
unsigned char bDescriptorType;
unsigned short bcdHID;
unsigned char bCountryCode;
unsigned char bNumDescriptors;
unsigned char bnDescriptorType;
unsigned short wnDescriptorLength;
};
/*昗弨僄儞僪億僀儞僩僨傿僗僋儕僾僞*/
struct ENDPOINT_DESCRIPTOR {
unsigned char bLength;
unsigned char bDescriptorType;
unsigned char EndpointAddress;
unsigned char bmAttributes;
unsigned short wMaxPacketSize;
unsigned char bInterval;
};
const unsigned char dev_desc[] = {
0x12, /*bLength 僨傿僗僋儕僾僞偺僒僀僘*/
0x01, /*bDescriptorType 僨傿僗僋儕僾僞偺僞僀僾*/
0x00,0x01, /*bcdUSB BCD昞尰偺USB巇條儕儕乕僗斣崋1.00*/
0x00, /*bDeviceClass 僋儔僗僐乕僪 僋儔僗柍偟*/
0x00, /*bDeviceSubClass 僒僽僋儔僗僐乕僪*/
0x00, /*bDeviceProtocol 僾儘僩僐儖僐乕僪*/
0x08, /*bMaxPacketSize0 僄儞僪億僀儞僩0偺嵟戝僷働僢僩僒僀僘*/
0xff,0xff, /*idVendor 儀儞僟ID*/
0x00,0x00, /*idProduct 僾儘僟僋僩ID儀儞僟偑妱傝摉偰傞*/
0x00,0x00, /*bcdDevice BCD昞尰偺僨僶僀僗偺儕儕乕僗斣崋*/
0x00, /*iManufacture 惢憿幰傪昞偡僗僩儕儞僌僨傿僗僋儕僾僞傊偺僀儞僨僢僋僗*/
0x00, /*iProduct 惢昳傪昞偡僗僩儕儞僌僨傿僗僋儕僾僞傊偺僀儞僨僢僋僗*/
0x00, /*iSerialNumber 僨僶僀僗偺惢憿斣崋傪昞偡僗僩儕儞僌僨傿僗僋儕僾僞傊偺僀儞僨僢僋僗*/
0x01 /*bNumConfiguraions 峔惉壜擻側悢*/
};
const unsigned char config_inf[] = {
0x22, /* 僨乕僞挿*/
/*僐儞僼傿僌僨傿僗僋儕僾僞*/
0x09, /* bLength 僨傿僗僋儕僾僞偺僒僀僘*/
0x02, /* bDescriptor 僨傿僗僋儕僾僞偺僞僀僾*/
0x22,0x00, /* wTotalLength 峔惉慡懱偺挿偝乮僐儞僼傿僌丄僀儞僞乕僼僃乕僗丄俫俬俢丄僄儞僪億僀儞僩乯0倶0022*/
0x01, /* bNumInterfaces 峔惉偺帩偮僀儞僞乕僼僃乕僗偺悢*/
0x01, /* bConfigurationValue SetConfiguration偱偙偺峔惉傪慖戰偡傞偨傔偺堷偒悢抣*/
0x00, /* iConfiguration 峔惉傪昞偡僗僩儕儞僌僨傿僗僋儕僾僞傊偺僀儞僨僢僋僗*/
0xc0, /* bmAttributes 峔惉偺摿惈 僙儖僼僷儚乕僪丄儕儌乕僩僂僃僀僋傾僢僾柍偟*/
/* 0x80, */ /* bmAttributes 峔惉偺摿惈 僶僗僷儚乕僪丆儕儌乕僩僂僃僀僋傾僢僾柍偟*/
50, /* bMaxPower 徚旓揹棳 100mA*/
/*僀儞僞乕僼僃乕僗僨傿僗僋儕僾僞*/
0x09, /* bLength 僨傿僗僋儕僾僞偺僒僀僘*/
0x04, /* bDescriptorType 僨傿僗僋儕僾僞偺僞僀僾*/
0x00, /* bInterfaceNumber 僀儞僞乕僼僃乕僗偺僀儞僨僢僋僗斣崋*/
0x00, /* bAlternateSetting Setinterface偱戙懼愝掕傪慖戰偡傞偨傔偺堷偒悢抣*/
0x01, /* bNumEndpoints 乮僄儞僪億僀儞僩0傪彍偔乯僀儞僞乕僼僃乕僗偺帩偮僄儞僪億僀儞僩偺悢*/
0x03, /* bInterfaceClass 僋儔僗僐乕僪*/
0x01, /* bInterfaceSubClass 僒僽僋儔僗僐乕僪*/
0x02, /* bInterfaceProtocol 僾儘僩僐儖僐乕僪*/
0x00, /* iInterface 偙偺僀儞僞乕僼僃乕僗傪昞偡僗僩儕儞僌僨傿僗僋儕僾僞傊偺僀儞僨僢僋僗*/
/*俫俬俢僨傿僗僋儕僾僞*/
0x09, /* bLength 僨傿僗僋儕僾僞偺僒僀僘*/
0x21, /* bDescriptorType 僨傿僗僋儕僾僞偺僞僀僾*/
0x00,0x01, /* bcdHID BCD昞尰偺俫俬俢僋儔僗婯奿1.00*/
0x00, /* bCountryCode */
0x01, /* bNumDescriptors 俫俬俢娭楢偺僨傿僗僋儕僾僞偼1屄晅懏*/
0x22, /* bnDescriptorType 儗億乕僩僨傿僗僋儕僾僞*/
0x34,0x00, /* wnDescriptorLength 儗億乕僩僨傿僗僋儕僾僞挿*/
/*僄儞僪億僀儞僩僨傿僗僋儕僾僞*/
0x07, /* bLength 僨傿僗僋儕僾僞僒僀僘*/
0x05, /* bDescriptorType 僨傿僗僋儕僾僞偺僞僀僾*/
0x81, /* EndpointAddress IN曽岦傾僪儗僗1*/
0x03, /* bmAttributes 僀儞僞儔僾僩揮憲*/
0x10,0x00, /* wMaxPacketSize 嵟戝僷働僢僩僒僀僘16byte*/
10 /* bInterval 10ms枅偵億乕儕儞僌*/
};
/*儗億乕僩僨傿僗僋儕僾僞*/
const unsigned char report_desc[] = {
0x34, /* Length */
0x05, 0x01, /* UsagePage */
0x09, 0x02, /* Usage */
0xa1, 0x01, /* Collection */
0x05, 0x09, /* UsagePage */
0x15, 0x00, /* LogicalMinimum */
0x25, 0x01, /* LogicalMaximum */
0x95, 0x03, /* ReportCount */
0x75, 0x01, /* ReportSize */
0x19, 0x01, /* UsageMinimum */
0x29, 0x03, /* UsageMaximum */
0x65, 0x00, /* Unit */
0x81, 0x02, /* Input */
0x95, 0x01, /* ReportCount */
0x75, 0x05, /* ReportSize */
0x81, 0x01, /* Input */
0x05, 0x01, /* UsagePage */
0x09, 0x01, /* Usage */
0xa1, 0x00, /* Collection */
0x15, 0x81, /* LogicalMinimum */
0x25, 0x7f, /* LogicalMaximum */
0x75, 0x08, /* ReportSize */
0x95, 0x02, /* ReportCount */
0x09, 0x30, /* Usage */
0x09, 0x31, /* Usage */
0x81, 0x06, /* Input */
0xc0, /* EndCollection */
0xc0 /* EndCollection */
};
/*儅僂僗偺峔憿懱*/
struct MOUSE {
char x; /*儅僂僗億僀儞僞x幉堏摦検 -127乣+127*/
char y; /*儅僂僗億僀儞僞y幉堏摦検 -127乣+127*/
unsigned char button; /*儃僞儞 0bit:L 1bit:R*/
};
/*曄悢*/
struct MOUSE mouse;
unsigned char ep0_rxdat[8]; /*EP0庴怣僶僢僼傽*/
unsigned char *ep0_txptr; /*EP0憲怣僨乕僞偺億僀儞僞*/
unsigned short ep0_txsize; /*EP0憲怣僒僀僘*/
int usb_init( void ); /*撪憼USB儌僕儏乕儖弶婜壔張棟*/
int usb_connect( void ); /*USB愙懕*/
int usb_disconnect( void ); /*USB愗抐*/
void device_request( struct DEVICE_REQUEST *devreq ); /*僨僶僀僗儕僋僄僗僩敾暿*/
void get_descriptor( struct DEVICE_REQUEST *devreq ); /*憲怣偡傞僨傿僗僋儕僾僞傪敾暿*/
void ep0_tx( void ); /*僄儞僪億僀儞僩0憲怣張棟*/
void sci_print( char *data ); /*僔儕傾儖暥帤楍昞帵*/
void sci_reg_print( unsigned long data ); /*儘儞僌儚乕僪俫俤倃弌椡*/
void sci_init( void ); /*僔儕傾儖弶婜壔*/
int sci_read( void ); /*侾僶僀僩僨乕僞僔儕傾儖擖椡*/
void sci_write( unsigned char data ); /*侾僶僀僩僨乕僞僔儕傾儖弌椡*/
void wait( int msec ); /*嬻儖乕僾*/
/****************************************************************/
/* 儊僀儞儖乕僠儞 */
/****************************************************************/
void main( void )
{
unsigned int i, data;
#ifdef DEBUG
sci_init(); /*僔儕傾儖弶婜壔*/
sci_print("\n<< 2215 USB MOUSE Program >>\n"); /*僞僀僩儖昞帵*/
#endif
*P1DDR = 0xff; /*億乕僩1偺0乣7bit傪傪弌椡偵愝掕*/
*P3DR = *P3DR & 0xbf; /*D+僾儖傾僢僾OFF*/
*P3DDR = 0x40; /*P36傪弌椡僺儞偵愝掕*/
wait(1000);
usb_init(); /*USB弶婜壔*/
set_imask_ccr( 0 ); /*妱傝崬傒儅僗僋儗儀儖傪壓偘傞*/
while( 1 ){
data = 0x01;
for( i = 0; i < 14; i++ ){
*P1DR = ~data;
if( i >= 7 ){
data = data >> 1;
}
else{
data = data << 1;
}
wait(1000);
}
}
}
/****************************************************************/
/* USB弶婜壔張棟 */
/****************************************************************/
int usb_init( void )
{
*UCTLR = *UCTLR & 0xc3 | 0x1c; /*16MHz悈徎敪怳傪PLL偱3掽攞*/
*MSTPCRB = *MSTPCRB & 0xfe; /*USB儌僕儏乕儖僗僩僢僾夝彍( MSTPCRB/MSTPB0=0 )*/
/*USB摦嶌僋儘僢僋埨掕専弌偱妱傝崬傒傪偐偗傞*/
*UISR3 = 0x00; /*CK48READY妱傝崬傒偼EXIRQ0*/
*UIER3 = 0x80; /*CK48READY妱傝崬傒嫋壜*/
}
#pragma interrupt( exirq0, exirq1 )
/****************************************************************/
/* 妱傝崬傒張棟 */
/****************************************************************/
void exirq0( void )
{
int i;
#ifdef DEBUG
sci_reg_print( *UIFR0 );
sci_reg_print( *UIFR1 );
sci_reg_print( *UIFR2 );
sci_reg_print( *UIFR3 );
sci_print("\n");
#endif
/*USB摦嶌僋儘僢僋埨掕専弌*/
if( ( *UIFR3 & 0x80 ) != 0 ){
*UIFR3 = *UIFR3 & 0x7f; /*UIFR3/CK48READY=0僋儕傾*/
#ifdef DEBUG
sci_print("clock ok\n");
#endif
*UCTLR = *UCTLR & 0xfd; /*USB僀儞僞乕僼僃乕僗儕僙僢僩夝彍( UCTLR/UIFRST=0僋儕傾 )*/
/*EPINFO偺愝掕 115僶僀僩偺僨乕僞傪UEPIR00_0乣22_4傊儔僀僩*/
*UEPIR00_0 = 0x00;
*UEPIR00_1 = 0x00;
*UEPIR00_2 = 0x40;
*UEPIR00_3 = 0x00;
*UEPIR00_4 = 0x00;
*UEPIR01_0 = 0x14;
*UEPIR01_1 = 0x1c;
*UEPIR01_2 = 0x10;
*UEPIR01_3 = 0x00;
*UEPIR01_4 = 0x01;
*UEPIR02_0 = 0x24;
*UEPIR02_1 = 0x14;
*UEPIR02_2 = 0x40;
*UEPIR02_3 = 0x00;
*UEPIR02_4 = 0x02;
*UEPIR03_0 = 0x24;
*UEPIR03_1 = 0x10;
*UEPIR03_2 = 0x40;
*UEPIR03_3 = 0x00;
*UEPIR03_4 = 0x03;
*UEPIR04_0 = 0x35;
*UEPIR04_1 = 0x0c;
*UEPIR04_2 = 0x00;
*UEPIR04_3 = 0x00;
*UEPIR04_4 = 0x04;
*UEPIR05_0 = 0x35;
*UEPIR05_1 = 0x08;
*UEPIR05_2 = 0x00;
*UEPIR05_3 = 0x00;
*UEPIR05_4 = 0x05;
*UEPIR06_0 = 0x35;
*UEPIR06_1 = 0x2c;
*UEPIR06_2 = 0x09;
*UEPIR06_3 = 0x00;
*UEPIR06_4 = 0x06;
*UEPIR07_0 = 0x35;
*UEPIR07_1 = 0x28;
*UEPIR07_2 = 0x09;
*UEPIR07_3 = 0x00;
*UEPIR07_4 = 0x07;
*UEPIR08_0 = 0x35;
*UEPIR08_1 = 0x4c;
*UEPIR08_2 = 0x11;
*UEPIR08_3 = 0x00;
*UEPIR08_4 = 0x08;
*UEPIR09_0 = 0x35;
*UEPIR09_1 = 0x48;
*UEPIR09_2 = 0x11;
*UEPIR09_3 = 0x00;
*UEPIR09_4 = 0x09;
*UEPIR10_0 = 0x35;
*UEPIR10_1 = 0x6c;
*UEPIR10_2 = 0x19;
*UEPIR10_3 = 0x00;
*UEPIR10_4 = 0x0a;
*UEPIR11_0 = 0x35;
*UEPIR11_1 = 0x68;
*UEPIR11_2 = 0x19;
*UEPIR11_3 = 0x00;
*UEPIR11_4 = 0x0b;
*UEPIR12_0 = 0x35;
*UEPIR12_1 = 0x8c;
*UEPIR12_2 = 0x21;
*UEPIR12_3 = 0x00;
*UEPIR12_4 = 0x0c;
*UEPIR13_0 = 0x35;
*UEPIR13_1 = 0x88;
*UEPIR13_2 = 0x21;
*UEPIR13_3 = 0x00;
*UEPIR13_4 = 0x0d;
*UEPIR14_0 = 0x35;
*UEPIR14_1 = 0xac;
*UEPIR14_2 = 0x31;
*UEPIR14_3 = 0x00;
*UEPIR14_4 = 0x0e;
*UEPIR15_0 = 0x35;
*UEPIR15_1 = 0xa8;
*UEPIR15_2 = 0x31;
*UEPIR15_3 = 0x00;
*UEPIR15_4 = 0x0f;
*UEPIR16_0 = 0x35;
*UEPIR16_1 = 0xcc;
*UEPIR16_2 = 0x00;
*UEPIR16_3 = 0x00;
*UEPIR16_4 = 0x11;
*UEPIR17_0 = 0x35;
*UEPIR17_1 = 0xc8;
*UEPIR17_2 = 0x00;
*UEPIR17_3 = 0x00;
*UEPIR17_4 = 0x11;
*UEPIR18_0 = 0x35;
*UEPIR18_1 = 0xec;
*UEPIR18_2 = 0x00;
*UEPIR18_3 = 0x00;
*UEPIR18_4 = 0x12;
*UEPIR19_0 = 0x35;
*UEPIR19_1 = 0xe8;
*UEPIR19_2 = 0x00;
*UEPIR19_3 = 0x00;
*UEPIR19_4 = 0x13;
*UEPIR20_0 = 0x46;
*UEPIR20_1 = 0x14;
*UEPIR20_2 = 0x00;
*UEPIR20_3 = 0x00;
*UEPIR20_4 = 0x14;
*UEPIR21_0 = 0x46;
*UEPIR21_1 = 0x10;
*UEPIR21_2 = 0x00;
*UEPIR21_3 = 0x00;
*UEPIR21_4 = 0x15;
*UEPIR22_0 = 0x56;
*UEPIR22_1 = 0x1c;
*UEPIR22_2 = 0x00;
*UEPIR22_3 = 0x00;
*UEPIR22_4 = 0x16;
/*奺妱傝崬傒愝掕*/
*UISR0 = 0x30; /*EP1妱傝崬傒偼EXIRQ1偦偺懠偼EXIRQ0*/
*UIER0 = 0xff; /*僶僗儕僙僢僩丄EP1,EP0丄僙僢僩傾僢僾僐儅儞僪庴怣妱傝崬傒嫋壜*/
*UISR1 = 0x00; /*僨僼僅儖僩*/
*UIER1 = 0x00; /*僨僼僅儖僩*/
*UISR2 = 0x00; /*僨僼僅儖僩*/
*UIER2 = 0x00; /*僨僼僅儖僩*/
*UISR3 = 0x00; /*CK48READY妱傝崬傒丄VBUS妱傝崬傒偼EXIRQ0*/
*UIER3 = 0x81; /*CK48READY妱傝崬傒丄VBUS妱傝崬傒嫋壜*/
/*愙懕懸偪or愙懕張棟傊*/
/* if( ( *UIFR3 & 0x02 ) != 0 ){*/ /*愙懕忬懺*/
/* usb_connect();*/
/* }*/
return;
}
/*VBUS妱傝崬傒乮働乕僽儖愙懕乛愗抐帪偵婲偙傞乯*/
if( ( *UIFR3 & 0x01 ) != 0 ){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -