Simple PS/2 Keyboard

J

John

Guest
Hello,

Does anyone know of freely available Verilog code for a simple PS/2
keyboard controller. I'm looking to add some really basic keyboard
support to a project that I'm working on and if someone has already
made some sample code available it could speed up my work quite a bit.
I'm looking for something really simple. I just need to be able to
read the scancodes (no mouse support or support for sending data to
the keyboard). Please let me know.

Thanks
 
TheCppMaster@aol.com (John) wrote in message news:<a25b4de5.0405231003.6570d018@posting.google.com>...
Take a look at opencores.org. I've used the ps/2 core, and it was
really easy to integrate into my design. This particular design
supports bidirectional communication with a keyboard, but if you only
want to receive scan codes you can just disable the other features. I
only wanted the scan codes, and it works fine. It doesn't take up much
space either.

Thanks for the tip. For some reason I can't get this module hooked
into my project. Maybe I'm misunderstanding where I'm supposed to
interface with it. All I want is to be able to put this into my
project, hook it up to the main clock, ps2_clock, and ps2_data lines,
and then have the ability know which keys are being held down at any
given time. If you (or anyone else who also has experience using this
module) could explain how you accomplished this it would be great
because the project appears to have no documentation (that I could
find atleast).

Thanks again for your time.
John,

Here's a snippet of my code where I instantiated the ps/2 module:

// *** Local Wire Declarations ***
wire [7:0] ps2_scan_code;
wire [7:0] ps2_ascii;
wire [7:0] ps2_status;
wire [7:0] rs232_rx_character = 8'd0; // Don't transmit any data to kb
wire ps2_key_data_ready;
wire ps2_key_released;
wire rs232_rx_data_ready = 1'b0; // Don't transmit any data to kb.

// Instantiate the PS/2 Keyboard Interface Module:
ps2_keyboard_interface #(2640, // number of clks for 60usec.
12, // number of bits needed for 60usec. timer
63, // number of clks for debounce
6, // number of bits needed for debounce timer
1 // Trap the shift keys, no event generated
)
ps2_block (.rx_extended(ps2_status[5]),
.rx_released(ps2_key_released),
.rx_shift_key_on(ps2_status[7]),
.rx_scan_code(ps2_scan_code), // Scan Code from kb.
.rx_ascii(ps2_ascii), // Decoded ASCII Code
.rx_data_ready(ps2_key_data_ready),
.tx_write_ack_o(),
.tx_error_no_keyboard_ack(ps2_status[4]),

.ps2_clk(ps2_clk),
.ps2_data(ps2_data),

.clk(clk_cpu_50MHz),
.reset(~n_rst),
.rx_read(ps2_key_data_ready),
.tx_data({rs232_rx_character[5],//Set upper nibble E, F or 0
rs232_rx_character[5],
rs232_rx_character[5],
rs232_rx_character[4],
rs232_rx_character[3:0]}),
.tx_write(rs232_rx_data_ready)
);

Pick off the rx_ascii and rx_scan_code registers and use them as needed.

Hope this helps,

Jeremy
 
Hello,

It seems that we were looking at two different things (although they
are probably just different versions of the same thing). I was
confused at first when your instantiation didn't match up with the
module I was looking at. I Googled the module name and the only
legitimate result gave me the code to the module that I assume you
were referring me too. This looks much simpler than the one I was
looking at earlier (it made up of was about 8 files).

However I still can't get it to work. I really can't think of what
could be causing the problem. Is there any special way in which I need
to process the scan-codes or ascii-codes? This is pretty strange (it
seems as if everything is working but the design just won't read the
codes).

Thanks again (especially for inadvertently pointing me to the right
module!)
 
TheCppMaster@aol.com (John) wrote in message news:<a25b4de5.0405241902.3bce10d@posting.google.com>...
Hello,

It seems that we were looking at two different things (although they
are probably just different versions of the same thing). I was
confused at first when your instantiation didn't match up with the
module I was looking at. I Googled the module name and the only
legitimate result gave me the code to the module that I assume you
were referring me too. This looks much simpler than the one I was
looking at earlier (it made up of was about 8 files).

However I still can't get it to work. I really can't think of what
could be causing the problem. Is there any special way in which I need
to process the scan-codes or ascii-codes? This is pretty strange (it
seems as if everything is working but the design just won't read the
codes).

Thanks again (especially for inadvertently pointing me to the right
module!)
John,

Do you have pull ups on your clock and data lines of the PS/2
interface. If you read in the header of ps2_keyboard.v, there's some
information on the interface to the PS/2 keyboard. You should only
need one file and that's ps2_keyboard.v. I've posted it on my
personal website for you to grab:

http://webpages.charter.net/jeremywebb/ps2_keyboard.v

I'll leave it there for a couple of days before I remove it. When I
did the first spin of my board I forgot to add the pull ups on my
board, but was able to use the Spartan IIE's internal pull ups to fix
the mistake. I also found a neat part from ST Microelectronics. It's
part number is KBMF01SC6. It's an EMI FILTER AND LINE TERMINATION FOR
PS/2 MOUSE OR KEYBOARD PORTS. It's got the pull ups and emi filter all
in one chip.

As for any post processing of the scan codes or ascii codes, you are
not required to do anything. Just interpret them as normal ascii or
scan codes. The ps2_keyboard.v file has a big decoder that translates
a select portion of the keyboard mapping scan codes to ascii codes.

Hope this helps,

Jeremy
 
You'll also notice that I had to customize some of the parameters for
the ps2_keyboard.v module. These are design dependent, and will
change depending on what clock you feed it. The actual clock rate
that I fed the module was 44.4MHz.
Hello again. Sorry for the wait. I've been unable to post for the past
few days. I'm using a 50MHz clock and I had adjusted the timers
accordingly but I think I may have found an error. The module itself
defaults to a clock of a little more than 49 MHz (I think). But for
their 5u sec clock value they use 186 clks. For the same timer you use
63 clks. Why are these values so different for clocks of such close
timing? When I did the calculation myself I came up with a completely
different number altogether (for both 44, 49, and 50MHz). Could this
be a problem? Am I misunderstanding how this clock is calculated? Is
it expected that two independent physical clocks are used? Also, your
code mentions clk_cpu_50MHz. IS this yet another clock or is it just
the name of your 44.4MHz?

As for the pullups. I'm not sure if the board includes them as I am
simply prototyping this design using a readymade board (inexpensive
Digilent Pegasus). This design is nowhere near a stage where a final
custom board could be cost effective. I've contacted the company to
ask if this was put in place (I don't see why a commercial vendor
would omit these but you never know) but I have yet to get a response.

Thanks again for your help,
John
 
John,

See my comments below:

-----Original Message-----
From: John Morgan [mailto:jmorgan@webb.org]
Sent: Sunday, May 30, 2004 3:03 PM
To: jeremy.webb@folsom.com
Subject: PS/2 Interface (Verilog Newsgroup)

Hello,
It seems that the thread about the PS/2 keyboard interface is not showing
up correctly in Google Groups (which the headers state as the method you
were using to read and post). It is only showing the first 7 posts. I'm
not sure what the problem is but I've included below a copy of my latest
post.

You'll also notice that I had to customize some of the parameters for
the ps2_keyboard.v module. These are design dependent, and will
change depending on what clock you feed it. The actual clock rate
that I fed the module was 44.4MHz.

Hello again. Sorry for the wait. I've been unable to post for the past
few days. I'm using a 50MHz clock and I had adjusted the timers
accordingly but I think I may have found an error. The module itself
defaults to a clock of a little more than 49 MHz (I think). But for
their 5u sec clock value they use 186 clks. For the same timer you use
63 clks. Why are these values so different for clocks of such close
timing?
When I used this module, I didn't spend much time calculating the
numbers, I just put some numbers in and it worked. When I calculate
the numbers for the parameters I get 2640 for the 60usec counter and
220 for the 5usec counter. The 5usec counter value that I used produced
about a 1usec debounce, but my ps/2 interface still worked. This effect of
this parameter is design dependent. Like I said, I just did some quick
pencil and paper calculations, and apparently I was off. The 60usec counter is
more important than the 5usec clock if you're only going to be receiving
information from the keyboard as it is used for receiving scan codes. The 5usec
counter is used for transmitting to the keyboard.

When I did the calculation myself I came up with a completely
different number altogether (for both 44, 49, and 50MHz). Could this
be a problem? Am I misunderstanding how this clock is calculated? Is
it expected that two independent physical clocks are used? Also, your
code mentions clk_cpu_50MHz. IS this yet another clock or is it just
the name of your 44.4MHz?
Like I said earlier clk_cpu_50MHz is actually ~44.4 MHz. The off the shelf
cpu board that I used claimed that it had a 50MHz peripheral clock, but
when measured turned out to be ~44.4 MHz. I had all of my code written
before I got my boards, so I didn't bother changing the port name.

As for the pullups. I'm not sure if the board includes them as I am
simply prototyping this design using a readymade board (inexpensive
Digilent Pegasus). This design is nowhere near a stage where a final
custom board could be cost effective. I've contacted the company to
ask if this was put in place (I don't see why a commercial vendor
would omit these but you never know) but I have yet to get a response.
Pull-ups are definitely needed in this interface. If you're using a Xilinx
FPGA then you can place pull ups on the clock and data pins on your
device. If you're using the Xilinx ISE tools this can be done in either
your Verilog code or in your UCF (constraints) file.

Thanks again for your help,
John

It seems that the clock code is a possible hangup. What values would you
use if you had a 50MHz clock? I've tried many different combinations but
currently I'm using this:

ps2_keyboard_interface #(3000, 12, 56, 6, 1)
Here's what I would use:

ps2_keyboard_interface #(3000,12,250,8,1)


Thanks again
Hope this helps,

Jeremy
 

Welcome to EDABoard.com

Sponsor

Back
Top