Page 1 of 1

Setting speed of /dev/spidev0.0

Posted: Thu Mar 28, 2019 9:23 am
by markvr
(reposting from "Troubleshooting" forum)

Basically: How do I set the speed of the "/dev/spidev0.0" interface when writing data to it?

I have Java and Python programs that control WS2801 lights by just writing to /dev/spidev0.0 as if it were a normal file.

This is convenient, because I don't need extra python or Java libraries, and USED to work fine on an old version of raspian. I've wiped the SD card to install the latest version of raspbian, so not quite sure what version it was, but probably about 2016 or earlier.

Anyway, now the same programs don't work, the LEDs flicker a bit but not controlled.

What DOES work is the adafruit examples: https://github.com/adafruit/Adafruit_Python_WS2801.git

I've read quite a lot about changes to SPI modules (and got a bit lost in it all) and suspect the bus speed might now be wrong for my lights?
Mainly from this thread: https://github.com/raspberrypi/linux/issues/2165

Is there anyway to set the old bus speed back just from the OS? i.e. a boot parameter somewhere? All the examples I've read are using the Python spidev library to set this, but nearly all my code is (a) in Java, and (b) already used to work...!

The docs (https://www.raspberrypi.org/documentati ... /README.md) talk about setting the bus speed, but don't explain how to do it.

Any advice would be great thanks!

Re: Setting speed of /dev/spidev0.0

Posted: Fri Aug 30, 2019 9:28 am
by grifferscott
Hi,
I cant help, I have the same problem I thinks...……… and I've spent months...….. trying to get this working again.
Did you sort it?
or any help much appreciated, code I'm using is Andy Pi pixel lights

Code: Select all

#!/usr/bin/env python
#
# Title:      WS2801 SPI control class for Raspberry Pi
# Author:     AndyPi (http://andypi.co.uk/)
# Based on:   Adafruit https://raw.githubusercontent.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/master/Adafruit_LEDpixels/Adafruit_LEDpixels.py
#
# Hardware: WS2801 pixels, CLOCK=RPi23; Data=RPi19, GND=RpiGND, +5v=Rpi+5v

import RPi.GPIO as GPIO, time, os, sys

class AndyPiPixelLights:

 # import RPi.GPIO as GPIO, time, os
 NUMBER_OF_PIXELS=650 # set number of pixels in your strip
 DEBUG = 1
 GPIO.setmode(GPIO.BCM)

 def slowspiwrite(self, clockpin, datapin, byteout):
	GPIO.setup(clockpin, GPIO.OUT)
	GPIO.setup(datapin, GPIO.OUT)
	for i in range(8):
		if (byteout & 0x80):
			GPIO.output(datapin, True)
		else:
			GPIO.output(clockpin, False)
		byteout <<= 1
		GPIO.output(clockpin, True)
		GPIO.output(clockpin, False)


 SPICLK = 23 # The SPI clock pin on the raspberry pi, pin 23
 SPIDO = 19 # The SPI data line (MOSI) on the raspberry pi, pin 19
 ledpixels = [0] * NUMBER_OF_PIXELS

 def writestrip(self, pixels):
	spidev = file("/dev/spidev0.0", "w")
	for i in range(len(pixels)):
		spidev.write(chr((pixels[i]>>16) & 0xFF))
		spidev.write(chr((pixels[i]>>8) & 0xFF))
		spidev.write(chr(pixels[i] & 0xFF))
	spidev.close()
	time.sleep(0.002)

 def Color(self, r, g, b):
	return ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF)

 def setpixelcolor(self, pixels, n, r, g, b):
	if (n >= len(pixels)):
		return
	pixels[n] = self.Color(r,g,b)

 def setpixelcolor(self, pixels, n, c):
	if (n >= len(pixels)):
		return
	pixels[n] = c

 def colorwipe(self, pixels, c, delay):
	for i in range(len(pixels)):
		self.setpixelcolor(pixels, i, c)
		self.writestrip(pixels)
		time.sleep(delay)		

 def Wheel(self, WheelPos):
	if (WheelPos < 85):
   		return self.Color(WheelPos * 3, 255 - WheelPos * 3, 0)
	elif (WheelPos < 170):
   		WheelPos -= 85;
   		return self.Color(255 - WheelPos * 3, 0, WheelPos * 3)
	else:
		WheelPos -= 170;
		return self.Color(0, WheelPos * 3, 255 - WheelPos * 3)

 def rainbowCycle(self, pixels, wait):
	for j in range(256): # one cycle of all 256 colors in the wheel
    	   for i in range(len(pixels)):
 # tricky math! we use each pixel as a fraction of the full 96-color wheel
 # (thats the i / strip.numPixels() part)
 # Then add in j which makes the colors go around per pixel
 # the % 96 is to make the wheel cycle around
      		self.setpixelcolor(pixels, i, self.Wheel( ((i * 256 / len(pixels)) + j) % 256) )
	   self.writestrip(pixels)
	   time.sleep(wait)
 
 def cls(self, pixels):
          for i in range(len(pixels)):
                self.setpixelcolor(pixels, i, self.Color(0,0,0))
          self.writestrip(pixels)


 def main(self):
   try:  
    self.colorwipe(self.ledpixels, self.Color(255, 0, 0), 0.05)
    self.colorwipe(self.ledpixels, self.Color(0, 255, 0), 0.05)
    self.colorwipe(self.ledpixels, self.Color(0, 0, 255), 0.05)
    self.rainbowCycle(self.ledpixels, 0.00)
    self.cls(self.ledpixels)
   
   except KeyboardInterrupt:
        self.cls(self.ledpixels)
        sys.exit(0)



if  __name__ =='__main__':
        LEDs=AndyPiPixelLights()
        LEDs.main()


thanks Gareth