Add files via upload

This commit is contained in:
Frank Adams 2018-12-02 17:19:29 -08:00 committed by GitHub
parent 3afbdef04d
commit 89473e89a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,23 +9,23 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
// It controls a Lenovo ThinkPad T61 Laptop Keyboard and PS/2 Trackpoint using a Teensy LC on // This software controls a Lenovo ThinkPad T61 Laptop Keyboard and PS/2 Trackpoint using a Teensy LC on
// a daughterboard with a 44 pin FPC connector. The keyboard part number is 42T3177. // a daughterboard with a 44 pin FPC connector. The keyboard part number is 42T3177.
// This routine uses the Teensyduino "Micro-Manager Method" to send Normal and Modifier // This routine uses the Teensyduino "Micro-Manager Method" to send Normal and Modifier
// keys over USB. Only the volume control multi-media keys are supported by this routine. // keys over USB. Only the volume control multi-media keys are supported by this routine.
// Description of Teensyduino keyboard functions is at www.pjrc.com/teensy/td_keyboard.html // Description of Teensyduino keyboard functions is at www.pjrc.com/teensy/td_keyboard.html
// The PS/2 code for the Trackpoint was originally from https://playground.arduino.cc/uploads/ComponentLib/mouse.txt // The ps/2 code uses the USB PJRC Mouse functions at www.pjrc.com/teensy/td_mouse.html
// but the interface to the host was changed from RS232 serial to USB using the PJRC Mouse functions. // The ps/2 code has a watchdog timer so the code can't hang if a clock edge is missed.
// A watchdog timer was also added to the "while loops" so the code can't hang if a clock edge is missed.
// In the Arduino IDE, select Tools, Teensy LC. Also under Tools, select Keyboard+Mouse+Joystick // In the Arduino IDE, select Tools, Teensy LC. Also under Tools, select Keyboard+Mouse+Joystick
// //
// Revision History // Revision History
// Initial Release Nov 20, 2018 // Rev 1.0 - Nov 20, 2018 - Original Release
// Rev 1.1 - Dec 2, 2018 - Replaced ps/2 trackpoint code from playground arduino with my own code
// //
#define MODIFIERKEY_FN 0x8f // give Fn key a HID code #define MODIFIERKEY_FN 0x8f // give Fn key a HID code
// Trackpoint signals // Trackpoint signals
#define MDATA 0 // ps/2 data to trackpoint (via BSS138 level translator) #define TP_DATA 0 // ps/2 data to trackpoint (via BSS138 level translator)
#define MCLK 1 // ps/2 clock to trackpoint (via BSS138 level translator) #define TP_CLK 1 // ps/2 clock to trackpoint (via BSS138 level translator)
// Keyboard LEDs // Keyboard LEDs
#define CAPS_LED 13 // includes 715 ohm resistor for 2ma LED #define CAPS_LED 13 // includes 715 ohm resistor for 2ma LED
// Set the keyboard row & column size // Set the keyboard row & column size
@ -143,185 +143,258 @@ int mod_alt_l = 0;
int mod_alt_r = 0; int mod_alt_r = 0;
int mod_gui = 0; int mod_gui = 0;
// //
// *****************Functions for Trackpoint*************************** // **************Functions common to keyboard and trackpoint**************************
// Function to send the trackpoint a command //
void trackpoint_write(char data) // Function to set a pin to high impedance (acts like open drain output)
void go_z(int pin)
{ {
char i; pinMode(pin, INPUT);
char parity = 1; digitalWrite(pin, HIGH);
// put pins in output mode }
go_z(MDATA); //
go_z(MCLK); // Function to set a pin as an input with a pullup
elapsedMillis watchdog; // set watchdog to zero void go_pu(int pin)
delayMicroseconds(300); {
go_0(MCLK); pinMode(pin, INPUT_PULLUP);
delayMicroseconds(300); digitalWrite(pin, HIGH);
go_0(MDATA); }
delayMicroseconds(10); //
// start bit // Function to send a pin to a logic low
go_z(MCLK); void go_0(int pin)
// wait for trackpoint to take control of clock) {
while (digitalRead(MCLK) == HIGH) { pinMode(pin, OUTPUT);
if (watchdog >= 200) { //check for infinite loop digitalWrite(pin, LOW);
trackpoint_error = HIGH; // set error flag }
break; //
} // Function to send a pin to a logic high
} void go_1(int pin)
// clock is low, and we are clear to send data {
for (i=0; i < 8; i++) { pinMode(pin, OUTPUT);
if (data & 0x01) { digitalWrite(pin, HIGH);
go_z(MDATA); }
} // *****************Functions for Trackpoint***************************
else { //
go_0(MDATA); // Function to send the trackpoint a byte of data (command)
} //
// wait for clock cycle void tp_write(char send_data)
while (digitalRead(MCLK) == LOW) { {
if (watchdog >= 200) { //check for infinite loop unsigned int timeout = 200; // breakout of loop if over this value in msec
trackpoint_error = HIGH; // set error flag elapsedMillis watchdog; // zero the watchdog timer clock
break; char odd_parity = 0; // clear parity bit count
} // Enable the bus by floating the clock and data
} go_z(TP_CLK); //
while (digitalRead(MCLK) == HIGH) { go_z(TP_DATA); //
if (watchdog >= 200) { //check for infinite loop delayMicroseconds(250); // wait before requesting the bus
trackpoint_error = HIGH; // set error flag go_0(TP_CLK); // Send the Clock line low to request to transmit data
break; delayMicroseconds(100); // wait for 100 microseconds per bus spec
} go_0(TP_DATA); // Send the Data line low (the start bit)
} delayMicroseconds(1); //
parity = parity ^ (data & 0x01); go_z(TP_CLK); // Release the Clock line so it is pulled high
data = data >> 1; delayMicroseconds(1); // give some time to let the clock line go high
} while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low
// parity if (watchdog >= timeout) { //check for infinite loop
if (parity) { trackpoint_error = HIGH; // set error flag
go_z(MDATA); break; // break out of infinite loop
} }
else { }
go_0(MDATA); // send the 8 bits of send_data
} for (int j=0; j<8; j++) {
// wait for clock cycle if (send_data & 1) { //check if lsb is set
while (digitalRead(MCLK) == LOW) { go_z(TP_DATA); // send a 1 to TP
if (watchdog >= 200) { //check for infinite loop odd_parity = odd_parity + 1; // keep running total of 1's sent
trackpoint_error = HIGH; // set error flag }
break; else {
} go_0(TP_DATA); // send a 0 to TP
} }
while (digitalRead(MCLK) == HIGH) { delayMicroseconds(1); // delay to let the clock settle out
if (watchdog >= 200) { //check for infinite loop while (digitalRead(TP_CLK) == LOW) { // loop until the clock goes high
trackpoint_error = HIGH; // set error flag if (watchdog >= timeout) { //check for infinite loop
break; trackpoint_error = HIGH; // set error flag
} break; // break out of infinite loop
} }
// stop bit }
go_z(MDATA); delayMicroseconds(1); // delay to let the clock settle out
delayMicroseconds(50); while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low
while (digitalRead(MCLK) == HIGH) { if (watchdog >= timeout) { //check for infinite loop
if (watchdog >= 200) { //check for infinite loop trackpoint_error = HIGH; // set error flag
trackpoint_error = HIGH; // set error flag break; // break out of infinite loop
break; }
} }
} send_data = send_data >> 1; // shift data right by 1 to prepare for next loop
// wait for trackpoint to switch modes }
while ((digitalRead(MCLK) == LOW) || (digitalRead(MDATA) == LOW)) { // send the parity bit
if (watchdog >= 200) { //check for infinite loop if (odd_parity & 1) { //check if lsb of parity is set
trackpoint_error = HIGH; // set error flag go_0(TP_DATA); // already odd so send a 0 to TP
break; }
} else {
} go_z(TP_DATA); // send a 1 to TP to make parity odd
// put a hold on the incoming data. }
go_0(MCLK); 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
trackpoint_error = HIGH; // set error flag
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
trackpoint_error = HIGH; // set error flag
break; // break out of infinite loop
}
}
go_z(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
trackpoint_error = HIGH; // set error flag
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
trackpoint_error = HIGH; //bad ack bit so set the error flag
}
while ((digitalRead(TP_CLK) == LOW) || (digitalRead(TP_DATA) == LOW)) { // loop if clock or data are low
if (watchdog >= timeout) { //check for infinite loop
trackpoint_error = HIGH; // set error flag
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 trackpoint // Function to get a byte of data from the trackpoint
// //
char trackpoint_read(void) char tp_read(void)
{ {
char data = 0x00; unsigned int timeout = 200; // breakout of loop if over this value in msec
int i; elapsedMillis watchdog; // zero the watchdog timer clock
char bity = 0x01; char rcv_data = 0; // initialize to zero
// start the clock char mask = 1; // shift a 1 across the 8 bits to select where to load the data
elapsedMillis watchdog; // set watchdog to zero char rcv_parity = 0; // count the ones received
go_z(MCLK); go_z(TP_CLK); // release the clock
go_z(MDATA); go_z(TP_DATA); // release the data
delayMicroseconds(50); delayMicroseconds(5); // delay to let clock go high
while (digitalRead(MCLK) == HIGH) { while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low
if (watchdog >= 200) { //check for infinite loop if (watchdog >= timeout) { //check for infinite loop
trackpoint_error = HIGH; // set error flag trackpoint_error = HIGH; // set error flag
break; break; // break out of infinite loop
} }
} }
delayMicroseconds(5); // wait for clock ring to settle if (digitalRead(TP_DATA)) { // Start bit s/b low from tp
while (digitalRead(MCLK) == LOW) { // eat start bit trackpoint_error = HIGH; // No start bit so set the error flag
if (watchdog >= 200) { //check for infinite loop }
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
trackpoint_error = HIGH; // set error flag trackpoint_error = HIGH; // set error flag
break; break; // break out of infinite loop
} }
} }
for (i=0; i < 8; i++) { for (int k=0; k<8; k++) {
while (digitalRead(MCLK) == HIGH) { delayMicroseconds(1); // delay to let the clock settle out
if (watchdog >= 200) { //check for infinite loop while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low
if (watchdog >= timeout) { //check for infinite loop
trackpoint_error = HIGH; // set error flag trackpoint_error = HIGH; // set error flag
break; break; // break out of infinite loop
} }
} }
if (digitalRead(MDATA) == HIGH) { if (digitalRead(TP_DATA)) { // check if data is high
data = data | bity; rcv_data = rcv_data | mask; // set the appropriate bit in the rcv data
rcv_parity++; // increment the parity bit counter
} }
while (digitalRead(MCLK) == LOW) { mask = mask << 1;
if (watchdog >= 200) { //check for infinite loop 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
trackpoint_error = HIGH; // set error flag trackpoint_error = HIGH; // set error flag
break; break; // break out of infinite loop
} }
} }
bity = bity << 1;
} }
// ignore parity bit // receive parity
while (digitalRead(MCLK) == HIGH) { delayMicroseconds(1); // delay to let the clock settle out
if (watchdog >= 200) { //check for infinite loop while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low
if (watchdog >= timeout) { //check for infinite loop
trackpoint_error = HIGH; // set error flag trackpoint_error = HIGH; // set error flag
break; break; // break out of infinite loop
} }
} }
while (digitalRead(MCLK) == LOW) { if (digitalRead(TP_DATA)) { // check if received parity is high
if (watchdog >= 200) { //check for infinite loop 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
trackpoint_error = HIGH; //bad parity so set the error flag
}
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
trackpoint_error = HIGH; // set error flag trackpoint_error = HIGH; // set error flag
break; break; // break out of infinite loop
} }
} }
// eat stop bit // stop bit
while (digitalRead(MCLK) == HIGH) { delayMicroseconds(1); // delay to let the clock settle out
if (watchdog >= 200) { //check for infinite loop while (digitalRead(TP_CLK) == HIGH) { // loop until the clock goes low
if (watchdog >= timeout) { //check for infinite loop
trackpoint_error = HIGH; // set error flag trackpoint_error = HIGH; // set error flag
break; break; // break out of infinite loop
} }
} }
while (digitalRead(MCLK) == LOW) { if (digitalRead(TP_DATA) == LOW) { // check if stop bit is bad (low)
if (watchdog >= 200) { //check for infinite loop trackpoint_error = HIGH; //bad stop bit so set the error flag
}
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
trackpoint_error = HIGH; // set error flag trackpoint_error = HIGH; // set error flag
break; break; // break out of infinite loop
} }
} }
// put a hold on the incoming data. // Inhibit the bus so the tp only talks when we're listening
go_0(MCLK); go_0(TP_CLK);
return data; return rcv_data; // pass the received data back
} }
//
void trackpoint_init() void trackpoint_init()
{ {
trackpoint_error = LOW; // start with no error trackpoint_error = LOW; // start with no error
go_z(MCLK); // float the clock and data to trackpoint go_z(TP_CLK); // float the clock and data to trackpoint
go_z(MDATA); go_z(TP_DATA);
delay(1000); // wait 1 second to let power on reset to release delay(1000); // wait 1 second to let power on reset release
// Sending reset command to trackpoint // Sending reset command to trackpoint
trackpoint_write(0xff); tp_write(0xff);
trackpoint_read(); // ack byte if (tp_read() != 0xfa) { // verify correct ack byte
// Read ack byte trackpoint_error = HIGH;
trackpoint_read(); // blank }
trackpoint_read(); // blank delayMicroseconds(100); // give the tp time to run its self diagnostic
// verify proper response from tp
if (tp_read() != 0xaa) { // verify basic assurance test passed
trackpoint_error = HIGH;
}
if (tp_read() != 0x00) { // verify correct device id
trackpoint_error = HIGH;
}
// Sending remote mode code so the trackpoint will send data only when polled // Sending remote mode code so the trackpoint will send data only when polled
trackpoint_write(0xf0); // remote mode tp_write(0xf0); // remote mode
trackpoint_read(); // Read ack byte if (tp_read() != 0xfa) { // verify correct ack byte
trackpoint_error = HIGH;
}
if (trackpoint_error == HIGH) { // check for any errors from tp
delayMicroseconds(300); // wait before trying to initialize tp one last time
tp_write(0xff); // send tp reset code
tp_read(); // read but don't look at response from tp
// Read ack byte
tp_read(); // read but don't look at response from tp
tp_read(); // read but don't look at response from tp
// Sending remote mode code so the trackpoint will send data only when polled
tp_write(0xf0); // remote mode
tp_read(); // read but don't look at response from tp
delayMicroseconds(100); delayMicroseconds(100);
}
} }
// //
// *****************Functions for Keyboard***************************** // *****************Functions for Keyboard*****************************
@ -442,35 +515,7 @@ void send_normals() {
Keyboard.set_key6(slot6); Keyboard.set_key6(slot6);
Keyboard.send_now(); Keyboard.send_now();
} }
// **************Functions common to keyboard and trackpoint**************************
//
// 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 send a pin to a logic low
void go_0(int pin)
{
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
//
// Function to send a pin to a logic high
void go_1(int pin)
{
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
}
// //
//************************************Setup******************************************* //************************************Setup*******************************************
void setup() { void setup() {
@ -598,11 +643,11 @@ void loop() {
// poll the trackpoint for new movement data // poll the trackpoint for new movement data
over_flow = 0; // assume no overflow until status is received over_flow = 0; // assume no overflow until status is received
trackpoint_error = LOW; // start with no error trackpoint_error = LOW; // start with no error
trackpoint_write(0xeb); // request data tp_write(0xeb); // request data
trackpoint_read(); // ignore ack tp_read(); // ignore ack
mstat = trackpoint_read(); // save into status variable mstat = tp_read(); // save into status variable
mx = trackpoint_read(); // save into x variable mx = tp_read(); // save into x variable
my = trackpoint_read(); // save into y variable my = tp_read(); // save into y variable
if (((0x80 & mstat) == 0x80) || ((0x40 & mstat) == 0x40)) { // x or y overflow bits set? if (((0x80 & mstat) == 0x80) || ((0x40 & mstat) == 0x40)) { // x or y overflow bits set?
over_flow = 1; // set the overflow flag over_flow = 1; // set the overflow flag
} }