bleachworthy
Posts: 14
Joined: Sun Jun 22, 2014 12:40 am

Halp! This project is way over my head!

Sun Jun 22, 2014 1:07 am

Hello, when I bought my pi I also ordered a keyboard. Let me correct that, I hastily ordered the cheapest USB keyboard/mouse combo I could see. It got here, it was not a combo, and it was not USB! I ended up with a PS/2 Logitech K100 classic keyboard, which is not on the list of verified keyboards. Not wanting to wait for a return, I chopped off the ps/2 connector and tried for the serial over USB method by simply changing the plug to a male USB. When I plugged it into a windows computer, the lights on the keyboard would flash on and off, and the numlock and capslock lights worked, but windows will not recognize it. same thing happened when I plugged it into the raspi. I have found that this keyboard is not compatible with serial over USB, and have started trying to find a way to get this keyboard working on the Pi. I found this, pi2ps2, I have built the hardware properly, attached and tested it. the light comes on, and the data is below 3.3v when GPIO pin 10 is disconnected. I have successfully turned off the serial terminal and made uart available, then rebooted, then compiled the ps2test.c as SU, then ran it as SU. The test has you hold down the ; key until it exits (presumably it's brute forcing something). I have left this key pressed for like 2 hours in one sitting, and there have been many re-tries trying to see if there is a problem. I know it's easier to just go buy a USB keyboard, but I simply cannot afford one in the next couple months, and it makes it very difficult to use the raspberry pi. I have synergy set up on the latest raspbian, which lets me use my laptop mouse and keyboard on the pi, but it has to be in X, which means unless I'm in X, I have no way to type anything. I really appreciate any help that anyone can offer!

User avatar
joan
Posts: 14196
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Halp! This project is way over my head!

Sun Jun 22, 2014 7:33 am

You could use putty (Windows) or ssh (Macs/Linux) to log into the Pi over a network. Of course this assumes you have a PC and the Pi is networked.

johndough
Posts: 254
Joined: Sun Jan 13, 2013 2:00 pm

Re: Halp! This project is way over my head!

Sun Jun 22, 2014 9:29 am

Hi

The pinouts for keyboard and mouse are

Pin Keyboard Dir Description
1 DATA Key Data
2 n/c or DATA2 for dual PS2 - Not connected
3 GND Gnd
4 VCC Power , +5 VDC
5 CLK Clock
6 n/c or CLK2 for dual PS2 - Not connected

Pin Mouse Dir Description
1 DATA Key Data
2 n/c - Not connected
3 GND Gnd
4 VCC Power , +5 VDC
5 CLK Clock
6 n/c - Not connected

and for USB

Pin USB Cable color Description
1 VCC Red +5 VDC
2 D- White Data -
3 D+ Green Data +
4 GND Black Ground


So with 6 pins per PS2, removing the N/C, leaving 4 to match the USB, it not so obvious what connects to where.

Pin USB Cable color Description Keyboard
1 VCC Red +5 VDC Pin 4
2 D- White Data -
3 D+ Green Data + Pin 1
4 GND Black Ground Pin 3

The Data minus may need to be Pin 3 also.



I did find this.....

USB to PS/2 mouse or keyboard adapter pinout

Connecting USB-mouse to PS/2 connector of computer. It works only with device which supports both interfaces (USB & PS/2).
USB Pin
Name USB Pin
Number Direction Usb Color PS/2 mouse controller Pin
Number PS/2 mouse controller Pin
Name Description PS/2 Color
VDC +5V 1 <-- Red 4 VDC +5V Red
Data- 2 -?- White 1 Data White
Data+ 3 -?- Green 5 Clock Brown
Ground 4 -?- Black 3 Ground Black

Very nice to have one more empty USB connector :).
It's good practice to pull up data/clock lines with some resistors of 10k or so. Especially when connecting to newer motherboards. In some cases signal has levels between 1.5 and 2.5 volts, and mouse don't work. Pulling up helps in that situations and doesn't harm if unnecessary.

bleachworthy
Posts: 14
Joined: Sun Jun 22, 2014 12:40 am

