Author Topic: ZynqBerry SPI  (Read 3020 times)

mj

  • Active Member
  • *
  • Posts: 17
ZynqBerry SPI
« 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

Andrei Errapart

  • Active Member
  • *
  • Posts: 20
Re: ZynqBerry SPI
« Reply #1 on: February 06, 2017, 11:26:32 AM »
Before running the application, add the execute permission to the file as follows:
Code: [Select]
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:
Code: [Select]
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.

mj

  • Active Member
  • *
  • Posts: 17
Re: ZynqBerry SPI
« Reply #2 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.

Andrei Errapart

  • Active Member
  • *
  • Posts: 20
Re: ZynqBerry SPI
« Reply #3 on: February 06, 2017, 02:41:30 PM »
Can you try without the line
Code: [Select]
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.
« Last Edit: February 06, 2017, 03:48:35 PM by Andrei Errapart »

mj

  • Active Member
  • *
  • Posts: 17
Re: ZynqBerry SPI
« Reply #4 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


Andrei Errapart

  • Active Member
  • *
  • Posts: 20
Re: ZynqBerry SPI
« Reply #5 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.


mj

  • Active Member
  • *
  • Posts: 17
Re: ZynqBerry SPI
« Reply #6 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!

Andrei Errapart

  • Active Member
  • *
  • Posts: 20
Re: ZynqBerry SPI
« Reply #7 on: February 07, 2017, 04:52:25 PM »
Unfortunately, I have to admit my lack of knowledge in that particular area, CS decoding.

mj

  • Active Member
  • *
  • Posts: 17
Re: ZynqBerry SPI
« Reply #8 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!