Trenz Electronic GmbH Support Forum

Open Source => Open source hardware => Topic started by: sergio on February 04, 2017, 11:05:38 AM

Title: ZynqBerry SPI
Post by: sergio on February 04, 2017, 11:05:38 AM
Hello everyone!

I'm trying to interface an SPI device with my ZynqBerry board. For that I enabled Zynq PS' SPI0 on my hardware project and routed it via EMIO to the J8 connector of the board.
Then on PetaLinux I made sure that Cadence SPI controller, Xilinx SPI controller common module, Xilinx Zynq QSPI controller and User mode SPI device driver support are all enabled on the kernel configuration menu. Finally I also added to my system-top.dts file the following lines:

&spi1 {
  is-decoded-cs = <0>;
  num-cs = <1>;
  status = "okay";
  spidev@0x00 {
    compatible = "spidev";
    spi-max-frequency = <1000000>;
    reg = <0>;
  };
};


For now I just to want to test a single SPI slave to verify if everything is working correctly. Later I'll need 4 SPI slaves connected to the same master and I know that I'll need a slave decoder but one step at a time.
PetaLinux creates a /dev/spidev32765.0 interface. I've connected to my board a LSM9DS0 breakout board from Adafruit and I want to probe this device only to see if SPI is working correctly. I've researched the subject and I found that everyone uses the spidev_test.c application for that purpose. I've compiled it with the following command:

arm-xilinx-linux-gnueabi-gcc -o spidevtest /opt/Petalinux/petalinux-v2016.2-final/components/linux-kernel/xlnx-4.4/Documentation/spi/spidev_test.c

This will create a new file named spidevtest on that directory. Then I copy that file to PetaLinux via SSH (with scp command) to /usr/ directory. Then I try to run the application:

./spidevtest -d /dev/spidev32765.0

But nothing happens and I get the following response:

-sh: ./spidevtest: No such file or directory

Am I missing something? Is there a simpler way to probe SPI from the command line with a tool such i2c-tools for I2C ?

Thanks for the attention!

Best regards,

mj
Title: Re: ZynqBerry SPI
Post by: Andrei Errapart on February 06, 2017, 11:26:32 AM
Before running the application, add the execute permission to the file as follows:
chmod +x spidevtest

Without it the Linux kernel won't recognize the file as executable. Let me note that the Linux kernel itself has no concept of file extensions. The dot "." does play a special role: there are special directories named "." and ".." and there is a convention that filenames beginning with dot are usually hidden from the user unless requested for.

The program you are using should suffice for most of the needs for SPI testing. I am not aware of any other tools that come with petalinux. Creating a corresponding petalinux application is trivial, though.

The name of the device created can be changed by adding aliases to the device tree, for example, as follows:
aliases {
   spi100 = &spi0; // creates /dev/spidev100
};


I have found (out the hard way) that alias named "spi0" will cause u-boot to search for the root image on the SPI flash memory connected to this bus and boot will fail when this is not the case, that's why the "spi100" in the example. Depending on your circumstances, this might not apply to you.
Title: Re: ZynqBerry SPI
Post by: sergio on February 06, 2017, 02:21:37 PM
Hello Andrei, thanks for the reply!

I did try the chmod command but without success. Nevertheless was able to compile spidev_test.c with PetaLinux.
This is what I did:
I've created a new C application with the petalinux-create command and replaced the default code with spidev_test.c source code. With this Petalinux successfully compiles the code and I can successfully run the application created.

Maybe the problem was that Petalinux uses a different compiler than I was using.

Right now I am able to test SPI but I still couldn't get it to work.
I'm using an oscilloscope to see SPI chip select and clock signals but I get no activity in both of them.

I believe that I routed them correctly in Vivado. Any ideas?

Best regards.
Title: Re: ZynqBerry SPI
Post by: Andrei Errapart on February 06, 2017, 02:41:30 PM
Can you try without the line
status = "okay";
?

