zmatt icon

BBB SPI tutorial

zmatt | PRO | 03/08/21 04:58:04 PM UTC (Edited) | 0 ⭐ | 587 👁️ | Never ⏰ | []
text |

1.84 KB

|

None

|

0 👍

/

0 👎

# this uses:
#    P9.22 clock out (sck)
#    P9.18 data out (mosi)
#    P9.21 data in (miso)
#    P9.17 chip select out (cs)
# for testing connect P9.18 (data out) to P9.21 (data in)
 ### This assumes you're using the latest debian IoT image
# currently: debian buster (10.3) 2020-04-06 IoT
#
# note: I recommend flashing to eMMC, unless you have a very old beaglebone with only 2GB of eMMC.
# If you're going to run from SD card instead of flashing to eMMC, it is strongly advised to
# wipe eMMC (using "sudo blkdiscard /dev/mmcblk1") since an old bootloader on eMMC can cause all
# sorts of mysterious problems.
  ### configure pins:
# You can _either_ use runtime pin configuration (aka "cape-universal") _or_ use an overlay.
 # option 1:  configure the pins at runtime using the config-pin utility:
config-pin P9_22 spi_sclk
config-pin P9_21 spi
config-pin P9_18 spi
config-pin P9_17 spi_cs
 # option 2:  configure the pins at runtime using pure python:
# save https://pastebin.com/raw/MKtWJ8G8 as bone_pinmux.py
from bone_pinmux import set_pinmux_state
set_pinmux_state( 'P9_22', 'spi_sclk' )
set_pinmux_state( 'P9_21', 'spi' )
set_pinmux_state( 'P9_18', 'spi' )
set_pinmux_state( 'P9_17', 'spi_cs' )
 # option 3:  enable the BB-SPIDEV0 overlay in /boot/uEnv.txt, e.g.:
uboot_overlay_addr4=/lib/firmware/BB-SPIDEV0-00A0.dtbo
# (you can use any of uboot_overlay_addr4..7 or dtb_overlay for this)
  ### use device:
 # option 1: using Adafruit_BBIO (installed by default):
 from Adafruit_BBIO.SPI import SPI
spi = SPI(0, 0)
spi.mode = 3
out_data = [ 111, 222 ]
in_data = spi.xfer( out_data )
print( in_data )
  # option 2: using py-spidev (install using "pip3 install spidev", do not use sudo):
 from spidev import SpiDev
spi = SpiDev(0, 0)
spi.mode = 3
out_data = [ 111, 222 ]
in_data = spi.xfer( out_data )
print( in_data )

Comments