Raspi spidev dual chip select pulse

I am trying to use a new raspi 4 to talk with another microprocessor. I would like to use the SPI protocol to communicate. I am having issues with the spidev trans and trans2 functions when sending out simple values ( like number ascii in hex 0X31 ). With or without the wires connected to the microprocessor there is a second chip select pulse. I have tried looking at the raspi forums, and stackoverflow, but I can’t seem to find a solution. I have looked and searched in core electronic forums, and did not see anything there. I don’t want to use a “gpio pin” for the chip select due to the amount of time it takes for the pin to toggle on and off ( what should be 60usec for spi, turns into almost 115 usec with second pulse, which turns into 185 usec using a gpio pin).



I have just started using the raspi 4 boards after I watched the oakd video, and purchased an oak d to put on my rover, so I am not that great at programming in Python, but it is a very simple test to see if all of the spi functions are working. This is the code:
import spidev
import time
import RPi.GPIO as GPIO

spi = spidev.SpiDev() # create spi object
#GPIO.setmode(GPIO.BCM)
#GPIO.setmode(GPIO.BOARD)
#GPIO.setup(22, GPIO.OUT, initial=GPIO.HIGH)

spi.open(0, 0) # open spi port 0, device (CS) 1

#spi.max_speed_hz =1000000
spi.max_speed_hz =500000
#try:

#GPIO.setup(22, GPIO.OUT)
#GPIO.output(22, GPIO.HIGH)
while True:
#GPIO.output(22, GPIO.LOW)
# resp = spi.xfer2([0x35,0x41,0x42]) # transfer one byte
# resp = spi.xfer2([0x35,0x41,0x42],1) # transfer one byte
# time.sleep(0.0001) # sleep for 0.1 seconds
resp = spi.xfer([0x36]) # transfer one byte

    #resp = spi.xfer([0x44]) # transfer one byte
    #resp = spi.xfer([0x45]) # transfer one byte
   # GPIO.output(22, GPIO.HIGH)
    time.sleep(.5) # sleep for 0.1 seconds
   # GPIO.output(22, GPIO.LOW)
   # resp = spi.xfer2([0x35,0x41,0x42]) # transfer one byte
   # resp = spi.xfer2([0x35,0x41,0x42],1) # transfer one byte
   # time.sleep(0.0001) # sleep for 0.1 seconds
    resp = spi.xfer([0x31]) # transfer one byte

    #resp = spi.xfer([0x44]) # transfer one byte
    #resp = spi.xfer([0x45]) # transfer one byte
   # GPIO.output(22, GPIO.HIGH)
    time.sleep(.5) # sleep for 0.1 seconds
   # GPIO.output(22, GPIO.LOW) 
   # resp = spi.xfer2([0x35,0x41,0x42]) # transfer one byte
   # resp = spi.xfer2([0x35,0x41,0x42],1) # transfer one byte
   # time.sleep(0.0001) # sleep for 0.1 seconds
    resp = spi.xfer([0x32]) # transfer one byte

    #resp = spi.xfer([0x44]) # transfer one byte
    #resp = spi.xfer([0x45]) # transfer one byte
   # GPIO.output(22, GPIO.HIGH)
    time.sleep(.5) # sleep for 0.1 seconds

#end while
#except KeyboardInterrupt: # Ctrl+C pressed, so…

spi.close() # … close the port before exit

#end try

Again, just trying to send simple hex values out. This appears to be a very old problem, possibly dating back to 2012, but I still can’t seem to find a solution–that I can implement.

Hi Donald
Welcome
I don’t profess to know anything about what you are trying to achieve but I think you need a separate GPIO pin for every “chip select” that you use. This would be completely separate to any data pins.
Cheers Bob

Is there more then one device on the SPI Bus, or just your master and slave?

In theory, if just two devices, you could tie the slave CS to selected. and not set at the master (as its always selected).

Thank you for your quick reply.

Right now I am only using one microprocessor to communicate back and forth to the raspi. The problem that I am having is that the raspi (at least my perspective out of compliance with the standard) is sending two chip selects. One with the information, and then a second at some unknown time frame–without any information, or clock.

