?? ps2.c
字號(hào):
/* created : hw-chen */
/* descript : 重新發(fā)送數(shù)據(jù)包,由于數(shù)據(jù)一直在發(fā)送緩沖區(qū),只需要重新復(fù)制長(zhǎng)度就可以 */
/********************************************************************************/
void ps2_resend(void)
{ ps2_xmit.ps2_xmit = ps2_xmit.ps2_length;
}
/********************************************************************************/
/* function : ps2_set_default_state() */
/* created : hw-chen */
/* descript : 設(shè)置鼠標(biāo)的缺省狀態(tài) */
/********************************************************************************/
void ps2_set_default_state(void)
{ ps2_parameter.report_rate = 100; //* 100 reports per second
ps2_parameter.report_interval = 10; //* is equal to a report every 10 msec
ps2_parameter.scale = SC_1_1; //* 1-to-1 scaling
ps2_parameter.stream = 0x1; //* stream mode on
ps2_parameter.resolution = RES_1MM; //* 1 MM resolution
ps2_parameter.enable = 0x0; //* not enabled
ps2_parameter.zmouse = 0x0; //* z-wheel off.
ps2_parameter.wrap = 0x0;
}
/********************************************************************************/
/* function : ps2_disable() */
/* created : hw-chen */
/* descript : 禁止鼠標(biāo) */
/********************************************************************************/
void ps2_disable(void)
{ ps2_parameter.enable = 0x0;
}
/********************************************************************************/
/* function : ps2_enable() */
/* created : hw-chen */
/* descript : 允許鼠標(biāo)操作 */
/********************************************************************************/
void ps2_enable(void)
{ ps2_parameter.enable=0x1;
return;
}
/********************************************************************************/
/* function : ps2_set_sample_rate_ack() */
/* created : hw-chen */
/* descript : ACK設(shè)置鼠標(biāo)采樣,由于第二字節(jié)是采樣率,這里是接收到第一字節(jié),所以這 */
/* 里不處理 */
/********************************************************************************/
void ps2_set_sample_rate_ack(void)
{
}
/********************************************************************************/
/* function : ps2_set_sample_rate() */
/* created : hw-chen */
/* descript : 設(shè)置鼠標(biāo)的采樣率 */
/********************************************************************************/
char ps2_set_sample_rate(char rate)
{ char i;
for(i=0x0;i<PS2_NUM_RATES;i++)
{ if((rate==ps2_sample_rate[i]))
{ ps2_parameter.report_interval=ps2_sample_intervals[i];
ps2_parameter.report_rate=rate;
ps2_rst_interval();
return(PS2_ACK);
}
}
return(PS2_RESEND);
}
/********************************************************************************/
/* function : ps2_read_device_type() */
/* created : hw-chen */
/* descript : 讀取設(shè)備的類(lèi)型,由于本設(shè)計(jì)采用了5鍵鼠標(biāo),需要回答0x4,如果是3鍵 */
/* 鼠標(biāo)需要回應(yīng)0x3,否則應(yīng)答0x0 */
/********************************************************************************/
void ps2_read_device_type(void) /* 0xF2 */
{ ps2_xmit.ps2_buffer[0x0]=0x0;
ps2_xmit.ps2_length = 0x1;
ps2_xmit.ps2_xmit = 0x1;
}
/********************************************************************************/
/* function : ps2_set_remote_mode() */
/* created : hw-chen */
/* descript : 設(shè)置成為REM模式 */
/********************************************************************************/
void ps2_set_remote_mode(void)
{ ps2_parameter.stream = 0x0;
}
/********************************************************************************/
/* function : ps2_rst_wrap_mode() */
/* created : hw-chen */
//* descript : 根據(jù)IBM建議,如果STREAM模式是允許的,如果WRAP模式RST,鼠標(biāo)被禁止 */ */
/********************************************************************************/
void ps2_rst_wrap_mode(void)
{ ps2_parameter.wrap=0x0;
if((ps2_parameter.stream))
{ ps2_parameter.enable=0x0;
}
}
/********************************************************************************/
/* function : ps2_set_wrap_mode() */
/* created : hw-chen */
/* descript : 允許WRAP模式 */
/********************************************************************************/
void ps2_set_wrap_mode(void)
{ ps2_parameter.wrap = 1;
}
/********************************************************************************/
/* function : ps2_set_stream_mode() */
/* created : hw-chen */
/* descript : 設(shè)置流模式 */
/********************************************************************************/
void ps2_set_stream_mode(void)
{ ps2_parameter.stream = 1;
ps2_rst_interval();
}
/********************************************************************************/
/* function : ps2_read_data() */
/* created : hw-chen */
/* descript : 返回鼠標(biāo)數(shù)據(jù)包給主機(jī),回應(yīng)主機(jī)的讀數(shù)據(jù)命令 */
/********************************************************************************/
void ps2_read_data(void)
{ mouse_status.change_flag = 1;
ps2_rst_interval();
mouse_status.change_flag|=mouse_poll_adns(); //* 讀取光學(xué)器件,查看鼠標(biāo)是否有移動(dòng)
ps2_packet_mouse_mdata();
}
/********************************************************************************/
/* function : ps2_status_request() */
/* created : hw-chen */
/* descript : 發(fā)送狀態(tài)響應(yīng)給主機(jī) */
/********************************************************************************/
void ps2_status_request(void)
{ ps2_xmit.ps2_buffer[0] = 0x20;
ps2_xmit.ps2_buffer[1] = 0x3;
ps2_xmit.ps2_buffer[2] = 100;
ps2_xmit.ps2_length = 0x3;
ps2_xmit.ps2_xmit = 0x3;
return;
}
/********************************************************************************/
/* function : ps2_set_resolution_ack() */
/* created : hw-chen */
/* descript : 設(shè)置RESOLUTION的ACK,他是兩個(gè)字節(jié),第二字節(jié)是RESOLUTION,這個(gè)函數(shù) */
/* 是第一字節(jié)調(diào)用的,所以不處理 */
/********************************************************************************/
void ps2_set_resolution_ack(void)
{ return;
}
/********************************************************************************/
/* function : ps2_set_resolution() */
/* created : hw-chen */
/* descript : 設(shè)置鼠標(biāo)的分辨率 */
/********************************************************************************/
char ps2_set_resolution(char bRes)
{ switch (bRes)
{
case RES_1MM: //* only 4 valid resolutions accepted
case RES_2MM:
case RES_4MM:
case RES_8MM:
ps2_parameter.resolution = bRes;
return(PS2_ACK);
default:
break;
}
return(PS2_RESEND); //*
}
/********************************************************************************/
/* function : ps2_set_scaling() */
/* created : hw-chen */
/* descript : 設(shè)置SCALING */
/********************************************************************************/
void ps2_set_scaling(void)
{ ps2_parameter.scale = SC_2_1;
}
/********************************************************************************/
/* function : ps2_rst_scaling() */
/* created : hw-chen */
/* descript : 復(fù)位SCALING */
/********************************************************************************/
void ps2_rst_scaling(void)
{ ps2_parameter.scale = SC_1_1;
}
unsigned char ps2_interval_count; //interval count, in msec, for reporting mouse packets
void ps2_rst_interval(void)
{ ps2_interval_count = ps2_parameter.report_interval;
}
/********************************************************************************/
/* function : ps2_mouse_tasking() */
/* created : hw-chen */
/* descript : 鼠標(biāo)在上電后或應(yīng)答RESET(0xFF)命令就進(jìn)入RESET模式,進(jìn)入這個(gè)模式后 */
/* 鼠標(biāo)執(zhí)行象前面提到的BAT基本保證測(cè)試一樣的自檢并設(shè)置如下的缺省值 */
/* 1.采樣速率100 采樣點(diǎn)/秒 */
/* 2.分辨率4 個(gè)計(jì)數(shù)值/毫米 */
/* 3.縮放比例1:1 */
/* 4.數(shù)據(jù)報(bào)告被禁止 */
/* 然后發(fā)送BAT完成代碼,這個(gè)代碼不是0xAA就是0xFC 錯(cuò)誤(BAT成功),如果 */
/* 主機(jī)收到了不是0xAA的回應(yīng),它可能重新給鼠標(biāo)供電這樣來(lái)引起鼠標(biāo)復(fù)位 */
/* 并重新執(zhí)行BAT */
/********************************************************************************/
void ps2_pro_bat(void)
{ dev_wait_x_msecond(250); //* 延時(shí)500毫秒
dev_wait_x_msecond(250);
ps2_xmit.ps2_buffer[0]=0xAA; //* send AA 00 to host
ps2_xmit.ps2_buffer[1]=0x00; //* fe
ps2_xmit.ps2_length =0x2;
ps2_xmit.ps2_xmit =0x2;
}
/********************************************************************************/
/* function : ps2_send_next_byte() */
/* created : hw-chen */
/* descript : 鼠標(biāo)發(fā)送下一字節(jié)數(shù)據(jù) */
/********************************************************************************/
void ps2_send_next_byte(void)
{ if((ps2_send_hostbyte(ps2_xmit.ps2_buffer[ps2_xmit.ps2_length-ps2_xmit.ps2_xmit]))) //send the byte
{ ps2_xmit.ps2_xmit--;
}
return;
}
/********************************************************************************/
/* function : ps2_mouse_tasking() */
/* created : hw-chen */
/* descript : 處理PS2鼠標(biāo)的任務(wù) */
/********************************************************************************/
void ps2_mouse_tasking(void)
{ if((msecond_status.ms_counter&0x3)==0x0) //* 4MS定時(shí)器
{ mouse_status.change_flag|=mouse_poll_buttons(); //* 查看是否有按鍵動(dòng)作
}
if((ps2_interval_count!=0x0)) //* PS2的報(bào)告時(shí)間沒(méi)有到
{ return;
}
if((ps2_parameter.enable!=0x1))
{ return;
}
if((ps2_parameter.stream!=0x1))
{ return;
}
ps2_rst_interval(); //* reset the interval
mouse_status.change_flag|=mouse_poll_adns(); //* 讀取光學(xué)器件,查看鼠標(biāo)是否有移動(dòng)
if((mouse_status.change_flag)==0x0)
{ return;
}
ps2_packet_mouse_mdata(); //* 發(fā)送數(shù)據(jù)給主機(jī)
}
/********************************************************************************/
/* function : ps2_start() */
/* created : hw-chen */
/* descript : 處理PS2鼠標(biāo)的任務(wù) */
/********************************************************************************/
void ps2_start(void)
{ P10CR = 0x03; //* PS2_PULLUP_ENABLE,OUTPUT_ENABLE;
P11CR = 0x05; //* OPEN_DRAIN,OUTPUT_ENABLE
USBCR = 0x00; //* 禁止USB
USBXCR = 0x00; //* 禁止USB上拉
P1DATA |= (PS2_SCLK_BIT|PS2_DATA_BIT);
}
/********************************************************************************/
/* function : ps2_main_process() */
/* created : hw-chen */
/* descript : 處理PS2的主函數(shù) */
/********************************************************************************/
void ps2_main_process(void)
{ ps2_start(); //* 初始化PS2的IO引腳
ps2_set_default_state(); //* 初始化成為缺省的狀態(tài)
ps2_pro_bat(); //* send the BAT code
while(1){
/****************************************************************************/
/* 如果正確接收到數(shù)據(jù) */
/****************************************************************************/
if((ps2_recv_hostbyte())){
ps2_pro_host_command(ps2_host_byte);
continue;
}
/****************************************************************************/
/* 如果有數(shù)據(jù)需要發(fā)送 */
/****************************************************************************/
if((ps2_xmit.ps2_xmit)){
ps2_send_next_byte();
continue;
}
/****************************************************************************/
/* 如果1MS定時(shí)器到 */
/****************************************************************************/
if((msecond_status.ms_flag&ONE_MSEC_FLAG)==ONE_MSEC_FLAG){
msecond_status.ms_flag&=~ONE_MSEC_FLAG;
ps2_mouse_tasking();
continue;
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -