2

I followed the instructions given in the blog and everything got executed without any errors. But in the end

https://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/README.md

Ran this script:

wget https://raw.githubusercontent.com/raspberrypi/linux/rpi-3.10.y/Documentation/spi/spidev_test.c
gcc -o spidev_test spidev_test.c
./spidev_test -D /dev/spidev0.0

Expected output:

spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 KHz)

FF FF FF FF FF FF
40 00 00 00 00 95
FF FF FF FF FF FF
FF FF FF FF FF FF
FF FF FF FF FF FF
DE AD BE EF BA AD
F0 0D

Output which I got:

spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 KHz)

00 00 00 00 00 00
00 00 00 00 00 00
00 00 00 00 00 00
00 00 00 00 00 00
00 00 00 00 00 00
00 00 00 00 00 00
00 00 

I did connect MOSI and MISO. Also lsmod does show spibcm2708 drivers being enabled.

I tested the same tutorial on 2 Raspberry Pi, but same result. Where am I going wrong? What am I missing?

enter image description here enter image description here

EDIT:

Error fixed. Thanks to @dastaan for spotting. I was using a 26 pin reference and hence was doing a wrong connection. It's working fine for me now.

  • Things to check : Is SPI listed under /etc/modprobe.d/raspi-blacklist.conf ? If yes, comment it out by putting # in front of it. Can you see spidev under /dev ? Run ` ls -l /dev/spidev. It should list out spidev-0.0 and spidev-0.1. For more info read : learn.adafruit.com/downloads/pdf/… | configuring SPI section. Hope it helps. – dhruvvyas90 Jul 21 '15 at 11:18
  • I checked everything you told. Everything is working fine. Any idea? Thanks for the response. – vik-y Jul 21 '15 at 15:23
  • Can you give us connection details ? How are you connecting SPI pins ? – dhruvvyas90 Jul 21 '15 at 15:56
  • I just connected MSIO pin of RaspberryPi with its MISO (as told in the tutorial i followed). No other connection. – vik-y Jul 21 '15 at 16:35
  • You will need to connect SCLK as well. Otherwise there will be no reference clk between master (Rpi) and slave. Why don't you connect all four lines of SPI bus and see what happens. – dhruvvyas90 Jul 22 '15 at 5:13
4

Error fixed. Thanks to @dastaan for spotting.

My bad. I was using a 26 pin reference and hence was doing a wrong connection. I used this reference given below and it started working for me.

enter image description here

1

It may be because the SPI struture is not fully initialised. This error did not use to matter but now causes problems.

struct spi_ioc_transfer tr = {
        .tx_buf = (unsigned long)tx,
        .rx_buf = (unsigned long)rx,
        .len = ARRAY_SIZE(tx),
        .delay_usecs = delay,
        .speed_hz = speed,
        .bits_per_word = bits,
    };

Perhaps try.

struct spi_ioc_transfer tr;
memset(&tr, 0, sizeof(tr));
tr.tx_buf = (unsigned long)tx;
tr.rx_buf = (unsigned long)rx;
tr.len = ARRAY_SIZE(tx);
tr.delay_usecs = delay;
tr.speed_hz = speed;
tr.bits_per_word = bits;

Edited to add.

The wrong gpios are being used. For the standard SPI device MOSI is pin 19 and MISO is pin 21.

           pin  pin
3V3         1    2      5V
0/2 (SDA)   3    4      5V
1/3 (SCL)   5    6      0V
4           7    8      14 (TXD)
0V          9   10      15 (RXD)
17 (ce1)   11   12      18 (ce0)
21/27      13   14      0V
22         15   16      23
3V3        17   18      24
10 (MOSI)  19   20      0V
9 (MISO)   21   22      25
11 (SCLK)  23   24      8 (CE0)
0V         25   26      7 (CE1)
           .......
0 (ID_SD)  27   28      1 (ID_SC)
5          29   30      0V
6          31   32      12
13         33   34      0V
19 (miso)  35   36      16 (ce2)
26         37   38      20 (mosi)
0V         39   40      21 (sclk)
  • I tried it. It didn't work. Again the same output. What am I might be missing? Any idea? – vik-y Jul 22 '15 at 5:44
  • Do /dev/spidev0.0 and /dev/spidev0.1 exist? Is the spi_bcm2708 or spi_bcm2853 loaded (lsmod)? Are you using the correct gpios? A photo will be useful to check your connections. – joan Jul 22 '15 at 7:21
  • Yes, they do, checked using ls -l /dev/spidev* Yes, lsmod shows spi_bcm2708 I'm connecting the correct pins [Photo Uploaded]. Infact, I tried the same in 2 different pi's, same problem :( What could be the issue? – vik-y Jul 22 '15 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.