Re: Halp! This project is way over my head!

Sun Jun 22, 2014 9:37 am

Thanks for your reply. I'm aiming to not need another computer to operate the raspberry pi. I have managed to get the keyboard to talk to the pi, it has passed the test "ps2test.c" in the link provided above, I am now trying to compile the main program "pi2ps2.c" also from the link above. I am now receiving an error stating "linux/module.h" does not exist, I am using Geany. I have read in a few places that I may need something called "kernel headers", I am just beginning c, I don't really know what I am doing. I have also read that I can use a makefile to get around the error, but yet again I know too little, and can't even make a working makefile. I've made it past the hardware part, now i just need this compiler to shut up and compile!

Here's the source of the main program that I keep getting errors with. again, I'm using Geany to compile

Code: Select all

/*   pi2ps2.c -- Raspberry Pi PS/2 keyboard device driver using the UART.
 *
 *   Copyright 2014 Stephen Burke     7w0n0d3@gmail.com
 *
 *   This file is subject to the terms and conditions of the GNU General Public
 *   License. See the file COPYING in the Linux kernel source for more details.
 *
 *   The following resources helped me greatly:
 *
 *   The Linux kernel module programming howto
 *   Copyright (C) 2001 Jay Salzman
 *
 *   The Linux USB input subsystem Part 1
 *   Copyright (C) 2007 Brad Hards
 *
 *   PS/2 keyboard interfacing
 *   Copyright (C) 1998-2013 Adam Chapweske
 *
 *   kbd FAQ
 *   Copyright (C) 2009 Andries Brouwer
 *
 *   Keyboard data and clock lines must be pulled up to +5V with 4.7K resistors
 *   Data line must be limited to 3.0V.
 *   Connecting a blue or white LED between the data line and ground is a simple way to accomplish this.
 *   Data line is then connected to P1 pin 10 (RX) on the Raspberry Pi.
 *   Clock line is left unconnected.
 *   The keyboard also needs +5v (about 125 mA) and ground.
 *
 */

//remember to remove all ttyAMA0 references in /boot/cmdline.txt and /etc/inittab
//use companion userspace program ps2test to determine module parameters
//and use them like this: insmod ps2.ko integer=15 fractional=38
//default parameters should be correct for an IBM Model M keyboard.

//You don't need to do anything special to compile this module.

#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/input.h>
#include <linux/io.h>

#define IO_BASE         0x20000000

//gpio
#define GPFSEL1         0x200004/4

//uart
#define DR              0x201000/4
#define CR              0x201030/4
#define IBRD            0x201024/4
#define FBRD            0x201028/4
#define LCRH            0x20102c/4
#define IMSC            0x201038/4

