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:
http://www.computer-engineering.org/ps2protocol/
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.
Is this good so far?
I am using a ATmega168 running the Arduino software.
Here is where I got information on the ps/2 protocol:
http://www.computer-engineering.org/ps2protocol/
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.
Rich (BB code):
char readHost()
{
int count = 0; //count for the parity
int data[7]; //holds the data received from host
while(islow(MCLK)) //wait while host keeps clock low
{
//twiddle
}
delayMicroseconds(10); //wait a tad
for(int i = 0; i < 11; i++) //device must cycle clock 11 times
{
golo(MCLK); //bring clock low
delayMicroseconds(40); //twiddle
gohi(MCLK); //bring clock back high
delayMicroseconds(20); //twiddle //only wait 20 because we want to read in the middle of the 40 microseconds
boolean low = islow(MDATA); //returns true if the data line is low
if(!low) //if data line is not low, increment count for the parity
count++;
if(i <= 7) //data bits 0-7 //first 8 bits are the data bits, put them into the data array
data = low;
if(i == 8) //parity //9th bit is the parity bit, not yet implemented
{
//parity... can ignore for now
}
if(i == 9) //10th bit is where the device has to send the ACK, going low
{
//send the ack!!!
golo(MDATA);
}
if(i == 10) //11th bit, release the ACK. reading is now complete
{
//undo the ack!
gohi(MDATA);
}
delayMicroseconds(20); //twiddle
}
}
void loop() //loop continuously
{
while(islow(MCLK)) //if clock is low, the host may be trying to do something
{
if(islow(MDATA)) //data is low also! the host is trying to communicate
{
int data = readHost(); //read what the host has to say
break;
}
}
}
Is this good so far?