Skip to content

Instantly share code, notes, and snippets.

@pdp7
Last active February 14, 2022 18:29
Show Gist options
  • Save pdp7/33a8ad95efcbcc0fadc3f96a70d4b159 to your computer and use it in GitHub Desktop.
Save pdp7/33a8ad95efcbcc0fadc3f96a70d4b159 to your computer and use it in GitHub Desktop.
beaglebone-spidev-test

Loopback

/boot/uEnv.txt

uname_r=4.4.39-ti-r79
cmdline=coherent_pool=1M quiet cape_universal=enable
cape_enable=bone_capemgr.enable_partno=cape-universala

config-pin

for i in 17 18 19 20 21 22 28 29 30 31 42; 
do echo config-pin P9.$i spi;
done
config-pin P9.17 spi
config-pin P9.18 spi
config-pin P9.19 spi
config-pin P9.20 spi
config-pin P9.21 spi
config-pin P9.22 spi
config-pin P9.28 spi
config-pin P9.29 spi
config-pin P9.30 spi
config-pin P9.31 spi
config-pin P9.42 spi

spidev_test:

Source: https://raw.githubusercontent.com/torvalds/linux/master/tools/spi/spidev_test.c

debian@beaglebone:~/spi$ sudo ./spidev_test --device /dev/spidev1.0
spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 KHz)

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 
DE AD BE EF BA AD 
F0 0D 
debian@beaglebone:~/spi$ sudo ./spidev_test --device /dev/spidev1.1
spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 KHz)

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 
DE AD BE EF BA AD 
F0 0D 
debian@beaglebone:~/spi$ sudo ./spidev_test --device /dev/spidev2.0
spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 KHz)

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 
DE AD BE EF BA AD 
F0 0D 
debian@beaglebone:~/spi$ sudo ./spidev_test --device /dev/spidev2.1
spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 KHz)

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 
DE AD BE EF BA AD 
F0 0D 

Adafruit_BBIO SPI test

test-spi.py

from Adafruit_BBIO.SPI import SPI

# From Adafruit Learning System:
# https://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black/spi     
#spi = SPI(bus, device) #/dev/spidev<bus>.<device>
#spi = SPI(0,0)	#/dev/spidev1.0
#spi = SPI(0,1)	#/dev/spidev1.1
#spi = SPI(1,0)	#/dev/spidev2.0
#spi = SPI(1,1)	#/dev/spidev2.1

spi = SPI(0,0) 
print spi.xfer2([32, 11, 110, 22, 220]) 
spi.close() 

output

debian@beaglebone:~/spi$ sudo python test-spi.py 
[32, 11, 110, 22, 220]

strace

debian@beaglebone:~/spi$ sudo strace -e open -f python ./test-spi.py 2>&1 |grep /dev/spidev
open("/dev/spidev1.0", O_RDWR|O_LARGEFILE) = 3
@Bradleyking4
Copy link

Bradleyking4 commented Apr 18, 2018

in the latest kernel 4.4.12

for i in 17 18 19 20 21 22 28 29 30 31 42;
do echo config-pin P9.$i spi;
done
no longer works.

for spidev1.0
config-pin P9.17 spi_cs
config-pin P9_18 spi
config-pin P9_21 spi
config-pin P9.22 spi_sclk

for spidev2.0
config-pin P9.31 spi_sclk
config-pin P9.28 spi_cs
config-pin P9.30 spi
config-pin P9.29 spi

@VarunPwr
Copy link

I tried the above steps but did not work in my case. Here is the log :
spi mode: 0x0
bits per word: 8
max speed: 500000 Hz (500 KHz)
Could you help me?

@psarvesh
Copy link

@VarunPwr I think you might have solved this already. But if not, you can add the --verbose option to see the TX/RX buffer.

@MVGavrilov
Copy link

MVGavrilov commented Mar 10, 2020

@VarunPwr I think you might have solved this already. But if not, you can add the --verbose option to see the TX/RX buffer.

debian@beaglebone:~/tmp$ sudo ./a.out --device /dev/spidev2.1 --verbose
spi mode: 0x0
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 |................................|

@pdp7
Copy link
Author

pdp7 commented Mar 10, 2020

Hello if people are still having issues please open an issue in
https://github.com/adafruit/adafruit-beaglebone-io-python/

thanks!

@MVGavrilov
Copy link

Hello if people are still having issues please open an issue in
https://github.com/adafruit/adafruit-beaglebone-io-python/

thanks!

Thanks a lot, it works now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment