zmatt

BBB SPI tutorial

Apr 29th, 2020 (edited)
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. # this uses:
  2. # P9.22 clock out (sck)
  3. # P9.18 data out (mosi)
  4. # P9.21 data in (miso)
  5. # P9.17 chip select out (cs)
  6. # for testing connect P9.18 (data out) to P9.21 (data in)
  7.  
  8. ### This assumes you're using the latest debian IoT image
  9. # currently: debian buster (10.3) 2020-04-06 IoT
  10. #
  11. # note: I recommend flashing to eMMC, unless you have a very old beaglebone with only 2GB of eMMC.
  12. # If you're going to run from SD card instead of flashing to eMMC, it is strongly advised to
  13. # wipe eMMC (using "sudo blkdiscard /dev/mmcblk1") since an old bootloader on eMMC can cause all
  14. # sorts of mysterious problems.
  15.  
  16.  
  17. ### configure pins:
  18. # You can _either_ use runtime pin configuration (aka "cape-universal") _or_ use an overlay.
  19.  
  20. # option 1: configure the pins at runtime using the config-pin utility:
  21. config-pin P9_22 spi_sclk
  22. config-pin P9_21 spi
  23. config-pin P9_18 spi
  24. config-pin P9_17 spi_cs
  25.  
  26. # option 2: configure the pins at runtime using pure python:
  27. # save https://pastebin.com/raw/MKtWJ8G8 as bone_pinmux.py
  28. from bone_pinmux import set_pinmux_state
  29. set_pinmux_state( 'P9_22', 'spi_sclk' )
  30. set_pinmux_state( 'P9_21', 'spi' )
  31. set_pinmux_state( 'P9_18', 'spi' )
  32. set_pinmux_state( 'P9_17', 'spi_cs' )
  33.  
  34. # option 3: enable the BB-SPIDEV0 overlay in /boot/uEnv.txt, e.g.:
  35. uboot_overlay_addr4=/lib/firmware/BB-SPIDEV0-00A0.dtbo
  36. # (you can use any of uboot_overlay_addr4..7 or dtb_overlay for this)
  37.  
  38.  
  39. ### use device:
  40.  
  41. # option 1: using Adafruit_BBIO (installed by default):
  42.  
  43. from Adafruit_BBIO.SPI import SPI
  44. spi = SPI(0, 0)
  45. spi.mode = 3
  46. out_data = [ 111, 222 ]
  47. in_data = spi.xfer( out_data )
  48. print( in_data )
  49.  
  50.  
  51. # option 2: using py-spidev (install using "pip3 install spidev", do not use sudo):
  52.  
  53. from spidev import SpiDev
  54. spi = SpiDev(0, 0)
  55. spi.mode = 3
  56. out_data = [ 111, 222 ]
  57. in_data = spi.xfer( out_data )
  58. print( in_data )
Add Comment
Please, Sign In to add comment