libi2c

repository·master·Indexed 41 days ago

https://github.com/amaork/libi2c

A Linux userspace library providing C/C++ and Python bindings (pylibi2c) for interacting with I2C devices. It supports 10-bit addressing, variable internal address lengths, page-aligned I/O, and the ability to ignore NAK signals via ioctl-based functions.

Tokens
1.9K
Snippets
5
Records
7
Agent score
2%

What's inside libi2c

  1. Install pylibi2c

    master

    You can install the Python bindings for libi2c using pip or by building from source using make.

    To install via pip:

    pip install pylibi2c

    To install from source:

    sudo python setup.py install
    # OR
    sudo make install
    # OR
    sudo make install PYTHON=pythonX.X
    pip install pylibi2c
  2. Ignore I2C NAK signals

    master

    To ignore the I2C device NAK (Not Acknowledge) signal, you must use the ioctl based functions and set the appropriate flag.

    In C/C++: Set device.flags = I2C_M_IGNORE_NAK; and use i2c_ioctl_read or i2c_ioctl_write.

    In Python: Set i2c.flags = pylibi2c.I2C_M_IGNORE_NAK and use i2c.ioctl_read() or i2c.write() (note: the Python write method uses ioctl internally for efficiency).

  3. How to use pylibi2c in Python

    master

    Using pylibi2c involves creating an I2CDevice instance and then calling its methods. You can adjust device parameters like delay, page_bytes, and flags as attributes.

    Example: Configuring a device and performing I/O

    import pylibi2c
    
    # Open i2c device @/dev/i2c-0, addr 0x50 with 16-bit internal address
    i2c = pylibi2c.I2CDevice('/dev/i2c-0', 0x50, iaddr_bytes=2)
    
    # Configure device parameters
    i2c.delay = 10
    i2c.page_bytes = 16
    
    # To ignore NAK signals, set the flag
    i2c.flags = pylibi2c.I2C_M_IGNORE_NAK
    
    # Prepare buffer (Python 3 style)
    buf = bytes(256)
    
    # Write data to internal address 0x0
    size = i2c.write(0x0, buf)
    
    # Read 256 bytes from internal address 0x0 using ioctl_read
    data = i2c.ioctl_read(0x0, 256)
  4. How to use libi2c in C/C++

    master

    To use libi2c in C/C++, follow these steps:

    1. Open the bus using i2c_open.
    2. Initialize the I2CDevice struct with the bus fd, slave address, and device-specific parameters (like iaddr_bytes and page_bytes).
    3. Perform I/O using i2c_read/i2c_write or i2c_ioctl_read/i2c_ioctl_write.
    4. Close the bus using i2c_close.

    Example: Reading 256 bytes from a 24C04 device

    int bus;
    if ((bus = i2c_open("/dev/i2c-0")) == -1) {
        /* Error process */
    }
    
    I2CDevice device;
    memset(&device, 0, sizeof(device));
    device.bus = bus;
    
    /* 24C04 configuration */
    device.addr = 0x50;
    
    /* 7-bit slave address, 1 byte internal address, 16 bytes per page */
    device.iaddr_bytes = 1;
    device.page_bytes = 16;
    
    unsigned char buffer[256];
    memset(buffer, 0, sizeof(buffer));
    ssize_t size = sizeof(buffer);
    
    /* Read 256 bytes from internal address 0x0 */
    if ((i2c_read(&device, 0x0, buffer, size)) != size) {
        /* Error process */
    }
    
    i2c_close(bus);
  5. Python API Reference

    master

    The pylibi2c module provides a Pythonic interface to the I2C library.

    I2CDevice Class

    Initialize a device object: I2CDevice(bus, addr, tenbit=False, iaddr_bytes=1, page_bytes=8, delay=1, flags=0)

    Arguments:

    • bus (required): The bus path (e.g., '/dev/i2c-0').
    • addr (required): The I2C slave address.
    • tenbit (optional): True for 10-bit addressing, False for 7-bit (default: False).
    • iaddr_bytes (optional): Number of bytes for the internal address (default: 1).
    • page_bytes (optional): Max bytes per page (default: 8).
    • delay (optional): Internal operation delay in ms (default: 1).
    • flags (optional): I2C flags (default: 0).

    Attributes (Setter/Getter): tenbit, delay, flags, page_bytes, and iaddr_bytes can be modified after initialization.

    Methods

    • write(iaddr, buf): Writes data to the device at the specified internal address iaddr. buf must be a read-only type.
    • ioctl_read(iaddr, len): Performs an ioctl read from the internal address iaddr for len bytes.
    import pylibi2c
    
    # Initialize
    i2c = pylibi2c.I2CDevice('/dev/i2c-0', 0x50, iaddr_bytes=2)
    
    # Configure attributes
    i2c.delay = 10
    i2c.page_bytes = 16
    i2c.flags = pylibi2c.I2C_M_IGNORE_NAK
    
    # Write (buf must be read-only)
    size = i2c.write(0x0, b'data')
    
    # Read
    data = i2c.ioctl_read(0x0, 256)
  6. C/C++ API Reference

    master

    The C/C++ interface provides functions for managing the I2C bus and performing I/O operations on specific devices.

    Bus Management

    • int i2c_open(const char *bus_name): Opens the specified I2C bus (e.g., "/dev/i2c-0") and returns the file descriptor (fd). Returns -1 on error.
    • void i2c_close(int bus): Closes the specified I2C bus using its file descriptor.

    I/O Operations

    • ssize_t i2c_read(const I2CDevice *device, unsigned int iaddr, void *buf, size_t len): Standard I2C read. Uses the device's internal address.
    • ssize_t i2c_write(const I2CDevice *device, unsigned int iaddr, const void *buf, size_t len): Standard I2C write. Uses the device's internal address.
    • ssize_t i2c_ioctl_read(const I2CDevice *device, unsigned int iaddr, void *buf, size_t len): I2C read using ioctl. Can be used to set I2C flags (e.g., ignoring NAK) and is useful if the device has no internal address.
    • ssize_t i2c_ioctl_write(const I2CDevice *device, unsigned int iaddr, const void *buf, size_t len): I2C write using ioctl. More efficient than i2c_write for transfers up to 16 bytes and allows setting flags.
  7. I2CDevice struct (C/C++)

    master

    The I2CDevice structure defines the configuration for a specific I2C slave device.

    FieldTypeDescription
    busintI2C Bus fd, returned from i2c_open
    addrunsigned shortI2C slave address
    tenbitunsigned charSet to non-zero if the device uses a 10-bit address
    delayunsigned charInternal operation delay in milliseconds
    flagsunsigned shortFlags used by i2c_ioctl_read/write
    page_bytesunsigned intMax bytes per page (e.g., 16, 32, 64)
    iaddr_bytesunsigned intNumber of bytes used for the device's internal (word) address (e.g., 1 for 24C04, 2 for 24C64)

    Note: If the device does not have an internal address, set iaddr_bytes = 0 and use the ioctl functions.

    typedef struct i2c_device {
    	int bus;
    	unsigned short addr;
    	unsigned char tenbit;
    	unsigned char delay;
    	unsigned short flags;
    	unsigned int page_bytes;
    	unsigned int iaddr_bytes;
    } I2CDevice;