How does a USB keyboard work?
The PS2 Keyboard Interface
This section introduces the PS2 keyboard interface and its simple protocol.
PS2 Keyboard Interface
- The old PS2 style keyboard interface uses only four pins, with two for ground and five volts to provide power to the keyboard.
- Two additional pins are used for communication signals when a key is pressed or released.
- The protocol consists of bursts of data sent on these two signals, including clock pulses, start bit, 8 bits of data, parity bit, and stop bit.
Latency Comparison between PS2 and USB Keyboards
This section discusses the anecdotal claim that PS2 keyboards have lower latency compared to USB keyboards.
Latency Comparison
- Due to the simplicity of the PS2 protocol, it is plausible that PS2 accessories may have lower latency than USB devices.
- Further investigation is needed to compare the complexity and latency of USB gadgets using an oscilloscope.
USB Keyboard Interface
This section introduces the USB keyboard interface and its pin configuration.
USB Keyboard Interface
- A standard USB 2 connection for a keyboard also uses four pins - ground, five volt power, and two data signals (D+ and D-).
- Unlike separate clock and data pins in the PS2 interface, USB utilizes a differential pair for communication.
- The signaling levels for D+ and D- determine different scenarios based on their high or low states.
Signaling Levels in USB Communication
This section explains the different signaling levels in USB communication based on D+ and D- states.
Signaling Levels
- There are four possible scenarios for D+ and D- states: differential one (D+ high, D- low), differential zero (D+ low, D- high), single-ended zero (both low), and single-ended one (both high).
- When a keyboard is connected, the initial state is a differential zero, indicating an idle state for low-speed communication.
USB Speed Modes
This section discusses the different speed modes in USB communication and their corresponding signaling levels.
USB Speed Modes
- USB operates at different speeds depending on the device connected.
- Low-speed communication has an idle state where D+ is low and D- is high.
- Full-speed communication has the opposite idle state with D+ high and D- low.
- After connecting at full speed, a device can switch to high speed or even higher speeds with USB 3.
Differentiating Keyboard Speed Modes
This section explains how keyboards indicate their speed mode through signaling levels.
Keyboard Speed Modes
- A keyboard signals its speed mode by pulling either D+ or D- to a specific level.
- A keyboard that pulls D- high indicates it is a low-speed device.
- A keyboard that pulls D+ high indicates it is a full-speed device.
Communication Initiation in USB Keyboards
This section clarifies that USB keyboards rely on the computer to initiate communication.
Communication Initiation
- In USB communication, all communication is initiated by the computer rather than the keyboard itself.
- The only information volunteered by the keyboard is its desired speed mode (low or full).
Analyzing USB Keyboard Communication
This section demonstrates analyzing USB keyboard communication using an oscilloscope.
Analyzing Communication
- By connecting the USB keyboard to an oscilloscope, we can observe the data communication signals (D+ and D-) when the keyboard is plugged in.
- Capturing screenshots of the waveform allows for detailed analysis and understanding of the communication process.
The transcript does not provide further information beyond this point.
New Section
In this section, the speaker discusses the differential nature of the signal and identifies different states observed in the data.
Understanding the Signal
- The signal is mostly differential, with D+ and D- being opposite to each other.
- Differential zero states correspond to one state, while single-ended zero states indicate a different state.
- Single-ended zeros are observed at specific points in the data, suggesting the presence of multiple packets.
New Section
This section focuses on interpreting packet information based on specific patterns observed in the data.
Packet Structure and Interpretation
- The end of a packet is indicated by a single-ended zero for approximately two bit times followed by a J state for one bit time.
- J represents a low-speed differential zero, while K represents its opposite.
- The idle state is represented by J, and when a packet starts, it transitions from idle to K state.
- Clock recovery is necessary to interpret the data since it is sent as a differential pair without a separate clock line.
New Section
Here, we explore why a differential pair is used for transmitting data and how it offers advantages over other methods.
Advantages of Differential Pair
- Using a differential pair helps avoid susceptibility to interference as any electromagnetic interference affects both signals equally.
- By subtracting one signal from another, interference can be canceled out during reception.
- Differential pairs also help reduce emissions by canceling out radio interference caused by high-frequency signals transmitted through wires.
New Section
This section delves into inferring clock information from transition intervals and determining the number of bits in each period.
Inferring Clock Information
- Clock inference becomes crucial due to using differential data transmission without a separate clock line.
- The clock speed is known to be approximately 1.5 megabits per second due to the idle state.
- Clock information is inferred by analyzing transition intervals and determining the number of bits in each period.
New Section
The final section discusses completing the interpretation of all bits and understanding their meaning.
Completing Interpretation
- Since the remaining signal is differential, we need to determine whether each bit is a J or K state.
- By decoding all the bits and understanding their meaning, we can fully interpret the data transmitted through the USB interface.
Understanding Packet Structure
This section explains the structure of packets in USB communication.
Packet End and Start
- A packet's end is signaled by a single-ended zero for approximately two bit times, followed by a J state for at least one bit time.
- The start of a packet is indicated when the data line switches from idle to a K state.
Sync Pattern
- Every packet starts with a sync pattern, which consists of three KJ pairs followed by two Ks, totaling eight symbols.
- The sync pattern helps synchronize the clock and determine bit timings.
NRZI Encoding
- The first few bits of the packet indicate the packet ID or type.
- The encoding used is NRZI (Non-Return to Zero Inverted), where a one is represented by no change in level and a zero is represented by a change in level.
- Transition between different voltage levels is easier to detect than measuring exact voltage levels at high speeds.
Bit Stuffing
- To ensure adequate signal transitions and prevent loss of bit count, USB protocol uses bit stuffing.
- Whenever there are six consecutive ones in the data stream, a zero is forced into the stream as an additional transition.
Decoding Example
- An example decoding shows how NRZI encoding works for the first four bits of a packet.
- No change in level (K to K) represents a one.
- Change in level (K to J) represents a zero.
- Change in level (J to K) also represents a zero.
Least Significant Bit First
- USB transmits least significant bit first, so if we read the decoded bits normally, it would be "1001".
- The first four bits indicate the packet ID, which can be looked up in the specification document. In this case, it corresponds to an IN packet.
Importance of Sync Pattern and Clock Recovery
This section highlights the importance of the sync pattern in USB communication and clock recovery.
Sync Pattern for Clock Recovery
- The sync pattern with KJ pairs provides a reliable oscillating signal that helps recover the clock.
- It allows counting the number of bits accurately, even in situations with longer periods of time between transitions.
Challenges with Encoding Clock and Data Together
- Encoding clock and data using the same signal can lead to difficulties in maintaining bit count when there are no level transitions for a period of time.
- Loss of track or miscounting can occur if clocks are not perfectly synchronized.
Bit Stuffing for Signal Transitions
- Bit stuffing is used to ensure adequate signal transitions, especially during long periods without transitions.
- In the example, seven consecutive bit times without a transition indicate six ones followed by a stuffed zero (transition).
Decoding Packet ID
This section explains how to decode the packet ID in USB communication.
NRZI Encoding for Packet ID
- The packet ID is not encoded using a direct mapping from bits to values.
- NRZI encoding is used, where a one is represented by no change in level and a zero is represented by a change in level.
Example Decoding
- An example decoding shows how NRZI encoding works for four bits representing the packet ID.
- Transition (K to J) represents a zero.
- No transition (K to K) represents a one.
Least Significant Bit First
- USB transmits least significant bit first, so if we read the decoded bits normally, it would be "0110".
- The decoded packet ID can be looked up in the specification document.
Understanding the Binary Structure of a Token Packet
In this section, we explore the binary structure of a token packet and its components.
Token Packet Structure
- A token packet consists of:
- 8-bit packet ID
- 7-bit address
- 4-bit endpoint
- 5-bit CRC (Cyclic Redundancy Check)
- The CRC checks the address and endpoint, but not the packet ID.
Decoding the Address and Endpoint
- The next 7 bits represent the address.
- The following 4 bits represent the endpoint.
- The remaining 5 bits are for CRC.
Converting Binary to Decimal
- To convert binary numbers to decimal, reverse the order of bits.
- Example:
0011000in reversed order is0001100, which is equivalent to decimal number12.
Decoding an In Command Token Packet
- We have decoded an in command token packet with address
24and endpoint1.
- This indicates that it is a request from the computer to receive data from device
24(a keyboard) at endpoint1.
Decoding Data Packets
Here, we decode a data packet received as a response to the in command token packet.
Identifying Data Packets
- The packet ID for data packets is
1100in binary orCin hexadecimal.
- The data itself consists of multiple bytes.
Analyzing Zero Data Values
- In this specific data packet, all values are zeros due to NRZI encoding where signal transitions represent zeros.
- Breaking down into bytes, we have eight consecutive zero bytes.
Bit Stuffing and Transition Interpretation
- After six consecutive ones, we encounter a transition that is ignored due to bit stuffing.
- The following
01represents the start of data.
Data Packet Format
- Data packets consist of an 8-bit packet ID, followed by the data itself (up to 8192 bits), and a 16-bit CRC for error checking.
Calculating the CRC
- The CRC for all zeros in this case is
F4BFin hexadecimal.
- This matches the expected CRC value for CRC16 USB.
Decoding Sync Packets
Here, we decode a sync packet and analyze its components.
Analyzing Sync Packets
- The packet ID for sync packets is
0100in binary or4in hexadecimal.
- Sync packets are used for synchronization purposes.
Identifying Transitions
- Analyzing the transitions within the sync packet, we observe multiple transitions and non-transitions.
- The pattern
0010corresponds to the packet ID of this sync packet.
Computer Keyboard Communication
This section explores the communication between a computer and a keyboard using USB signals. The transcript discusses how the computer pulls input from the keyboard, the data packets exchanged, and the handling of modifier keys.
Understanding Keyboard Communication
- The computer and keyboard use the same signal lines for communication.
- The computer requests input from the keyboard at endpoint 1, which corresponds to key presses.
- The keyboard responds with eight bytes of zeros when no keys are pressed.
- A CRC check is performed on this data by the keyboard.
- The computer acknowledges receipt of the data.
Observing Key Presses
- When pressing keys on the keyboard, bits in the data packet change.
- Capturing and decoding these packets manually can be challenging.
- Specialized oscilloscopes can automatically decode USB signals.
- By setting up serial decoding, changes in bits can be observed when pressing keys.
Different Types of Transactions
- Two types of transactions occur: full report and smaller transaction.
- Full report includes an in token packet, data packet, and acknowledgement.
- Smaller transaction includes an in token packet followed by a negative acknowledgement (NACK).
- NACK indicates that no new keys have been pressed or released.
Filtering Transactions with Acknowledgements
- It is possible to set up a trigger to only see transactions with acknowledgements.
- By configuring the trigger type as USB and selecting handshake packet and acknowledgement, only relevant transactions are displayed.
Modifier Keys and Key Codes
- Modifier keys such as Ctrl, Shift, Alt, and Windows key are handled differently.
- Each modifier key corresponds to a specific bit in the first byte of the data packet.
- Ctrl: 1
- Shift: 2
- Alt: 4
- Windows key: 8
- Holding down multiple modifier keys represents a combination of their values.
- The next byte is reserved and always zero.
- The remaining six bytes represent the keys that are pressed, with up to six keys being reported.
Limitations of Key Detection
- If more than six keys are pressed simultaneously, there is no way to detect them individually.
- Smaller combinations of keys may also result in errors due to shared connections in the keyboard matrix.
The transcript has been summarized and organized into key topics for easier study.
Initial Setup and USB Communication
This section focuses on the initial setup process when plugging in a keyboard and the USB communication that occurs.
Setting up the Keyboard
- The speaker sets up the environment to capture the initial setup process of a keyboard.
- A lister is brought up to easily observe what is happening during the setup.
USB Communication
- Upon plugging in the keyboard, a setup packet is sent followed by data communication with address 1e, which represents the endpoint assigned for the keyboard.
- There is further back and forth communication with input for address 1e and endpoint zero, which is used for setup and configuration parameters.
- The transferred data includes vendor ID (413c) corresponding to Dell, indicating that the keyboard was manufactured by Dell.
- Additional negotiation takes place before key presses can be sent between the keyboard and computer.
Latency Comparison: USB vs PS2
This section discusses whether USB introduces any significant latency compared to PS2 interface.
Data Transmission Comparison
- While PS2 keyboards send only 11 bits per key press at 16 kilobits per second, USB keyboards can transmit significantly more data at either 1.5 or 12 megabits per second depending on speed.
- However, there is a difference in how quickly key presses are transmitted:
- PS2 keyboards immediately send scan codes upon key press.
- USB keyboards require polling from the computer through in token packets, which occur approximately every 16 milliseconds.
Latency Analysis
- The serialization delay for sending an 11-bit scan code at 16 kilobits per second via PS2 is around 0.7 milliseconds.
- In contrast, due to polling every 16 milliseconds with USB keyboards, there may be a delay of up to 16 milliseconds before key presses are sent to the computer.
- This results in almost 20 times more latency with USB compared to PS2.
- However, it is important to note that this analysis is based on a low-speed USB keyboard.
Latency Comparison: USB Low-Speed vs USB Full-Speed
This section explores whether there is any practical advantage of using PS2 over USB when considering higher-quality keyboards operating at full speed.
Full-Speed Keyboard Behavior
- A better quality keyboard operating at full speed is connected for comparison.
- Unlike the periodic data transmission observed with the low-speed USB keyboard, this keyboard only sends data if there is something to report. Otherwise, it responds with a NACK (negative acknowledgment).
Polling Frequency Analysis
- The full-speed USB keyboard polls every 1 millisecond, as observed from the triggering and data shown on the scope.
- Comparing this polling frequency to the 16-millisecond polling interval of low-speed USB keyboards, the difference becomes significant.
- The serialization delay advantage of PS2 keyboards (0.7 milliseconds) over full-speed USB keyboards (1 millisecond) diminishes due to similar order-of-magnitude delays.
Conclusion
Based on the analysis conducted in this video, it can be concluded that there is no practical advantage of using PS2 over USB unless using extremely cheap keyboards. The higher data transmission rate and comparable polling frequencies make USB a viable option even for high-quality keyboards.
The transcript provided does not cover all parts of the video.
New Section Focusing on USB Keyboard and Website
This section discusses the USB keyboard and the importance of waiting for the next poll interval. It also mentions the website.
USB Keyboard and Poll Interval
- Waiting for the next poll interval from the USB keyboard is crucial.
- The importance of understanding that waiting for the next poll interval is similar to previous concepts.
- Appreciation expressed towards patrons for supporting the video through Patreon.
Website
- Encouragement to visit the speaker's website.
Timestamps are not available for this section.
Turn any video into a summary like this
YouTube links, meetings, lectures. With transcripts, search, and chat.