Lens Serial Interface

From Nikon Hacker
Jump to: navigation, search

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:

Pin Line Full Name Electrical Description
2 H/S Handshake Open-collector, pulled high. Can be asserted by lens and camera simultaneously.
3 SCLK Serial Clock Unidirectional, driven by camera body.
Not free-running, toggles only when bits are on the wire as in SPI. Idles high.
4 DATA Data Bidirectional, data is clocked in on the rising edge of SCLK.
The receiver's SO (Serial Out) pin goes high impedance when it expects data.
Lenses contain a series protection resistor in case both body and lens try to transmit simultaneously.

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

A typical command with data.

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.

Command (hex) ->Lens Parameters (hex) ->Lens Answer (hex) <-Lens Extra parameters (hex) ->Lens Description
40 00 00 01 04 00 00 01 02 1A Request lens capabilities and set mode (D5100: 02 1A, D800: 02 1B). If this command was communicated successfully, D5100 sets baud rate up to 156 KBps. Otherwise baud rate stays at 96 KBps.
41 02 1A Returns extra parameters send before with command 40h
22 2A 7C 00 C4 FC 17 1A 00 00 35 2B 20 8A 00 00 44 2A 02 42 44 5C 2A 34 08 2A Lens ID information request. Old version, non-D lens. Data return 25 bytes.
27 23 10 54 00 F5 FA 13 0A 07 08 02 20 60 7E 00 00 11 0C 4A 40 11 11 2C 0C 4D 2C 00 FF FF FF FE FE 09 39 2E 3B 00 01 04 FD Request lens status/ID information. Third party AE lens from Samyang.
28 10 14 ff ff cc fa 07 03 03 8d 04 00 40 6c f9 fb 44 14 9f 58 44 44 14 14 a1 14 00 13 5e 08 07 f8 06 43 2b 44 00 01 00 fa 00 00 45 04 Request lens status/ID information. New version. At least one such command must be send to lens or lens may not react on any other commands.

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