diff --git a/Example_Touchpads/Touchpad_Stream_mode.ino b/Example_Touchpads/Touchpad_Stream_mode.ino new file mode 100644 index 0000000..c565eeb --- /dev/null +++ b/Example_Touchpads/Touchpad_Stream_mode.ino @@ -0,0 +1,581 @@ +// +// This software interfaces a Teensy 3.2 with a PS/2 laptop touchpad. +// The touchpad is commanded to be in Stream Mode (not remote mode). +// This causes the touchpad to send data when ever it detects finger movement. +// The ps/2 code uses the USB PJRC Mouse functions at www.pjrc.com/teensy/td_mouse.html +// The ps/2 code has a watchdog timer so the code can't hang if a clock edge is missed. +// In the Arduino IDE, select Tools, Teensy 3.2. Also under Tools, select Keyboard+Mouse+Joystick +// +// Revision History +// Rev 1.0 - Aug 1, 2020 - Original Release +// +// The touchpad ps/2 data and clock lines are connected to the following Teensy I/O pins +#define TP_DATA 0 +#define TP_CLK 1 +// Teensy LED will be turned ON to show if errors are detected during initialization or during normal use +#define ERROR_LED 13 +// +// Declare variables that will be used by functions +boolean init_error = LOW; // set high if initialization detects an error +boolean bit_error = LOW; // set high if main loop detects an error +char mstat; // touchpad status 8 bit register = Y overflow, X overflow, Y sign bit, X sign bit, Always 1, Middle Btn, Right Btn, Left Btn +char mx; // touchpad x movement = 8 data bits. The sign bit is in the status register to + // make a 9 bit 2's complement value. Left to right on the touchpad gives a positive value. +char my; // touchpad y movement also 8 bits plus sign bit in status reg. Touchpad movement away from the user gives a positive value. +// +// Function to set a pin to high impedance (acts like open drain output) +void go_z(int pin) +{ + pinMode(pin, INPUT); + digitalWrite(pin, HIGH); +} +// +// Function to set a pin as an input with a pullup +void go_pu(int pin) +{ + pinMode(pin, INPUT_PULLUP); + digitalWrite(pin, HIGH); +} +// +// Function to set a pin to a logic low +void go_0(int pin) +{ + pinMode(pin, OUTPUT); + digitalWrite(pin, LOW); +} +// +// Function to set a pin to a logic high +void go_1(int pin) +{ + pinMode(pin, OUTPUT); + digitalWrite(pin, HIGH); +} +// +// *****************Functions for Touchpad*************************** +// +// Function to send the touchpad a byte of data (command) +// +void tp_write(char send_data) +{ + unsigned int timeout = 200; // breakout of loop if watchdog over this value in msec + elapsedMillis watchdog; // zero the watchdog timer clock + char odd_parity = 0; // clear parity bit count +// Enable the PS/2 bus by floating the clock and data + go_pu(TP_CLK); // + go_pu(TP_DATA); // + delayMicroseconds(250); // wait before requesting the bus + go_0(TP_CLK); // Send the Clock line low to request to transmit data + delayMicroseconds(100); // wait for 100 microseconds per bus spec + go_0(TP_DATA); // Send the Data line low (the start bit) + delayMicroseconds(1); // + go_pu(TP_CLK); // Release the Clock line so it is pulled high + delayMicroseconds(1); // give some time to let the clock line go high + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + if (watchdog >= timeout) { //check for infinite loop because tp did not respond + init_error = HIGH; + break; // break out of infinite loop + } + } +// send the 8 bits of send_data + for (int j=0; j<8; j++) { + if (send_data & 1) { //check if lsb is set + go_pu(TP_DATA); // send a 1 to TP + odd_parity = odd_parity + 1; // keep running total of 1's sent + } + else { + go_0(TP_DATA); // send a 0 to TP + } + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == LOW) { // loop until the clock goes high + if (watchdog >= timeout) { //check for infinite loop + init_error = HIGH; + break; // break out of infinite loop + } + } + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + if (watchdog >= timeout) { //check for infinite loop + init_error = HIGH; + break; // break out of infinite loop + } + } + send_data = send_data >> 1; // shift data right by 1 to prepare for next loop + } +// send the parity bit + if (odd_parity & 1) { //check if lsb of parity is set + go_0(TP_DATA); // already odd so send a 0 to TP + } + else { + go_pu(TP_DATA); // send a 1 to TP to make parity odd + } + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == LOW) { // loop until the clock goes high + if (watchdog >= timeout) { //check for infinite loop + init_error = HIGH; + break; // break out of infinite loop + } + } + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + if (watchdog >= timeout) { //check for infinite loop + init_error = HIGH; + break; // break out of infinite loop + } + } + go_pu(TP_DATA); // Release the Data line so it goes high as the stop bit + delayMicroseconds(80); // testing shows delay at least 40us + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + if (watchdog >= timeout) { //check for infinite loop + init_error = HIGH; + break; // break out of infinite loop + } + } + delayMicroseconds(1); // wait to let the data settle + if (digitalRead(TP_DATA)) { // Ack bit s/b low if good transfer + } + while ((digitalRead(TP_CLK) == LOW) || (digitalRead(TP_DATA) == LOW)) { // loop if clock or data are low + if (watchdog >= timeout) { //check for infinite loop + init_error = HIGH; + break; // break out of infinite loop + } + } +// Inhibit the bus so the tp only talks when we're listening + go_0(TP_CLK); +} +// +// Function to get a byte of data from the touchpad +// +char tp_read(void) +{ + unsigned int timeout = 200; // breakout of loop if over this value in msec + elapsedMillis watchdog; // zero the watchdog timer clock + char rcv_data = 0; // initialize to zero + char mask = 1; // shift a 1 across the 8 bits to select where to load the data + char rcv_parity = 0; // count the ones received + go_pu(TP_CLK); // release the clock + go_pu(TP_DATA); // release the data + delayMicroseconds(5); // delay to let clock go high + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + if (watchdog >= timeout) { //check for infinite loop + init_error = HIGH; + break; // break out of infinite loop + } + } + if (digitalRead(TP_DATA)) { // Start bit s/b low from tp + // start bit not correct - put error handler here if desired + } + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == LOW) { // loop until the clock goes high + if (watchdog >= timeout) { //check for infinite loop + init_error = HIGH; + break; // break out of infinite loop + } + } + for (int k=0; k<8; k++) { + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + if (watchdog >= timeout) { //check for infinite loop + init_error = HIGH; + break; // break out of infinite loop + } + } + if (digitalRead(TP_DATA)) { // check if data is high + rcv_data = rcv_data | mask; // set the appropriate bit in the rcv data + rcv_parity++; // increment the parity bit counter + } + mask = mask << 1; + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == LOW) { // loop until the clock goes high + if (watchdog >= timeout) { //check for infinite loop + init_error = HIGH; + break; // break out of infinite loop + } + } + } +// receive parity + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + if (watchdog >= timeout) { //check for infinite loop + init_error = HIGH; + break; // break out of infinite loop + } + } + if (digitalRead(TP_DATA)) { // check if received parity is high + rcv_parity++; // increment the parity bit counter + } + rcv_parity = rcv_parity & 1; // mask off all bits except the lsb + if (rcv_parity == 0) { // check for bad (even) parity + init_error = HIGH; + } + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == LOW) { // loop until the clock goes high + if (watchdog >= timeout) { //check for infinite loop + init_error = HIGH; + break; // break out of infinite loop + } + } +// stop bit + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + if (watchdog >= timeout) { //check for infinite loop + init_error = HIGH; + break; // break out of infinite loop + } + } + if (digitalRead(TP_DATA) == LOW) { // check if stop bit is bad (low) + // send bad stop bit to future error handler + } + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == LOW) { // loop until the clock goes high + if (watchdog >= timeout) { //check for infinite loop + init_error = HIGH; + break; // break out of infinite loop + } + } +// Inhibit the bus so the tp only talks when we're listening + go_0(TP_CLK); + return rcv_data; // pass the received data back +} +// +// Function to decode 3 bytes of data from the touchpad = status, X Delta, Y Delta +// +void tp_packet(void) +{ +// ******************************Receive Byte 0 = Status*********************** + char rcv_data = 0; // initialize to zero + char mask = 1; // shift a 1 across the 8 bits to select where to load the data + char rcv_parity = 0; // count the ones received + go_pu(TP_CLK); // release the clock + go_pu(TP_DATA); // release the data + delayMicroseconds(5); // delay to let clock go high + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + } + if (digitalRead(TP_DATA)) { // Start bit s/b low from tp + // start bit not correct - put error handler here if desired + } + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == LOW) { // loop until the clock goes high + } + for (int k=0; k<8; k++) { + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + } + if (digitalRead(TP_DATA)) { // check if data is high + rcv_data = rcv_data | mask; // set the appropriate bit in the rcv data + rcv_parity++; // increment the parity bit counter + } + mask = mask << 1; + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == LOW) { // loop until the clock goes high + } + } +// receive parity + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + } + if (digitalRead(TP_DATA)) { // check if received parity is high + rcv_parity++; // increment the parity bit counter + } + rcv_parity = rcv_parity & 1; // mask off all bits except the lsb + if (rcv_parity == 0) { // check for bad (even) parity + bit_error = HIGH; + } + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == LOW) { // loop until the clock goes high + } +// stop bit + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + } + if (digitalRead(TP_DATA) == LOW) { // check if stop bit is bad (low) + bit_error = HIGH; + } + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == LOW) { // loop until the clock goes high + } + mstat = rcv_data; // save data result in status byte +// ******************************Receive Byte 1 = Delta X************************** + rcv_data = 0; // initialize to zero + mask = 1; // shift a 1 across the 8 bits to select where to load the data + rcv_parity = 0; // count the ones received + delayMicroseconds(5); // delay to let the clock stop ringing + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + } + if (digitalRead(TP_DATA)) { // Start bit s/b low from tp + // start bit not correct - put error handler here if desired + } + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == LOW) { // loop until the clock goes high + } + for (int k=0; k<8; k++) { + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + } + if (digitalRead(TP_DATA)) { // check if data is high + rcv_data = rcv_data | mask; // set the appropriate bit in the rcv data + rcv_parity++; // increment the parity bit counter + } + mask = mask << 1; + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == LOW) { // loop until the clock goes high + } + } +// receive parity + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + } + if (digitalRead(TP_DATA)) { // check if received parity is high + rcv_parity++; // increment the parity bit counter + } + rcv_parity = rcv_parity & 1; // mask off all bits except the lsb + if (rcv_parity == 0) { // check for bad (even) parity + bit_error = HIGH; + } + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == LOW) { // loop until the clock goes high + } +// stop bit + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + } + if (digitalRead(TP_DATA) == LOW) { // check if stop bit is bad (low) + bit_error = HIGH; + } + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == LOW) { // loop until the clock goes high + } + mx = rcv_data; // save data result in delta x byte +// Receive Byte 1 = Delta Y +// ******************************Receive Byte 2 = Delta Y************************** + rcv_data = 0; // initialize to zero + mask = 1; // shift a 1 across the 8 bits to select where to load the data + rcv_parity = 0; // count the ones received + delayMicroseconds(5); // delay to let the clock stop ringing + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + } + if (digitalRead(TP_DATA)) { // Start bit s/b low from tp + // start bit not correct - put error handler here if desired + } + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == LOW) { // loop until the clock goes high + } + for (int k=0; k<8; k++) { + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + } + if (digitalRead(TP_DATA)) { // check if data is high + rcv_data = rcv_data | mask; // set the appropriate bit in the rcv data + rcv_parity++; // increment the parity bit counter + } + mask = mask << 1; + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == LOW) { // loop until the clock goes high + } + } +// receive parity + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + } + if (digitalRead(TP_DATA)) { // check if received parity is high + rcv_parity++; // increment the parity bit counter + } + rcv_parity = rcv_parity & 1; // mask off all bits except the lsb + if (rcv_parity == 0) { // check for bad (even) parity + bit_error = HIGH; + } + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == LOW) { // loop until the clock goes high + } +// stop bit + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low + } + if (digitalRead(TP_DATA) == LOW) { // check if stop bit is bad (low) + bit_error = HIGH; + } + delayMicroseconds(1); // delay to let the clock settle out + while (digitalRead(TP_CLK) == LOW) { // loop until the clock goes high + } + my = rcv_data; // save data result in delta y byte +// +// Inhibit the bus so the tp only talks when we're listening + go_0(TP_CLK); +} +// +void touchpad_init() +{ + init_error = LOW; // start with no error + go_pu(TP_CLK); // float the clock and data to touchpad + go_pu(TP_DATA); + // Sending reset command to touchpad + tp_write(0xff); + if (tp_read() != 0xfa) { // verify correct ack byte + init_error = HIGH; + } + delay(1000); // wait 1 second so tp can run its self diagnostic + // verify proper response from tp + if (tp_read() != 0xaa) { // verify basic assurance test passed + init_error = HIGH; + } + if (tp_read() != 0x00) { // verify basic assurance test passed + init_error = HIGH; + } + // Send touchpad disable code so that the mode byte can be loaded next + tp_write(0xf5); // tp disable + if (tp_read() != 0xfa) { // verify correct ack byte + init_error = HIGH; + } + // Load Mode Byte with 00 using the following special sequence from page 38 of Synaptics Interfaceing Guide. + // Send set resolution to 0 four times followed by a set sample rate to 0x14 + // The resolution and sample rate are not actually changed but instead it loads the mode byte. +// #1 set resolution + tp_write(0xe8); // set resolution (actually part of setting mode byte) + if (tp_read() != 0xfa) { // verify correct ack byte + init_error = HIGH; + } + tp_write(0x00); // to zero + if (tp_read() != 0xfa) { // verify correct ack byte + init_error = HIGH; + } +// #2 set resolution (actually part of setting mode byte) + tp_write(0xe8); // set resolution + if (tp_read() != 0xfa) { // verify correct ack byte + init_error = HIGH; + } + tp_write(0x00); // to zero + if (tp_read() != 0xfa) { // verify correct ack byte + init_error = HIGH; + } +// #3 set resolution + tp_write(0xe8); // set resolution + if (tp_read() != 0xfa) { // verify correct ack byte + init_error = HIGH; + } + tp_write(0x00); // to zero + if (tp_read() != 0xfa) { // verify correct ack byte + init_error = HIGH; + } +// #4 set resolution (actually part of setting mode byte) + tp_write(0xe8); // set resolution + if (tp_read() != 0xfa) { // verify correct ack byte + init_error = HIGH; + } + tp_write(0x00); // to zero + if (tp_read() != 0xfa) { // verify correct ack byte + init_error = HIGH; + } +// Set sample rate (actually part of setting mode byte) + tp_write(0xf3); // set sample rate + if (tp_read() != 0xfa) { // verify correct ack byte + init_error = HIGH; + } + tp_write(0x14); // to 14 hex + if (tp_read() != 0xfa) { // verify correct ack byte + init_error = HIGH; + } +// This completes the mode byte load + // set the resolution for real + tp_write(0xe8); // Sending resolution command + if (tp_read() != 0xfa) { // verify correct ack byte + init_error = HIGH; + } + tp_write(0x03); // value of 0x03 = 8 counts/mm resolution (default is 4 counts/mm) + if (tp_read() != 0xfa) { // verify correct ack byte + init_error = HIGH; + } + // set the sample rate for real + tp_write(0xf3); // Sending sample rate command + if (tp_read() != 0xfa) { // verify correct ack byte + init_error = HIGH; + } + tp_write(0x28); // 0x28 = 40 samples per second, the default value used for Synaptics TP + if (tp_read() != 0xfa) { // verify correct ack byte + init_error = HIGH; + } + // Load stream mode command so tp will send data whenever it's available + tp_write(0xea); // stream mode + if (tp_read() != 0xfa) { // verify correct ack byte + init_error = HIGH; + } + // Send touchpad enable code + tp_write(0xf4); // tp enable + if (tp_read() != 0xfa) { // verify correct ack byte + init_error = HIGH; + } +} +// +// ************************************Begin Routine********************************************************* +void setup() { + pinMode(ERROR_LED, OUTPUT); // define teensy I/O 13 as an output + touchpad_init(); // reset tp and check that self diagnostic passed. Put tp in stream mode and enable it + digitalWrite(ERROR_LED, init_error); //LED off means touchpad passed its power up test, ACKed all communications, and never caused a watchdog timeout + delay(5000); // wait 5 seconds to show init_error LED status + digitalWrite(ERROR_LED, LOW); // Turn off LED for 1/2 second + delay(500); // 1/2 second delay + digitalWrite(ERROR_LED, HIGH); // Turn on LED momentarily to indicate code is in main loop + delay(500); // 1/2 second delay + digitalWrite(ERROR_LED, bit_error); // Turn off LED and switch its function to show bit errors in main loop +} +// +// declare and initialize variables + boolean over_flow; // set if x or y movement values are bad due to overflow + boolean left_button = 0; // on/off variable for left button = bit 0 of mstat + boolean right_button = 0; // on/off variable for right button = bit 1 of mstat + boolean old_left_button = 0; // on/off variable for left button status the previous polling cycle + boolean old_right_button = 0; // on/off variable for right button status the previous polling cycle + boolean button_change = 0; // Active high, shows when a touchpad left or right button has changed since last polling cycle +// +// ************************************Main Loop*************************************************************** +void loop() { +// Touchpad is enabled and in stream mode + tp_packet(); // this function loops until there is bus activity and then saves data into mstat, mx, and my bytes for USB transmission below. + if (((0x80 & mstat) == 0x80) || ((0x40 & mstat) == 0x40)) { // x or y overflow bits set? + over_flow = 1; // set the overflow flag + } +// change the x data from 9 bit to 8 bit 2's complement by throwing away lsb + mx = mx & 0x7f; // mask off 8th bit + if ((0x10 & mstat) == 0x10) { // move the sign into + mx = 0x80 | mx; // the 8th bit position + } +// change the y data from 9 bit to 8 bit 2's complement by throwing away lsb + my = my & 0x7f; // mask off 8th bit + if ((0x20 & mstat) == 0x20) { // move the sign into + my = 0x80 | my; // the 8th bit position + } +// Invert the 8 bit value because y movement in ps/2 format is the opposite direction in touchpad.move function + my = (~my + 0x01); // invert the y data by taking the 2's complement (invert and add 1) +// zero out mx and my if over_flow is detected because the data is garbage + if (over_flow) { + mx = 0x00; + my = 0x00; + bit_error = HIGH; // flag an error + } +// Send the x and y data back via usb if either one is non-zero + if ((mx != 0x00) || (my != 0x00)) { + Mouse.move(mx,my); + } +// +// pull out the left and right button status bits from the mstat byte + if ((0x01 & mstat) == 0x01) { // is bit 1 set + left_button = 1; // set the left_button status + } + else { // clear the left_button status + left_button = 0; + } + if ((0x02 & mstat) == 0x02) { // is bit 2 set + right_button = 1; // set the right_button status + } + else { // clear the right_button status + right_button = 0; + } +// Determine if the left or right touch pad buttons have changed + button_change = (left_button ^ old_left_button) | (right_button ^ old_right_button); +// Don't send button status if there's no change since last usb button transmission. + if (button_change){ + Mouse.set_buttons(left_button, 0, right_button); // send button status + old_left_button = left_button; // remember new button status for next polling cycle + old_right_button = right_button; + } +// + digitalWrite(ERROR_LED, bit_error); // LED is used to show NACK, bad parity, overflow, or bad stop bits. Any error is latched "on" +}