| 80 | |
| 81 | == spidev - Userspace SPI API == |
| 82 | |
| 83 | 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. |
| 84 | {{{ |
| 85 | #include <fcntl.h> |
| 86 | #include <unistd.h> |
| 87 | #include <sys/ioctl.h> |
| 88 | #include <linux/types.h> |
| 89 | #include <linux/spi/spidev.h> |
| 90 | }}} |
| 91 | Some reasons you might want to use this programming interface include: |
| 92 | |
| 93 | * Prototyping in an environment that's not crash-prone; stray pointers in userspace won't normally bring down any Linux system. |
| 94 | * Developing simple protocols used to talk to microcontrollers acting as SPI slaves, which you may need to change quite often. |
| 95 | |
| 96 | 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. |
| 97 | |
| 98 | Userspace access to SPI devices is done through the /dev/spidev<bus>.<chip-select> device interface. In order to use this you must have spidev enabled in the kernel (CONFIG_SPI_SPIDEV) and have a spidev node defined under the SPI controller in the device-tree. |
| 99 | |
| 100 | For more info see: |
| 101 | * [https://web.archive.org/web/20170619204541/https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/spi/spidev spidev Kernel Documentation] |
| 102 | * [https://web.archive.org/web/20170619204541/https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/spi/spidev spidev examples] |
| 103 | * [https://web.archive.org/web/20170619204541/https://lxr.missinglinkelectronics.com/linux/drivers/spi/spidev.c spidev kernel driver] |