-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPS 2 Port
More file actions
20 lines (16 loc) · 2.08 KB
/
PS 2 Port
File metadata and controls
20 lines (16 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
NIOS II For Quartus® Prime
9.6 PS/2 #include "address_map_nios2.h"
/* function prototypes */ void HEX_PS2(char, char, char);
/* This program demonstrates use of the PS/2 port by displaying the last three * bytes of data received from the PS/2 port on the HEX displays. ******************************************************************************/ int main(void) { /* Declare volatile pointers to I/O registers (volatile means that IO load and store instructions will be used to access these pointer locations, instead of regular memory loads and stores) */ volatile int * PS2_ptr = (int *)PS2_BASE;
int PS2_data, RVALID; char byte1 = 0, byte2 = 0, byte3 = 0;
// PS/2 mouse needs to be reset (must be already plugged in) *(PS2_ptr) = 0xFF; // reset
while (1) { PS2_data = *(PS2_ptr); // read the Data register in the PS/2 port RVALID = PS2_data & 0x8000; // extract the RVALID field if (RVALID) { /* shift the next data byte into the display */ byte1 = byte2; byte2 = byte3; byte3 = PS2_data & 0xFF; HEX_PS2(byte1, byte2, byte3);
if ((byte2 == (char)0xAA) && (byte3 == (char)0x00)) // mouse inserted; initialize sending of data *(PS2_ptr) = 0xF4;
}
}
}
/* Subroutine to show a string of HEX data on the HEX displays */ void HEX_PS2(char b1, char b2, char b3) { volatile int * HEX3_HEX0_ptr = (int *)HEX3_HEX0_BASE; volatile int * HEX5_HEX4_ptr = (int *)HEX5_HEX4_BASE;
/* SEVEN_SEGMENT_DECODE_TABLE gives the on/off settings for all segments in * a single 7-seg display in the DE1-SoC Computer, for the hex digits 0 - F */ unsigned char seven_seg_decode_table[] = {
0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x67, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71}; unsigned char hex_segs[] = {0, 0, 0, 0, 0, 0, 0, 0}; unsigned int shift_buffer, nibble; unsigned char code; int i;
shift_buffer = (b1 << 16) | (b2 << 8) | b3; for (i = 0; i < 6; ++i) { nibble = shift_buffer & 0x0000000F; // character is in rightmost nibble code = seven_seg_decode_table[nibble]; hex_segs[i] = code; shift_buffer = shift_buffer >> 4; } /* drive the hex displays */ *(HEX3_HEX0_ptr) = *(int *)(hex_segs); *(HEX5_HEX4_ptr) = *(int *)(hex_segs + 4);
}