Mouse / Keyboard interface

Assembler, C/C++ related topics
simon
Site Admin
Posts: 744
Joined: Thu Sep 13, 2012 9:35 am
Location: Luedenscheid, Germany
Contact:

Mouse / Keyboard interface

Postby simon » Tue Sep 03, 2013 1:18 pm

Hi,

the last days I worked on a new ATmega8 firmware which supports the second PS/2 port for mouse handling. I can receive and send data from and to the mouse. Sending has to be implemented as the mouse doesn't report it's state after reset.
Now I am working on a protocol to send both - mouse- and keyboard-data - via one serial link. My idea is the ATmega8 fills two buffers within its interrupt routines: a kbd and a mouse buffer. The main function contains a simple loop only. This loop sends data as packets only and adds a magic byte for mouse data. E.g. every byte is interpreted as Ascii except 0x82 for example. If the kbd/mouse driver of the 68k recognizes this 0x82 it knows that the following three bytes are a mouse-data-packet.
In theory this should work but what about the details. What happens if the m68k wants to read a key but for a while the ATmega8 is sending mouse packets? Should it just drop the mouse packets? Should the packets be buffered? If the packets are buffered, the mouse routine has to read bytes from the buffer instead of accessing the hardware direct.
I came around that I would need three functions. The first is the only function which communicates with the ATmega. This function dispatches received data for two buffers. These buffers are for the two other functions. One to read from the keyboard and the other to read from the mouse. The last question is when to call function 1? I think it would be nice to call it independently. A periodical interrupt could do this task. Or I could call it from both routines. Thus I wouldn't waste a interrupt... What do you think?

Simon

simon
Site Admin
Posts: 744
Joined: Thu Sep 13, 2012 9:35 am
Location: Luedenscheid, Germany
Contact:

Re: Mouse / Keyboard interface

Postby simon » Wed Sep 04, 2013 12:55 pm

There is still a bug in the mouse communication. Within the ATmega8 there is an interrupt routine which fills an rx-buffer just like the keyboard routine. In the main function there is an 3 byte long char array. When all three bytes are received all three are sent in a row to the 68k. But somehow some bytes seem to get lost on the way to the CPU. Can you program AVRs? You could check my code or help with this interface...

Simon

Chris
Developer
Posts: 286
Joined: Thu Apr 04, 2013 7:53 pm
Location: England, UK

Re: Mouse / Keyboard interface

Postby Chris » Wed Sep 04, 2013 3:34 pm

I have done some AVR programming in my time so I could take a look. First question is what type of mouse are you using and do you initialise it? setting MS Intellimouse mode make it send 4 bytes not 3 as they have Z data for the scroll button. This site has some great info http://www.computer-engineering.org/ps2mouse/

Best Regards

Chris

simon
Site Admin
Posts: 744
Joined: Thu Sep 13, 2012 9:35 am
Location: Luedenscheid, Germany
Contact:

Re: Mouse / Keyboard interface

Postby simon » Wed Sep 04, 2013 4:49 pm

Hi Chris,

yes, the mouse is initalised. I send 0xff and 0xf4 to reset and enable stream mode. As the bytes are getting sent I asume the problem is 68k<->AVR communication and not mouse<->AVR. I can't test it with my PC as the other end but maybe the buffer of the MC68681 gets overrun.
Another problem is sending keyboard and mouse events at the same time. It seems both ends get out of sync.

Thank you Chris! My actual code is attached.

Simon
Attachments
keyb_mouse-0.0.1.tgz
Buggy AVR firmware.
(7.9 KiB) Downloaded 686 times

simon
Site Admin
Posts: 744
Joined: Thu Sep 13, 2012 9:35 am
Location: Luedenscheid, Germany
Contact:

Re: Mouse / Keyboard interface

Postby simon » Thu Sep 05, 2013 7:00 am

I didn't test much yesterday. But I did a test with a simple for-loop. The AVR continues sending bytes from 0x00 to 0xff. The 68k seem to get only garbage. When I get home I will check the buffer overrun flag of the MC68681.

Simon

Chris
Developer
Posts: 286
Joined: Thu Apr 04, 2013 7:53 pm
Location: England, UK

Re: Mouse / Keyboard interface

Postby Chris » Thu Sep 05, 2013 7:02 pm

Just a quick look but the actual send logic seems ok so I would agree it seems an 'hard' comms problem

Regards

Chris

simon
Site Admin
Posts: 744
Joined: Thu Sep 13, 2012 9:35 am
Location: Luedenscheid, Germany
Contact:

Re: Mouse / Keyboard interface

Postby simon » Fri Sep 06, 2013 10:59 am

Thank you Chris! Indeed the buffer got overrun but I didn't have the time to program a workaround yet.

Simon

simon
Site Admin
Posts: 744
Joined: Thu Sep 13, 2012 9:35 am
Location: Luedenscheid, Germany
Contact:

Re: Mouse / Keyboard interface

Postby simon » Sun Sep 08, 2013 3:26 pm

Hello,

the mouse interface is working now. I reduced sample rate down to 10samples/second and wrote data checks. Invalid mouse data packets are just dropped. I tested three different mice: One optical Microsoft mouse (works, but doesn't report middle mouse button if not in intellimouse mode), an old Logitech ball mouse (works flawlessly) and a noname optical mini-mouse (seem not to reduce sample rate. Kind of working but with high latency).
I wrote a little paint program to play with the new feature. Well, I should think about increasing sample rate again or better paint algorhythm: if you move the mouse fast you'll get dotted lines... :D

Simon

paint1.jpg
A first paint program for the mouse.
paint1.jpg (186.95 KiB) Viewed 14361 times

simon
Site Admin
Posts: 744
Joined: Thu Sep 13, 2012 9:35 am
Location: Luedenscheid, Germany
Contact:

Re: Mouse / Keyboard interface

Postby simon » Mon Sep 09, 2013 10:05 am

I increased the sampling rate up to 40samples/second. Now I have a smooth and low latency mouse reaction. In addition I added a timeout in the mouse init function of the Atmega8 firmware. This prevents hanging in a loop forever when there is no mouse connected.

Simon

Chris
Developer
Posts: 286
Joined: Thu Apr 04, 2013 7:53 pm
Location: England, UK

Re: Mouse / Keyboard interface

Postby Chris » Mon Sep 09, 2013 10:11 am

simon wrote:In addition I added a timeout in the mouse init function of the Atmega8 firmware. This prevents hanging in a loop forever when there is no mouse connected.

Simon


Good call, I had noticed there was no handling of the retun codes from the mouse just reading byte and move on. I had assumed it was just demo code so didn't mention it :)

I have read a few articles where people have noted some mice don't appear to be able to have their sampling rates changed, as long as it works with Microsoft & Logitec they are fairly standard. Something has to give if the mouse only costs $3 and I guess its compatibility.

Regards

Chris


cron