Skip to main content

Module i2c_tiny_usb

Module i2c_tiny_usb 

Source
Expand description

i2c-tiny-usb — a vendor-specific USB to I2C bridge.

Implements the protocol of Till Harbaum’s i2c-tiny-usb adapter, spoken by the Linux i2c-tiny-usb driver (drivers/i2c/busses/i2c-tiny-usb.c) and by the i2c_usb test tool shipped with the original firmware. Every transfer travels over the control pipe, so the function declares one vendor-specific interface and no endpoints at all.

§Protocol

bRequestwValuewIndexDataAnswer
cmd::ECHOvalue——the value bytes, for link testing
cmd::GET_FUNC———functionality as a little-endian u32
cmd::SET_DELAYbit delay in µs———
cmd::GET_STATUS———status of the last transfer
cmd::I2C_IO with flagsi2c_msg flags7-bit addressmessage bytesread data on reads

The host marks the first message of a transaction with cmd::I2C_IO_BEGIN and the last one with cmd::I2C_IO_END; a message without BEGIN continues the previous one with a repeated START, one without END leaves the bus busy. After every message the host reads GET_STATUS and gives up with -EREMOTEIO when it is status::ADDRESS_NAK.

The I2C address travels in wIndex, which normally holds the interface number, so requests are answered for any interface index. That matches the reference firmware, which is a single-function device. Vendor requests the adapter does not recognise are left alone, so it can coexist with another vendor-specific function.

§Supported transactions

  • one message — address, optional data, START and STOP
  • write followed by read — the repeated-start pattern that SMBus emulation uses, and therefore what i2cdetect, i2cget and i2cdump produce
  • several writes — concatenated into a single message

A hardware I2C peripheral cannot pause in the middle of a transaction, so the bytes of a message that does not end the transaction are buffered (up to PENDING_WRITE_MAX of them) and replayed together with the message that ends it. Reads must be the last message of a transaction, 10-bit addresses are not supported, SMBus block reads need the read phase to continue after the first byte (flags::RECV_LEN, so func::SMBUS_READ_BLOCK_DATA is not advertised either), and a transfer has to fit the 256-byte control buffer. Such requests are stalled instead of being silently mistranslated.

§Example

ⓘ
use my_hal_usb::{UsbBusAllocator, UsbDeviceBuilder};
use my_hal_usb::class::i2c_tiny_usb::I2cTinyUsb;
use my_hal_usb::MyBus;

let alloc = UsbBusAllocator::new(MyBus::new(/* peripheral */));
let mut adapter = I2cTinyUsb::new(&alloc, MyI2cBus::new());
let mut dev = UsbDeviceBuilder::new(&alloc, 0x0403, 0xC631)
    .device_release(0x0100)
    .manufacturer("harbaum.org")
    .product("i2c-tiny-usb")
    .build();

dev.bus().connect();
loop {
    dev.poll(&mut [&mut adapter]);
}

Modules§

cmd
Vendor control requests (bRequest).
flags
Message flags the host passes in wValue (I2C_M_* from include/uapi/linux/i2c.h).
func
Functionality bits reported by cmd::GET_FUNC (I2C_FUNC_* from include/uapi/linux/i2c.h).
status
Values returned by cmd::GET_STATUS.

Structs§

I2cTinyUsb
i2c-tiny-usb adapter: wraps an I2cBus and serves the vendor protocol.

Enums§

I2cError
Failure of an I2cBus operation.

Constants§

PENDING_WRITE_MAX
Bytes of a not-yet-finished write that may precede a repeated-start read.

Traits§

I2cBus
I2C master the adapter drives.