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.