📄 test02.c
字号:
//////////////////////////////////////////////////////////////////////
// //////
// Author: Tony Chen (July 22, 2005) //////
// //////
//////////////////////////////////////////////////////////////////////
#include "REG515A.H"
#include "USB2PS2.h"
#define GET_STATUS 0
#define CLEAR_FEATURE 1
#define SET_FEATURE 3
#define SET_ADDRESS 5
#define GET_DESCRIPTOR 6
#define SET_DESCRIPTOR 7
#define GET_CONFIGURATION 8
#define SET_CONFIGURATION 9
#define GET_INTERFACE 10
#define SET_INTERFACE 11
#define SYNC_FRAME 12
#define DEVICE_DESC 1
#define CONFIGURATION_DESC 2
#define STRING_DESC 3
#define INTERFACE_DESC 4
#define ENDPOINT_DESC 5
#define DEVICE_QUALIFIER 6
#define OTHER_SPEED_CONFIGURATION 7
#define INTERFACE_POWER 8
#define CLASS_HID_DESC 0x21
#define CLASS_REPORT_DESC 0x22
#define CLASS_PHY_DESC 0x23
#define GET_REPORT 0x01
#define GET_IDLE 0x02
#define GET_PROTOCAL 0x03
#define SET_REPORT 0x09
#define SET_IDLE 0x0A
#define SET_PROTOCAL 0x0B
#define KB_BUF_MAX_LEN 16
#define PS2_MS_RESET 0xFF //No ACK, *
#define PS2_MS_RESEND 0xFE // *
#define PS2_MS_SET_DEFAULT 0xF6 // *
#define PS2_MS_DISABLE 0xF5 // *
#define PS2_MS_ENABLE 0xF4 // *
#define PS2_MS_SET_SAMPLE_RATE 0xF3
#define PS2_MS_GET_DEVICE_TYPE 0xF2 // *
#define PS2_MS_SET_REMOTE 0xF0 // *
#define PS2_MS_SET_WRAP 0xEE // *
#define PS2_MS_RESET_WRAP 0xEC //No ACK
#define PS2_MS_READ_DATA 0xEB
#define PS2_MS_SET_STREAM 0xEA // *
#define PS2_MS_GET_STATUS 0xE9 // *
#define PS2_MS_SET_RESOLUTION 0xE8
#define PS2_MS_SCALING 0xE7 // *
#define PS2_MS_RESET_SCALING 0xE6 // *
#define KB_LED_OOC 0xED //(b2,b1,b0)=(CapsLock,NumLock,ScrolLock)
#define KB_ECHO 0xEE
#define KB_SET_SCAN_scan_code 0xF0
#define KB_SET_TYPEMATIC 0xF3
#define KB_ENABLE 0xF4
#define KB_STD_DISABLE 0xF5
#define KB_STD_ENABLE 0xF6
#define KB_RESEND 0xFE
#define KB_RESET 0xFF
unsigned char rx_ms_bcnt;
unsigned char len, len1;
unsigned char i,j;
unsigned char SETUPR0;
unsigned char SETUPR1;
unsigned char SETUPR2;
unsigned char SETUPR3;
unsigned char SETUPR4;
unsigned char SETUPR5;
unsigned char SETUPR6;
unsigned char SETUPR7;
unsigned char configuration;
unsigned char KB_idle_rate;
unsigned char KB_protocal;
unsigned char NEW_KB_DATA;
unsigned char KB_DATA[8];
bit NEW_SYS_KB_DATA;
unsigned char SYS_KB_DATA;
bit NEW_MM_KB_DATA;
unsigned char MM_KB_DATA[2];
unsigned char MOUSE_DATA[3];
unsigned char MOUSE_STATUS[3];
unsigned char LED;
unsigned char rd_kb_ptr;
unsigned char rx_kb_ptr;
unsigned char KB_BUF_DATA[KB_BUF_MAX_LEN];
unsigned char mm_key0, mm_key1;
unsigned char MS_idle_rate;
unsigned char kb_idle_cnt;
unsigned char ms_idle_cnt;
unsigned char tmr_10ms_cnt;
bit configured;
bit toggle;
bit ep1toggle;
bit ep2toggle;
bit remote_wku_enable;
bit left_shift_make;
bit right_shift_make;
bit left_ctrl_make;
bit right_ctrl_make;
bit left_alt_make;
bit right_alt_make;
bit left_gui_make;
bit right_gui_make;
bit kb_check;
bit caps_make;
bit numlock_make;
bit scroll_make;
bit power_make;
bit sleep_make;
bit wakeup_make;
bit key_make;
void xreg_init(void);
void SETUP_PROC(void);
void in_status(void);
void out_status(void);
unsigned char PS2A_MS_NOACK(unsigned char);
unsigned char PS2A_MS_ACK(unsigned char);
unsigned char PS2A_MS_GET_DEVICE_TYPE(void);
unsigned char PS2A_MS_GET_STATUS(void);
unsigned char PS2B_MS_NOACK(unsigned char);
unsigned char PS2B_MS_ACK(unsigned char);
void wait100us(void);
char PS2A_BAT(void);
char PS2B_BAT(void);
unsigned char get_next_scan_code(void);
unsigned char KB_SET_ONE(unsigned char, unsigned char);
unsigned char kb_scan(void);
void main (void) {
unsigned char usb_kb_ptr;
unsigned char key;
SP = 0x7f;
xreg_init();
kb_idle_cnt=0;
ms_idle_cnt=0;
tmr_10ms_cnt=0;
//
// According to recommendation from HID 1.1 spec
// Keyboard idle rate could be initialized to 500ms,
// Mouse idle rate is set to infinity (idle rate=0)
KB_idle_rate=125; //500ms/4ms=125
MS_idle_rate=0; //infinity
kb_check=0;
KB_protocal=0;
rd_kb_ptr = 0;
rx_kb_ptr = 0;
KB_DATA[1]=0;
usb_kb_ptr = 2;
key_make=0;
NEW_SYS_KB_DATA=0;
NEW_MM_KB_DATA=0;
EAL= 1;
//==== PS2A connected to KB
while(PS2A_MS_NOACK(PS2_MS_RESET));
PS2A_BAT();
PS2A_MS_GET_DEVICE_TYPE();
PS2A_MS_ACK(PS2_MS_SET_DEFAULT);
PS2A_MS_GET_STATUS();
PS2A_MS_ACK(PS2_MS_ENABLE);
//==== PS2B connected to Mouse
while(PS2B_MS_NOACK(PS2_MS_RESET));
PS2B_BAT();
PS2B_MS_ACK(PS2_MS_SET_DEFAULT);
PS2B_MS_ACK(PS2_MS_ENABLE);
// flash KeyBoard LED ON and then OFF
//
KB_SET_ONE(KB_LED_OOC, 0xff);
wait100us();
wait100us();
wait100us();
KB_SET_ONE(KB_LED_OOC, 0x00);
// start recieving data report
// enable ps2a module, set ps2en, rx_ie
// enable INT7 interrupt (PS2)
PS2A_STA = 0xFF;
PS2A_CTL = 0x36;
PS2B_STA = 0xFF;
PS2B_CTL = 0x36;
rx_ms_bcnt=0;
EX7 = 1;
configured=0;
//
// setup timer 3 for 1ms interrupt to check for kb buffer status
//
TIM_SEL = 0x03; //select timer 3
TIM_CTL = 0x48; //enable tie, xtal/16, stop, timer mode
TIM_PRE = 74; //(12M/16)/75/10=1ms
TIMER = 9;
TIM_SEL = 0x83; //enable
TIM_CTL = 0x40; //run
ET0 = 1;
// enable usb module, set usben & pull_en
USB_CTL0 = 0x90;
EX5 = 1;
USB_CTL4 = 0x40; // reset
restart: while(configured==0);
while(1){//ep1,2 processing
// check if any keyboard data input
if(kb_check) {
key_make=0;
while(rd_kb_ptr != rx_kb_ptr){
key=kb_scan();
if(key != 0) {
// new key pressed
if(usb_kb_ptr >= 8) {
//over-flow
KB_DATA[2]=0x01;
KB_DATA[3]=0x01;
KB_DATA[4]=0x01;
KB_DATA[5]=0x01;
KB_DATA[6]=0x01;
KB_DATA[7]=0x01;
//
usb_kb_ptr = 2;
}
else {
KB_DATA[usb_kb_ptr++]=key;
}
}
}
if(key | left_shift_make | right_shift_make | left_ctrl_make | right_ctrl_make
|left_alt_make | right_alt_make | right_gui_make | left_gui_make){
// new modifier press
if(left_shift_make) KB_DATA[0] |= 0x02; //bit 1
if(right_shift_make) KB_DATA[0] |= 0x20; //bit 5
if(left_ctrl_make) KB_DATA[0] |= 0x01; //bit 0
if(right_ctrl_make) KB_DATA[0] |= 0x10; //bit 4
if(left_alt_make) KB_DATA[0] |= 0x04; //bit 2
if(right_alt_make) KB_DATA[0] |= 0x40; //bit 6
if(left_gui_make) KB_DATA[0] |= 0x08; //bit 3
if(right_gui_make) KB_DATA[0] |= 0x80; //bit 7
NEW_KB_DATA = 1;
}
if(mm_key0 != 0 || mm_key1 != 0){
//new mm key pressed
MM_KB_DATA[0]=mm_key0;
MM_KB_DATA[1]=mm_key1;
NEW_MM_KB_DATA = 1;
}
if(power_make | sleep_make | wakeup_make){
//new sys key pressed
if(power_make) SYS_KB_DATA = 0x81;
else if(sleep_make) SYS_KB_DATA = 0x82;
else if(wakeup_make) SYS_KB_DATA = 0x83;
NEW_SYS_KB_DATA = 1;
}
kb_check=0;
}
if(NEW_KB_DATA && ((USB_CTL2 & 0x80)==0)){
NEW_KB_DATA=0;
USB_EPP = 0;
USB_EP1 = KB_DATA[0];
USB_EP1 = KB_DATA[1];
USB_EP1 = KB_DATA[2];
USB_EP1 = KB_DATA[3];
USB_EP1 = KB_DATA[4];
USB_EP1 = KB_DATA[5];
USB_EP1 = KB_DATA[6];
USB_EP1 = KB_DATA[7];
if(ep1toggle) USB_CTL2 = 0x98;
else USB_CTL2 = 0x88;
while(!(USB_STA0 & 0x04));
USB_CTL2 = USB_CTL2 & 0x7F; //clear tx1e
USB_STA0 = USB_STA0 | 0x04; //txd1f=0
ep1toggle = ~ep1toggle;
//
// flush buffer
//
KB_DATA[0] = 0;
KB_DATA[1] = 0;
KB_DATA[2] = 0;
KB_DATA[3] = 0;
KB_DATA[4] = 0;
KB_DATA[5] = 0;
KB_DATA[6] = 0;
KB_DATA[7] = 0;
//
usb_kb_ptr = 2;
kb_idle_cnt=0;
key_make = 1;
}
if(NEW_SYS_KB_DATA && ((USB_CTL3 & 0x80)==0)){
kb_idle_cnt=0;
// ep 2 traffic, 2 bytes data packet
NEW_SYS_KB_DATA=0;
USB_EPP=0;
USB_EP2 = 2; //Report Id=2;
USB_EP2 = SYS_KB_DATA;
if(ep2toggle) USB_CTL3 = 0x92;
else USB_CTL3 = 0x82;
while(!(USB_STA0 & 0x10));
USB_CTL3 = USB_CTL3 & 0x7F; //clear tx2e
USB_STA0 = USB_STA0 | 0x10; //txd2f=0
ep2toggle = ~ep2toggle;
//
// flush buffer
//
MM_KB_DATA[0] = 0;
MM_KB_DATA[1] = 0;
kb_idle_cnt=0;
}
if(NEW_MM_KB_DATA && ((USB_CTL3 & 0x80)==0)){
// ep2 traffic, 3 bytes data packet
NEW_MM_KB_DATA = 0;
USB_EPP = 0;
USB_EP2 = 3; //Report Id=3
USB_EP2 = MM_KB_DATA[0];
USB_EP2 = MM_KB_DATA[1];
if(ep2toggle) USB_CTL3 = 0x93;
else USB_CTL3 = 0x83;
while(!(USB_STA0 & 0x10));
USB_CTL3 = USB_CTL3 & 0x7F; //clear tx2e
USB_STA0 = USB_STA0 | 0x10; //txd2f=0
ep2toggle = ~ep2toggle;
//
// flush buffer
//
MM_KB_DATA[0] = 0;
MM_KB_DATA[1] = 0;
kb_idle_cnt=0;
}
//==============================================================//
if(key_make | (KB_idle_rate & (kb_idle_cnt > (KB_idle_rate << 2)))){
//forced to tx one kb packet
USB_EPP=0;
USB_EP1=0;
USB_EP1=0;
USB_EP1=0;
USB_EP1=0;
USB_EP1=0;
USB_EP1=0;
USB_EP1=0;
USB_EP1=0;
if(ep1toggle) USB_CTL2 = 0x98;
else USB_CTL2 = 0x88;
while(!(USB_STA0 & 0x04));
USB_CTL2 = USB_CTL2 & 0x7F; //clear tx1e
USB_STA0 = USB_STA0 | 0x04; //txd1f=0
ep1toggle = ~ep1toggle;
kb_idle_cnt = 0;
key_make = 0;
}
if(MS_idle_rate & (ms_idle_cnt>(MS_idle_rate << 2))){
//forced tx one mouse packet
USB_EPP = 0;
USB_EP2 = 1; //Report ID = 1
USB_EP2 = 0;
USB_EP2 = 0;
USB_EP2 = 0;
USB_EP2 = 0;
if(ep2toggle) USB_CTL3 = 0x95;
else USB_CTL3 = 0x85;
while(!(USB_STA0 & 0x10));
USB_CTL3 = USB_CTL3 & 0x7F; //clear tx2e
USB_STA0 = USB_STA0 | 0x10; //txd2f=0
ep2toggle = ~ep2toggle;
ms_idle_cnt=0;
}
if(configured == 0) goto restart;
} // while loop
}
void wait100us(void)
{
int iii;
for(iii=0;iii<1000;iii++);
}
void xreg_init(void)
{
IEN0 = 0;
IEN1 = 0;
//
USB_CTL0 = 0;
USB_CTL1 = 0;
USB_CTL2 = 0;
USB_CTL3 = 0;
USB_CTL4 = 0;
USB_CTL5 = 0; //device address
USB_STA0 = 0xFF;
USB_EPP = 0;
PS2A_CTL = 0xF2; //enable PS2A
PS2A_STA = 0xFF;
//
PS2B_CTL = 0xF2; //enable PS2B
PS2B_STA = 0xFF;
}
void USB_ISR (void) interrupt 12
{
if(USB_STA0 & 0x40) { //host reset signalling
USB_CTL0 = 0x90; //set usben,pullen
USB_CTL1 = 0x00;
USB_CTL2 = 0x00;
USB_CTL3 = 0x00;
USB_CTL4 = 0x41; //set urstie,rxd0ie
USB_CTL5 = 0x00;
USB_EPP = 0x00;
USB_STA0 = 0xff; //clr all interrupt flags
configured= 0;
ep1toggle = 0;
ep2toggle = 0;
return;
}
if(USB_STA0 & 0x80) { //host resume signalling
USB_STA0 = USB_STA0 | 0x80; //clr resumef
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -