Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12600 Discussions

PS/2 keyboard not able to detect.

Altera_Forum
Honored Contributor II
1,884 Views

Hi, 

 

i am trying to interface my ps/2 keyboard in nios 2 but the device is not getting detected. below is my code. 

when i am printing ps2->device_type, it appears blank. also see the output below the program. 

# include "system.h"# include "stdio.h"# include "altera_up_avalon_ps2.h"# include "altera_up_ps2_keyboard.h"# include "io.h" 

 

 

/*Function that read keyboard input and show on monitor*/ 

void read_keyboard(alt_up_ps2_dev *ps2_kb) 

KB_CODE_TYPE decode_mode; 

alt_u8 buffer; 

char ascii; 

char *inputStr; 

while(1) 

if (decode_scancode(ps2_kb, &decode_mode, &buffer, &ascii)==0) //If get a make code from keyboard 

if (decode_mode == KB_ASCII_MAKE_CODE ); 

translate_make_code(decode_mode, buffer, inputStr); //translate the code like "SPCAE", "BACKSPACE" 

 

 

int main() 

 

 

alt_up_ps2_dev *ps2;# ifdef PS2_0_BASE 

ps2 = alt_up_ps2_open_dev("/dev/ps2_0"); //open PS/2 device# endif 

 

 

alt_printf("Detected device: %d\n",ps2); 

alt_printf("Detected device: %d\n",ps2->device_type); 

 

 

alt_up_ps2_init(ps2); 

alt_up_ps2_clear_fifo(ps2); 

 

 

if (ps2->device_type == PS2_KEYBOARD) //Check if the device connected to PS/2 is keyboard // doesn't satisfy this condition. 

 

 

read_keyboard(ps2); 

else 

printf("Error...\n"); 

return -1; 

 

output : 

 

Detected device: 

Detected device: 

Error... 

 

Please reply.......
0 Kudos
4 Replies
Altera_Forum
Honored Contributor II
651 Views

Please can anyone help me.

0 Kudos
Altera_Forum
Honored Contributor II
651 Views

Hello Akshat999, 

 

I might be able to help you: 

First: You do not need to call alt_up_ps2_init, the system code does this for you during initialisation startup: 

ct0 calls alt_main, alt_main calls alt_sys_init, alt_sys_init calls alt_up_ps2_init. 

after that you end up in main() 

 

 

#include <stdio.h> # include <altera_up_ps2_keyboard.h> int main() { alt_up_ps2_dev *crt_kbd_ps2 = NULL; crt_kbd_ps2 = alt_up_ps2_open_dev(PS2_0_NAME); if (crt_kbd_ps2->device_type==PS2_UNKNOWN) { printf("Keyboard init bad !\n"); } else { alt_up_ps2_clear_fifo(crt_kbd_ps2); printf("Keyboard init ok !\n"); while (1) { KB_CODE_TYPE decode_mode=1; alt_u8 data; char ascii; if (decode_scancode(crt_kbd_ps2,&decode_mode,&data,&ascii)==0) { printf("%c",ascii); } } } return 0; }  

 

2) it could be that the name of the module is not correct, you can see this by inspecting crt_kbd_ps2 

if it remains 00 after alt_up_ps2_open_dev, you could have a naming problem. 

 

 

3) I tried to connect to a keyboard on my DE0-CV, it did not work: the Nios II-f processor was too fast. Timouts are generated by a counter that counts in clock cycles, if processor is too fast, timout is not correct. 

 

Solution: had to modify the "timeout" parameter in the altara_up_avalon_ps2.h  

 

#define ALTERA_UP_AVALON_PS2_INSTANCE(name, device) static alt_up_ps2_dev device = { { ALT_LLIST_ENTRY, name##_NAME, NULL , /* open */ NULL , /* close */ alt_up_ps2_read_fd , /* read */ alt_up_ps2_write_fd , /* write */ NULL , /* lseek */ NULL , /* fstat */ NULL , /* ioctl */ }, name##_BASE, name##_IRQ, 0x002ffff, PS2_UNKNOWN } 

 

(used 2 x the actual value, it worked, do not know the exact value) 

 

4) You could see if there is communcation with the keyboard adding the following code to your Qsys instatiation: 

//======================================================= // REG/WIRE declarations //======================================================= reg PS2_CLK_PREV; reg PS2_DAT_PREV; integer hex7seg; 

 

and 

 

assign sd_card_b_SD_dat3=1'bz; assign LEDR=PS2_CLK; assign LEDR=~PS2_CLK; assign LEDR=PS2_DAT; assign LEDR=~PS2_DAT; assign LEDR=PS2_CLK2; assign LEDR=~PS2_CLK2; assign LEDR=PS2_DAT2; assign LEDR=~PS2_DAT2; always @(posedge CLOCK_50) begin if (PS2_CLK!=PS2_CLK_PREV) begin PS2_CLK_PREV<=PS2_CLK; hex7seg<=hex7seg+1; end; if (PS2_DAT_PREV!=PS2_DAT) begin hex7seg<=hex7seg+1; PS2_DAT_PREV<=PS2_DAT; end; end // send data to 7 seg display: hexto7segment s0(hex7seg,HEX0); hexto7segment s1(hex7seg,HEX1); hexto7segment s2(hex7seg,HEX2); hexto7segment s3(hex7seg,HEX3); hexto7segment s4(hex7seg,HEX4); hexto7segment s5(hex7seg,HEX5);  

 

The verilog code for hexto7segment module used above is 

 

module hexto7segment(x,z); input wire x; output reg z; always @(*) case (x) 4'b0000 : z = 7'b1000000; //Hexadecimal 0 4'b0001 : z = 7'b1111001; //Hexadecimal 1 4'b0010 : z = 7'b0100100; //Hexadecimal 2 4'b0011 : z = 7'b0110000; //Hexadecimal 3 4'b0100 : z = 7'b0011001; //Hexadecimal 4 4'b0101 : z = 7'b0010010; //Hexadecimal 5 4'b0110 : z = 7'b0000010; //Hexadecimal 6 4'b0111 : z = 7'b1111000; //Hexadecimal 7 4'b1000 : z = 7'b0000000; //Hexadecimal 8 4'b1001 : z = 7'b0010000; //Hexadecimal 9 4'b1010 : z = 7'b0001000; //Hexadecimal A 4'b1011 : z = 7'b0000011; //Hexadecimal B 4'b1100 : z = 7'b1000110; //Hexadecimal C 4'b1101 : z = 7'b0100001; //Hexadecimal D 4'b1110 : z = 7'b0000110; //Hexadecimal E 4'b1111 : z = 7'b0001110; //Hexadecimal F // low logic = burning led endcase endmodule 

 

With this code you can see the activity on your PS2 bus and you can see if the keyboard is allive or not. 

 

Best Regards, 

Johi.
0 Kudos
Altera_Forum
Honored Contributor II
651 Views

Hello, 

And to continue, I just found that in the pin assignment editor, the naming convention of the PS2 pins are different depending on the terrasic application builder version. 

So if your application does not work, go to pin planner and look for symbols that are not assigned. PS2_KBCLK, PS2_KBDAT is sometimes PS2_CLK and PS2_DAT. 

So if you combine .v examples with a project that was generated automatically by the terrasic tools, you could get in trouble. 

Best Regards, 

Johi.
0 Kudos
Altera_Forum
Honored Contributor II
651 Views

 

--- Quote Start ---  

May be some one would help you 

--- Quote End ---  

 

 

Hello, 

 

Problem was solved before already the post was for other forum members that could benefit from this info. 

 

Best regards, 

Johi
0 Kudos
Reply