### PS2_get Source: https://github.com/acidanthera/voodoops2/blob/master/Docs/SynapticsRevB.pdf Reads one byte from the PS/2 device. It waits for the start bit, reads 8 data bits, checks parity and stop bit, and returns the received byte. It also inhibits the bus after receiving the byte. ```APIDOC ## PS2_get ### Description Reads one byte from the PS/2 device. It waits for the start bit, reads 8 data bits, checks parity and stop bit, and returns the received byte. It also inhibits the bus after receiving the byte. ### Method Function ### Endpoint PS2_get() ### Parameters None ### Request Example No request body. ### Response #### Success Response - **byte** (byte) - The byte read from the device. #### Response Example ``` 0x5A ``` ### Error Handling - Calls PS2_error() on parity error or framing error. ``` -------------------------------- ### TouchPad initialization sequence for Absolute mode Source: https://github.com/acidanthera/voodoops2/blob/master/Docs/SynapticsRevB.pdf Typical sequence to initialize the TouchPad and enable Stream mode using the Absolute data format. It performs a Reset command, identifies the device as a Synaptics TouchPad, enables Absolute mode, and starts data transmission. The sequence assumes interrupt-driven operation and leaves the bus inhibited until the end. ```C PS2_send(0xFF); /* Reset command. */ if (PS2_get() != 0xAA) /* Note: This may need an extra-long timeout. */ PS2_error(); if (PS2_get() != 0x00) PS2_error(); ``` ```C send_tp_arg(0x00); /* Send "Identify TouchPad" sequence (section 4.4)*/ PS2_send(0xE9); /* Status Request command. */ minor = PS2_get(); /* First status byte: TouchPad minor rev. */ if (PS2_get() != 0x47) PS2_error(); /* Second status byte: 0x47 == Synaptics TouchPad. */ major = PS2_get() & 0x0F; /* Third status byte: Major rev in low 4 bits. */ ``` ```C send_tp_arg(0x80); /* Send "Set Modes" sequence (see section 4.2). */ PS2_send(0xF3); /* Set Sample Rate command. */ PS2_send(0x14); /* Sample Rate argument of 20. */ ``` ```C PS2_send(0xF4); /* Enable command. */ enable_interrupt_handler(); /* Ready to receive data. */ set_CLOCK(1); /* Release PS/2 bus inhibit. */ ``` -------------------------------- ### PS2_get - Read Byte from PS/2 Device Source: https://github.com/acidanthera/voodoops2/blob/master/Docs/SynapticsRevB.pdf Reads one byte from the PS/2 device, handling start bit, data bits, parity, and stop bit. It inhibits the bus after receiving the byte and waits 50 µs to avoid misreading the stop bit. Includes parity and framing error checks. ```c byte PS2_get() { byte i, bit, value = 0, p = 0; set_CLOCK(1); /* Release inhibit, if necessary. */ wait_CLOCK(0); /* Wait for start bit clock. */ wait_CLOCK(1); /* End of start bit clock. */ for (i = 0; i < 8; i++) { wait_CLOCK(0); /* Wait for clock pulse. */ bit = read_DATA(); value = value + (bit << i); /* Read data bit from pin. */ p = p + bit; /* Accumulate data bit into parity. */ wait_CLOCK(1); } /* Wait for end of clock pulse. */ wait_CLOCK(0); /* Parity bit clock. */ p = p + read_DATA(); /* Accumulate parity bit into parity. */ if ((p & 0x01) == 0) /* Check for odd parity. */ PS2_error(); /* Parity error! */ wait_CLOCK(1); /* End of parity bit clock. */ wait_CLOCK(0); /* Stop bit clock. */ if (read_DATA() == 0) /* Check for valid stop bit. */ PS2_error(); /* Framing error! */ set_CLOCK(0); /* Pull low during stop bit to inhibit. */ wait_us(50); /* Wait out the final clock pulse. */ return value; } ``` -------------------------------- ### PS2_send - Send Byte to PS/2 Device Source: https://github.com/acidanthera/voodoops2/blob/master/Docs/SynapticsRevB.pdf Sends a byte to the PS/2 device, including inhibit, start bit, data bits, parity, stop bit, and line control bit. It waits for the acknowledge byte (0xFA) and calls PS2_error on failure. Lines 4-5 can be skipped if the bus is kept inhibited between calls. ```c void PS2_send(byte value) { byte i, ack, p = 1; set_CLOCK(0); /* Begin inhibit, if necessary. */ wait_us(100); /* Inhibit for about 100us. */ set_DATA(0); /* Hold data pin low while still inhibited. */ set_CLOCK(1); for (i = 0; i < 8; i++) { /* Establish request-to-send state. */ wait_CLOCK(0); /* Wait for clock pulse. */ set_DATA(value & 0x01); /* Output i’th data bit. */ p = p + value; /* Accumulate parity. */ wait_CLOCK(1); /* Wait for end of clock pulse. */ value = value >> 1; } /* Shift right to get next bit. */ wait_CLOCK(0); /* Parity bit clock. */ set_DATA(p & 0x01); /* Output parity bit. */ wait_CLOCK(1); /* End of parity bit clock. */ wait_CLOCK(0); /* Stop bit clock. */ set_DATA(1); /* Stop bit must be 1. */ wait_CLOCK(1); /* End of stop bit clock. */ wait_CLOCK(0); /* Line control bit clock. */ if (read_DATA() == 1) PS2_error(); /* Missing line control bit! */ wait_CLOCK(1); /* End of line control bit clock. */ ack = PS2_get(); if (ack != 0xFA) /* Receive acknowledge byte from device. */ PS2_error(); /* Probably got an FE or FC error code. */ } ``` -------------------------------- ### Bootstrap VoodooInput.kext with curl command Source: https://github.com/acidanthera/voodoops2/blob/master/README.md Run in the project directory before compiling a custom VoodooPS2Trackpad.kext so the VoodooInput.kext dependency is bootstrapped. The Xcode project normally does this automatically; this command is only needed when you want manual control over the process. ```bash src=$(/usr/bin/curl -Lfs https://raw.githubusercontent.com/acidanthera/VoodooInput/master/VoodooInput/Scripts/bootstrap.sh) && eval "$src" || exit 1 ``` -------------------------------- ### PS/2 Low-Level I/O Primitives Source: https://github.com/acidanthera/voodoops2/blob/master/Docs/SynapticsRevB.pdf Declares the required low-level functions for reading and controlling the PS/2 clock and data pins, plus a delay function and an error handler. These are the building blocks for the PS/2 protocol implementation. ```c typedef unsigned char byte; extern byte read_CLOCK(); extern byte read_DATA(); extern void set_CLOCK(byte); extern void set_DATA(byte); extern void wait_us(byte); extern void PS2_error(); ``` -------------------------------- ### send_tp_arg helper function for sending 8-bit argument Source: https://github.com/acidanthera/voodoops2/blob/master/Docs/SynapticsRevB.pdf Sends an 8-bit argument encoded as a sequence of four Set Resolution commands (0xE8). The argument bits are sent in pairs, from most significant to least significant. This helper is used for TouchPad special command sequences described in section 4.2. ```C void send_tp_arg(byte arg) { byte i; for (i = 0; i < 4; i++) PS2_send(0xE8); PS2_send((arg >> (6-2*i)) & 3); } ``` -------------------------------- ### wait_CLOCK - Wait for PS/2 Clock Level Source: https://github.com/acidanthera/voodoops2/blob/master/Docs/SynapticsRevB.pdf Waits until the PS/2 clock pin reaches the desired state. In a real application, this should time out and call PS2_error if the level does not appear within about 25 ms. ```c void wait_CLOCK(byte state) { while (read_CLOCK() != state) /* Do nothing */; } ``` -------------------------------- ### PS2_send Source: https://github.com/acidanthera/voodoops2/blob/master/Docs/SynapticsRevB.pdf Sends a byte to the PS/2 device. It inhibits the bus, sends 8 data bits with parity, a stop bit, and then waits for the acknowledge byte from the device. ```APIDOC ## PS2_send ### Description Sends a byte to the PS/2 device. It inhibits the bus, sends 8 data bits with parity, a stop bit, and then waits for the acknowledge byte from the device. ### Method Function ### Endpoint PS2_send(byte value) ### Parameters #### Path Parameters - **value** (byte) - Required - The byte to send to the device. ### Request Example ``` PS2_send(0xF4); ``` ### Response #### Success Response - **ack** (byte) - The acknowledge byte received from the device (should be 0xFA). #### Response Example ``` 0xFA ``` ### Error Handling - Calls PS2_error() if the line control bit is missing or if the acknowledge byte is not 0xFA. ```