int divider = 15;
int fractional = 38;
static unsigned keyup = 0;
static unsigned escape = 0;
static unsigned pause = 0;
static unsigned *(uart);
volatile unsigned *(io_base);
static struct input_dev *ps2;
static unsigned char translate[256] = {

/* Raw SET 2 scancode table */

/* 00 */  KEY_RESERVED, KEY_F9,        KEY_RESERVED,  KEY_F5,        KEY_F3,        KEY_F1,       KEY_F2,        KEY_F12,
/* 08 */  KEY_ESC,      KEY_F10,       KEY_F8,        KEY_F6,        KEY_F4,        KEY_TAB,      KEY_GRAVE,     KEY_RESERVED,
/* 10 */  KEY_RESERVED, KEY_LEFTALT,   KEY_LEFTSHIFT, KEY_RESERVED,  KEY_LEFTCTRL,  KEY_Q,        KEY_1,         KEY_RESERVED,
/* 18 */  KEY_RESERVED, KEY_RESERVED,  KEY_Z,         KEY_S,         KEY_A,         KEY_W,        KEY_2,         KEY_RESERVED, 
/* 20 */  KEY_RESERVED, KEY_C,         KEY_X,         KEY_D,         KEY_E,         KEY_4,        KEY_3,         KEY_RESERVED,
/* 28 */  KEY_RESERVED, KEY_SPACE,     KEY_V,         KEY_F,         KEY_T,         KEY_R,        KEY_5,         KEY_RESERVED,
/* 30 */  KEY_RESERVED, KEY_N,         KEY_B,         KEY_H,         KEY_G,         KEY_Y,        KEY_6,         KEY_RESERVED,
/* 38 */  KEY_RESERVED, KEY_RIGHTALT,  KEY_M,         KEY_J,         KEY_U,         KEY_7,        KEY_8,         KEY_RESERVED,
/* 40 */  KEY_RESERVED, KEY_COMMA,     KEY_K,         KEY_I,         KEY_O,         KEY_0,        KEY_9,         KEY_RESERVED,
/* 48 */  KEY_RESERVED, KEY_DOT,       KEY_SLASH,     KEY_L,         KEY_SEMICOLON, KEY_P,        KEY_MINUS,     KEY_RESERVED,
/* 50 */  KEY_RESERVED, KEY_RESERVED,  KEY_APOSTROPHE,KEY_RESERVED,  KEY_LEFTBRACE, KEY_EQUAL,    KEY_RESERVED,  KEY_RESERVED,
/* 58 */  KEY_CAPSLOCK, KEY_RIGHTSHIFT,KEY_ENTER,     KEY_RIGHTBRACE,KEY_RESERVED,  KEY_BACKSLASH,KEY_RESERVED,  KEY_RESERVED,
/* 60 */  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED, KEY_BACKSPACE, KEY_RESERVED,
/* 68 */  KEY_RESERVED, KEY_KP1,       KEY_RESERVED,  KEY_KP4,       KEY_KP7,       KEY_RESERVED, KEY_HOME,      KEY_RESERVED,
/* 70 */  KEY_KP0,      KEY_KPDOT,     KEY_KP2,       KEY_KP5,       KEY_KP6,       KEY_KP8,      KEY_ESC,       KEY_NUMLOCK,
/* 78 */  KEY_F11,      KEY_KPPLUS,    KEY_KP3,       KEY_KPMINUS,   KEY_KPASTERISK,KEY_KP9,      KEY_SCROLLLOCK,KEY_RESERVED,
/* 80 */  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,  KEY_F7,        KEY_SYSRQ,     KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,
/* 88 */  KEY_PAUSE,    KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,
/* 90 */  KEY_RESERVED, KEY_RIGHTALT,  KEY_RESERVED,  KEY_RESERVED,  KEY_RIGHTCTRL, KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,
/* 98 */  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,
/* a0 */  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,
/* a8 */  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,
/* b0 */  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,
/* b8 */  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,
/* c0 */  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,
/* c8 */  KEY_RESERVED, KEY_RESERVED,  KEY_KPSLASH,   KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,
/* d0 */  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,
/* d8 */  KEY_RESERVED, KEY_RESERVED,  KEY_KPENTER,   KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,
/* e0 */  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED,  KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,
/* e8 */  KEY_RESERVED, KEY_END,       KEY_RESERVED,  KEY_LEFT,      KEY_HOME,      KEY_RESERVED, KEY_RESERVED,  KEY_RESERVED,
/* f0 */  KEY_INSERT,   KEY_DELETE,    KEY_DOWN,      KEY_RESERVED,  KEY_RIGHT,     KEY_UP,       KEY_RESERVED,  KEY_RESERVED,
/* f8 */  KEY_RESERVED, KEY_RESERVED,  KEY_PAGEDOWN,  KEY_RESERVED,  KEY_PRINT,     KEY_PAGEUP,   KEY_RESERVED,  KEY_RESERVED

}
;

module_param(divider,int,0);
module_param(fractional,int,0);

irq_handler_t irq_handler(int irq, void *dev_id, struct pt_regs *regs)
{
        unsigned key = *(uart);
                
        if ((key & (1<<8)) != 0) printk(KERN_INFO "pi2ps2 framing error\n");

        if ((key & (1<<9)) != 0) printk(KERN_INFO "pi2ps2 parity error\n");

        if ((key & (1<<10)) != 0) printk(KERN_INFO "pi2ps2 break error\n");

        if ((key & (1<<11)) != 0) printk(KERN_INFO "pi2ps2 overrun error\n");

        key = key & 0xff;

        if (key == 0xf0) {
            keyup = 1;
            return 0;
        }
        
        if (key == 0xe0) {
            escape = 1;
            return 0;
        }
        
        if (key == 0xe1) {
            pause = 2;
            return 0;
        }
        
        if (pause == 2) {
            pause = 1;
            return 0;
        }

        if (pause == 1) {
            key = 0x88;
            pause = 0;
        }

        if (escape == 1) {
            key |= 0x80;
            escape = 0;
        }
        key = translate[key];

        if (keyup == 1) {
            input_report_key(ps2,key,0);
            keyup = 0;
        }

        else input_report_key(ps2,key,1);

        input_sync(ps2);

        return 0;
}

int init_module(void)
{
        int i, retval;
        unsigned key;
        
        io_base = ioremap(IO_BASE,0x400000);
        //set pin 10 as uart rxd
        *(io_base+GPFSEL1) &= ~(7<<15);
        *(io_base+GPFSEL1) |= (4<<15);
        //set uart to receive 8 bit, 1 stop, odd parity
        *(io_base+CR) = 0;
        *(io_base+LCRH) = ((3<<5)|(1<<1));
        //set baud rate to command line parameters
        *(io_base+IBRD) = divider;
        *(io_base+FBRD) = fractional;
        //flush buffer
        key=*(io_base+DR);
        //interrupts on
        *(io_base+IMSC) = (1<<4);
        //uart on
        *(io_base+CR) = (1<<9);
        *(io_base+CR) |= 1;
        iounmap(io_base);
        uart = ioremap(IO_BASE+(DR*4),1);
        ps2=input_allocate_device();

        ps2->name = "pi2ps2";
        ps2->phys = "ps2/input0";
        ps2->id.bustype = BUS_HOST;
        ps2->id.vendor = 0x0001;
        ps2->id.product = 0x0001;
        ps2->id.version = 0x0100;
        ps2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
        ps2->keycode = translate;
        ps2->keycodesize = sizeof(unsigned char);
        ps2->keycodemax = ARRAY_SIZE(translate);
        for (i = 1; i < 0x256; i++) set_bit(i,ps2->keybit);
        retval = input_register_device(ps2);
//      free_irq(83,NULL);
        retval = request_irq(83,(irq_handler_t)irq_handler,IRQF_SHARED,"pi2ps2",(void *)irq_handler);
        printk(KERN_INFO "pi2ps2 device installed divider %i fractional %i\n", divider, fractional);

        //numlock on, should query state first
        input_report_key(ps2,KEY_NUMLOCK,1);
        input_sync(ps2);

        return 0;
}

void cleanup_module(void)
{
        io_base = ioremap(IO_BASE,0x400000);
        //set pin 10 as input
        *(io_base+GPFSEL1) &= ~(7<<15);
        //interrupts off
        *(io_base+IMSC) = 0;
        //uart on
        *(io_base+CR) = 0;
        iounmap(io_base);
        iounmap(uart);
        input_unregister_device(ps2);
        free_irq(83,(void *)irq_handler);
        printk(KERN_INFO "pi2ps2 device removed\n");
        return;
}
//handle uart interrupt, translate to proper keycode

MODULE_LICENSE("GPL");

User avatar
rpdom
Posts: 15019
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Halp! This project is way over my head!

Sun Jun 22, 2014 10:07 am

I have used an active PS2 to USB converter with my Raspberry Pi at times. It works perfectly with old PS2 keyboards and mice that don't support the USB over PS2 protocol.

I got mine from ebay for under a couple of quid each. Go for one that has two PS2 sockets - one for mouse and one for keyboard, as those need active components, as opposed to the simple adaptors which rely on the device supporting USB via the PS2 plug.

bleachworthy
Posts: 14
Joined: Sun Jun 22, 2014 12:40 am

Re: Halp! This project is way over my head!

Sun Jun 22, 2014 11:28 am

:evil: Why won't this thing compile?! :evil:

Return to “C/C++”