Jump to content
43oh

PS2 mouse timing debugging


Recommended Posts

I'm running into a problem with interfacing a PS/2 mouse and I think the source of the problem is related to timing errors. My current setup is to have the PS/2 mouse data and clock line connected to a bidirectional MOSFET voltage level converter (http://www.hobbytronics.co.uk/mosfet-voltage-level-converter) into 2 pins on a 48-pin MSP430F5510. As for the code, I'm using Arduino's PS2Mouse library because it is known to work.

 

It's probably bad practice but I wrote a digital I/O class similar to Energia to get the PS/2 library working (http://pastebin.com/Wkumn6v6) I've read Chapweske's write-up on PS/2 interface in general and look at other microcontroller's code so I guess I have fair understanding of what's needed to be done.

 

This is the code I'm working with for the PS/2. It's pretty much the Arduino library code, except it's for use in C. http://pastebin.com/VRZBTiVX. This morning I tried to use Arduino PS/2 library with Energia for MSP430 Launchpad without a crystal and it worked (now it doesn't anymore) so it shows the bidirectional converter works and the problem resides in the timing.

 

The clock on the 5510 is running at ~8MHz and UART running at 38400 baud. Yesterday somehow I managed to get TPwrite() (write to PS/2) to work by modifying the constant in __delay_cycles() to 5 in delay_us(). With 5 instead of 8, the delay_us() was spot on. Today, TPwrite() stops working altogether. 

 

So my questions are:

1. When working with communication protocols, how would you debug? Since timing is critical, pausing and stepping through the code wouldn't work.

2. Is there a way to output the MCLK/SMCLK onto a pin on the MSP430F5510? I'm using Olimex's MSP430-T5510 which uses a 48-pin one and doesn't have P2.2 which normally outputs SMCLK.

 

I have a DPScope SE, which is a cheap USB oscilloscope. It has a few logic analyzer pins but so far I've found it be less than useful.

Link to post
Share on other sites

I'd use the logic analyzer to check the timings and bytes are correct and fix any trouble.

That's probably the only way to do so right now. 

 

 

 

 

How did you wire everything?

 

My current setup is to have the PS/2 mouse data and clock line connected to a bidirectional MOSFET voltage level converter (http://www.hobbytronics.co.uk/mosfet-voltage-level-converter) into 2 pins on a 48-pin MSP430F5510. As for the code, I'm using Arduino's PS2Mouse library because it is known to work.

 

 

Also isn't the clock supposed to be in the 10KHz range? 38400bps might be too high.

38400 bps is for UART with the computer. If there is a less bit-bang way to do PS/2 with MSP430's UART/SPI/I2c module, that would be great but I've never pondered into the software behind those so I'm not sure if it's possible to do so.

Link to post
Share on other sites

I asked again about the wiring because this source say you need 4 pins connected as open collector with a resistor to +5V (http://www.computer-engineering.org/ps2protocol/), but it may work with only 2 pins.

 

The protocol looks like standard synchronous serial 8bits data with start and stop bits and odd parity I thought the PS2 communication was done in hardware.

 

From the same page "The Clock line must be continuously high for at least 50 microseconds before the device can begin to transmit its data.", your delay may be doing just that.

Link to post
Share on other sites

I asked again about the wiring because this source say you need 4 pins connected as open collector with a resistor to +5V (http://www.computer-engineering.org/ps2protocol/), but it may work with only 2 pins.

Was it? Sorry if it wasn't clear. I was only using 2 lines.

 

 

The protocol looks like standard synchronous serial 8bits data with start and stop bits and odd parity I thought the PS2 communication was done in hardware.

The 5510 I'm using only have one pair of TX and RX and I'd much rather use that for UART, but if nothing else works, maybe it's worth a try. By "done in hardware", do you mean the MSP430 has a module to do that or are you saying to adapt I2C or SPI to PS/2?

 

About the delay, I don't think it's the case. I'm setting the clock to run from XT2 now and let's see if it works. 

 

Still, is there a way to output MCLK/SMCLK if there are no dedicated pins for those? I've used an oscilloscope to calibrate the microseconds, milliseconds delays by litting an LED, wait, turn off LED, wait and divide the period by 2 but I'd still like to see if the clocks is running correctly.

Link to post
Share on other sites

I'm not familiar with this exact MCU model, I suppose your best bet to get something close to the MCLK is to setup a PWM on some pin. 

That's a great idea. 

 

 

Yes the 430s can do synchronous serial

Can you elaborate? Is it family specific? In the x5xx family guide, I only see UART, SPI, I2C mode availabe. How do you force the module to do some other modes?

 

Other than that, I found out that the 3.3V-5V level converter was wired a bit incorrectly where some resistors which is connected to 5V are connected to 3.3V. Using another breadboard with separation on the power lines, the code for the FLL 8MHz works.

Link to post
Share on other sites

SPI is a synchronous serial protocol.

 

But I just had a quick glance at the datasheets, you don't get the start and stop bits in synchronous mode, so you can't use the hardware as is (you'd have to generate these bits in software, not really useful).

It's kinda strange in fact that PS2 needs these bits, you must have them with asynchronous serial but they're useless if you have a clock.

Well, sorry for giving you false hope.

Link to post
Share on other sites

SPI is a synchronous serial protocol.

 

But I just had a quick glance at the datasheets, you don't get the start and stop bits in synchronous mode, so you can't use the hardware as is (you'd have to generate these bits in software, not really useful).

It's kinda strange in fact that PS2 needs these bits, you must have them with asynchronous serial but they're useless if you have a clock.

Well, sorry for giving you false hope.

That's alright. At least I have some sort of working code now, even though it only works at this one specific clock frequency but it does work with different development boards.

Link to post
Share on other sites
  • 2 months later...

EDIT: It was a hardware problem with the level shifter. All is well now.

 

Can I revive this thread quickly and get help with my existing code? Same setup as above, I'm testing this with the F5529 Launchpad. Using Energia with Arduino's PS2 mouse library, it works perfectly so I wrote the code in C for using without Energia. In this case, I'm using XT2 at 4MHz and UART at 9600 baud (maximum the F5529LP can handle with debugging going on).

 

main.c : http://pastebin.com/dFqB6sPp

PS2.h : http://pastebin.com/7JphbyUX

PS2.c : http://pastebin.com/WTAtLFch

 

Most of the time, the code above works a bit - the MSP430 transmit data fine but the response is rubbish (x=-1, y=-1 all the time) (screenshot), sometimes the code freezes because the device isn't taking over the clock but that can be fixed.

 

I'm not sure how fix this. Any help would be fantastic.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...