Chip123 科技應用創新平台

標題: CPLD PS/2 Keyboard 程式問題..thx [打印本頁]

作者: ghoustchieh    時間: 2008-1-17 05:04 PM
標題: CPLD PS/2 Keyboard 程式問題..thx
CPLD MAXII 1270  PS/2 Keyboard 程式問題+ f4 ~% e9 }+ Z. {8 x0 t1 K: @; L1 Z
我在網路上收尋到FPGA 收 PS/2 Keyboard程式,將程式套用在CPLD上,Compiler後,無問題,依照程式將Pin腳定義,提供系統CLK,Reset,和PS/2 CLK,PS/2 Data,將DoRead和Scan_DAV(偵測PS/2是否送出信號)相接,Scan_Err接一顆LED(險示Overflow錯誤),Scan_Code接8顆LED(顯示PS/2 8bit Data),但燒錄後,CPLD啟動後,按鍵按下,毫無反應,並且PS/2 CLOCK信號已被降至低準位,似乎鍵盤已被抑制送出信號,請高手們幫忙提供意見好嗎?感謝......
1 h; n! Q* B/ L; \程式:
& }* h( w- ~9 J) j7 U' x- X-- PS2_Ctrl.vhd0 y5 d7 N9 \, D/ x' @- \" b- s
-- ------------------------------------------------
0 d) R0 O  b( @" l1 o% }/ m7 j-- Simplified PS/2 Controller (kbd, mouse...)" |1 A( l0 J: \% D- [4 g6 t! ]
-- ------------------------------------------------
$ F- M1 G# O& A! r-- Only the Receive function is implemented !/ K9 e1 H4 U* _2 L& q+ O. f6 {$ Q
-- (c) ALSE. http://www.alse-fr.com
9 q; M* \+ v0 a! ^& R: h% t+ Alibrary IEEE;% h4 H, z5 ^2 i! s6 m+ h" W
use IEEE.STD_LOGIC_1164.all;
* d, V% U' z1 H' puse IEEE.Numeric_std.all;1 ^) M  {7 [1 J) K
-- --------------------------------------: ^8 `  ^+ V$ y* ?4 u$ U
Entity PS2_Ctrl is
* t2 e) O' b+ L! C/ u6 v# W! p; _9 V-- --------------------------------------* ?! F, s8 ?! X1 Q
generic (FilterSize : positive := 8);8 z- ?; j: J; e) s
port( Clk : in std_logic; -- System Clock# J% Q! Q: y$ q9 Y0 Y  }' N
Reset : in std_logic; -- System Reset& n( A& m* ?8 P( x% v' K7 ]
PS2_Clk : in std_logic; -- Keyboard Clock Line
) s3 \4 L7 |& q* ~0 F2 R- wPS2_Data : in std_logic; -- Keyboard Data Line8 p1 T$ V/ Q# J- [) w: R
DoRead : in std_logic; -- From outside when reading the scan code
2 \3 S  K8 z% |, S. n0 Y. R9 ^+ b: mScan_Err : out std_logic; -- To outside : Parity or Overflow error
  N6 n5 p+ w! [+ W! M6 M2 EScan_DAV : out std_logic; -- To outside when a scan code has arrived
7 \+ R& o; _8 D* Q2 qScan_Code : out std_logic_vector(7 downto 0) -- Eight bits Data Out
. w- a! k" h- f. g9 W' h);# ?# n7 z: I9 ]
end PS2_Ctrl;4 M) d, T6 E$ \' b/ b
-- --------------------------------------
7 ?/ \9 v7 C3 k2 G1 rArchitecture ALSE_RTL of PS2_Ctrl is! M- J: Y, _/ O& I; r
-- --------------------------------------
; b; Q$ A0 Y* e6 c3 |4 N" ~" ^-- (c) ALSE. http://www.alse-fr.com# }2 H0 b! A+ F
-- Author : Bert Cuzeau.
3 U( b2 k( f; o" V6 b7 v) ~7 R4 G-- Fully synchronous solution, same Filter on PS2_Clk.
  w0 B" t7 O+ E9 l& O& h* x/ K-- Still as compact as "Plain_wrong"...
6 t9 G4 `# N. \) G5 _& S-- Possible improvement : add TIMEOUT on PS2_Clk while shifting
0 Q% ?! a* K& V8 @+ [0 [2 z5 u-- Note: PS2_Data is resynchronized though this should not be
( D* k" ?- p3 P& n6 j-- necessary (qualified by Fall_Clk and does not change at that time)./ y2 j: b1 T' M* d4 ]! J
-- Note the tricks to correctly interpret 'H' as '1' in RTL simulation.
0 J$ U6 L2 L  h8 A7 L2 Zsignal PS2_Datr : std_logic;4 \0 n' W7 T1 d3 \  K
subtype Filter_t is std_logic_vector(FilterSize-1 downto 0);
8 f: \$ j  M. H% E1 }7 c9 K- Osignal Filter : Filter_t;
+ S7 Z" K. d6 u; E( \. z3 dsignal Fall_Clk : std_logic;
$ n9 c; z, y4 fsignal Bit_Cnt : unsigned (3 downto 0);8 v6 T4 |/ L2 M
signal Parity : std_logic;
  C8 L  j) R$ W7 msignal Scan_DAVi : std_logic;
; {% ?; `" j* A8 T5 ^signal S_Reg : std_logic_vector(8 downto 0);3 a6 b* d6 p1 n$ \
signal PS2_Clk_f : std_logic;
& [, a3 @0 k! e* f! c& fType State_t is (Idle, Shifting);
* I! C5 A" w; p2 f* R6 Z2 J$ B, Esignal State : State_t;
3 N" ?; u6 o3 B1 {9 Nbegin/ b0 v1 ]/ U# o. J
Scan_DAV <= Scan_DAVi;; l$ s$ ~- K& h) p4 N0 u! E
-- This filters digitally the raw clock signal coming from the keyboard :0 P  ~, x( F, q
-- * Eight consecutive PS2_Clk=1 makes the filtered_clock go high
- i2 |/ H! B: j-- * Eight consecutive PS2_Clk=0 makes the filtered_clock go low
4 f7 v% l! u' }8 E8 A$ P7 w2 ~' S1 F-- Implies a (FilterSize+1) x Tsys_clock delay on Fall_Clk wrt Data7 o# h2 o- a$ w: I9 F0 |0 T3 m
-- Also in charge of the re-synchronization of PS2_Data! V6 r6 Z$ l4 U) z" s+ l2 N, S4 d
process (Clk,Reset)
+ b7 j  y" n% ^, _( s7 qbegin
" _) g2 m+ I0 U9 I0 q% P/ Sif Reset='1' then
( y2 Q4 Z4 h1 m1 A  {! QPS2_Datr <= '0';5 k. V1 r) M' g/ ^8 w+ Y
PS2_Clk_f <= '0';( o) N( f; l6 a! e/ p) M- u1 g3 g( t
Filter <= (others=>'0');
7 M& S/ u- a' q5 E. t) S/ NFall_Clk <= '0';
( Z: Q" @; ?2 C0 Selsif rising_edge (Clk) then' r) a$ l: K, S9 `7 z+ z2 u
PS2_Datr <= PS2_Data and PS2_Data; -- also turns 'H' into '1'+ R3 g: T$ W5 V( `9 t( Q
Fall_Clk <= '0';
/ \7 B/ ?+ t) L( ~! P% \Filter <= (PS2_Clk and PS2_CLK) & Filter(Filter'high downto 1);
& E' Q) @4 J, S+ ^; b: Aif Filter = Filter_t'(others=>'1') then
$ A$ ~: x: q  L+ e2 d3 BPS2_Clk_f <= '1';4 U& ^* _: O) I+ K% h' m, }
elsif Filter = Filter_t'(others=>'0') then- T6 ]6 H2 Q/ _, \) h* j
PS2_Clk_f <= '0';3 f# B. |3 s0 {4 m
if PS2_Clk_f = '1' then3 X) n9 E- e- x7 F
Fall_Clk <= '1';, }' N& f# P  V- h4 I1 T/ q
end if;
$ l+ [1 L: ]' q! T& C* h7 send if;
4 V! Q8 I4 ^( [/ i1 @$ T2 W" ?end if;
1 d6 e* c' j. E+ ~end process;
5 Z! c* k2 R$ e$ X1 a. R8 y-- This simple State Machine reads in the Serial Data# ^/ H( J5 o. Q6 ]
-- coming from the PS/2 peripheral.
0 r8 ?  q! e, l% R" A% h! N! E8 eprocess(Clk,Reset), j$ w1 H: j7 t' q  v. i" B) X, M$ M
begin
3 h4 W* w+ f6 F+ Xif Reset='1' then
8 N' v: x8 d; Z9 K; oState <= Idle;5 h( U7 z9 J0 n' T- l. ^# ?
Bit_Cnt <= (others => '0');- O& c" n1 c( x
S_Reg <= (others => '0');
# @2 p9 g1 @' g# ]Scan_Code <= (others => '0');
7 Z) A7 t5 |( b  ]) E7 XParity <= '0';7 \" V9 K" W& O- w- E9 P  s) q6 I
Scan_Davi <= '0';
1 k+ Y' g; L: @+ n; O! }$ K. vScan_Err <= '0';8 Z  s6 r2 f- ]3 j# ^/ L; o
elsif rising_edge (Clk) then" W6 a- j0 p' m+ @0 }
if DoRead='1' then. \8 H6 ?/ D7 T4 G
Scan_Davi <= '0'; -- note: this assgnmnt can be overriden/ [' D# t! S# b. H' s' t
end if;
! _2 [" l' N; g( o$ gcase State is
7 K9 g0 M! {: `" x, Q. fwhen Idle =>/ S0 Q! f4 M8 E( D% m
Parity <= '0';
7 d0 D' _4 Z. P0 T+ Y/ d1 X! ABit_Cnt <= (others => '0');+ ?( \$ Q% {% }2 q" j2 N% k
-- note that we dont need to clear the Shift Register8 F$ g' O5 l1 D9 d: b1 a
if Fall_Clk='1' and PS2_Datr='0' then -- Start bit
: ]7 x7 v- C  b9 O0 PScan_Err <= '0';' M, `) b3 a3 Z7 j& }
State <= Shifting;3 H! ^: D" C/ D, _
end if;/ _8 x1 t- v& p9 F4 m
when Shifting =>& r3 h6 W) N* \' W3 p4 P. B
if Bit_Cnt >= 9 then# n) Z& t, V* H' w8 v* ]3 W
if Fall_Clk='1' then -- Stop Bit
- e- d9 Q! y- k5 ]" t/ s-- Error is (wrong Parity) or (Stop='0') or Overflow
$ n0 M6 l8 w# ]7 X  r: \* [( [Scan_Err <= (not Parity) or (not PS2_Datr) or Scan_DAVi;
7 a8 e9 C& e% }0 G7 X( l; c" y. W2 uScan_Davi <= '1';
' T0 ]! O9 o. k1 z! D+ u( H/ TScan_Code <= S_Reg(7 downto 0);* R/ N- w' r7 N
State <= Idle;! R" i6 u7 @3 d% |
end if;
, h3 m& u5 b5 V7 X% Relsif Fall_Clk='1' then
2 f/ X( d( o' G& {4 E+ ^# zBit_Cnt <= Bit_Cnt + 1;, t: m7 r1 N5 }
S_Reg <= PS2_Datr & S_Reg (S_Reg'high downto 1); -- Shift right
- u* r9 `* B" L/ _/ G- Y9 dParity <= Parity xor PS2_Datr;
* J( I2 n! h+ G* n7 }end if;
4 R/ N& e5 c# A$ d0 V5 Nwhen others => -- never reached5 K9 @/ Q' ]5 r# ?% \) \
State <= Idle;3 G/ r+ H/ l/ [5 A9 R
end case;5 l) X; f% g& c. J3 u6 j
end if;9 m1 q/ q3 V$ X% u
end process;
0 K* {! Z. E4 ~end ALSE_RTL;
作者: ghoustchieh    時間: 2008-1-17 05:07 PM
補充:2 y  G1 t+ w1 z# }8 q& ^* U6 V; S

3 _4 Q! E% S/ R; d+ z  P我系統CLK是直接提供CPLD 16MHz振盪,Keyboard為15KHz,是否因為頻率關係,而無法啟動呢?
# B+ B+ m2 F4 ?0 j- i$ e; A6 }
0 a! A% k9 k7 |- ~# I8 Z! TTHX....
作者: kolong    時間: 2008-1-17 08:24 PM
你確定你找到的code會動......??, b( U+ [' @4 _9 ]: `3 X
你把內部的資料拉出來量量看吧
作者: A9107114    時間: 2008-1-18 01:27 AM
標題: 來逛逛
在正式燒到CPLD之前一定要先用軟體跑過波形,
* z3 q  l' U- v7 O# S' l6 w: V" H最好對protocol有一點聊解,6 o0 z6 {& Z7 H# r7 b1 O
這樣你就可以預先在軟體模擬波形是不是跟protocol一樣  Z# \8 ~) x2 n- X
等到正確了,再將程式燒錄進CPLD才有意義
作者: addn    時間: 2008-1-18 10:07 AM
您好" P- U3 F9 U8 W8 V0 m0 Y; U
建議先將ps/2 protocol弄懂
* U% t8 |3 W0 a1 M然後再找ps/2鍵盤IC的Data sheet研究一下
) v" {9 e0 f4 c2 \! [0 L* Z! r! J  }! c再來就是自己從頭開始寫4 x- s; ~5 w6 H. ?/ H$ H" N
這樣會比拿別人的code來改,還快




歡迎光臨 Chip123 科技應用創新平台 (http://chip123.com/) Powered by Discuz! X3.2