-1

I enabled SPI on my Raspberry pi 4 like in many Tutorials explained.

In sudo raspi-config I enabled SPI. In /boot/config.txt I added dtparam=spi=on

$ ls -l /dev/spidev*** shows

crw-rw---- 1 root spi 153, 0 Jul 23 12:43 /dev/spidev0.0

crw-rw---- 1 root spi 153, 1 Jul 23 12:43 /dev/spidev0.1

$ lsmod | grep -i spi** shows

spidev                 20480  0

spi_bcm2835            24576  0

But if I try a SPI Loopbacktest https://github.com/rm-hull/spidev-test

it shows

$ ./spidev_test -v

spi mode: 0x4
bits per word: 8
max speed: 500000 Hz (500 KHz)
TX | 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 F0 0D  | ......@....?..................?.
RX | 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  | ................................

until I write:

$ sudo pigpiod

$ pigs spio 0 50000 0**

in the Console

Then the Loopbacktest works and it shows:

spi mode: 0x4
bits per word: 8
max speed: 500000 Hz (500 KHz)
TX | 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 F0 0D  | ......@....�..................�.
RX | 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 F0 0D  | ......@....�..................�.

EDIT:

With another Script based on spidev I have the same problems.

from   time import sleep
import spidev

spiPort0 = spidev.SpiDev()
spiPort0.open(0,0)
spiPort0.max_speed_hz = 100000

def spiSendRecvOneByte(spiPort, sendByte):
    sendByteArray = [sendByte]
    recvByteArray = spiPort.xfer(sendByteArray)    
    return recvByteArray

def repeatSendOneByte(spiPort, sendByte, pauseTimeBetweenBytes, repeatCount):
    print('\nBegin repeatSendByte(),....')
    for i in range(repeatCount):
        spiSendRecvOneByte(spiPort, sendByte)
        sleep(pauseTimeBetweenBytes)
    print('End   repeatSendByte().')
    return

def loopBackOneByte(spiPort, sendByte):
    recvByteArray     = spiSendRecvOneByte(spiPort, sendByte)
    recvByte          = recvByteArray[0]

    print('\nBegin testLoopbackOneByte(),....')
    #print('')
    print('      sendByte  = ', hex(sendByte))
    print('      recvByte  = ', hex(recvByte))
    #print('')
    print('End   testLoopbackOneByte(),....')
    return

def testRepeatSendOneByte():
    repeatSendOneByte(spiPort0, 0x5b, 0.0001, 2000000)
    return

def testLoopbackOneByte():
    loopBackOneByte(spiPort0, 0x5b)
    return

#testRepeatSendOneByte()
testLoopbackOneByte()

''' Smple output tlfong 01 2019apr07hkt2047
Begin testLoopbackOneByte(),....
      sendByte  =  0x5b
      recvByte  =  0x5b
End   testLoopbackOneByte(),....
'''

# *** End ***

When I reboot the system SPI is not working and I have to write the commands angain. I can't explain this situation and I was searching a lot for this Error.

I would be very happy if someone could help me and explain to me why SPI works after these commands and what I could do to keep it like this after the restart.

EDIT: I got a solution in newinstalling the Raspberry pi OS. Now SPI loopbacktest is working fine and also a small Project with the MCP3002 analog to digital converter

Best Regards

Sven

0

I think it is a fault in spidev_test.c. I note it hasn't been updated for several years.

There was a change in the Linux SPI driver which meant it was important to zero the spi_ioc_transfer structure to make sure it was properly initialised.

Make the following change to your copy of spidev_test.c

From line 116.

struct spi_ioc_transfer tr;

memset(&tr, 0, sizeof(tr));

tr.tx_buf = (unsigned long)tx;
tr.rx_buf = (unsigned long)rx;
tr.len = len;
tr.delay_usecs = delay;
tr.speed_hz = speed;
tr.bits_per_word = bits;

pigpio/pigs is not relevant as it does not use the Linux SPI driver.

12
  • I changed the script. Still it dosen't worked without "$sudo pigpiod, $pigs spio 0 50000 0" commands.
    – h4l01991
    Dec 16 '20 at 23:52
  • I also tested a other Loopbacktest based on spidev. There is the same error.
    – h4l01991
    Dec 16 '20 at 23:53
  • Both scripts are working fine when I added the pigpiods commands first
    – h4l01991
    Dec 16 '20 at 23:54
  • I wonder if the other scripts aren't setting the speed properly.
    – joan
    Dec 17 '20 at 8:57
  • It's the same, I tried to set the speed to "spiPort0.max_speed_hz = 50000"
    – h4l01991
    Dec 17 '20 at 10:02

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.