📄 test02.c
字号:
case 0x02: // End Point stall
if((SETUPR2 == 0) & (SETUPR6==0) & (SETUPR7==0))
{
if((SETUPR4 == 0x81))
{
//reset in ep1 toggle/stall bit
USB_CTL2 = USB_CTL2 & 0xCF;
}
if((SETUPR4 == 0x82))
{
//reset in ep2 toggle/stall bit
USB_CTL3 = USB_CTL3 & 0xCF;
}
in_status();
}
else {
USB_CTL1 = USB_CTL1 | 0x20; //ostall0 = 1
USB_CTL2 = USB_CTL2 | 0x40; //istall0 = 1
}
break;
default: {
USB_CTL1 = USB_CTL1 | 0x20; //ostall0 = 1
USB_CTL2 = USB_CTL2 | 0x40; //istall0 = 1
break;
}
}
break;
case SET_FEATURE:
switch(SETUPR0)
{
case 0x00: // Device
if((SETUPR2 == 1) & (SETUPR6==0) & (SETUPR7==0)){
remote_wku_enable = 1; // Enable Remote Wakeup
in_status();
}
else {
USB_CTL1 = USB_CTL1 | 0x20;
USB_CTL2 = USB_CTL2 | 0x40;
}
break;
case 0x02: // End Point stall
if((SETUPR2 == 0) & (SETUPR6==0) & (SETUPR7==0)){
if(SETUPR4 == 0x81) {
USB_CTL2 = USB_CTL2 | 0x20; //set in1 stall
in_status();
}
else if(SETUPR4 == 0x82){
USB_CTL3 = USB_CTL3 | 0x20; //set in2 stall
in_status();
}
else {
USB_CTL1 = USB_CTL1 | 0x20;
USB_CTL2 = USB_CTL2 | 0x40;
}
break;
}
else {
USB_CTL1 = USB_CTL1 | 0x20;
USB_CTL2 = USB_CTL2 | 0x40;
break;
}
break;
default: {
USB_CTL1 = USB_CTL1 | 0x20;
USB_CTL2 = USB_CTL2 | 0x40;
break;
}
}
break;
default:{
USB_CTL1 = USB_CTL1 | 0x20;
USB_CTL2 = USB_CTL2 | 0x40;
break;
}
} //switch of standard
} //if standard request
else if((SETUPR0==0x21) | (SETUPR0==0xA1)){
switch(SETUPR1){
case SET_REPORT: //R0=0x21, R1=SET_REPORT, R2=REPORT_ID, R3=TYPE, R4=interface, R6=len
if((SETUPR4==0) & (SETUPR5==0) & (SETUPR3==2) & (SETUPR6==1) & (SETUPR7==0)){
// receive one byte from host
USB_CTL1 = USB_CTL1 | 0x40; //enable rxd0e
while(!(USB_STA0 & 0x01)); //wait rxd0f=1
USB_STA0 = USB_STA0 | 0x01; //clear rxd0f
USB_CTL1 = USB_CTL1 & 0xBF; //disable rxd0e
if((USB_STA1 & 0x80)) break; //double setup--abort
USB_EPP = 0;
LED = USB_EP0; // turn on or off the LED indicator
//
KB_SET_ONE(KB_LED_OOC, (unsigned char) LED);
//
in_status();
}
else
{
USB_CTL1 = USB_CTL1 | 0x20;
USB_CTL2 = USB_CTL2 | 0x40;
}
break;
case GET_REPORT: //R0=0xA1,R2=Report ID, R3=Report Type, R4R5=interface, R6R7=Len
if((SETUPR0==0xA1) & (SETUPR3==0x00) & (SETUPR4==0x00)){
//get kb input type report,8 bytes
USB_EPP = 0;
//USB_EP0 = 0x00; //Report ID
//if(NEW_KB_MODIFIER) USB_EP0 = KB_MODIFIER;
//else USB_EP0 = 0;
//USB_EP0 = 0;
for(i=0;i<8;i++) //modifier, reserved, array[8]
if(NEW_KB_DATA) USB_EP0 = KB_DATA[i];
else USB_EP0 = 0;
//
USB_CTL1 = 0x98;
while(!(USB_STA0 & 0x02));
USB_CTL1 = USB_CTL1 & 0x7F; //clear tx0e
USB_STA0 = USB_STA0 | 0x02; //txd0f=0
out_status();
NEW_KB_DATA = 0;
//NEW_KB_MODIFIER= 0;
}
else if((SETUPR0==0xA1) & (SETUPR3==0x01) & (SETUPR4==0x01)){
//get mouse input type report,5 bytes
USB_EPP = 0;
USB_EP0 = 0x01; //Mouse Report ID
for(i=0;i<3;i++) //button, x,y,wheel
//if(NEW_MS_DATA) USB_EP0 = MOUSE_DATA[i];
//else USB_EP0 = 0;
USB_EP0 = 0;
//fifth bytes, wheel movement
USB_EP0 = 0;
//
USB_CTL1 = 0x95;
while(!(USB_STA0 & 0x02));
USB_CTL1 = USB_CTL1 & 0x7F; //clear tx0e
USB_STA0 = USB_STA0 | 0x02; //txd0f=0
out_status();
//NEW_MS_DATA = 0;
}
else if((SETUPR0==0xA1) & (SETUPR3==0x02) & (SETUPR4==0x01)){
//get system keypad input type report,3 bytes
USB_EPP = 0;
USB_EP0 = 0x02; //System keypads Report ID
if(NEW_SYS_KB_DATA) USB_EP0 = SYS_KB_DATA;
else USB_EP0 = 0;
//
USB_CTL1 = 0x92;
while(!(USB_STA0 & 0x02));
USB_CTL1 = USB_CTL1 & 0x7F; //clear tx0e
USB_STA0 = USB_STA0 | 0x02; //txd0f=0
out_status();
NEW_SYS_KB_DATA = 0;
}
else if((SETUPR0==0xA1) & (SETUPR3==0x03) & (SETUPR4==0x01)){
//get system keypad input type report,3 bytes
USB_EPP = 0;
USB_EP0 = 0x03; //multi-media keypads Report ID
for(i=0;i<2;i++) //
if(NEW_MM_KB_DATA) USB_EP0 = MM_KB_DATA[i];
else USB_EP0 = 0;
//
USB_CTL1 = 0x93;
while(!(USB_STA0 & 0x02));
USB_CTL1 = USB_CTL1 & 0x7F; //clear tx0e
USB_STA0 = USB_STA0 | 0x02; //txd0f=0
out_status();
NEW_MM_KB_DATA = 0;
}
else {
USB_CTL1 = USB_CTL1 | 0x20;
USB_CTL2 = USB_CTL2 | 0x40;
}
break;
case SET_IDLE:
if((SETUPR0==0x21) & (SETUPR4==0x00) & (SETUPR6==0) & (SETUPR7==0)){
//set kb idle rate
KB_idle_rate = SETUPR3;
in_status();
}
else if((SETUPR0==0x21) & (SETUPR4==0x01) & (SETUPR6==0) & (SETUPR7==0)){
//set kb idle rate
MS_idle_rate = SETUPR3;
in_status();
}
else {
USB_CTL1 = USB_CTL1 | 0x20;
USB_CTL2 = USB_CTL2 | 0x40;
}
break;
case GET_IDLE:
if((SETUPR0==0xA1) & (SETUPR4==0x00) & (SETUPR6==1) & (SETUPR7==0)){
USB_EPP = 0;
USB_EP0 = KB_idle_rate;
USB_CTL1 = 0x91;
while(!(USB_STA0 & 0x02));
USB_CTL1 = USB_CTL1 & 0x7F; //clear tx0e
USB_STA0 = USB_STA0 | 0x02; //txd0f=0
out_status();
}
else if((SETUPR0==0xA1) & (SETUPR4==0x01) & (SETUPR6==1) & (SETUPR7==0)){
USB_EPP = 0;
USB_EP0 = MS_idle_rate;
USB_CTL1 = 0x91;
while(!(USB_STA0 & 0x02));
USB_CTL1 = USB_CTL1 & 0x7F; //clear tx0e
USB_STA0 = USB_STA0 | 0x02; //txd0f=0
out_status();
}
else {
USB_CTL1 = USB_CTL1 | 0x20;
USB_CTL2 = USB_CTL2 | 0x40;
}
break;
case SET_PROTOCAL: //R0=0x21,R2R3=protocal,R4R5=intf,R6R7=0
if((SETUPR0==0x21) & (SETUPR4==0x00) & (SETUPR6==0) & (SETUPR7==0)){
//set kb report protocal
KB_protocal = SETUPR2;
in_status();
}
//else if((SETUPR0==0x21) & (SETUPR4==0x01) & (SETUPR6==0) & (SETUPR7==0)){
// //set mouse report protocal
// MS_protocal = SETUPR2;
// in_status();
//}
else {
USB_CTL1 = USB_CTL1 | 0x20;
USB_CTL2 = USB_CTL2 | 0x40;
}
break;
case GET_PROTOCAL:
if((SETUPR0==0xA1) & (SETUPR4==0x00) & (SETUPR6==1) & (SETUPR7==0)){
USB_EPP = 0;
USB_EP0 = KB_protocal;
USB_CTL1 = 0x91;
while(!(USB_STA0 & 0x02));
USB_CTL1 = USB_CTL1 & 0x7F; //clear tx0e
USB_STA0 = USB_STA0 | 0x02; //txd0f=0
out_status();
}
else {
USB_CTL1 = USB_CTL1 | 0x20;
USB_CTL2 = USB_CTL2 | 0x40;
}
break;
default:{
// Invalid Command
USB_CTL1 = USB_CTL1 | 0x20;
USB_CTL2 = USB_CTL2 | 0x40;
break;
}
}//class switch
}//if
else {
// Invalid Command
USB_CTL1 = USB_CTL1 | 0x20;
USB_CTL2 = USB_CTL2 | 0x40;
}
} // else
}
void in_status(void)
{
//control-write status stage
USB_CTL1 = USB_CTL1 & 0xF0; //tx_sz = 0;
USB_CTL1 = USB_CTL1 | 0x90; //tx0e=1, t0seq=1
while (!(USB_STA0 & 0x02)); //wait txd0f=1
USB_CTL1 = USB_CTL1 & 0x7F; //clear tx0e
USB_STA0 = USB_STA0 | 0x02; //clear txd0f
return;
}
void out_status(void)
{
//control-read status stage
USB_CTL1 = USB_CTL1 | 0x40; //enable rxd0e
while(!(USB_STA0 & 0x01)); //wait rxd0f=1
if(!(USB_STA1 & 0x80)) USB_STA0 = USB_STA0 | 0x01; //clear rxd0f
USB_CTL1 = USB_CTL1 & 0xBF; //disable rxd0e
return;
}
char PS2A_BAT(void)
{
unsigned char BAT;
BAT=0;
PS2A_CTL = 0x32;
do {
if((PS2A_STA & 0x01)){ //rx mouse data
PS2A_STA = PS2A_STA | 0x01; //clr rx_if
if(PS2A_D==0xAA) BAT=1;
else BAT=0;
}
} while(BAT==0);
return 0;
}
char PS2B_BAT(void)
{
unsigned char BAT;
BAT=0;
PS2B_CTL = 0x32;
do {
if((PS2B_STA & 0x01)){ //rx mouse data
PS2B_STA = PS2B_STA | 0x01; //clr rx_if
if(PS2B_D==0x00) BAT=1;
else BAT=0;
}
} while(BAT==0);
return 0;
}
//RESET, RESET WRAP(2)
unsigned char PS2A_MS_NOACK(unsigned char cmd) //no DATA, no ACK command
{
PS2A_CTL = PS2A_CTL | 0x32;
PS2A_STA = 0xFF;
//
PS2A_CTL = PS2A_CTL | 0x01; //set tx_on;
PS2A_D = cmd;
//wait for tx_if=1
while(!(PS2A_STA & 0x10));
//check if tx_to=1
if(PS2A_STA & 0x20) {return 1;} //command fail, time-out
//check if tx_ack=0
if((PS2A_STA & 0x40)) {return 2;}
PS2A_STA = 0x10; //clr tx_if
return 0; //command succeed
}
//RESET, RESET WRAP(2)
unsigned char PS2B_MS_NOACK(unsigned char cmd) //no DATA, no ACK command
{
PS2B_CTL = PS2B_CTL | 0x32;
PS2B_STA = 0xFF;
//
PS2B_CTL = PS2B_CTL | 0x01; //set tx_on;
PS2B_D = cmd;
//wait for tx_if=1
while(!(PS2B_STA & 0x10));
//check if tx_to=1
if(PS2B_STA & 0x20) {return 1;} //command fail, time-out
//check if tx_ack=0
if((PS2B_STA & 0x40)) {return 2;}
PS2B_STA = 0x10; //clr tx_if
return 0; //command succeed
}
//RESEND, DISABLE, ENABLE, SET REMOTE, RESET REMOTE, SET STREAM, SCALING, RESET SCALING, SET DEFAULT(9)
unsigned char PS2A_MS_ACK(unsigned char cmd) //no DATA, with ACK command
{
PS2A_CTL = PS2A_CTL | 0x32;
PS2A_STA = 0xFF;
//
PS2A_CTL = PS2A_CTL | 0x01; //set tx_on;
PS2A_D = cmd;
//wait for tx_if
while(!(PS2A_STA & 0x10));
if(PS2A_STA & 0x20) return 1; //command fail, time-out
//check tx_ack
if((PS2A_STA & 0x40)) return 2;
PS2A_STA = 0x10; //clr tx_if
//
//ACK stage
//
while(!(PS2A_STA & 0x01)); //wait rx_if
PS2A_STA = PS2A_STA & 0x01; //clr rx_if
if((PS2A_STA & 0x0E)) return 3; //command fail
if(PS2A_D != 0xFA) return 4; //rx data is not ACK
else {return 0;} //command succeed
}
//RESEND, DISABLE, ENABLE, SET REMOTE, RESET REMOTE, SET STREAM, SCALING, RESET SCALING, SET DEFAULT(9)
unsigned char PS2B_MS_ACK(unsigned char cmd) //no DATA, with ACK command
{
PS2B_CTL = PS2B_CTL | 0x32;
PS2B_STA = 0xFF;
//
PS2B_CTL = PS2B_CTL | 0x01; //set tx_on;
PS2B_D = cmd;
//wait for tx_if
while(!(PS2B_STA & 0x10));
if(PS2B_STA & 0x20) return 1; //command fail, time-out
//check tx_ack
if((PS2B_STA & 0x40)) return 2;
PS2B_STA = 0x10; //clr tx_if
//
//ACK stage
//
while(!(PS2B_STA & 0x01)); //wait rx_if
PS2B_STA = PS2B_STA & 0x01; //clr rx_if
if((PS2B_STA & 0x0E)) return 3; //command fail
if(PS2B_D != 0xFA) return 4; //rx data is not ACK
else {return 0;} //command succeed
}
unsigned char PS2A_MS_GET_DEVICE_TYPE(void)
{
unsigned char ch_1;
PS2A_CTL = PS2A_CTL | 0x32;
PS2A_STA = 0xFF;
//
PS2A_CTL = PS2A_CTL | 0x01; //set tx_on;
PS2A_D = PS2_MS_GET_DEVICE_TYPE;
//wait for tx_if
while(!(PS2A_STA & 0x10));
if(PS2A_STA & 0x20) return 1; //command fail, time-out
//check tx_ack
if((PS2A_STA & 0x40)) return 2;
PS2A_STA = 0x10; //clr tx_if
//
//ACK stage
//
while(!(PS2A_STA & 0x01)); //wait rx_if
PS2A_STA = PS2A_STA & 0x01; //clr rx_if
if((PS2A_STA & 0x0E)) return 3; //command fail
if(PS2A_D != 0xFA) return 4; //rx data is not ACK
//
// Get 0xab 101/102 KB Type
//
while(!(PS2A_STA & 0x01)); //wait rx_if
PS2A_STA = PS2A_STA & 0x01; //clr rx_if
if((PS2A_STA & 0x0E)) return 3; //command fail
if(ch_1 != 0xab) return 4;
//
// Get 0x83 101/102 KB Type
//
while(!(PS2A_STA & 0x01)); //wait rx_if
PS2A_STA = PS2A_STA & 0x01; //clr rx_if
if((PS2A_STA & 0x0E)) return 3; //command fail
if(ch_1 != 0x83) return 4;
}
unsigned char PS2A_MS_GET_STATUS(void)
{
PS2A_CTL = PS2A_CTL | 0x32;
PS2A_STA = 0xFF;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -