Lens Serial Interface

Nikon F-mount cameras use a serial protocol to communicate with attached lenses and gather basic lens information such as maximum aperture, current focal length, rough focus distance, etc. It is also used to control Vibration Reduction, Autofocus (for AF-I and AF-S lenses), and so on.

Although most Nikkor lenses use the F-mount's mechanical aperture control, E and PC-E lenses opt for purely electronic control, for example due to the difficulty of designing a mechanical coupling through a lens that tilts, shifts, and rotates. Commands to control the aperture on these types of lenses are also sent via the serial interface.

The pinout can be found on the F-Mount page.

=Electrical Interface= The lens serial interface is a variant of SPI with the following changes:
 * The Slave Select (SS) line is omitted.
 * SO and SI are tied together at each end to form the singular Data line (pin 4 on the F-Mount connector).
 * A handshake line is added. This line is sometimes referred to as R/W or In/Out.

The interface operates at 5V and uses 3 lines: The bus also has a "Power-Down" / "Sleep" state where all three lines are low.

=Protocol= Data is always sent one byte (8 bits) at a time, least significant bit first.

The serial clock starts out at 96kHz then negotiates to a higher speed if supported by the lens. 156kHz seems common among newer AF-S lenses, with older lenses staying at 96kHz (see command 0x40 below).

Command Sequence
The camera body always initiates communication by asserting (pulling low) the H/S line for 1600μs communicating with 96 KBps and 100μs for 156 KBps baudrate (time based on testing with D5100), then releasing it. The camera then waits for the lens to assert H/S in response, indicating the lens is ready to receive a command. If the lens does not respond within some timeout period, the command is aborted. Otherwise, seeing that the lens is ready, the camera clocks out the command byte. The lens continues to assert H/S throughout reception of the command byte, finally releasing it to acknowledge full receipt of the byte.

A command is only one byte, but may have data following it in either direction. Based on observation, it appears that the amount of data and direction of each data byte is fixed for a given command and is known by the camera and lens ahead of time. In other words, the protocol itself does not convey any information about the length or direction of data bytes.

If a command has associated data, the lens asserts H/S when it is ready to receive or transmit the next byte, releasing H/S to acknowledge completion of the byte just like with the initial command byte.

Each side can cancel communication attempt: lens just do not assert H/S line anymore or camera by do not supplying clock signal for next byte to be received/transmitted. After 5ms without any bytes sending/receiving communication attempt considered to be aborted by both sides. Afterwards, communication attempt for next command can be started by camera.

Against some web sources and patents, it was found that modern lenses do not send byte 0xFF back if command is not supported. And it is not expected/checked in camera. Unknown commands are aborted by 5ms silence without asserting H/S line (Pin 2) as described above.

Lens commands
Most answers come from Nikkor 35mm 1.8/G. Command bytes are send/receive in the same order as they are listed in this table.

Misc Notes
Some older Nikon cameras may expect a lens responds to unrecognized commands with a single 0xFF byte. But modern cameras like D5100/D800 do not check this, expecting abort on timeout instead. It was even observed that RokiBowYang 8 f3.5 HD Fisheye responds with 0xFF on command 0x40, but send no any byte and aborts on timeout on command 0x28.

The camera is able to terminate commands early, such as if only the first few bytes of a lens response are needed, by simply not toggling SCLK after the lens asserts H/S to indicate readiness for the next byte. After 5ms, the lens MCU will timeout and abort the command, returning to idle state. Provided the escape time is less than the time required to transmit the remaining bytes, this can be used to speed up bus transfers.

=References=
 * US4896181
 * Lots of captured data, see this topic.