cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
x_abacadaba
Observer
Observer
449 Views
Registered: ‎12-21-2017

petalinux libi2c link failure

Jump to solution

I am using vivado/petalinux 2018.3.

I recently attempted to make use of the i2c smbus protocol from user-mode, as described in the kernel documentation here:
https://github.com/Xilinx/linux-xlnx/blob/xilinx-v2018.3/Documentation/i2c/dev-interface

which clearly states that I should be able to access the the i2c and smbus api from user-mode by including <linux/i2c-dev.h>.  the make file looks like this:

APP = testapp
LIBS = -lpthread -li2c
# Add any other object files to this list below
APP_OBJS = testapp.o
all: build
build: $(APP)
$(APP): $(APP_OBJS)
	$(CC) $(LDFLAGS) -o $@ $(APP_OBJS) $(LIBS) $(LDLIBS)
clean:
	rm -rf *.o $APP

(I've also tried including <linux/i2c.h> and <i2c/smbus.h>)  i2c.h has no effect, and smbus.h causes a "file not found error"

I've also added this to my .bb file though I don't think its necessary:

DEPENDS += "i2c-tools"

i2c-tools are also enabled in my rootfs-config, though I also think that is unrelated to this issue as well. 

I receive the following compile error indicating that libi2c does not exist???

| testapp.c:23:5: warning: implicit declaration of function 'i2c_smbus_read_word_data' [-Wimplicit-function-declaration]
|      i2c_smbus_read_word_data(fd, 5);

and the link error:

 /home/engineer/smbtest-proj/build/tmp/work/aarch64-xilinx-linux/testapp/1.0-r0/recipe-sysroot-native/usr/bin/aarch64-xilinx-linux/../../libexec/aarch64-xilinx-linux/gcc/aarch64-xilinx-linux/7.3.0/ld: cannot find -li2c


So I'm wondering is this library omitted from the cross compiler toolchain for petalinux?  If so does anyone know a quick way to fix this?
Thanks

 

0 Kudos
1 Solution

Accepted Solutions
sandeepg
Moderator
Moderator
294 Views
Registered: ‎04-24-2017

Hi @x_abacadaba ,

This is based on 2018.x release.

  1. Create a user space application from petalinux tools.
    $ petalinux-create -t apps -n i2c-test --template c --enable
    $ tree project-spec/meta-user/recipes-apps/i2c-test/
    project-spec/meta-user/recipes-apps/i2c-test/
    ├── files
    │   ├── i2c-test.c
    │   └── Makefile
    ├── i2c-test.bb
    └── README
     
    1 directory, 4 files
    $​
  2. Modify the c application and recipes as shown below.

    $ vim project-spec/meta-user/recipes-apps/i2c-test/files/i2c-test.c
     
     
    /*
    * Copyright (C) 2013 - 2016  Xilinx, Inc.  All rights reserved.
    *
    * Permission is hereby granted, free of charge, to any person
    * obtaining a copy of this software and associated documentation
    * files (the "Software"), to deal in the Software without restriction,
    * including without limitation the rights to use, copy, modify, merge,
    * publish, distribute, sublicense, and/or sell copies of the Software,
    * and to permit persons to whom the Software is furnished to do so,
    * subject to the following conditions:
    *
    * The above copyright notice and this permission notice shall be included
    * in all copies or substantial portions of the Software.
    *
    * Use of the Software is limited solely to applications:
    * (a) running on a Xilinx device, or (b) that interact
    * with a Xilinx device through a bus or interconnect.
    *
    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    * IN NO EVENT SHALL XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    *
    * Except as contained in this notice, the name of the Xilinx shall not be used
    * in advertising or otherwise to promote the sale, use or other dealings in this
    * Software without prior written authorization from Xilinx.
    *
    */
     
    #include <errno.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    /* If you are using x86 host machine then header should be i2c-dev.h
     * http://cgit.openembedded.org/openembedded-core/tree/meta/recipes-devtools/i2c-tools/i2c-tools_3.1.2.bb?h=rocko
     */
    #include <linux/i2c-dev-user.h>
    #include <sys/ioctl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
     
    static void usage() {
        printf("Usage:\n");
        printf("\targv[1]: I2CBUS ID is an integer got it from `i2cdetect -l` command \n");
        printf("\targv[2]: I2C slave device address is an integer detected from `i2cdetect -y <bus_id>` command \n");
        printf("\targv[3]: I2C device register address is an integer detected from `i2cget -f -y <bus_id> <reg_addr> b` command \n");
        printf("\targv[4]: Number of bytes to be read start from argv[3] I2C device register address \n");
    }
     
    int main (int argc, char **argv) {
        int file, i2c_bus_id, i2c_addr, res, i2c_reg, i, nbytes;
        char filename[40];
      
        if (argc <= 4) {
            usage();
            return -1;
        }
     
        sscanf(argv[1], "%d", &i2c_bus_id);
        sscanf(argv[2], "%x", &i2c_addr);
        sscanf(argv[3], "0x%02x\n", &i2c_reg);
        sscanf(argv[4], "0x%x\n", &nbytes);
     
        snprintf(filename, 39, "/dev/i2c-%d", i2c_bus_id);
     
        if ((file = open(filename,O_RDWR)) < 0) {
            printf("Failed to open the bus.");
            /* ERROR HANDLING; you can check errno to see what went wrong */
            exit(1);
        }
     
        if (ioctl(file, I2C_SLAVE, i2c_addr) < 0) {
            printf("Failed to acquire bus access and/or talk to slave %s\n", strerror(errno));
            exit(1);
        }
     
        /* Using SMBus commands */
        for (i=i2c_reg; i <= (i2c_reg + nbytes); i++) {
            res = i2c_smbus_read_byte_data(file, i);
            if (res < 0) {
                /* ERROR HANDLING: i2c transaction failed */
                printf("Failed to read the transcation %s\n", strerror(errno));
            } else {
                /* res contains the read word */
                printf("I2C bus id: [%d] slave addr: [0x%02x] and reg offset: [0x%02x] value = 0x%02x\n", i2c_bus_id, i2c_addr, i, res);
            }
        }
        
        return 0;
    }
    $ vim project-spec/meta-user/recipes-apps/i2c-test/i2c-test.bb
     
    #
    # This file is the i2c-test recipe.
    #
     
    SUMMARY = "Simple i2c-test application"
    SECTION = "PETALINUX/apps"
    LICENSE = "MIT"
    LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
     
    SRC_URI = "file://i2c-test.c \
               file://Makefile \
              "
     
    S = "${WORKDIR}"
     
    DEPENDS += " \
        i2c-tools \
        "
    
    CFLAGS += "-pthread"
    LDFLAGS += "-lpthread"
    
    EXTRA_OEMAKE = "'CFLAGS=${CFLAGS}' 'LDFLAGS=${LDFLAGS}'"
     
    do_compile() {
             oe_runmake
    }
     
    do_install() {
             install -d ${D}${bindir}
             install -m 0755 i2c-test ${D}${bindir}
    }
    ​
     
  3. Add the dependency packages in rootfs.

    $ petalinux-config -c rootfs ---> Filesystem Packages ---> base ---> i2c-tools
        [*] i2c-tools
        [*] i2c-tools-dev 
        [*] i2c-tools-misc 
        [ ] i2c-tools-dbg
  4. Build the images.

    $ petalinux-build
     
  5. Test procedure on target. Note the "UU" indicates devices owned by the kernel, being managed by a device driver. The remaining devices are available for users to interact with (no owner).

    root@zcu102-axi-i2c:~# i2cdetect -l
    i2c-15  i2c             i2c-1-mux (chan_id 7)                   I2C adapter
    i2c-3   i2c             ZynqMP DP AUX                           I2C adapter
    i2c-23  i2c             i2c-1-mux (chan_id 7)                   I2C adapter
    i2c-13  i2c             i2c-1-mux (chan_id 5)                   I2C adapter
    i2c-1   i2c             Cadence I2C at ff030000                 I2C adapter
    i2c-21  i2c             i2c-1-mux (chan_id 5)                   I2C adapter
    i2c-11  i2c             i2c-1-mux (chan_id 3)                   I2C adapter
    i2c-8   i2c             i2c-1-mux (chan_id 0)                   I2C adapter
    i2c-18  i2c             i2c-1-mux (chan_id 2)                   I2C adapter
    i2c-6   i2c             i2c-0-mux (chan_id 2)                   I2C adapter
    i2c-16  i2c             i2c-1-mux (chan_id 0)                   I2C adapter
    i2c-4   i2c             i2c-0-mux (chan_id 0)                   I2C adapter
    i2c-14  i2c             i2c-1-mux (chan_id 6)                   I2C adapter
    i2c-2   i2c             xiic-i2c                                I2C adapter
    i2c-22  i2c             i2c-1-mux (chan_id 6)                   I2C adapter
    i2c-12  i2c             i2c-1-mux (chan_id 4)                   I2C adapter
    i2c-0   i2c             Cadence I2C at ff020000                 I2C adapter
    i2c-20  i2c             i2c-1-mux (chan_id 4)                   I2C adapter
    i2c-9   i2c             i2c-1-mux (chan_id 1)                   I2C adapter
    i2c-10  i2c             i2c-1-mux (chan_id 2)                   I2C adapter
    i2c-19  i2c             i2c-1-mux (chan_id 3)                   I2C adapter
    i2c-7   i2c             i2c-0-mux (chan_id 3)                   I2C adapter
    i2c-17  i2c             i2c-1-mux (chan_id 1)                   I2C adapter
    i2c-5   i2c             i2c-0-mux (chan_id 1)                   I2C adapter
    root@zcu102-axi-i2c:~# i2cdetect -y 8
    Error: Can't use SMBus Quick Write command on this bus
    root@zcu102-axi-i2c:~# i2cdetect -y -r 8
         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
    50: -- -- -- -- UU UU UU UU -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- UU UU -- --
    root@zcu102-axi-i2c:~#
     
    # Reading MAC ID(00:0a:35:04:b6:2e) from EEPROM address 0x54
    root@zcu102-axi-i2c:~# i2cdump -f -y 8 0x54
    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: 38 38 31 34 32 36 38 35 31 38 30 38 2d 38 32 33    881426851808-823
    10: 37 34 ff ff ff ff ff ff ff ff ff ff ff ff ff ff    74..............
    20: 00 0a 35 04 b6 2e ff ff ff ff ff ff ff ff ff ff    .?5??...........
    30: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    40: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    50: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    60: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    70: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    80: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    90: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    a0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    b0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    c0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    d0: 5a 43 55 31 30 32 20 20 20 20 20 ff ff ff ff ff    ZCU102     .....
    e0: 31 2e 30 ff ff ff ff ff ff ff ff ff ff ff ff ff    1.0.............
    f0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    root@zcu102-axi-i2c:~#
    root@zcu102-axi-i2c:~# ifconfig
    eth0      Link encap:Ethernet  HWaddr 00:0A:35:04:B6:2E
              inet addr:172.20.9.109  Bcast:172.20.11.255  Mask:255.255.252.0
              inet6 addr: fe80::20a:35ff:fe04:b62e%4882584/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:4934 errors:0 dropped:0 overruns:0 frame:0
              TX packets:695 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:467846 (456.8 KiB)  TX bytes:148745 (145.2 KiB)
              Interrupt:31
     
    lo        Link encap:Local Loopback
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1%4882584/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:65536  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
     
    root@zcu102-axi-i2c:~#
    root@zcu102-axi-i2c:~# i2cdump -f -y 8 0x48
    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: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    root@zcu102-axi-i2c:~#
    root@zcu102-axi-i2c:~# i2c-test 8 0x48 0x0 0x6
    I2C bus id: [8] slave addr: [0x48] and reg offset: [0x00] value = 0x00
    I2C bus id: [8] slave addr: [0x48] and reg offset: [0x01] value = 0x00
    I2C bus id: [8] slave addr: [0x48] and reg offset: [0x02] value = 0x00
    I2C bus id: [8] slave addr: [0x48] and reg offset: [0x03] value = 0x00
    I2C bus id: [8] slave addr: [0x48] and reg offset: [0x04] value = 0x00
    I2C bus id: [8] slave addr: [0x48] and reg offset: [0x05] value = 0x00
    I2C bus id: [8] slave addr: [0x48] and reg offset: [0x06] value = 0x00
    root@zcu102-axi-i2c:~#
    root@zcu102-axi-i2c:~# i2cdetect -y 3
         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: 30 -- -- -- -- -- -- 37 -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: 50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- -- --
    root@zcu102-axi-i2c:~# i2cdump -f -y 3 0x50
    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: 00 ff ff ff ff ff ff 00 10 ac a2 a0 4c 33 44 35    ........????L3D5
    10: 16 18 01 04 a5 35 1e 78 3e 7e 75 a7 55 52 9c 27    ?????5?x>~u?UR?'
    20: 0f 50 54 a5 4b 00 71 4f 81 80 a9 c0 a9 40 d1 c0    ?PT?K.qO?????@??
    30: 01 01 01 01 01 01 02 3a 80 18 71 38 2d 40 58 2c    ???????:??q8-@X,
    40: 45 00 0f 28 21 00 00 1e 00 00 00 ff 00 52 39 46    E.?(!..?.....R9F
    50: 31 50 34 35 52 35 44 33 4c 0a 00 00 00 fc 00 44    1P45R5D3L?...?.D
    60: 45 4c 4c 20 55 32 34 31 34 48 0a 20 00 00 00 fd    ELL U2414H? ...?
    70: 00 38 4c 1e 53 11 00 0a 20 20 20 20 20 20 01 0a    .8L?S?.?      ??
    80: 02 03 19 f1 4c 90 05 04 03 02 07 16 01 14 1f 12    ????L???????????
    90: 13 23 09 07 07 83 01 00 00 02 3a 80 18 71 38 2d    ?#?????..?:??q8-
    a0: 40 58 2c 45 00 0f 28 21 00 00 1e 01 1d 80 18 71    @X,E.?(!..?????q
    b0: 1c 16 20 58 2c 25 00 0f 28 21 00 00 9e 01 1d 00    ?? X,%.?(!..???.
    c0: 72 51 d0 1e 20 6e 28 55 00 0f 28 21 00 00 1e 8c    rQ?? n(U.?(!..??
    d0: 0a d0 8a 20 e0 2d 10 10 3e 96 00 0f 28 21 00 00    ??? ?-??>?.?(!..
    e0: 18 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ?...............
    f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 c1    ...............?
    root@zcu102-axi-i2c:~#
    root@zcu102-axi-i2c:~# i2c-test 3 0x50 0x20 0x5
    I2C bus id: [3] slave addr: [0x50] and reg offset: [0x20] value = 0x0f
    I2C bus id: [3] slave addr: [0x50] and reg offset: [0x21] value = 0x50
    I2C bus id: [3] slave addr: [0x50] and reg offset: [0x22] value = 0x54
    I2C bus id: [3] slave addr: [0x50] and reg offset: [0x23] value = 0xa5
    I2C bus id: [3] slave addr: [0x50] and reg offset: [0x24] value = 0x4b
    I2C bus id: [3] slave addr: [0x50] and reg offset: [0x25] value = 0x00
    root@zcu102-axi-i2c:~#
    root@zcu102-axi-i2c:~# i2cdetect -y 2
         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- 0a 0b -- -- -- --
    10: 10 -- -- 13 14 15 16 17 18 -- 1a 1b -- 1d -- --
    20: 20 21 -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- 72 73 -- 75 -- --
    root@zcu102-axi-i2c:~#
    root@zcu102-axi-i2c:~# i2cdump -f -y 2 0xa
    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: XX XX 16 5d XX XX XX XX XX XX XX XX XX XX XX XX    XX?]XXXXXXXXXXXX
    10: 00 20 1f XX XX 74 4b XX XX a0 XX XX XX XX XX XX    . ?XXtKXX?XXXXXX
    20: 14 9a 00 XX f6 48 ec 43 00 XX XX XX XX XX XX XX    ??.X?H?C.XXXXXXX
    30: XX XX XX 58 XX fe c3 62 00 00 XX XX XX XX XX XX    XXXXX??b..XXXXXX
    40: a1 80 XX XX 97 00 1f bf XX XX XX XX XX XX XX 98    ??XX?.??XXXXXXX?
    50: c0 f8 XX XX XX 80 c0 XX XX 1b c0 XX XX XX 40 97    ??XXX??XX??XXX@?
    60: 7c 66 f4 bf 45 66 XX XX XX XX XX XX XX XX XX XX    |f??EfXXXXXXXXXX
    70: XX XX XX XX XX XX XX XX 02 02 00 00 00 00 c0 XX    XXXXXXXX??....?X
    80: 00 XX XX XX XX XX XX XX fc XX XX 9b 9f c7 a0 XX    .XXXXXXX?XX????X
    90: XX XX XX XX 72 58 XX XX 22 08 0d 07 08 06 0d XX    XXXXrXXX"??????X
    a0: XX XX XX XX XX XX XX XX XX XX XX XX XX 0c 08 XX    XXXXXXXXXXXXX??X
    b0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
    c0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
    d0: 4b 00 00 00 XX 00 00 XX XX 02 XX 06 00 0a XX XX    K...X..XX?X?.?XX
    e0: 20 00 00 20 00 00 00 13 00 XX XX XX XX XX XX XX     .. ...?.XXXXXXX
    f0: 10 0d e0 XX XX XX XX XX XX XX XX XX XX XX XX XX    ???XXXXXXXXXXXXX
    root@zcu102-axi-i2c:~#
    root@zcu102-axi-i2c:~# i2c-test 2 0xa 0x20 0x5
    I2C bus id: [2] slave addr: [0x0a] and reg offset: [0x20] value = 0x14
    I2C bus id: [2] slave addr: [0x0a] and reg offset: [0x21] value = 0x9a
    I2C bus id: [2] slave addr: [0x0a] and reg offset: [0x22] value = 0x00
    I2C bus id: [2] slave addr: [0x0a] and reg offset: [0x23] value = 0x00
    I2C bus id: [2] slave addr: [0x0a] and reg offset: [0x24] value = 0xf6
    I2C bus id: [2] slave addr: [0x0a] and reg offset: [0x25] value = 0x48
    root@zcu102-axi-i2c:~#
 
Thanks,
Sandeep
PetaLinux Yocto | Embedded SW Support

---------------------------------------------------------------------------
Don’t forget to Reply, Kudo, and Accept as Solution.
---------------------------------------------------------------------------

View solution in original post

3 Replies
342 Views
Registered: ‎03-02-2021

My ld also cannot find -li2c despite everything existing in my project. If this is a potential issue with the toolchain it would be great to get Xilinx eyes on this.

 

0 Kudos
sandeepg
Moderator
Moderator
295 Views
Registered: ‎04-24-2017

Hi @x_abacadaba ,

This is based on 2018.x release.

  1. Create a user space application from petalinux tools.
    $ petalinux-create -t apps -n i2c-test --template c --enable
    $ tree project-spec/meta-user/recipes-apps/i2c-test/
    project-spec/meta-user/recipes-apps/i2c-test/
    ├── files
    │   ├── i2c-test.c
    │   └── Makefile
    ├── i2c-test.bb
    └── README
     
    1 directory, 4 files
    $​
  2. Modify the c application and recipes as shown below.

    $ vim project-spec/meta-user/recipes-apps/i2c-test/files/i2c-test.c
     
     
    /*
    * Copyright (C) 2013 - 2016  Xilinx, Inc.  All rights reserved.
    *
    * Permission is hereby granted, free of charge, to any person
    * obtaining a copy of this software and associated documentation
    * files (the "Software"), to deal in the Software without restriction,
    * including without limitation the rights to use, copy, modify, merge,
    * publish, distribute, sublicense, and/or sell copies of the Software,
    * and to permit persons to whom the Software is furnished to do so,
    * subject to the following conditions:
    *
    * The above copyright notice and this permission notice shall be included
    * in all copies or substantial portions of the Software.
    *
    * Use of the Software is limited solely to applications:
    * (a) running on a Xilinx device, or (b) that interact
    * with a Xilinx device through a bus or interconnect.
    *
    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    * IN NO EVENT SHALL XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    *
    * Except as contained in this notice, the name of the Xilinx shall not be used
    * in advertising or otherwise to promote the sale, use or other dealings in this
    * Software without prior written authorization from Xilinx.
    *
    */
     
    #include <errno.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    /* If you are using x86 host machine then header should be i2c-dev.h
     * http://cgit.openembedded.org/openembedded-core/tree/meta/recipes-devtools/i2c-tools/i2c-tools_3.1.2.bb?h=rocko
     */
    #include <linux/i2c-dev-user.h>
    #include <sys/ioctl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
     
    static void usage() {
        printf("Usage:\n");
        printf("\targv[1]: I2CBUS ID is an integer got it from `i2cdetect -l` command \n");
        printf("\targv[2]: I2C slave device address is an integer detected from `i2cdetect -y <bus_id>` command \n");
        printf("\targv[3]: I2C device register address is an integer detected from `i2cget -f -y <bus_id> <reg_addr> b` command \n");
        printf("\targv[4]: Number of bytes to be read start from argv[3] I2C device register address \n");
    }
     
    int main (int argc, char **argv) {
        int file, i2c_bus_id, i2c_addr, res, i2c_reg, i, nbytes;
        char filename[40];
      
        if (argc <= 4) {
            usage();
            return -1;
        }
     
        sscanf(argv[1], "%d", &i2c_bus_id);
        sscanf(argv[2], "%x", &i2c_addr);
        sscanf(argv[3], "0x%02x\n", &i2c_reg);
        sscanf(argv[4], "0x%x\n", &nbytes);
     
        snprintf(filename, 39, "/dev/i2c-%d", i2c_bus_id);
     
        if ((file = open(filename,O_RDWR)) < 0) {
            printf("Failed to open the bus.");
            /* ERROR HANDLING; you can check errno to see what went wrong */
            exit(1);
        }
     
        if (ioctl(file, I2C_SLAVE, i2c_addr) < 0) {
            printf("Failed to acquire bus access and/or talk to slave %s\n", strerror(errno));
            exit(1);
        }
     
        /* Using SMBus commands */
        for (i=i2c_reg; i <= (i2c_reg + nbytes); i++) {
            res = i2c_smbus_read_byte_data(file, i);
            if (res < 0) {
                /* ERROR HANDLING: i2c transaction failed */
                printf("Failed to read the transcation %s\n", strerror(errno));
            } else {
                /* res contains the read word */
                printf("I2C bus id: [%d] slave addr: [0x%02x] and reg offset: [0x%02x] value = 0x%02x\n", i2c_bus_id, i2c_addr, i, res);
            }
        }
        
        return 0;
    }
    $ vim project-spec/meta-user/recipes-apps/i2c-test/i2c-test.bb
     
    #
    # This file is the i2c-test recipe.
    #
     
    SUMMARY = "Simple i2c-test application"
    SECTION = "PETALINUX/apps"
    LICENSE = "MIT"
    LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
     
    SRC_URI = "file://i2c-test.c \
               file://Makefile \
              "
     
    S = "${WORKDIR}"
     
    DEPENDS += " \
        i2c-tools \
        "
    
    CFLAGS += "-pthread"
    LDFLAGS += "-lpthread"
    
    EXTRA_OEMAKE = "'CFLAGS=${CFLAGS}' 'LDFLAGS=${LDFLAGS}'"
     
    do_compile() {
             oe_runmake
    }
     
    do_install() {
             install -d ${D}${bindir}
             install -m 0755 i2c-test ${D}${bindir}
    }
    ​
     
  3. Add the dependency packages in rootfs.

    $ petalinux-config -c rootfs ---> Filesystem Packages ---> base ---> i2c-tools
        [*] i2c-tools
        [*] i2c-tools-dev 
        [*] i2c-tools-misc 
        [ ] i2c-tools-dbg
  4. Build the images.

    $ petalinux-build
     
  5. Test procedure on target. Note the "UU" indicates devices owned by the kernel, being managed by a device driver. The remaining devices are available for users to interact with (no owner).

    root@zcu102-axi-i2c:~# i2cdetect -l
    i2c-15  i2c             i2c-1-mux (chan_id 7)                   I2C adapter
    i2c-3   i2c             ZynqMP DP AUX                           I2C adapter
    i2c-23  i2c             i2c-1-mux (chan_id 7)                   I2C adapter
    i2c-13  i2c             i2c-1-mux (chan_id 5)                   I2C adapter
    i2c-1   i2c             Cadence I2C at ff030000                 I2C adapter
    i2c-21  i2c             i2c-1-mux (chan_id 5)                   I2C adapter
    i2c-11  i2c             i2c-1-mux (chan_id 3)                   I2C adapter
    i2c-8   i2c             i2c-1-mux (chan_id 0)                   I2C adapter
    i2c-18  i2c             i2c-1-mux (chan_id 2)                   I2C adapter
    i2c-6   i2c             i2c-0-mux (chan_id 2)                   I2C adapter
    i2c-16  i2c             i2c-1-mux (chan_id 0)                   I2C adapter
    i2c-4   i2c             i2c-0-mux (chan_id 0)                   I2C adapter
    i2c-14  i2c             i2c-1-mux (chan_id 6)                   I2C adapter
    i2c-2   i2c             xiic-i2c                                I2C adapter
    i2c-22  i2c             i2c-1-mux (chan_id 6)                   I2C adapter
    i2c-12  i2c             i2c-1-mux (chan_id 4)                   I2C adapter
    i2c-0   i2c             Cadence I2C at ff020000                 I2C adapter
    i2c-20  i2c             i2c-1-mux (chan_id 4)                   I2C adapter
    i2c-9   i2c             i2c-1-mux (chan_id 1)                   I2C adapter
    i2c-10  i2c             i2c-1-mux (chan_id 2)                   I2C adapter
    i2c-19  i2c             i2c-1-mux (chan_id 3)                   I2C adapter
    i2c-7   i2c             i2c-0-mux (chan_id 3)                   I2C adapter
    i2c-17  i2c             i2c-1-mux (chan_id 1)                   I2C adapter
    i2c-5   i2c             i2c-0-mux (chan_id 1)                   I2C adapter
    root@zcu102-axi-i2c:~# i2cdetect -y 8
    Error: Can't use SMBus Quick Write command on this bus
    root@zcu102-axi-i2c:~# i2cdetect -y -r 8
         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
    50: -- -- -- -- UU UU UU UU -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- UU UU -- --
    root@zcu102-axi-i2c:~#
     
    # Reading MAC ID(00:0a:35:04:b6:2e) from EEPROM address 0x54
    root@zcu102-axi-i2c:~# i2cdump -f -y 8 0x54
    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: 38 38 31 34 32 36 38 35 31 38 30 38 2d 38 32 33    881426851808-823
    10: 37 34 ff ff ff ff ff ff ff ff ff ff ff ff ff ff    74..............
    20: 00 0a 35 04 b6 2e ff ff ff ff ff ff ff ff ff ff    .?5??...........
    30: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    40: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    50: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    60: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    70: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    80: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    90: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    a0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    b0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    c0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    d0: 5a 43 55 31 30 32 20 20 20 20 20 ff ff ff ff ff    ZCU102     .....
    e0: 31 2e 30 ff ff ff ff ff ff ff ff ff ff ff ff ff    1.0.............
    f0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
    root@zcu102-axi-i2c:~#
    root@zcu102-axi-i2c:~# ifconfig
    eth0      Link encap:Ethernet  HWaddr 00:0A:35:04:B6:2E
              inet addr:172.20.9.109  Bcast:172.20.11.255  Mask:255.255.252.0
              inet6 addr: fe80::20a:35ff:fe04:b62e%4882584/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:4934 errors:0 dropped:0 overruns:0 frame:0
              TX packets:695 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:467846 (456.8 KiB)  TX bytes:148745 (145.2 KiB)
              Interrupt:31
     
    lo        Link encap:Local Loopback
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1%4882584/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:65536  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
     
    root@zcu102-axi-i2c:~#
    root@zcu102-axi-i2c:~# i2cdump -f -y 8 0x48
    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: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    root@zcu102-axi-i2c:~#
    root@zcu102-axi-i2c:~# i2c-test 8 0x48 0x0 0x6
    I2C bus id: [8] slave addr: [0x48] and reg offset: [0x00] value = 0x00
    I2C bus id: [8] slave addr: [0x48] and reg offset: [0x01] value = 0x00
    I2C bus id: [8] slave addr: [0x48] and reg offset: [0x02] value = 0x00
    I2C bus id: [8] slave addr: [0x48] and reg offset: [0x03] value = 0x00
    I2C bus id: [8] slave addr: [0x48] and reg offset: [0x04] value = 0x00
    I2C bus id: [8] slave addr: [0x48] and reg offset: [0x05] value = 0x00
    I2C bus id: [8] slave addr: [0x48] and reg offset: [0x06] value = 0x00
    root@zcu102-axi-i2c:~#
    root@zcu102-axi-i2c:~# i2cdetect -y 3
         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: 30 -- -- -- -- -- -- 37 -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: 50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- -- --
    root@zcu102-axi-i2c:~# i2cdump -f -y 3 0x50
    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: 00 ff ff ff ff ff ff 00 10 ac a2 a0 4c 33 44 35    ........????L3D5
    10: 16 18 01 04 a5 35 1e 78 3e 7e 75 a7 55 52 9c 27    ?????5?x>~u?UR?'
    20: 0f 50 54 a5 4b 00 71 4f 81 80 a9 c0 a9 40 d1 c0    ?PT?K.qO?????@??
    30: 01 01 01 01 01 01 02 3a 80 18 71 38 2d 40 58 2c    ???????:??q8-@X,
    40: 45 00 0f 28 21 00 00 1e 00 00 00 ff 00 52 39 46    E.?(!..?.....R9F
    50: 31 50 34 35 52 35 44 33 4c 0a 00 00 00 fc 00 44    1P45R5D3L?...?.D
    60: 45 4c 4c 20 55 32 34 31 34 48 0a 20 00 00 00 fd    ELL U2414H? ...?
    70: 00 38 4c 1e 53 11 00 0a 20 20 20 20 20 20 01 0a    .8L?S?.?      ??
    80: 02 03 19 f1 4c 90 05 04 03 02 07 16 01 14 1f 12    ????L???????????
    90: 13 23 09 07 07 83 01 00 00 02 3a 80 18 71 38 2d    ?#?????..?:??q8-
    a0: 40 58 2c 45 00 0f 28 21 00 00 1e 01 1d 80 18 71    @X,E.?(!..?????q
    b0: 1c 16 20 58 2c 25 00 0f 28 21 00 00 9e 01 1d 00    ?? X,%.?(!..???.
    c0: 72 51 d0 1e 20 6e 28 55 00 0f 28 21 00 00 1e 8c    rQ?? n(U.?(!..??
    d0: 0a d0 8a 20 e0 2d 10 10 3e 96 00 0f 28 21 00 00    ??? ?-??>?.?(!..
    e0: 18 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ?...............
    f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 c1    ...............?
    root@zcu102-axi-i2c:~#
    root@zcu102-axi-i2c:~# i2c-test 3 0x50 0x20 0x5
    I2C bus id: [3] slave addr: [0x50] and reg offset: [0x20] value = 0x0f
    I2C bus id: [3] slave addr: [0x50] and reg offset: [0x21] value = 0x50
    I2C bus id: [3] slave addr: [0x50] and reg offset: [0x22] value = 0x54
    I2C bus id: [3] slave addr: [0x50] and reg offset: [0x23] value = 0xa5
    I2C bus id: [3] slave addr: [0x50] and reg offset: [0x24] value = 0x4b
    I2C bus id: [3] slave addr: [0x50] and reg offset: [0x25] value = 0x00
    root@zcu102-axi-i2c:~#
    root@zcu102-axi-i2c:~# i2cdetect -y 2
         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- 0a 0b -- -- -- --
    10: 10 -- -- 13 14 15 16 17 18 -- 1a 1b -- 1d -- --
    20: 20 21 -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- 72 73 -- 75 -- --
    root@zcu102-axi-i2c:~#
    root@zcu102-axi-i2c:~# i2cdump -f -y 2 0xa
    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: XX XX 16 5d XX XX XX XX XX XX XX XX XX XX XX XX    XX?]XXXXXXXXXXXX
    10: 00 20 1f XX XX 74 4b XX XX a0 XX XX XX XX XX XX    . ?XXtKXX?XXXXXX
    20: 14 9a 00 XX f6 48 ec 43 00 XX XX XX XX XX XX XX    ??.X?H?C.XXXXXXX
    30: XX XX XX 58 XX fe c3 62 00 00 XX XX XX XX XX XX    XXXXX??b..XXXXXX
    40: a1 80 XX XX 97 00 1f bf XX XX XX XX XX XX XX 98    ??XX?.??XXXXXXX?
    50: c0 f8 XX XX XX 80 c0 XX XX 1b c0 XX XX XX 40 97    ??XXX??XX??XXX@?
    60: 7c 66 f4 bf 45 66 XX XX XX XX XX XX XX XX XX XX    |f??EfXXXXXXXXXX
    70: XX XX XX XX XX XX XX XX 02 02 00 00 00 00 c0 XX    XXXXXXXX??....?X
    80: 00 XX XX XX XX XX XX XX fc XX XX 9b 9f c7 a0 XX    .XXXXXXX?XX????X
    90: XX XX XX XX 72 58 XX XX 22 08 0d 07 08 06 0d XX    XXXXrXXX"??????X
    a0: XX XX XX XX XX XX XX XX XX XX XX XX XX 0c 08 XX    XXXXXXXXXXXXX??X
    b0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
    c0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
    d0: 4b 00 00 00 XX 00 00 XX XX 02 XX 06 00 0a XX XX    K...X..XX?X?.?XX
    e0: 20 00 00 20 00 00 00 13 00 XX XX XX XX XX XX XX     .. ...?.XXXXXXX
    f0: 10 0d e0 XX XX XX XX XX XX XX XX XX XX XX XX XX    ???XXXXXXXXXXXXX
    root@zcu102-axi-i2c:~#
    root@zcu102-axi-i2c:~# i2c-test 2 0xa 0x20 0x5
    I2C bus id: [2] slave addr: [0x0a] and reg offset: [0x20] value = 0x14
    I2C bus id: [2] slave addr: [0x0a] and reg offset: [0x21] value = 0x9a
    I2C bus id: [2] slave addr: [0x0a] and reg offset: [0x22] value = 0x00
    I2C bus id: [2] slave addr: [0x0a] and reg offset: [0x23] value = 0x00
    I2C bus id: [2] slave addr: [0x0a] and reg offset: [0x24] value = 0xf6
    I2C bus id: [2] slave addr: [0x0a] and reg offset: [0x25] value = 0x48
    root@zcu102-axi-i2c:~#
 
Thanks,
Sandeep
PetaLinux Yocto | Embedded SW Support

---------------------------------------------------------------------------
Don’t forget to Reply, Kudo, and Accept as Solution.
---------------------------------------------------------------------------

View solution in original post

x_abacadaba
Observer
Observer
270 Views
Registered: ‎12-21-2017

Great thorough answer!  The specific part that I was missing was the #include "i2c-dev-user.h" since it was missing from that documentation page provided with the linux kernel.  Thanks a lot for the help!

0 Kudos