It creates a problem for the Interrupt Service Routine (ISR) of the processor that I am using. When the first chip select goes low, it toggles the ISR and retrieves the information and returns. When the second chip select appears, there is no information or clock, but the ISR is triggered and now waits until the microprocessor has a time out. Since there was no information–I am guessing the receiving microprocessor thinks it received a “Null”, this seems to be creating more problems the faster that I run the Python program ( right now it is very slow with 3 timeouts a 1.5 seconds).

After I understand and can get rid of the second chip select pulse, I do plan on hooking up a second microprocessor–and using the second chip select output to control information back and forth to that chip.

This is not a “new problem” it has been around a while. There is a video from ADAFRUIT that has video of the second pulse occuring 8 years ago ( raspberry pi and python spi deep dive with tond@adafruit live ) at 55:37 into the video ( he has inverted the chip select on purpose in the video, but it shows up earlier in the video also.

I don’t know if there is a different way for the pi to use spi, other than “spidev”.

It is important to me to transmit and receive, because the rover that I am building will operate wireless, from my computer to the raspi, and to keep from having incorrect commands transmitted, I run a function with my computer program that won’t send out any commands until the information received matches the information sent. This is simply because I have had issues with ADC values, and my bad programming habits allowed a motor to turn on full force instead of slowing down because there was a glitch in the information received.

Im not really a big Pi user, nor python. So this is more from a github search.

I note this is talking about that “extra” CS.

Src : py-spidev/spidev_module.c at master · doceme/py-spidev · GitHub

Maybe have a look as see if its a fix for your issue then see if you can set the needed flag (I think it was Read0; but I did not spend enough time going over the source.

Good evening Michael,

thank you for looking into this some more for me. I have come across this, but I have a problem. I don’t know how to pull this code into the raspberry pi, so that it can be used, I just started looking at how the Raspi could be used 4 months ago.

So I watched a video and figured out how to get something out of the repository, and I have it installed under a folder called test. My question is–with the python code that I have would I delete the “import spidev” and put in “import pi-spidev”?

Hello Micheal,

After doing more research I found another link with the same problem.

https://github.com/doceme/py-spidev/issues/113

This could occur on different platforms other that raspi, in this link, it was also with the beaglebone.

You were really helpful, and I really appreciate you looking into this.

The main key to the fix was to comment out three lines of code ( 611 730 and 883), then re-perform the setup using:

sudo python3 setup.py install, while still in the same directory.

So now I have to try to find how many other places in my raspi that have the spidev .

I really hope I can become better at programming.

Thank you

Good to hear you got the patch working and it fixed your issue.

Hi Donald
As I said I am a complete dummy when it comes to RPi and Python.
But. Looking at your screen shots I would guess that Ch 1 is chip select, Ch 2 is clock and Ch 3 is data. Could be wrong but I don’t think so.
It is my assumption then that when you call the line "

the chip select is generated and taken care of within the SPI library.
Surely then the origin of this second pulse is within said library and has nothing to do with what you do but is a built in problem. It is possible then you could be looking for a solution in the wrong place. Like trying to put a band aid on it instead of getting to the root cause.

Is there possibly another SPI library to do this job by another author you can try. I think diving into the library itself would require some in depth knowledge of Python or whatever language is used. I suggest this as I think Arduino often have libraries by different authors to do the same job. Probably all with their own Pros and Cons.
Cheers Bob

Good evening/morning Robert,

Michael and you have both been helping me on this problem, I was able to get it sorted out, here is my response back to michael

After doing more research I found another link with the same problem.

https://github.com/doceme/py-spidev/issues/113

This could occur on different platforms other that raspi, in this link, it was also with the beaglebone.

You were really helpful, and I really appreciate you looking into this.

The main key to the fix was to comment out three lines of code ( 611 730 and 883), then re-perform the setup using:

sudo python3 setup.py install, while still in the same directory.

So now I have to try to find how many other places in my raspi that have the spidev .

I really hope I can become better at programming.

Thank you

2 Likes