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