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