This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Linux/AM3351: SMBus support on I2C

Part Number: AM3351

Tool/software: Linux

Hi,

the usb hub 2517 supports only smbus block read/write.

i am using below application, but write/read not happning.

/*
 * Simple I2C example
 *
 * Copyright 2017 Joel Stanley <joel@jms.id.au>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <err.h>
#include <errno.h>
#include <linux/types.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,
                                     int size, union i2c_smbus_data *data)
{
    struct i2c_smbus_ioctl_data args;

    args.read_write = read_write;
    args.command = command;
    args.size = size;
    args.data = data;
    return ioctl(file,I2C_SMBUS,&args);
}


static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command)
{
    union i2c_smbus_data data;
    if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
                         I2C_SMBUS_BYTE_DATA,&data))
        return -1;
    else
        return 0x0FF & data.byte;
}

static inline __s32 i2c_smbus_read_block_data(int file, __u8 command,
                                              __u8 *values)
{
    union i2c_smbus_data data;
    int i;
    if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
                         I2C_SMBUS_BLOCK_DATA,&data))
        return -1;
    else {
        for (i = 1; i <= data.block[0]; i++)
            values[i-1] = data.block[i];
            return data.block[0];
    }
}

static inline __s32 i2c_smbus_write_block_data(int file, __u8 command,
                                               __u8 length, __u8 *values)
{
    union i2c_smbus_data data;
    int i;
    if (length > 32)
        length = 32;
    for (i = 1; i <= length; i++)
        data.block[i] = values[i-1];
    data.block[0] = length;
    return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
                            I2C_SMBUS_BLOCK_DATA, &data);
}


int main(int argc, char **argv)
{
    uint8_t data=0x00, addr = 0x00,reg = 0x00;
    __u8 read_data[32]={0x00},write_data[32]={0x00},offset=0;
    const char *path = argv[1];
    int file, rc,i=0,r_w=0;

    if (argc == 1)
        errx(-1, "path [i2c address] [register]");

    if (argc > 2)
        addr = strtoul(argv[2], NULL, 0);
    if (argc > 3)
        reg = strtoul(argv[3], NULL, 0);

    file = open(path, O_RDWR);
    if (file < 0)
        err(errno, "Tried to open '%s'", path);

    rc = ioctl(file, I2C_SLAVE, addr);
    if (rc < 0)
    {
        printf("slave not detected\n");
            close(file);
        return 0;    
    }
        printf("please enter 0/1 for read/write \n");
        scanf("%d",&r_w);
        if(r_w==1)
        {

      for(i=0;i<32;i++)
      {
            write_data[i] = (__u8)i;
          }
      rc=i2c_smbus_write_block_data(file,reg,8,write_data);
          if(rc<0)
            printf("write failed\n");
        }
        //data = i2c_smbus_read_byte_data(file, reg);
        //printf("%s: device 0x%02x at address 0x%02x: 0x%02x\n",    path, addr, reg, data);
        else
        {
      rc=i2c_smbus_read_block_data(file, reg, read_data);
     //rc=i2c_smbus_read_byte_data(file,reg);
          if(rc<0)
          {
            printf("read failed\n");     
          }
          else
          {
       // printf("Value returned from read%x\n",rc);
            for(i=0;i<32;i++)
             printf("%x ",read_data[i]);
            printf("\n");
          }
        }
        close(file);

}

///opt/sdk_ti/linux-devkit/sysroots/x86_64-arago-linux/usr/bin/arm-linux-gnueabihf-gcc /opt/sdk_ti/hub_config.c -o hub_block_read_2c

//www.linux-mag.com/.../

   

  • Which linux SDK is this?

    Best Regards,
    Yordan
  • hi yordan, thanks for your reply.
    i am using "ti-processor-sdk-linux-am335x-evm-03.00.00.04"
  • Hi,

    Can you share your defconfig & dts/dtsi files?

    Best Regards,
    Yordan
  • hi please check attachment.dts.tar.gz

  • Hi,

    Your I2C interfaces are set correctly. Can you verify that you see the I2C interfaces in /dev?

    If yes, then can you try accessing the I2C2 (I assume you're accessing the usb hub on i2c2) using the command line, see www.acmesystems.it/user_i2c. Use i2c_detect, i2c_get & i2c_set. After that try the simple read/write programs from the same link, to verify the i2c2 interface is correctly set up by the kernel.

    Then in your program, I don't see an i2c address set. You define uint8_t data=0x00, addr = 0x00,reg = 0x00; and in the kernel you define:
    lm75@48, usb2517@2C, tps@24 & goodix_ts@14, but in your user space program you never reference any of these addresses.

    EDIT: Also the kernel documentation explains quite well how you can work with I2C devices:
     

    Best Regards,
    Yordan

  • Hi yordan,

     my slaves address 0x2c, using i2cget i am reading data, i2cset was not returning any error, but write was not happend.

    please ckeck below logs.

    root@am335x-evm:~# ls /dev/i2c-
    i2c-0  i2c-2  
    root@am335x-evm:~# i2cdetect -y 2
    Error: Can't use SMBus Quick Write command on this bus
    root@am335x-evm:~# i2cdetect -r 2                                                                                                              
    WARNING! This program can confuse your I2C bus, cause data loss and worse!
    I will probe file /dev/i2c-2 using read byte commands.
    I will probe address range 0x03-0x77.
    Continue? [Y/n] Y
         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- -- --                         

    root@am335x-evm:/home# i2cdump -f -y 2 0x2c  // it reads entire chip

    No size specified (using byte-data access)
         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
    00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    40: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    e0: 20 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11     ???????????????
    f0: 10 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01    ????????????????


    root@am335x-evm:/home# i2cget -y 2 0x2c 0xff b //read from 0xff offset
    0x01
    root@am335x-evm:/home# i2cget -y 2 0x2c 0x00 b    //read from 0x00 offset                                                                                         
    0x20


    root@am335x-evm:/home# i2cset -f -y 2 0x2c 0xff 2    //write 2 0xff offset                                                
    root@am335x-evm:/home# i2cdump -f -y 2 0x2c
    No size specified (using byte-data access)
         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
    00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    40: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                    
    e0: 20 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11     ???????????????
    f0: 10 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01    ????????????????

  • Hi,
    Can you try the proposal in this thread:
    e2e.ti.com/.../379901

    Best Regards,
    Yordan