基于FPGA的PS/2鼠標接口設(shè)計方法及其應(yīng)用
如圖6,當狀態(tài)機m2_state復位時,即進入m2_reset狀態(tài),并在 一個clk周期后進入m2_hold_clk_l狀態(tài),當ps2_clk_hi_z(時鐘線)被拉低并保持400μS后進入m2_data_low_1狀態(tài),此時向鼠標 發(fā)送起始 位和d[0]、d[1](d[0]=d[1]=0)。完成后進入m2_data_high_1狀態(tài), 發(fā)送d[2](d[2]=1)并進入m2_data_low_2狀態(tài),此時向鼠標發(fā)送d[3]位(d[3]=0), 完成發(fā)送進入m2_data_high_2狀態(tài),向鼠標發(fā)送d[4]、d[5]、d[6]、d[7](d[4]=d[5]=d[6]=d[7]=1),完成發(fā)送進入m2_data_low_3狀 態(tài),向鼠標發(fā)送奇偶校驗位,然后進入m2_data_high_3狀態(tài),將數(shù)據(jù)線拉高,等待鼠標返回應(yīng)答信號。若PS/2時鐘信號下降沿來臨時,數(shù)據(jù)線仍未變?yōu)楦唠娖剑瑒t進入m2_error_no_ack狀態(tài),此時握手失敗,系統(tǒng)將保持m2_error_no_ack狀態(tài)直到下一次復位,否則進入m2_await_response狀態(tài)接收應(yīng)答字,接收完成進入m2_verify數(shù)據(jù)校驗,然后進入m2_use狀態(tài),鎖存輸出數(shù)據(jù),并進入m2_wait狀態(tài),等待接收數(shù)據(jù)。當檢測到時鐘下降沿后進入m2_gather狀態(tài),接收鼠標數(shù)據(jù)包,接收完成進入m2_verify狀態(tài),此時便形成了數(shù)據(jù)接收循環(huán)。
PS/2程序源碼
entity mouse is
Port (clk : in std_logic; reset : in std_logic; ps2_clk : inout std_logic; ps2_data : inout std_logic; left_button : out std_logic; right_button : out std_logic; mousex: buffer std_logic_vector(9 downto 0); mousey: buffer std_logic_vector(9 downto 0); data_ready : out std_logic; error_no_ack : out std_logic);
end mouse;
architecture Behavioral of mouse is
--變量、信號定義(略)
begin
ps2_clk = '0' when ps2_clk_hi_z='0' else 'Z';
ps2_data = '0' when ps2_data_hi_z='0' else 'Z';
--檢測ps2clk上升沿和下降沿(略)
m2statech: process (reset, clk) ------------------m2 狀態(tài)
begin
if (reset='0') then
m2_state = m2_reset;
elsif (clk'event and clk='1') then
m2_state = m2_next_state;
end if;
end procESS;
--m2 狀態(tài)傳輸邏輯
m2statetr: process (m2_state, q, fall,rise,watchdog_timer_done,bitcount,ps2_data,packet_good)
begin
ps2_clk_hi_z = '1';
ps2_data_hi_z = '1';
error_no_ack = '0';
output_strobe = '0';
case m2_state is
when m2_reset => -- 復位后向鼠標發(fā)送命令字
m2_next_state = m2_hold_clk_l;
when m2_wait =>
if (fall='1') then
m2_next_state = m2_gather;
else
m2_next_state = m2_wait;
end if;
when m2_gather =>
if ((watchdog_timer_done='1') and (bitcount=TOTAL_BITS))then
m2_next_state = m2_verify;
else
m2_next_state = m2_gather;
end if;
when m2_verify =>
--if (bitcount TOTAL_BITS) then
--m2_next_state = m2_wait;
--else
m2_next_state = m2_use;
--end if;
when m2_use =>
output_strobe = '1';
m2_next_state = m2_wait;
-- 用狀態(tài)機的9個狀態(tài)實現(xiàn)命令字傳輸,使鼠標進入"streaming"模式,并等待鼠標正確應(yīng)答
評論