PS/2 Interface suggestions

Discussion in 'Embedded Systems and Microcontrollers' started by Razor Concepts, Jun 3, 2009.

  1. Razor Concepts

    Thread Starter Senior Member

    Oct 7, 2008
    Hello, I am trying to emulate a mouse with the ps/2 protocol.
    I am using a ATmega168 running the Arduino software.
    Here is where I got information on the ps/2 protocol:

    Could you look over the code to see if I did anything blatantly wrong?
    Assume that the other little methods like islow or gohi, etc work fine.

    The code should be commented enough, but here is some background information. The host (the computer) can send data directly to the device (in this case, the mega168 emulating a mouse). There are two lines, data and clock. To tell the device that the host wants to communicate data, the host will first bring the clock low, bring data low, and then release clock. I have put that code in the method loop(). It checks if clock is low, and while it is low, checks if data is low. If it finds that data is low, it is confirmed that the host wants to communicate. Thus, it enters the readHost() method.

    In the very beginning of readHost(), the device waits while clock is low (the host may not have relased it yet). When the clock is released by the host, the device then takes control of the clock, switching it low and releasing it for 40 microsecond intervals, 11 times. While the clock is low, the host will change the data line accordingly, so the device should wait. Then the device releases clock. 20 microseconds after releasing clock, it reads the status of data. This is to ensure that the host had enough time to change data.

    The first 8 cycles are for the 8 data bits. For now I just put them into an array, will work with those later.

    The 9th bit is the parity bit, right now I have a counter going to keep track of odd/even, but it is not implemented yet for simplicity reasons.

    The 10th bit is the stop bit, which should always be high (which is why I dont bother reading it). Also now is the time for the device to bring data low as the ACK.

    The 11th bit is still the ack... and then the device releases ACK and the communication is terminated.
    Code ( (Unknown Language)):
    2. char readHost()
    3. {
    4.   int count = 0; //count for the parity
    5.   int data[7];   //holds the data received from host
    6.   while(islow(MCLK))  //wait while host keeps clock low
    7.   {
    8.     //twiddle
    9.   }
    10.   delayMicroseconds(10);  //wait a tad
    11.   for(int i = 0; i < 11; i++)  //device must cycle clock 11 times
    12.   {
    13.     golo(MCLK);                            //bring clock low
    14.     delayMicroseconds(40); //twiddle    
    15.     gohi(MCLK);                            //bring clock back high
    16.     delayMicroseconds(20); //twiddle       //only wait 20 because we want to read in the middle of the 40 microseconds
    17.     boolean low = islow(MDATA);            //returns true if the data line is low
    18.     if(!low)                               //if data line is not low, increment count for the parity
    19.     count++;        
    20.     if(i <= 7)      //data bits 0-7        //first 8 bits are the data bits, put them into the data array
    21.     data[i] = low;
    22.     if(i == 8)      //parity               //9th bit is the parity bit, not yet implemented
    23.     {
    24.       //parity... can ignore for now
    25.     }
    26.     if(i == 9)                             //10th bit is where the device has to send the ACK, going low
    27.     {
    28.       //send the ack!!!
    29.       golo(MDATA);
    30.     }
    31.     if(i == 10)                            //11th bit, release the ACK. reading is now complete
    32.     {
    33.       //undo the ack!
    34.       gohi(MDATA);
    35.     }
    36.     delayMicroseconds(20); //twiddle
    37.   }
    38. }
    39. void loop()  //loop continuously
    40. {
    41.   while(islow(MCLK))   //if clock is low, the host may be trying to do something
    42.   {
    43.     if(islow(MDATA))  //data is low also! the host is trying to communicate
    44.     {
    45.       int data = readHost();  //read what the host has to say
    46.       break;
    47.     }
    48.   }
    49. }
    50. [/i]

    Is this good so far?