Difference between revisions of "BeagleBone Black Enable SPIDEV"
(added a warning about SPI not working unless enabled at boot) |
|||
(15 intermediate revisions by 3 users not shown) | |||
Line 30: | Line 30: | ||
= Enabling SPIDEV with Cape Manager = | = Enabling SPIDEV with Cape Manager = | ||
<br> | <br> | ||
− | |||
<div style="margin:0; margin-top:10px; margin-right:10px; border:1px solid #dfdfdf; padding:0 1em 1em 1em; background-color:red; align:right; "> | <div style="margin:0; margin-top:10px; margin-right:10px; border:1px solid #dfdfdf; padding:0 1em 1em 1em; background-color:red; align:right; "> | ||
− | '''NOTE:''' You CAN NOT use SPI1 on the BeagleBone Black WITHOUT disabling the HDMI interface | + | '''NOTE:''' You CAN NOT use SPI1 on the BeagleBone Black WITHOUT disabling the HDMI interface<br> |
+ | '''NOTE:''' You CAN NOT use either SPI0 or SPI1 at runtime, IT MUST BE ENABLED AT BOOT in uEnv.txt | ||
</div> | </div> | ||
+ | <br> | ||
+ | <br> | ||
+ | == SPI1 D1 Output and D0 Input == | ||
+ | |||
Start nano | Start nano | ||
<pre> | <pre> | ||
Line 39: | Line 43: | ||
</pre> | </pre> | ||
<br> | <br> | ||
− | Copy-paste this into the file(or download a copy [[media:BB-SPI1-01-00A0.txt|BB-SPI1-00A0]]): | + | Copy-paste this into the file(or download a copy [[media:BB-SPI1-STD-01-00A0.txt|BB-SPI1-STD-01-00A0]]): |
<pre> | <pre> | ||
/dts-v1/; | /dts-v1/; | ||
/plugin/; | /plugin/; | ||
+ | |||
+ | /* SPI1 */ | ||
+ | /* D1 Output and D0 Input */ | ||
/ { | / { | ||
Line 73: | Line 80: | ||
pinctrl-names = "default"; | pinctrl-names = "default"; | ||
pinctrl-0 = <&spi1_pins_s0>; | pinctrl-0 = <&spi1_pins_s0>; | ||
+ | |||
+ | spidev@1 { | ||
+ | spi-max-frequency = <24000000>; | ||
+ | reg = <0>; | ||
+ | compatible = "linux,spidev"; | ||
+ | }; | ||
+ | }; | ||
+ | }; | ||
+ | }; | ||
+ | </pre> | ||
+ | Close the file (Ctrl-o, Ctrl-x) and compile using dtc: | ||
+ | <pre> | ||
+ | dtc -O dtb -o BB-SPI1-01-00A0.dtbo -b 0 -@ BB-SPI1-01-00A0.dts | ||
+ | </pre> | ||
+ | Then, copy the file into /lib/firmware/: | ||
+ | <pre> | ||
+ | cp BB-SPI1-01-00A0.dtbo /lib/firmware/ | ||
+ | </pre> | ||
+ | Then enable the device tree overlay: | ||
+ | <pre> | ||
+ | echo BB-SPI1-01 > /sys/devices/bone_capemgr.*/slots | ||
+ | </pre> | ||
+ | |||
+ | Plug in your BBB to a host computer using the mini usb data cable. | ||
+ | |||
+ | Go to My Computer>BeagleBone Getting Started> and open uEnv.txt | ||
+ | Copy and paste this command into the .txt file. Make sure to save your changes. | ||
+ | (Ctrl+s) | ||
+ | |||
+ | <pre> | ||
+ | optargs=quiet drm.debug=7 capemgr.disable_partno=BB-BONELT-HDMI,BB-BONELT-HDMIN | ||
+ | capemgr.enable_partno=BB-SPI1-01 | ||
+ | </pre> | ||
+ | after you save the changes, reboot your beaglebone black | ||
+ | <br> | ||
+ | Make sure it is enabled | ||
+ | You should now have two spidev-files in the folder /dev/ | ||
+ | <pre> | ||
+ | ls -al /dev/spidev1.* | ||
+ | </pre> | ||
+ | You should also be able to see the pingroups: | ||
+ | <pre> | ||
+ | cat /sys/kernel/debug/pinctrl/44e10800.pinmux/pingroups | ||
+ | </pre> | ||
+ | <br> | ||
+ | <br> | ||
+ | |||
+ | == SPI1 D0 Output and D1 Input == | ||
+ | NOTE: an option is available to swap the MISO/MOSI pins using the device tree entry. to enable the pin swap change the feature section by adding “ti,pindir-d0-out-d1-in" property and changing the default pin mux for the two data pins | ||
+ | |||
+ | Start nano | ||
+ | <pre> | ||
+ | nano BB-SPI1-01-00A0.dts | ||
+ | </pre> | ||
+ | <br> | ||
+ | Copy-paste this into the file(or download a copy [[media:BB-SPI1-SWP-01-00A0.txt|BB-SPI1-SWP-01-00A0]]): | ||
+ | <pre> | ||
+ | /dts-v1/; | ||
+ | /plugin/; | ||
+ | |||
+ | /* SPI1 */ | ||
+ | /* D0 Output and D1 Input */ | ||
+ | |||
+ | / { | ||
+ | compatible = "ti,beaglebone", "ti,beaglebone-black"; | ||
+ | |||
+ | /* identification */ | ||
+ | part-number = "spi1mux"; | ||
+ | |||
+ | fragment@0 { | ||
+ | target = <&am33xx_pinmux>; | ||
+ | __overlay__ { | ||
+ | spi1_pins_s0: spi1_pins_s0 { | ||
+ | pinctrl-single,pins = < | ||
+ | 0x190 0x33 /* mcasp0_aclkx.spi1_sclk, INPUT_PULLUP | MODE3 */ | ||
+ | 0x194 0x13 /* mcasp0_fsx.spi1_d0, OUTPUT_PULLUP | MODE3 */ | ||
+ | 0x198 0x33 /* mcasp0_axr0.spi1_d1, INPUT_PULLUP | MODE3 */ | ||
+ | 0x19c 0x13 /* mcasp0_ahclkr.spi1_cs0, OUTPUT_PULLUP | MODE3 */ | ||
+ | >; | ||
+ | }; | ||
+ | }; | ||
+ | }; | ||
+ | |||
+ | fragment@1 { | ||
+ | target = <&spi1>; | ||
+ | __overlay__ { | ||
+ | |||
+ | #address-cells = <1>; | ||
+ | #size-cells = <0>; | ||
+ | status = "okay"; | ||
+ | pinctrl-names = "default"; | ||
+ | pinctrl-0 = <&spi1_pins_s0>; | ||
+ | ti,pindir-d0-out-d1-in = <1>; | ||
spidev@1 { | spidev@1 { | ||
Line 173: | Line 273: | ||
dtc -O dtb -o BB-SPI0-01-00A0.dtbo -b 0 -@ BB-SPI0-01-00A0.dts | dtc -O dtb -o BB-SPI0-01-00A0.dtbo -b 0 -@ BB-SPI0-01-00A0.dts | ||
</pre> | </pre> | ||
+ | (if dtc complains about the -@, you need to get a newer version of dtc. See this [http://learn.adafruit.com/introduction-to-the-beaglebone-black-device-tree/compiling-an-overlay adafruit] link for a script that will retrieve and compile a suitable version of dtc) | ||
+ | |||
Then, copy the file into /lib/firmware/: | Then, copy the file into /lib/firmware/: | ||
<pre> | <pre> | ||
Line 182: | Line 284: | ||
</pre> | </pre> | ||
+ | Plug in your BBB to a host computer using the mini usb data cable. | ||
+ | Go to My Computer>BeagleBone Getting Started> and open uEnv.txt Copy and paste this command into the .txt file. Make sure to save your changes. (Ctrl+s) | ||
<pre> | <pre> | ||
optargs=quiet drm.debug=7 capemgr.enable_partno=BB-SPI0-01 | optargs=quiet drm.debug=7 capemgr.enable_partno=BB-SPI0-01 | ||
Line 218: | Line 322: | ||
</pre> | </pre> | ||
− | 3. Run the spidev_test program (sources in kernel/Documentation/spi), the | + | 3. Run the spidev_test program (sources in kernel/Documentation/spi), the following is a typical log: |
<pre> | <pre> | ||
bash-4.2# /spidev_test -D /dev/spidev1.0 | bash-4.2# /spidev_test -D /dev/spidev1.0 |
Revision as of 01:00, 19 April 2017
Contents
SPIDEV Interface
Description
SPI devices have a limited userspace API, supporting basic half-duplex read() and write() access to SPI slave devices. Using ioctl() requests, full duplex transfers and device I/O configuration are also available.
#include <fcntl.h> #include <unistd.h> #include <sys/ioctl.h> #include <linux/types.h> #include <linux/spi/spidev.h>
Some reasons you might want to use this programming interface include:
- Prototyping in an environment that's not crash-prone; stray pointers in userspace won't normally bring down any Linux system.
- Developing simple protocols used to talk to microcontrollers acting as SPI slaves, which you may need to change quite often.
Of course there are drivers that can never be written in userspace, because
they need to access kernel interfaces (such as IRQ handlers or other layers
of the driver stack) that are not accessible to userspace.
References
- SPIDEV Kernel Documentation
- SPI Wikipedia Page
- SPIDEV on PandaBoard.org (good examples)
- spidev-test application
Enabling SPIDEV with Cape Manager
NOTE: You CAN NOT use SPI1 on the BeagleBone Black WITHOUT disabling the HDMI interface
NOTE: You CAN NOT use either SPI0 or SPI1 at runtime, IT MUST BE ENABLED AT BOOT in uEnv.txt
SPI1 D1 Output and D0 Input
Start nano
nano BB-SPI1-01-00A0.dts
Copy-paste this into the file(or download a copy BB-SPI1-STD-01-00A0):
/dts-v1/; /plugin/; /* SPI1 */ /* D1 Output and D0 Input */ / { compatible = "ti,beaglebone", "ti,beaglebone-black"; /* identification */ part-number = "spi1mux"; fragment@0 { target = <&am33xx_pinmux>; __overlay__ { spi1_pins_s0: spi1_pins_s0 { pinctrl-single,pins = < 0x190 0x33 /* mcasp0_aclkx.spi1_sclk, INPUT_PULLUP | MODE3 */ 0x194 0x33 /* mcasp0_fsx.spi1_d0, INPUT_PULLUP | MODE3 */ 0x198 0x13 /* mcasp0_axr0.spi1_d1, OUTPUT_PULLUP | MODE3 */ 0x19c 0x13 /* mcasp0_ahclkr.spi1_cs0, OUTPUT_PULLUP | MODE3 */ >; }; }; }; fragment@1 { target = <&spi1>; __overlay__ { #address-cells = <1>; #size-cells = <0>; status = "okay"; pinctrl-names = "default"; pinctrl-0 = <&spi1_pins_s0>; spidev@1 { spi-max-frequency = <24000000>; reg = <0>; compatible = "linux,spidev"; }; }; }; };
Close the file (Ctrl-o, Ctrl-x) and compile using dtc:
dtc -O dtb -o BB-SPI1-01-00A0.dtbo -b 0 -@ BB-SPI1-01-00A0.dts
Then, copy the file into /lib/firmware/:
cp BB-SPI1-01-00A0.dtbo /lib/firmware/
Then enable the device tree overlay:
echo BB-SPI1-01 > /sys/devices/bone_capemgr.*/slots
Plug in your BBB to a host computer using the mini usb data cable.
Go to My Computer>BeagleBone Getting Started> and open uEnv.txt Copy and paste this command into the .txt file. Make sure to save your changes. (Ctrl+s)
optargs=quiet drm.debug=7 capemgr.disable_partno=BB-BONELT-HDMI,BB-BONELT-HDMIN capemgr.enable_partno=BB-SPI1-01
after you save the changes, reboot your beaglebone black
Make sure it is enabled
You should now have two spidev-files in the folder /dev/
ls -al /dev/spidev1.*
You should also be able to see the pingroups:
cat /sys/kernel/debug/pinctrl/44e10800.pinmux/pingroups
SPI1 D0 Output and D1 Input
NOTE: an option is available to swap the MISO/MOSI pins using the device tree entry. to enable the pin swap change the feature section by adding “ti,pindir-d0-out-d1-in" property and changing the default pin mux for the two data pins
Start nano
nano BB-SPI1-01-00A0.dts
Copy-paste this into the file(or download a copy BB-SPI1-SWP-01-00A0):
/dts-v1/; /plugin/; /* SPI1 */ /* D0 Output and D1 Input */ / { compatible = "ti,beaglebone", "ti,beaglebone-black"; /* identification */ part-number = "spi1mux"; fragment@0 { target = <&am33xx_pinmux>; __overlay__ { spi1_pins_s0: spi1_pins_s0 { pinctrl-single,pins = < 0x190 0x33 /* mcasp0_aclkx.spi1_sclk, INPUT_PULLUP | MODE3 */ 0x194 0x13 /* mcasp0_fsx.spi1_d0, OUTPUT_PULLUP | MODE3 */ 0x198 0x33 /* mcasp0_axr0.spi1_d1, INPUT_PULLUP | MODE3 */ 0x19c 0x13 /* mcasp0_ahclkr.spi1_cs0, OUTPUT_PULLUP | MODE3 */ >; }; }; }; fragment@1 { target = <&spi1>; __overlay__ { #address-cells = <1>; #size-cells = <0>; status = "okay"; pinctrl-names = "default"; pinctrl-0 = <&spi1_pins_s0>; ti,pindir-d0-out-d1-in = <1>; spidev@1 { spi-max-frequency = <24000000>; reg = <0>; compatible = "linux,spidev"; }; }; }; };
Close the file (Ctrl-o, Ctrl-x) and compile using dtc:
dtc -O dtb -o BB-SPI1-01-00A0.dtbo -b 0 -@ BB-SPI1-01-00A0.dts
Then, copy the file into /lib/firmware/:
cp BB-SPI1-01-00A0.dtbo /lib/firmware/
Then enable the device tree overlay:
echo BB-SPI1-01 > /sys/devices/bone_capemgr.*/slots
Plug in your BBB to a host computer using the mini usb data cable.
Go to My Computer>BeagleBone Getting Started> and open uEnv.txt Copy and paste this command into the .txt file. Make sure to save your changes. (Ctrl+s)
optargs=quiet drm.debug=7 capemgr.disable_partno=BB-BONELT-HDMI,BB-BONELT-HDMIN capemgr.enable_partno=BB-SPI1-01
after you save the changes, reboot your beaglebone black
Make sure it is enabled
You should now have two spidev-files in the folder /dev/
ls -al /dev/spidev1.*
You should also be able to see the pingroups:
cat /sys/kernel/debug/pinctrl/44e10800.pinmux/pingroups
SPI0
Start nano
nano BB-SPI0-01-00A0.dts
Copy-paste this into the file(or download a copy BB-SPI0-00A0):
/dts-v1/; /plugin/; / { compatible = "ti,beaglebone", "ti,beaglebone-black"; /* identification */ part-number = "spi0pinmux"; fragment@0 { target = <&am33xx_pinmux>; __overlay__ { spi0_pins_s0: spi0_pins_s0 { pinctrl-single,pins = < 0x150 0x30 /* spi0_sclk, INPUT_PULLUP | MODE0 */ 0x154 0x30 /* spi0_d0, INPUT_PULLUP | MODE0 */ 0x158 0x10 /* spi0_d1, OUTPUT_PULLUP | MODE0 */ 0x15c 0x10 /* spi0_cs0, OUTPUT_PULLUP | MODE0 */ >; }; }; }; fragment@1 { target = <&spi0>; __overlay__ { #address-cells = <1>; #size-cells = <0>; status = "okay"; pinctrl-names = "default"; pinctrl-0 = <&spi0_pins_s0>; spidev@0 { spi-max-frequency = <24000000>; reg = <0>; compatible = "linux,spidev"; }; }; }; };
Close the file (Ctrl-o, Ctrl-x) and compile using dtc:
dtc -O dtb -o BB-SPI0-01-00A0.dtbo -b 0 -@ BB-SPI0-01-00A0.dts
(if dtc complains about the -@, you need to get a newer version of dtc. See this adafruit link for a script that will retrieve and compile a suitable version of dtc)
Then, copy the file into /lib/firmware/:
cp BB-SPI0-01-00A0.dtbo /lib/firmware/
Then enable the device tree overlay:
echo BB-SPI0-01 > /sys/devices/bone_capemgr.*/slots
Plug in your BBB to a host computer using the mini usb data cable. Go to My Computer>BeagleBone Getting Started> and open uEnv.txt Copy and paste this command into the .txt file. Make sure to save your changes. (Ctrl+s)
optargs=quiet drm.debug=7 capemgr.enable_partno=BB-SPI0-01
after you save the changes, reboot your beaglebone black
Make sure it is enabled You should now have two spidev-files in the folder /dev/
ls -al /dev/spidev0.*
You should also be able to see the pingroups:
cat /sys/kernel/debug/pinctrl/44e10800.pinmux/pingroups
You can also try to run a loopback test with beaglebone. Here is an example with loopback on SPI1 bus:
1. Short pins 29 and 30 on the P9 connector (power supply side on the board).
2. Apply the following diff to the kernel:
diff --git a/drivers/spi/spidev.c b/drivers/spi/spidev.c index 911e9e0..622adf5 100644 --- a/drivers/spi/spidev.c +++ b/drivers/spi/spidev.c @@ -646,6 +646,7 @@ static int spidev_remove(struct spi_device *spi) static const struct of_device_id spidev_dt_ids[] = { { .compatible = "rohm,dh2228fv" }, + { .compatible = "linux,spidev" }, {}, };
3. Run the spidev_test program (sources in kernel/Documentation/spi), the following is a typical log:
bash-4.2# /spidev_test -D /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