Does regenerating the FSBL help? The FSBL contains the initialization code for the various Zynq devices. Without initialization the devices (incl. SPI) will remain disabled for Linux, too.

The petalinux uses the hardware floating-point version of the toolchain; the C compiler executable should be "arm-linux-gnueabihf-gcc" and you can find it in the Petalinux installation directory, too.
Title: Re: ZynqBerry SPI
Post by: sergio on February 06, 2017, 08:42:22 PM
Hello Andrei. Thanks for the reply!

The problem was that added to my system-top.dts file &spi1, but I enabled SPI0 in Vivado. Once this was corrected eveything was working fine.

Right now I'm trying to read a specific register from my SPI device but still without success. Any hints on how to modify spidev_test.c to read a specific register and print the response on the terminal?

Best regards

Title: Re: ZynqBerry SPI
Post by: Andrei Errapart on February 07, 2017, 09:32:22 AM
Have you tried the "-p" option to specify some data to be sent to the SPI device? It is only when you write something to the SPI bus you get to read something back.

As to how to read a specific register, this is specific to the device you have on the SPI bus. SPI bus itself doesn't specify any commands like "read registers" or "start DMA". Read the datasheet of your SPI slave device; the description of the register read and write commands is usually there.

Title: Re: ZynqBerry SPI
Post by: sergio on February 07, 2017, 03:40:48 PM
Hello Andrei. Thanks for the reply!!

I'm finally able to retrieve data from my SPI slave device.

However I'm not done with SPI. Now I need 4 slaves connected to SPI0 master controller. I did some research and I know I'll need a 3-to-8 decoder (which I have already developed and tested). I also noticed that is-decoded-cs  must be set to 1 in the system-top.dts file.

Is there anything else that needs to be configured to use a decoder and connect more than 3 slaves to Zynq PS' SPI master controller?

Best regards!
Title: Re: ZynqBerry SPI
Post by: Andrei Errapart on February 07, 2017, 04:52:25 PM
Unfortunately, I have to admit my lack of knowledge in that particular area, CS decoding.
Title: Re: ZynqBerry SPI
Post by: sergio on February 07, 2017, 06:43:04 PM
Hello Andrei! Thanks for the replies!

Everything is working fine. I added the decoder without problem, it's straighforwad.

Best regards!
Title: Re: ZynqBerry SPI
Post by: Deepak on April 26, 2021, 02:30:20 PM
Hello,
I am also facing similar problem. I have enabled spi vivado hw design, petalinux kernel config and device tree.
https://forum.trenz-electronic.de/index.php/topic,1460.new.html#new
Now, /dev/spidev1.0 is available but I do not know how to test my spi.
Could you please let me know how can I test my spi communication.

Thanks
Title: Re: ZynqBerry SPI
Post by: JH on April 28, 2021, 07:12:19 AM
Hi,
check these pages:
https://www.hackster.io/news/microzed-chronicles-using-spidev-in-petalinux-18ff937807c5
https://www.beyond-circuits.com/wordpress/tutorial/tutorial26/


br
John
Title: Re: ZynqBerry SPI
Post by: ChanceTran on October 22, 2021, 04:42:30 PM
Hi....Comprehend the contrast between the MIO and EMIO choices found in the IO section. The MIO (Multiplexed IO) pins are a proper arrangement of devoted pins that specific fringe capacities can be appointed to. These are traded as a feature of the FIXED_IO port that Vivado made during the Block Automation prior. MIO's are simply associated with the PS and not to the PL.

In the event that a fringe is directed through EMIO (Extended Multiplexed IO), Vivado will make an extraordinary port for that fringe on the Zynq PS block that can be relegated like some other sign in the PL. EMIOs are basically wires from the PS to the PL.
Title: Re: ZynqBerry SPI
Post by: JH on October 26, 2021, 01:41:52 PM
Hi, sorry, maybe I misunderstood something but that was just a statement and not a question, right?

br
John