Post Go back to editing

Using AD7949 with pyadi-iio

Category: Software
Product Number: AD7949

Hello,
I am trying to access an AD7949 via I2C (Raspberry PI) with pyadi-iio. In Github this is listed as supported hardware. However, I can't find any reference to it in the documentation. Did I miss something?

I use libiio 0.25, pyadi-iio 0.0.17.

Thanks a lot

Thread Notes

Moved from Software Interface Tools to Linux Software Drivers on Monday, November 6, 2023 2:07:55 PM by travisfcollins

Parents
  • AD7949 is supported by the AD7689 class pyadi-iio/adi/ad7689.py at 24b753e22e5e3c440a8dc06941ed884db3e9fe8e · analogdevicesinc/pyadi-iio (github.com)

    pyadi-iio assumes you are using the IIO driver for the device. Is this the case?

    -Travis

  • Thank you, Travis.
    I got libiio and pylibiio installed.

    If try to run the example code of the AD7689 I get:
    "ERROR: Unable to create Avahi DNS-SD browser: Invalid host name"

    Is there anything i am doing wrong? This is my first time, I am a rookie and so I will make nearly every rookie mistake.
    Sorry for this in advance.

    iio_info shows only the devices of the raspberry (cpu thermal and rpi volt) if this is of any importance.

    Thank you
    -Max

  • "ERROR: Unable to create Avahi DNS-SD browser: Invalid host name"

    This likely means avahi (autodiscovery) is not correctly setup on your system. Make sure the service is running if you need that feature. Otherwise, just directly provide the constructor the URI.

    iio_info shows only the devices of the raspberry (cpu thermal and rpi volt) if this is of any importance.

    You likely don't have the driver setup. Are you using the ADI kernel (it comes with Kuiper Linux) and have you enabled the correct devicetree through the config.txt file?

    -Travis

  • Thank you. Since the AD7689 is not listed in the project files, it is not possible for me to access the AD7949, I guess?
    https://wiki.analog.com/resources/tools-software/linux-software/kuiper-linux/project-list.

    The AD7949 is on a self designed RPi-Shield and is connected to the SPI-Pins.

    -Max

  • I have installed kuiper linux. Since there is no driver for the 7949, I have added the driver.
    Could you please check if this process correct?

    git clone https://github.com/analogdevicesinc/linux
    cd linux
    git checkout rpi-5.10.y


    -I have download driver ad7949.c (https://github.com/torvalds/linux/blob/master/drivers/iio/adc/ad7949.c) and put the file inside this directory.:`/linux-adi/drivers/ad7949/ad7949.c`
        
    -I created the Makefile inside the ad7949 directory and edited it. This will appear like `/linux-adi/drivers/ad7949/Makefile`
       `obj-$(CONFIG_ad7949) += ad7949.o`

       
    -I created `Kconfig` file inside the ad7949 directory, this will appeare`/linux-adi/drivers/ad7949/Kconfig`
        
        config ad7949
        tristate "ad7949" //gives your driver description like vendor name etc.
        depends on ARM
        default y if ARM
        help
          my driver module.

    -Edit `Makefile` and `Kconfig` file  at `/linux-adi/drivers/Makefile` and `/linux-adi/drivers/Kconfig`
    Makefile:
         obj-$(CONFIG_AD7949)   += ad7040/
        
    Kconfig:   
        source "drivers/my_drvr/Kconfig"
        
    -Edited `Kconfig` file into `kernel_source/arch/arm/configs/adi_bcm2711_defconfig` since i am using RPi 4
               CONFIG_AD7949=y


    -Edit build_rpi_kernel_image.sh
                 DEFCONFIG=${DEFCONFIG:-adi_bcm2711_defconfig}

    -run  ./build_rpi_kernel_image.sh

    -Copy kernel7 to RPi /boot/

    If i run lsmod it does not show up but i don't get an error message when i run modprobe ad7949.

    Could you confirme this? As next step i will try to setup a device tree overlay.

    Thank you!

  • After setting up the device tree the ad7949 gets recognized in the iio_info

    analog@analog:~ $ sudo iio_info
    [sudo] password for analog:
    Library version: 0.24 (git tag: accb7b5)
    Compiled with backends: local xml ip usb serial
    IIO context created with local backend.
    Backend version: 0.24 (git tag: accb7b5)
    Backend description string: Linux analog 5.10.63-v7l+ #1 SMP Fri Mar 10 16:35:58 UTC 2023 armv7l
    IIO context has 4 attributes:
            hw_carrier: Raspberry Pi 4 Model B Rev 1.4
            dtoverlay: vc4-kms-v3d,ad7479
            local,kernel: 5.10.63-v7l+
            uri: local:
    IIO context has 4 devices:
            hwmon0: cpu_thermal
                    1 channels found:
                            temp1:  (input)
                            1 channel-specific attributes found:
                                    attr  0: input value: 49660
                    No trigger on this device
            hwmon1: rpi_volt
                    1 channels found:
                            in0:  (input)
                            1 channel-specific attributes found:
                                    attr  0: lcrit_alarm value: 0
                    No trigger on this device
            iio:device0: ad7949
                    8 channels found:
                            voltage6:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 16383
                                    attr  1: scale ERROR: Invalid argument (22)
                            voltage3:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 0
                                    attr  1: scale ERROR: Invalid argument (22)
                            voltage7:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 16383
                                    attr  1: scale ERROR: Invalid argument (22)
                            voltage0:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 0
                                    attr  1: scale ERROR: Invalid argument (22)
                            voltage4:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 0
                                    attr  1: scale ERROR: Invalid argument (22)
                            voltage1:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 250
                                    attr  1: scale ERROR: Invalid argument (22)
                            voltage5:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 16383
                                    attr  1: scale ERROR: Invalid argument (22)
                            voltage2:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 0
                                    attr  1: scale ERROR: Invalid argument (22)
                    1 debug attributes found:
                                    debug attr  0: direct_reg_access value: 0x3D79
                    No trigger on this device
            iio_sysfs_trigger:
                    0 channels found:
                    2 device-specific attributes found:
                                    attr  0: add_trigger ERROR: Permission denied (13)
                                    attr  1: remove_trigger ERROR: Permission denied (13)
                    No trigger on this device

    I can access the ad7949 via python but cannot ad7949.rx().

    Is there anything I have forgotten in my dts-file or what you cause this issue. Thank you a lot.

    /dts-v1/;
    /plugin/;
    
    / {
            compatible = "brcm,bcm2708", "brcm,bcm2709";
    
    
            fragment@0 {
                    target = <&spi0>;
                    __overlay__ {
                            #address-cells = <1>;
                            #size-cells = <0>;
                            status = "okay";
    
                            ad7949@0{
                                    #clock-cells = <0>;
                                    compatible = "ad7949";
                                    reg = <0>;
                                    spi-max-frequency = <1000000>;
                                    spi-cpol;
                                    vref-supply = <4096000>;
                            };
                    };
            };
    };
    
    

  • Hi Max,

    Can you try removing `spi-cpol;` from the device tree overlay?

    The devicetree binding documentation on ADI linux fork does not list the spi-cpol property.

    Also, in every place cpol appears in ad7949 datasheet it is set to zero (along with cpha property) or it is set to one (also along with the cpha property).

    The linux driver doesn't seem to handle the busy indicator functionality (which is the case for cpol == cpha == 1) so I would recommend removing cpol property from the device tree which will result in having default cpol == cpha == 0.

    Hope that helps.

  • Hi machschmitt,

    Thank you for your kind answer. I removed 'spi-cpol' and also tried to set 'spi-mode' = <0>, which should result in cpol == cpha == 0. Both did not help.

    Also could there be a problem with a wrong keyword in the overlay? I am stating vref-supply, but the documentation refers to adi,internal-ref-microvolt - could this be an issue.

    Also, I noticed if I  'watch iio_info' the raw values are changing randomly without any voltage attached (compatible = "brcm,bcm2708"). After that i tried the same thing with an overlay compatible = <brcm, bcm2711>, since I am using it with a Raspberry Pi 4. After that, the iio_info raw_values do not change any longer.

    At the moment it is quite frustrating because I don't know if the overlay, the driver or something else is causing this error message.

    Thanks anyway
    -Max

  • Added
        fragment@0 {
            target = "/";
            __overlay__ {
                adc_vref: fixedregulator@0 {
                    compatible = "regulator-fixed";
                    regulator-name = "fixed-supply";
                    regulator-min-microvolt = <xxx>;
                    regulator-max-microvolt = <xxx>;
                    regulator-boot-on;
                };

    to the overlay. see also:
    https://ez.analog.com/linux-software-drivers/f/q-a/87850/ad7193-driver-with-raspberry-pi

  • Hi,

    So, did that solve the problem?

    -Marcelo

  • Hi,

    Unfortunately not,
    in python the .rx() still does not work.
    Also the scale value is quite high and this does not change no matter which vref-supply is selected in dts.
    when i create voltages i get the maximum value all the time. Also the PERSMISSION is denied for the Tigger attribute.

    Library version: 0.24 (git tag: accb7b5)
    Compiled with backends: local xml ip usb serial
    IIO context created with local backend.
    Backend version: 0.24 (git tag: accb7b5)
    Backend description string: Linux analog 5.10.63-v7l+ #1 SMP Fri Mar 10 16:35:58 UTC 2023 armv7l
    IIO context has 4 attributes:
            hw_carrier: Raspberry Pi 4 Model B Rev 1.4
            dtoverlay: vc4-kms-v3d,ad7949
            local,kernel: 5.10.63-v7l+
            uri: local:
    IIO context has 4 devices:
            hwmon0: cpu_thermal
                    1 channels found:
                            temp1:  (input)
                            1 channel-specific attributes found:
                                    attr  0: input value: 56478
                    No trigger on this device
            hwmon1: rpi_volt
                    1 channels found:
                            in0:  (input)
                            1 channel-specific attributes found:
                                    attr  0: lcrit_alarm value: 0
                    No trigger on this device
            iio:device0: ad7949
                    8 channels found:
                            voltage6:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 16383
                                    attr  1: scale value: 500
                            voltage3:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 16383
                                    attr  1: scale value: 500
                            voltage7:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 0
                                    attr  1: scale value: 500
                            voltage0:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 148
                                    attr  1: scale value: 500
                            voltage4:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 460
                                    attr  1: scale value: 500
                            voltage1:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 16383
                                    attr  1: scale value: 500
                            voltage5:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 20
                                    attr  1: scale value: 500
                            voltage2:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 7168
                                    attr  1: scale value: 500
                    No trigger on this device
            iio_sysfs_trigger:
                    0 channels found:
                    2 device-specific attributes found:
                                    attr  0: add_trigger ERROR: Permission denied (13)
                                    attr  1: remove_trigger ERROR: Permission denied (13)
                    No trigger on this device

Reply
  • Hi,

    Unfortunately not,
    in python the .rx() still does not work.
    Also the scale value is quite high and this does not change no matter which vref-supply is selected in dts.
    when i create voltages i get the maximum value all the time. Also the PERSMISSION is denied for the Tigger attribute.

    Library version: 0.24 (git tag: accb7b5)
    Compiled with backends: local xml ip usb serial
    IIO context created with local backend.
    Backend version: 0.24 (git tag: accb7b5)
    Backend description string: Linux analog 5.10.63-v7l+ #1 SMP Fri Mar 10 16:35:58 UTC 2023 armv7l
    IIO context has 4 attributes:
            hw_carrier: Raspberry Pi 4 Model B Rev 1.4
            dtoverlay: vc4-kms-v3d,ad7949
            local,kernel: 5.10.63-v7l+
            uri: local:
    IIO context has 4 devices:
            hwmon0: cpu_thermal
                    1 channels found:
                            temp1:  (input)
                            1 channel-specific attributes found:
                                    attr  0: input value: 56478
                    No trigger on this device
            hwmon1: rpi_volt
                    1 channels found:
                            in0:  (input)
                            1 channel-specific attributes found:
                                    attr  0: lcrit_alarm value: 0
                    No trigger on this device
            iio:device0: ad7949
                    8 channels found:
                            voltage6:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 16383
                                    attr  1: scale value: 500
                            voltage3:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 16383
                                    attr  1: scale value: 500
                            voltage7:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 0
                                    attr  1: scale value: 500
                            voltage0:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 148
                                    attr  1: scale value: 500
                            voltage4:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 460
                                    attr  1: scale value: 500
                            voltage1:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 16383
                                    attr  1: scale value: 500
                            voltage5:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 20
                                    attr  1: scale value: 500
                            voltage2:  (input)
                            2 channel-specific attributes found:
                                    attr  0: raw value: 7168
                                    attr  1: scale value: 500
                    No trigger on this device
            iio_sysfs_trigger:
                    0 channels found:
                    2 device-specific attributes found:
                                    attr  0: add_trigger ERROR: Permission denied (13)
                                    attr  1: remove_trigger ERROR: Permission denied (13)
                    No trigger on this device

Children
  • Hi Max,

      attr  1: scale value: 500

    Yes, that value is high for a scale and certainly incorrect. ad7949 scale for 4096mV vref (internal or extarnal ref) should be 0.25. But it's good sign that we are getting non-constant _raw values so the ADC must be alive.

    I checked out the driver and made a few changes to it. Hope the patches/commits in the ad7949_scale_fix branch make things work better. The top commit on that fix branch has a couple of overlays I made for using ad7949 with raspberrypi. Please, have a look. Those overlays use the newer overlay format. Some documentation here if you be interested in.

    Getting kernel drivers and devicetree working can be tough sometimes (specially when you're new to them), but it looks like you have already got throgh the worst parts of it.

    About the permission denied issue, users must be logged as root (`su root` or `sudo su`) to be able to write to some sysfs files (prepending `sudo` to the command doesn't work).

    Anyway, please, let us know how the tests with this go.

    -Marcelo

  • Hi Marcelo,

    first - thank you. I will look into it, but it will take some time.
    I'll let you know when I have tested it.

    - Max

  • Hi Marcelo,

    so I tried to install the driver. I started with a fresh kuiper linux:

    iio_info

    Library version: 0.24 (git tag: accb7b5)
    Compiled with backends: local xml ip usb serial
    IIO context created with local backend.
    Backend version: 0.24 (git tag: accb7b5)
    Backend description string: Linux analog 5.10.63-v7l+ #1 SMP Fri Mar 10 16:35:58 UTC 2023 armv7l
    IIO context has 4 attributes:
            hw_carrier: Raspberry Pi 4 Model B Rev 1.4
            dtoverlay: vc4-kms-v3d
            local,kernel: 5.10.63-v7l+
            uri: local:
    IIO context has 3 devices:
            hwmon0: cpu_thermal
                    1 channels found:
                            temp1:  (input)
                            1 channel-specific attributes found:
                                    attr  0: input value: 35537
                    No trigger on this device
            hwmon1: rpi_volt
                    1 channels found:
                            in0:  (input)
                            1 channel-specific attributes found:
                                    attr  0: lcrit_alarm value: 0
                    No trigger on this device
            iio_sysfs_trigger:
                    0 channels found:
                    2 device-specific attributes found:
                                    attr  0: add_trigger ERROR: Permission denied (13)
                                    attr  1: remove_trigger ERROR: Permission denied (13)
                    No trigger on this device


    I downloadad your branch and compilied it on the RPi4


    sudo apt-get install raspberrypi-kernel-headers build-essential bc git wget bison flex libssl-dev make libncurses-dev
    git clone github.com/.../linux
    cd linux
    git checkout ad7949_scale_fix
    make adi_bcm2711_defconfig
    make menuconfig
    make -j4 zImage dtbs

    sudo cp ~/linux/arch/arm/boot/dts/*.dtb /boot/  
     
    sudo cp ~/linux/arch/arm/boot/dts/overlays/README /boot/overlays/  
    sudo cp ~/linux/arch/arm/boot/kernel7l.img arch/arm/boot/bp_kernel7l.img
    sudo cp ~/linux/arch/arm/boot/zImage /boot/kernel7l.img

    nano /boot/config.txt #added dtoverlay=rpi-ad7949 -spi-on

    Reboot

    iio_info

    Library version: 0.24 (git tag: accb7b5)
    Compiled with backends: local xml ip usb serial
    IIO context created with local backend.
    Backend version: 0.24 (git tag: accb7b5)
    Backend description string: Linux analog 6.1.54-v7l+ #1 SMP Thu Nov 16 11:08:20 GMT 2023 armv7l
    IIO context has 4 attributes:
            hw_carrier: Raspberry Pi 4 Model B Rev 1.4
            dtoverlay: vc4-kms-v3d,rpi-ad7949
            local,kernel: 6.1.54-v7l+
            uri: local:
    IIO context has 0 devices:

    so the kernel module is not loading any longer. Also, the kernel version changed to 6.1.
    Do you have any idea if this problem is related to the newer kernel version?

    I also tried to download rpi-5.10.y from the building instructions but cannot build it properly.

    Any idea ?
    Thank you

  • Hi Max, sorry for the delayed reply ...

    So, I tried the ad7949_scale_fix branch on a fresh Kuiper install and was able to probe the driver.

    I'm pasting the commands I ran here although I usually build things outside the kernel sources so you might have to adapt to your preferred way of building the kernel Slight smile.

    git checkout ad7949_scale_fix

    export ARCH=arm64

    export CROSS_COMPILE=aarch64-linux-gnu-

    make O=../adi_rpi_build KCONFIG_CONFIG=../adi_rpi_build/.config bcm2711_defconfig

    make O=../adi_rpi_build KCONFIG_CONFIG=../adi_rpi_build/.config menuconfig

    make O=../adi_rpi_build KCONFIG_CONFIG=../adi_rpi_build/.config Image modules dtbs -j$(($(nproc)/2))

    cp ../adi_rpi_build/arch/arm64/boot/Image /media/$USER/BOOT/kernel8.img

    sudo make -C ../adi_rpi_build/ INSTALL_MOD_PATH=/media/$USER/rootfs/ modules_install

    sudo cp ../adi_rpi_build/arch/arm64/boot/dts/overlays/rpi-ad7949.dtbo /media/$USER/BOOT/overlays/

    Have the following in `config.txt` file

    kernel=kernel8.img

    dtparam=spi=on

    # Device treee overlay for AD7949

    dtoverlay=rpi-ad7949

    After boot, check if the overlay has been applied:

    dtc -I fs /sys/firmware/devicetree/base -O dts -o current-dt.dts

    grep -ni --color ad7949 current-dt.dts -C 10

    One advice here is you might lose the wifi network interface when using kernel 6.1 with Kuiper. At least that happened here on my setup so I had to plug in keyboard and monitor to see what was going on. I had it set up with the official raspberrypi distro previously and it had wifi working with kernel 6.1. I haven't figured out why I had that problem but am reporting it so you might be aware.

    Also, one additional fix to the sacle value was pushed to the ad7949_scale_fix so check it out.

    Keep us up to date about how it goes and let us know if anything else is needed.