I would like to write a bash to light up an OLED screen with SSD1306 driver, I can use below code to send commands:

echo -ne $cmd > /dev/spidev0.0

however the SPI clock seems too fast for SSD1306, is there a way to slowdown the clock (to like 4Mhz) in the shell? thanks for any advice, I am using pi zero W.

  • I used to use python to change SPI speed. I found it easy. I vaguely remember that I don't even need to reboot. I used to use 400kHz and sometimes down to even 10kHz, without any problem. BTW, your bash command doesn't seem to change the speed. – tlfong01 Jul 30 '19 at 1:39
  • Details please. Does the oled work at all? Photos of the oled and wiring. Are you sure it's not I2C? – joan Jul 30 '19 at 3:56
  • hi Joan and tlfong01, thanks for comments. the OLED doesn't work at all, and yes it is SPI for sure, it works in my python code. I tried to adjust the clock in python, I found if I set to >16Mhz it will stop working, but works fine with clk lower than 16Mhz. the reason I want to use bash is because python is too slow at loading. what I want is user can see the display quickly after power up. any advice to achieve that? – Xuming Deng Jul 30 '19 at 5:27
  • If it doesn't work at all that doesn't prove it's a clock problem. I't just as likely to be a problem with the commands you are sending via bash. You need to add detail in your question. – joan Jul 30 '19 at 8:38

Sorry it's not a complete answer, but my experience so far

  • using the Python3 spidev library,
    • I can open /dev/spidev0.0 or /dev/spidev0.1,
    • I can set the clock speed (noting that this is not a precise frequency, but something like nearest power of two)
    • I can write data, via Python or directly with echo foo > /dev/spidev0.$N
    • It comes out on BCM10, BCM11.
    • BCM7 or BCM8 are selected for each device, sounds like an active high.
    • I'm hearing it with a pair of headphones (not recommended!) because I have no better kit handy.
    • after calling the close() method on the SpiDev object, data is either not written to the port anymore with echo or it happens at a speed too fast to hear on headphones.
  • I would guess that some kind of IOCTL call on the device sets the speed, and you might find this in the source for the Python library or the .so it calls. Sorry I haven't looked for you.
  • None of this is what I wanted, because I soldered my connector expecting to use the SPI1 bus on BCM20,BCM21... a separate question.

I'm using a Pi Zero v1.3

  • my separate SPI1 bus question is raspberrypi.stackexchange.com/questions/73346/… – mcast Jul 30 '19 at 16:00
  • SPI isn't a secret. The SPI chip select defaults to active low, i.e. if a chip is selected its chip select line will be held low. An IOCTL is used to set the speed and perform the transfer, example C code at abyz.me.uk/rpi/pigpio/code/spi-driver-speed.c – joan Jul 30 '19 at 17:42
  • I know it's not a secret, but I haven't read all the docs. I start with the obvious docs, then continue until I seem to have enough. – mcast Jul 30 '19 at 21:14

Build (make; sudo make install) this ioctl tool. Then you should be able to issue IOCTL requests supported by the SPI driver:

SPI_IOC_RD_MAX_SPEED_HZ, SPI_IOC_WR_MAX_SPEED_HZ ... pass a pointer to a u32 which will return (RD) or assign (WR) the maximum SPI transfer speed, in Hz. The controller can't necessarily assign that specific clock speed.

If you happen to use more scripting languages than just BASH, there may be easier ways, e.g. in Perl:

require "sys/ioctl.ph";

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.